Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Tor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Tor Project
Core
Tor
Commits
f4f9a9be
Commit
f4f9a9be
authored
8 years ago
by
David Goulet
Committed by
Nick Mathewson
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
test: Add base32_encode/decode unit tests
Signed-off-by:
David Goulet
<
dgoulet@torproject.org
>
parent
4e4a7d2b
Branches
bug16937-02
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/test_util_format.c
+94
-0
94 additions, 0 deletions
src/test/test_util_format.c
with
94 additions
and
0 deletions
src/test/test_util_format.c
+
94
−
0
View file @
f4f9a9be
...
...
@@ -289,6 +289,96 @@ test_util_format_base16_decode(void *ignored)
tor_free
(
real_dst
);
}
static
void
test_util_format_base32_encode
(
void
*
arg
)
{
(
void
)
arg
;
size_t
real_dstlen
=
32
;
char
*
dst
=
tor_malloc_zero
(
real_dstlen
);
/* Basic use case that doesn't require a source length correction. */
{
/* Length of 10 bytes. */
const
char
*
src
=
"blahbleh12"
;
size_t
srclen
=
strlen
(
src
);
/* Expected result encoded base32. This was created using python as
* such (and same goes for all test case.):
*
* b = bytes("blahbleh12", 'utf-8')
* base64.b32encode(b)
* (result in lower case)
*/
const
char
*
expected
=
"mjwgc2dcnrswqmjs"
;
base32_encode
(
dst
,
base32_encoded_size
(
srclen
),
src
,
srclen
);
tt_mem_op
(
expected
,
OP_EQ
,
dst
,
strlen
(
expected
));
/* Encode but to a larger size destination. */
memset
(
dst
,
0
,
real_dstlen
);
base32_encode
(
dst
,
real_dstlen
,
src
,
srclen
);
tt_mem_op
(
expected
,
OP_EQ
,
dst
,
strlen
(
expected
));
}
/* Non multiple of 5 for the source buffer length. */
{
/* Length of 8 bytes. */
const
char
*
expected
=
"mjwgc2dcnrswq"
;
const
char
*
src
=
"blahbleh"
;
size_t
srclen
=
strlen
(
src
);
memset
(
dst
,
0
,
real_dstlen
);
base32_encode
(
dst
,
base32_encoded_size
(
srclen
),
src
,
srclen
);
tt_mem_op
(
expected
,
OP_EQ
,
dst
,
strlen
(
expected
));
}
done:
tor_free
(
dst
);
}
static
void
test_util_format_base32_decode
(
void
*
arg
)
{
(
void
)
arg
;
int
ret
;
size_t
real_dstlen
=
32
;
char
*
dst
=
tor_malloc_zero
(
real_dstlen
);
/* Basic use case. */
{
/* Length of 10 bytes. */
const
char
*
expected
=
"blahbleh12"
;
/* Expected result encoded base32. */
const
char
*
src
=
"mjwgc2dcnrswqmjs"
;
ret
=
base32_decode
(
dst
,
strlen
(
expected
),
src
,
strlen
(
src
));
tt_int_op
(
ret
,
==
,
0
);
tt_str_op
(
expected
,
OP_EQ
,
dst
);
}
/* Non multiple of 5 for the source buffer length. */
{
/* Length of 8 bytes. */
const
char
*
expected
=
"blahbleh"
;
const
char
*
src
=
"mjwgc2dcnrswq"
;
ret
=
base32_decode
(
dst
,
strlen
(
expected
),
src
,
strlen
(
src
));
tt_int_op
(
ret
,
==
,
0
);
tt_mem_op
(
expected
,
OP_EQ
,
dst
,
strlen
(
expected
));
}
/* Invalid values. */
{
/* Invalid character '#'. */
ret
=
base32_decode
(
dst
,
real_dstlen
,
"#abcde"
,
6
);
tt_int_op
(
ret
,
==
,
-
1
);
/* Make sure the destination buffer has been zeroed even on error. */
tt_int_op
(
tor_mem_is_zero
(
dst
,
real_dstlen
),
==
,
1
);
}
done:
tor_free
(
dst
);
}
struct
testcase_t
util_format_tests
[]
=
{
{
"unaligned_accessors"
,
test_util_format_unaligned_accessors
,
0
,
NULL
,
NULL
},
...
...
@@ -297,6 +387,10 @@ struct testcase_t util_format_tests[] = {
NULL
,
NULL
},
{
"base64_decode"
,
test_util_format_base64_decode
,
0
,
NULL
,
NULL
},
{
"base16_decode"
,
test_util_format_base16_decode
,
0
,
NULL
,
NULL
},
{
"base32_encode"
,
test_util_format_base32_encode
,
0
,
NULL
,
NULL
},
{
"base32_decode"
,
test_util_format_base32_decode
,
0
,
NULL
,
NULL
},
END_OF_TESTCASES
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment