1 /* Copyright (c) 2010-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
9 #define UTIL_FORMAT_PRIVATE
10 #include "util_format.h"
12 #define NS_MODULE util_format
15 test_util_format_unaligned_accessors(void *ignored
)
18 char buf
[9] = "onionsoup"; // 6f6e696f6e736f7570
20 tt_u64_op(get_uint64(buf
+1), OP_EQ
,
21 tor_htonll(U64_LITERAL(0x6e696f6e736f7570)));
22 tt_uint_op(get_uint32(buf
+1), OP_EQ
, htonl(0x6e696f6e));
23 tt_uint_op(get_uint16(buf
+1), OP_EQ
, htons(0x6e69));
24 tt_uint_op(get_uint8(buf
+1), OP_EQ
, 0x6e);
26 set_uint8(buf
+7, 0x61);
27 tt_mem_op(buf
, OP_EQ
, "onionsoap", 9);
29 set_uint16(buf
+6, htons(0x746f));
30 tt_mem_op(buf
, OP_EQ
, "onionstop", 9);
32 set_uint32(buf
+1, htonl(0x78696465));
33 tt_mem_op(buf
, OP_EQ
, "oxidestop", 9);
35 set_uint64(buf
+1, tor_htonll(U64_LITERAL(0x6266757363617465)));
36 tt_mem_op(buf
, OP_EQ
, "obfuscate", 9);
42 test_util_format_base64_encode(void *ignored
)
50 src
= tor_malloc_zero(256);
51 dst
= tor_malloc_zero(1000);
57 res
= base64_encode(NULL
, 1, src
, 1, 0);
58 tt_int_op(res
, OP_EQ
, -1);
60 res
= base64_encode(dst
, 1, NULL
, 1, 0);
61 tt_int_op(res
, OP_EQ
, -1);
63 res
= base64_encode(dst
, 1, src
, 10, 0);
64 tt_int_op(res
, OP_EQ
, -1);
66 res
= base64_encode(dst
, SSIZE_MAX
-1, src
, 1, 0);
67 tt_int_op(res
, OP_EQ
, -1);
69 res
= base64_encode(dst
, SSIZE_MAX
-1, src
, 10, 0);
70 tt_int_op(res
, OP_EQ
, -1);
72 res
= base64_encode(dst
, 1000, src
, 256, 0);
73 tt_int_op(res
, OP_EQ
, 344);
74 tt_str_op(dst
, OP_EQ
, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh"
75 "8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
76 "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3"
77 "BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY"
78 "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wM"
79 "HCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp"
80 "6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
82 res
= base64_encode(dst
, 1000, src
, 256, BASE64_ENCODE_MULTILINE
);
83 tt_int_op(res
, OP_EQ
, 350);
85 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
86 "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
87 "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
88 "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
89 "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
90 "8PHy8/T19vf4+fr7/P3+/w==\n");
92 res
= base64_encode(dst
, 1000, src
+1, 255, BASE64_ENCODE_MULTILINE
);
93 tt_int_op(res
, OP_EQ
, 346);
95 for (i
= 0;i
<50;i
++) {
103 res
= base64_encode(dst
, 1000, src
, 54, BASE64_ENCODE_MULTILINE
);
104 tt_int_op(res
, OP_EQ
, 74);
106 res
= base64_encode(dst
, 1000, src
+1, 53, BASE64_ENCODE_MULTILINE
);
107 tt_int_op(res
, OP_EQ
, 74);
109 res
= base64_encode(dst
, 1000, src
+2, 52, BASE64_ENCODE_MULTILINE
);
110 tt_int_op(res
, OP_EQ
, 74);
112 res
= base64_encode(dst
, 1000, src
+3, 51, BASE64_ENCODE_MULTILINE
);
113 tt_int_op(res
, OP_EQ
, 70);
115 res
= base64_encode(dst
, 1000, src
+4, 50, BASE64_ENCODE_MULTILINE
);
116 tt_int_op(res
, OP_EQ
, 70);
118 res
= base64_encode(dst
, 1000, src
+5, 49, BASE64_ENCODE_MULTILINE
);
119 tt_int_op(res
, OP_EQ
, 70);
121 res
= base64_encode(dst
, 1000, src
+6, 48, BASE64_ENCODE_MULTILINE
);
122 tt_int_op(res
, OP_EQ
, 65);
124 res
= base64_encode(dst
, 1000, src
+7, 47, BASE64_ENCODE_MULTILINE
);
125 tt_int_op(res
, OP_EQ
, 65);
127 res
= base64_encode(dst
, 1000, src
+8, 46, BASE64_ENCODE_MULTILINE
);
128 tt_int_op(res
, OP_EQ
, 65);
136 test_util_format_base64_decode_oddsize(void *ignored
)
142 char *dst
, real_dst
[7];
143 char expected
[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
144 char real_src
[] = "ZXhhbXBsZQ";
145 char expected40
[] = "testing40characteroddsizebase64encoding!";
146 char src40
[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ";
147 char pad40
[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ==";
149 src
= tor_malloc_zero(256);
150 dst
= tor_malloc_zero(1000);
152 for (i
=0;i
<256;i
++) {
156 res
= base64_decode(dst
, 1, src
, 5);
157 tt_int_op(res
, OP_EQ
, -1);
159 const char *s
= "SGVsbG8gd29ybGQ";
160 res
= base64_decode(dst
, 1000, s
, strlen(s
));
161 tt_int_op(res
, OP_EQ
, 11);
162 tt_mem_op(dst
, OP_EQ
, "Hello world", 11);
165 res
= base64_decode(dst
, 9, s
, strlen(s
));
166 tt_int_op(res
, OP_EQ
, 9);
167 tt_mem_op(dst
, OP_EQ
, "Opa mundo", 9);
169 res
= base64_decode(real_dst
, sizeof(real_dst
), real_src
, 10);
170 tt_int_op(res
, OP_EQ
, 7);
171 tt_mem_op(real_dst
, OP_EQ
, expected
, 7);
173 res
= base64_decode(dst
, 40, src40
, strlen(src40
));
174 tt_int_op(res
, OP_EQ
, 40);
175 tt_mem_op(dst
, OP_EQ
, expected40
, 40);
177 res
= base64_decode(dst
, 40, pad40
, strlen(pad40
));
178 tt_int_op(res
, OP_EQ
, 40);
179 tt_mem_op(dst
, OP_EQ
, expected40
, 40);
187 test_util_format_base64_decode(void *ignored
)
193 char *dst
, *real_dst
;
194 uint8_t expected
[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
195 char real_src
[] = "ZXhhbXBsZQ==";
197 src
= tor_malloc_zero(256);
198 dst
= tor_malloc_zero(1000);
199 real_dst
= tor_malloc_zero(10);
201 for (i
=0;i
<256;i
++) {
205 res
= base64_decode(dst
, 1, src
, 100);
206 tt_int_op(res
, OP_EQ
, -1);
208 res
= base64_decode(dst
, 1, real_src
, 10);
209 tt_int_op(res
, OP_EQ
, -1);
211 const char *s
= "T3BhIG11bmRv";
212 res
= base64_decode(dst
, 9, s
, strlen(s
));
213 tt_int_op(res
, OP_EQ
, 9);
214 tt_mem_op(dst
, OP_EQ
, "Opa mundo", 9);
216 memset(dst
, 0, 1000);
217 res
= base64_decode(dst
, 100, s
, strlen(s
));
218 tt_int_op(res
, OP_EQ
, 9);
219 tt_mem_op(dst
, OP_EQ
, "Opa mundo", 9);
221 s
= "SGVsbG8gd29ybGQ=";
222 res
= base64_decode(dst
, 100, s
, strlen(s
));
223 tt_int_op(res
, OP_EQ
, 11);
224 tt_mem_op(dst
, OP_EQ
, "Hello world", 11);
226 res
= base64_decode(real_dst
, 10, real_src
, 10);
227 tt_int_op(res
, OP_EQ
, 7);
228 tt_mem_op(real_dst
, OP_EQ
, expected
, 7);
237 test_util_format_base16_decode(void *ignored
)
243 char *dst
, *real_dst
;
244 char expected
[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
245 char real_src
[] = "6578616D706C65";
247 src
= tor_malloc_zero(256);
248 dst
= tor_malloc_zero(1000);
249 real_dst
= tor_malloc_zero(10);
251 for (i
=0;i
<256;i
++) {
255 res
= base16_decode(dst
, 3, src
, 3);
256 tt_int_op(res
, OP_EQ
, -1);
258 res
= base16_decode(dst
, 1, src
, 10);
259 tt_int_op(res
, OP_EQ
, -1);
261 res
= base16_decode(dst
, ((size_t)INT_MAX
)+1, src
, 10);
262 tt_int_op(res
, OP_EQ
, -1);
264 res
= base16_decode(dst
, 1000, "", 0);
265 tt_int_op(res
, OP_EQ
, 0);
267 res
= base16_decode(dst
, 1000, "aabc", 4);
268 tt_int_op(res
, OP_EQ
, 2);
269 tt_mem_op(dst
, OP_EQ
, "\xaa\xbc", 2);
271 res
= base16_decode(dst
, 1000, "aabcd", 6);
272 tt_int_op(res
, OP_EQ
, -1);
274 res
= base16_decode(dst
, 1000, "axxx", 4);
275 tt_int_op(res
, OP_EQ
, -1);
277 res
= base16_decode(real_dst
, 10, real_src
, 14);
278 tt_int_op(res
, OP_EQ
, 7);
279 tt_mem_op(real_dst
, OP_EQ
, expected
, 7);
288 test_util_format_base32_encode(void *arg
)
291 size_t real_dstlen
= 32;
292 char *dst
= tor_malloc_zero(real_dstlen
);
294 /* Basic use case that doesn't require a source length correction. */
296 /* Length of 10 bytes. */
297 const char *src
= "blahbleh12";
298 size_t srclen
= strlen(src
);
299 /* Expected result encoded base32. This was created using python as
300 * such (and same goes for all test case.):
302 * b = bytes("blahbleh12", 'utf-8')
303 * base64.b32encode(b)
304 * (result in lower case)
306 const char *expected
= "mjwgc2dcnrswqmjs";
308 base32_encode(dst
, base32_encoded_size(srclen
), src
, srclen
);
309 tt_mem_op(expected
, OP_EQ
, dst
, strlen(expected
));
310 /* Encode but to a larger size destination. */
311 memset(dst
, 0, real_dstlen
);
312 base32_encode(dst
, real_dstlen
, src
, srclen
);
313 tt_mem_op(expected
, OP_EQ
, dst
, strlen(expected
));
316 /* Non multiple of 5 for the source buffer length. */
318 /* Length of 8 bytes. */
319 const char *expected
= "mjwgc2dcnrswq";
320 const char *src
= "blahbleh";
321 size_t srclen
= strlen(src
);
323 memset(dst
, 0, real_dstlen
);
324 base32_encode(dst
, base32_encoded_size(srclen
), src
, srclen
);
325 tt_mem_op(expected
, OP_EQ
, dst
, strlen(expected
));
333 test_util_format_base32_decode(void *arg
)
337 size_t real_dstlen
= 32;
338 char *dst
= tor_malloc_zero(real_dstlen
);
340 /* Basic use case. */
342 /* Length of 10 bytes. */
343 const char *expected
= "blahbleh12";
344 /* Expected result encoded base32. */
345 const char *src
= "mjwgc2dcnrswqmjs";
347 ret
= base32_decode(dst
, strlen(expected
), src
, strlen(src
));
348 tt_int_op(ret
, OP_EQ
, 0);
349 tt_str_op(expected
, OP_EQ
, dst
);
352 /* Non multiple of 5 for the source buffer length. */
354 /* Length of 8 bytes. */
355 const char *expected
= "blahbleh";
356 const char *src
= "mjwgc2dcnrswq";
358 ret
= base32_decode(dst
, strlen(expected
), src
, strlen(src
));
359 tt_int_op(ret
, OP_EQ
, 0);
360 tt_mem_op(expected
, OP_EQ
, dst
, strlen(expected
));
363 /* Invalid values. */
365 /* Invalid character '#'. */
366 ret
= base32_decode(dst
, real_dstlen
, "#abcde", 6);
367 tt_int_op(ret
, OP_EQ
, -1);
368 /* Make sure the destination buffer has been zeroed even on error. */
369 tt_int_op(tor_mem_is_zero(dst
, real_dstlen
), OP_EQ
, 1);
377 test_util_format_encoded_size(void *arg
)
384 crypto_rand((char *)inbuf
, sizeof(inbuf
));
385 for (i
= 0; i
<= sizeof(inbuf
); ++i
) {
386 /* XXXX (Once the return values are consistent, check them too.) */
388 base32_encode(outbuf
, sizeof(outbuf
), (char *)inbuf
, i
);
389 /* The "+ 1" below is an API inconsistency. */
390 tt_int_op(strlen(outbuf
) + 1, OP_EQ
, base32_encoded_size(i
));
392 base64_encode(outbuf
, sizeof(outbuf
), (char *)inbuf
, i
, 0);
393 tt_int_op(strlen(outbuf
), OP_EQ
, base64_encode_size(i
, 0));
394 base64_encode(outbuf
, sizeof(outbuf
), (char *)inbuf
, i
,
395 BASE64_ENCODE_MULTILINE
);
396 tt_int_op(strlen(outbuf
), OP_EQ
,
397 base64_encode_size(i
, BASE64_ENCODE_MULTILINE
));
404 struct testcase_t util_format_tests
[] = {
405 { "unaligned_accessors", test_util_format_unaligned_accessors
, 0,
407 { "base64_encode", test_util_format_base64_encode
, 0, NULL
, NULL
},
408 { "base64_decode_oddsize", test_util_format_base64_decode_oddsize
, 0,
410 { "base64_decode", test_util_format_base64_decode
, 0, NULL
, NULL
},
411 { "base16_decode", test_util_format_base16_decode
, 0, NULL
, NULL
},
412 { "base32_encode", test_util_format_base32_encode
, 0,
414 { "base32_decode", test_util_format_base32_decode
, 0,
416 { "encoded_size", test_util_format_encoded_size
, 0, NULL
, NULL
},