Move the codecs tests to their own test file
This commit is contained in:
parent
558355e566
commit
ee1d5c96d8
1
.gitignore
vendored
1
.gitignore
vendored
@ -99,6 +99,7 @@ test/default/box_easy2
|
|||||||
test/default/box_seal
|
test/default/box_seal
|
||||||
test/default/box_seed
|
test/default/box_seed
|
||||||
test/default/chacha20
|
test/default/chacha20
|
||||||
|
test/default/codecs
|
||||||
test/default/core1
|
test/default/core1
|
||||||
test/default/core2
|
test/default/core2
|
||||||
test/default/core3
|
test/default/core3
|
||||||
|
@ -21,6 +21,7 @@ EXTRA_DIST = \
|
|||||||
box_seal.exp \
|
box_seal.exp \
|
||||||
box_seed.exp \
|
box_seed.exp \
|
||||||
chacha20.exp \
|
chacha20.exp \
|
||||||
|
codecs.exp \
|
||||||
core1.exp \
|
core1.exp \
|
||||||
core2.exp \
|
core2.exp \
|
||||||
core3.exp \
|
core3.exp \
|
||||||
@ -93,6 +94,7 @@ DISTCLEANFILES = \
|
|||||||
box_seal.res \
|
box_seal.res \
|
||||||
box_seed.res \
|
box_seed.res \
|
||||||
chacha20.res \
|
chacha20.res \
|
||||||
|
codecs.res \
|
||||||
core1.res \
|
core1.res \
|
||||||
core2.res \
|
core2.res \
|
||||||
core3.res \
|
core3.res \
|
||||||
@ -166,6 +168,7 @@ CLEANFILES = \
|
|||||||
box_seal.final \
|
box_seal.final \
|
||||||
box_seed.final \
|
box_seed.final \
|
||||||
chacha20.final \
|
chacha20.final \
|
||||||
|
codecs.final \
|
||||||
core1.final \
|
core1.final \
|
||||||
core2.final \
|
core2.final \
|
||||||
core3.final \
|
core3.final \
|
||||||
@ -234,6 +237,7 @@ CLEANFILES = \
|
|||||||
box_seal.nexe \
|
box_seal.nexe \
|
||||||
box_seed.nexe \
|
box_seed.nexe \
|
||||||
chacha20.nexe \
|
chacha20.nexe \
|
||||||
|
codecs.nexe \
|
||||||
core1.nexe \
|
core1.nexe \
|
||||||
core2.nexe \
|
core2.nexe \
|
||||||
core3.nexe \
|
core3.nexe \
|
||||||
@ -315,6 +319,7 @@ TESTS_TARGETS = \
|
|||||||
box_seal \
|
box_seal \
|
||||||
box_seed \
|
box_seed \
|
||||||
chacha20 \
|
chacha20 \
|
||||||
|
codecs \
|
||||||
core1 \
|
core1 \
|
||||||
core2 \
|
core2 \
|
||||||
core3 \
|
core3 \
|
||||||
@ -430,6 +435,9 @@ box_seed_LDADD = $(TESTS_LDADD)
|
|||||||
chacha20_SOURCE = cmptest.h chacha20.c
|
chacha20_SOURCE = cmptest.h chacha20.c
|
||||||
chacha20_LDADD = $(TESTS_LDADD)
|
chacha20_LDADD = $(TESTS_LDADD)
|
||||||
|
|
||||||
|
codecs_SOURCE = cmptest.h codecs.c
|
||||||
|
codecs_LDADD = $(TESTS_LDADD)
|
||||||
|
|
||||||
core1_SOURCE = cmptest.h core1.c
|
core1_SOURCE = cmptest.h core1.c
|
||||||
core1_LDADD = $(TESTS_LDADD)
|
core1_LDADD = $(TESTS_LDADD)
|
||||||
|
|
||||||
|
225
test/default/codecs.c
Normal file
225
test/default/codecs.c
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
#define TEST_NAME "codecs"
|
||||||
|
#include "cmptest.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
unsigned char buf1[1000];
|
||||||
|
char buf3[33];
|
||||||
|
unsigned char buf4[4];
|
||||||
|
const char *b64;
|
||||||
|
char *b64_;
|
||||||
|
const char *b64_end;
|
||||||
|
unsigned char *bin;
|
||||||
|
unsigned char *bin_padded;
|
||||||
|
const char *hex;
|
||||||
|
const char *hex_end;
|
||||||
|
size_t b64_len;
|
||||||
|
size_t bin_len, bin_len2;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
|
||||||
|
16U));
|
||||||
|
hex = "Cafe : 6942";
|
||||||
|
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
|
||||||
|
&hex_end);
|
||||||
|
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
|
||||||
|
buf4[0], buf4[1], buf4[2], buf4[3]);
|
||||||
|
printf("dt1: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "Cafe : 6942";
|
||||||
|
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
|
||||||
|
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
|
||||||
|
buf4[0], buf4[1], buf4[2], buf4[3]);
|
||||||
|
|
||||||
|
hex = "deadbeef";
|
||||||
|
if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
|
||||||
|
printf("sodium_hex2bin() overflow not detected\n");
|
||||||
|
}
|
||||||
|
printf("dt2: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "de:ad:be:eff";
|
||||||
|
if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
|
||||||
|
printf(
|
||||||
|
"sodium_hex2bin() with an odd input length and a short output "
|
||||||
|
"buffer\n");
|
||||||
|
}
|
||||||
|
printf("dt3: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "de:ad:be:eff";
|
||||||
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
||||||
|
&bin_len, &hex_end) != -1) {
|
||||||
|
printf("sodium_hex2bin() with an odd input length\n");
|
||||||
|
}
|
||||||
|
printf("dt4: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "de:ad:be:eff";
|
||||||
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":",
|
||||||
|
&bin_len, &hex_end) != -1) {
|
||||||
|
printf("sodium_hex2bin() with an odd input length (2)\n");
|
||||||
|
}
|
||||||
|
printf("dt5: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "de:ad:be:eff";
|
||||||
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
||||||
|
&bin_len, NULL) != -1) {
|
||||||
|
printf("sodium_hex2bin() with an odd input length and no end pointer\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
hex = "de:ad:be:ef*";
|
||||||
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
||||||
|
&bin_len, &hex_end) != 0) {
|
||||||
|
printf("sodium_hex2bin() with an extra character and an end pointer\n");
|
||||||
|
}
|
||||||
|
printf("dt6: %ld\n", (long) (hex_end - hex));
|
||||||
|
|
||||||
|
hex = "de:ad:be:ef*";
|
||||||
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
||||||
|
&bin_len, NULL) != -1) {
|
||||||
|
printf("sodium_hex2bin() with an extra character and no end pointer\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
||||||
|
21U, sodium_base64_VARIANT_ORIGINAL));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
||||||
|
22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
||||||
|
21U, sodium_base64_VARIANT_URLSAFE));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
||||||
|
22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 1U, NULL,
|
||||||
|
0U, sodium_base64_VARIANT_ORIGINAL));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
|
||||||
|
1U, sodium_base64_VARIANT_ORIGINAL));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
|
||||||
|
2U, sodium_base64_VARIANT_ORIGINAL));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
||||||
|
3U, sodium_base64_VARIANT_ORIGINAL));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 1U, NULL,
|
||||||
|
0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
|
||||||
|
1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
|
||||||
|
2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||||
|
printf("%s\n",
|
||||||
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
||||||
|
3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||||
|
|
||||||
|
b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
|
||||||
|
memset(buf4, '*', sizeof buf4);
|
||||||
|
assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
|
||||||
|
buf4[bin_len] = 0;
|
||||||
|
printf("[%s]\n", (const char *) buf4);
|
||||||
|
printf("[%s]\n", b64_end);
|
||||||
|
|
||||||
|
memset(buf1, '*', sizeof buf1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
buf1[bin_len] = 0;
|
||||||
|
printf("[%s]\n", (const char *) buf1);
|
||||||
|
assert(*b64_end == 0);
|
||||||
|
|
||||||
|
memset(buf1, '*', sizeof buf1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
buf1[bin_len] = 0;
|
||||||
|
printf("[%s]\n", (const char *) buf1);
|
||||||
|
printf("[%s]\n", b64_end);
|
||||||
|
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||||
|
&b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
&b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||||
|
&b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
||||||
|
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
NULL, sodium_base64_VARIANT_ORIGINAL) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||||
|
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||||
|
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||||
|
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
|
||||||
|
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a=", (size_t) 2U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "~", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "*", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a==", (size_t) 3U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "~", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "*", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == -1);
|
||||||
|
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "O1R", (size_t) 3U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "O1Q", (size_t) 3U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "O1", (size_t) 2U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "Ow", (size_t) 2U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "O", (size_t) 1U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
||||||
|
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "kaw", (size_t) 3U, NULL, NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "@", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "*", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == -1);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "kaw=**", (size_t) 6U, "*", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "kaw*=*", (size_t) 6U, "~*", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
|
||||||
|
sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||||
|
|
||||||
|
for (i = 0; i < 1000; i++) {
|
||||||
|
assert(sizeof buf1 >= 100);
|
||||||
|
bin_len = (size_t) randombytes_uniform(100);
|
||||||
|
bin = (unsigned char *) sodium_malloc(bin_len);
|
||||||
|
b64_len = 1 + (bin_len + 2) / 3 * 4;
|
||||||
|
b64_ = (char *) sodium_malloc(b64_len);
|
||||||
|
randombytes_buf(bin, bin_len);
|
||||||
|
memcpy(buf1, bin, bin_len);
|
||||||
|
b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
|
||||||
|
sodium_base64_VARIANT_URLSAFE);
|
||||||
|
assert(b64 != NULL);
|
||||||
|
assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
|
||||||
|
NULL, NULL, &b64_end,
|
||||||
|
sodium_base64_VARIANT_URLSAFE) == 0);
|
||||||
|
assert(b64_end == &b64[b64_len - 1]);
|
||||||
|
assert(memcmp(bin, buf1, bin_len) == 0);
|
||||||
|
sodium_free(bin);
|
||||||
|
sodium_free(b64_);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
28
test/default/codecs.exp
Normal file
28
test/default/codecs.exp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
30313233343536373839414243444546
|
||||||
|
4:cafe6942
|
||||||
|
dt1: 11
|
||||||
|
4:cafe6942
|
||||||
|
dt2: 2
|
||||||
|
dt3: 11
|
||||||
|
dt4: 11
|
||||||
|
dt5: 11
|
||||||
|
dt6: 11
|
||||||
|
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
||||||
|
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
||||||
|
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
||||||
|
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
||||||
|
|
||||||
|
YQ==
|
||||||
|
YWI=
|
||||||
|
YWJj
|
||||||
|
|
||||||
|
YQ
|
||||||
|
YWI
|
||||||
|
YWJj
|
||||||
|
[]
|
||||||
|
[BpcyBhIGpvdXJu
|
||||||
|
ZXkgaW50by Bzb3VuZA==]
|
||||||
|
[This is a journey into sound]
|
||||||
|
[This is a journ]
|
||||||
|
[
|
||||||
|
ZXkgaW50by Bzb3VuZA==]
|
@ -10,17 +10,9 @@ main(void)
|
|||||||
unsigned char buf1_rev[1000];
|
unsigned char buf1_rev[1000];
|
||||||
unsigned char buf2_rev[1000];
|
unsigned char buf2_rev[1000];
|
||||||
char buf3[33];
|
char buf3[33];
|
||||||
unsigned char buf4[4];
|
|
||||||
unsigned char nonce[24];
|
unsigned char nonce[24];
|
||||||
char nonce_hex[49];
|
char nonce_hex[49];
|
||||||
const char *b64;
|
|
||||||
char *b64_;
|
|
||||||
const char *b64_end;
|
|
||||||
unsigned char *bin;
|
|
||||||
unsigned char *bin_padded;
|
unsigned char *bin_padded;
|
||||||
const char *hex;
|
|
||||||
const char *hex_end;
|
|
||||||
size_t b64_len;
|
|
||||||
size_t bin_len, bin_len2;
|
size_t bin_len, bin_len2;
|
||||||
size_t bin_padded_len;
|
size_t bin_padded_len;
|
||||||
size_t bin_padded_maxlen;
|
size_t bin_padded_maxlen;
|
||||||
@ -38,209 +30,6 @@ main(void)
|
|||||||
printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
|
printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
|
||||||
sodium_memzero(buf2, sizeof buf2 / 2);
|
sodium_memzero(buf2, sizeof buf2 / 2);
|
||||||
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
|
|
||||||
16U));
|
|
||||||
hex = "Cafe : 6942";
|
|
||||||
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
|
|
||||||
&hex_end);
|
|
||||||
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len, buf4[0], buf4[1],
|
|
||||||
buf4[2], buf4[3]);
|
|
||||||
printf("dt1: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "Cafe : 6942";
|
|
||||||
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
|
|
||||||
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
|
|
||||||
buf4[2], buf4[3], buf4[2], buf4[3]);
|
|
||||||
|
|
||||||
hex = "deadbeef";
|
|
||||||
if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
|
|
||||||
printf("sodium_hex2bin() overflow not detected\n");
|
|
||||||
}
|
|
||||||
printf("dt2: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "de:ad:be:eff";
|
|
||||||
if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
|
|
||||||
printf(
|
|
||||||
"sodium_hex2bin() with an odd input length and a short output "
|
|
||||||
"buffer\n");
|
|
||||||
}
|
|
||||||
printf("dt3: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "de:ad:be:eff";
|
|
||||||
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
|
||||||
&bin_len, &hex_end) != -1) {
|
|
||||||
printf("sodium_hex2bin() with an odd input length\n");
|
|
||||||
}
|
|
||||||
printf("dt4: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "de:ad:be:eff";
|
|
||||||
if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":",
|
|
||||||
&bin_len, &hex_end) != -1) {
|
|
||||||
printf("sodium_hex2bin() with an odd input length (2)\n");
|
|
||||||
}
|
|
||||||
printf("dt5: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "de:ad:be:eff";
|
|
||||||
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
|
||||||
&bin_len, NULL) != -1) {
|
|
||||||
printf("sodium_hex2bin() with an odd input length and no end pointer\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
hex = "de:ad:be:ef*";
|
|
||||||
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
|
||||||
&bin_len, &hex_end) != 0) {
|
|
||||||
printf("sodium_hex2bin() with an extra character and an end pointer\n");
|
|
||||||
}
|
|
||||||
printf("dt6: %ld\n", (long) (hex_end - hex));
|
|
||||||
|
|
||||||
hex = "de:ad:be:ef*";
|
|
||||||
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
|
|
||||||
&bin_len, NULL) != -1) {
|
|
||||||
printf("sodium_hex2bin() with an extra character and no end pointer\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
|
||||||
21U, sodium_base64_VARIANT_ORIGINAL));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
|
||||||
22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
|
||||||
21U, sodium_base64_VARIANT_URLSAFE));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
|
||||||
22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 1U, NULL,
|
|
||||||
0U, sodium_base64_VARIANT_ORIGINAL));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
|
|
||||||
1U, sodium_base64_VARIANT_ORIGINAL));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
|
|
||||||
2U, sodium_base64_VARIANT_ORIGINAL));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
|
||||||
3U, sodium_base64_VARIANT_ORIGINAL));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 1U, NULL,
|
|
||||||
0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
|
|
||||||
1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
|
|
||||||
2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
||||||
printf("%s\n",
|
|
||||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
|
||||||
3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
||||||
|
|
||||||
b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
|
|
||||||
memset(buf4, '*', sizeof buf4);
|
|
||||||
assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
|
|
||||||
buf4[bin_len] = 0;
|
|
||||||
printf("[%s]\n", (const char *) buf4);
|
|
||||||
printf("[%s]\n", b64_end);
|
|
||||||
|
|
||||||
memset(buf1, '*', sizeof buf1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
buf1[bin_len] = 0;
|
|
||||||
printf("[%s]\n", (const char *) buf1);
|
|
||||||
assert(*b64_end == 0);
|
|
||||||
|
|
||||||
memset(buf1, '*', sizeof buf1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
buf1[bin_len] = 0;
|
|
||||||
printf("[%s]\n", (const char *) buf1);
|
|
||||||
printf("[%s]\n", b64_end);
|
|
||||||
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
||||||
&b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
&b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
||||||
&b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
|
||||||
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
NULL, sodium_base64_VARIANT_ORIGINAL) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
||||||
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
||||||
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
||||||
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
|
|
||||||
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a=", (size_t) 2U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "~", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "*", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a==", (size_t) 3U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "~", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "*", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == -1);
|
|
||||||
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "O1R", (size_t) 3U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "O1Q", (size_t) 3U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "O1", (size_t) 2U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "Ow", (size_t) 2U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "O", (size_t) 1U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
|
|
||||||
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "kaw", (size_t) 3U, NULL, NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "@", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "*", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == -1);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "kaw=**", (size_t) 6U, "*", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "kaw*=*", (size_t) 6U, "~*", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
|
|
||||||
sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
||||||
|
|
||||||
for (i = 0; i < 1000; i++) {
|
|
||||||
assert(sizeof buf1 >= 100);
|
|
||||||
bin_len = (size_t) randombytes_uniform(100);
|
|
||||||
bin = (unsigned char *) sodium_malloc(bin_len);
|
|
||||||
b64_len = 1 + (bin_len + 2) / 3 * 4;
|
|
||||||
b64_ = (char *) sodium_malloc(b64_len);
|
|
||||||
randombytes_buf(bin, bin_len);
|
|
||||||
memcpy(buf1, bin, bin_len);
|
|
||||||
b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
|
|
||||||
sodium_base64_VARIANT_URLSAFE);
|
|
||||||
assert(b64 != NULL);
|
|
||||||
assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
|
|
||||||
NULL, NULL, &b64_end,
|
|
||||||
sodium_base64_VARIANT_URLSAFE) == 0);
|
|
||||||
assert(b64_end == &b64[b64_len - 1]);
|
|
||||||
assert(memcmp(bin, buf1, bin_len) == 0);
|
|
||||||
sodium_free(bin);
|
|
||||||
sodium_free(b64_);
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(nonce, 0, sizeof nonce);
|
memset(nonce, 0, sizeof nonce);
|
||||||
sodium_increment(nonce, sizeof nonce);
|
sodium_increment(nonce, sizeof nonce);
|
||||||
|
@ -3,34 +3,6 @@
|
|||||||
-1
|
-1
|
||||||
0
|
0
|
||||||
0
|
0
|
||||||
30313233343536373839414243444546
|
|
||||||
4:cafe6942
|
|
||||||
dt1: 11
|
|
||||||
4:69426942
|
|
||||||
dt2: 2
|
|
||||||
dt3: 11
|
|
||||||
dt4: 11
|
|
||||||
dt5: 11
|
|
||||||
dt6: 11
|
|
||||||
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
|
||||||
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
|
||||||
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
|
||||||
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
|
||||||
|
|
||||||
YQ==
|
|
||||||
YWI=
|
|
||||||
YWJj
|
|
||||||
|
|
||||||
YQ
|
|
||||||
YWI
|
|
||||||
YWJj
|
|
||||||
[]
|
|
||||||
[BpcyBhIGpvdXJu
|
|
||||||
ZXkgaW50by Bzb3VuZA==]
|
|
||||||
[This is a journey into sound]
|
|
||||||
[This is a journ]
|
|
||||||
[
|
|
||||||
ZXkgaW50by Bzb3VuZA==]
|
|
||||||
010000000000000000000000000000000000000000000000
|
010000000000000000000000000000000000000000000000
|
||||||
000000000000000000000000000000000000000000000000
|
000000000000000000000000000000000000000000000000
|
||||||
010100000000000000000000000000000000000000000000
|
010100000000000000000000000000000000000000000000
|
||||||
|
Loading…
Reference in New Issue
Block a user