test/base64.c

branch
dav-2
changeset 879
255a5ef46400
parent 747
efbd59642577
child 889
42cdbf9bbd49
equal deleted inserted replaced
878:68ea3a0fe66f 879:255a5ef46400
33 #include "base64.h" 33 #include "base64.h"
34 34
35 #include <cx/string.h> 35 #include <cx/string.h>
36 #include <libidav/utils.h> 36 #include <libidav/utils.h>
37 37
38 UCX_TEST(test_util_base64decode) { 38 CX_TEST(test_util_base64decode) {
39 char *s1 = util_base64decode("YWJj"); 39 char *s1 = util_base64decode("YWJj");
40 char *s2 = util_base64decode("aGVsbG8gd29ybGQ="); 40 char *s2 = util_base64decode("aGVsbG8gd29ybGQ=");
41 char *s3 = util_base64decode("MA=="); 41 char *s3 = util_base64decode("MA==");
42 char *s4 = util_base64decode("MHh4MQ=="); 42 char *s4 = util_base64decode("MHh4MQ==");
43 43
44 UCX_TEST_BEGIN; 44 CX_TEST_DO {
45 45 CX_TEST_ASSERT(!strcmp(s1, "abc"));
46 UCX_TEST_ASSERT(!strcmp(s1, "abc"), "s1 wrong"); 46 CX_TEST_ASSERT(!strcmp(s2, "hello world"));
47 UCX_TEST_ASSERT(!strcmp(s2, "hello world"), "s2 wrong"); 47 CX_TEST_ASSERT(!strcmp(s3, "0"));
48 UCX_TEST_ASSERT(!strcmp(s3, "0"), "s3 wrong"); 48 CX_TEST_ASSERT(!strcmp(s4, "0xx1"));
49 UCX_TEST_ASSERT(!strcmp(s4, "0xx1"), "s4 wrong"); 49 }
50
51 UCX_TEST_END;
52 50
53 free(s1); 51 free(s1);
54 free(s2); 52 free(s2);
55 free(s3); 53 free(s3);
56 free(s4); 54 free(s4);
57 } 55 }
58 56
59 UCX_TEST(test_util_base64decode_len) { 57 CX_TEST(test_util_base64decode_len) {
60 int len1, len2, len3, len4, len5, len6; 58 int len1, len2, len3, len4, len5, len6;
61 char *s1 = util_base64decode_len("aGVsbG8=", &len1); // 5 59 char *s1 = util_base64decode_len("aGVsbG8=", &len1); // 5
62 char *s2 = util_base64decode_len("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5", &len2); // 30 60 char *s2 = util_base64decode_len("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5", &len2); // 30
63 char *s3 = util_base64decode_len("Lg==", &len3); // 1 61 char *s3 = util_base64decode_len("Lg==", &len3); // 1
64 char *s4 = util_base64decode_len("bG9s", &len4); // 3 62 char *s4 = util_base64decode_len("bG9s", &len4); // 3
65 char *s5 = util_base64decode_len("YWJjLS0tLS0tM3g=", &len5); // 11 63 char *s5 = util_base64decode_len("YWJjLS0tLS0tM3g=", &len5); // 11
66 char *s6 = util_base64decode_len("YWJjZGVmZy4uLjs7MGhlbGxv", &len6); // 18 64 char *s6 = util_base64decode_len("YWJjZGVmZy4uLjs7MGhlbGxv", &len6); // 18
67 65
68 UCX_TEST_BEGIN; 66 CX_TEST_DO {
69 67 CX_TEST_ASSERT(!strcmp(s1, "hello"));
70 UCX_TEST_ASSERT(!strcmp(s1, "hello"), "s1 wrong"); 68 CX_TEST_ASSERT(len1 == 5);
71 UCX_TEST_ASSERT(len1 == 5, "len1 wrong"); 69
72 70 CX_TEST_ASSERT(!strcmp(s2, "012345678901234567890123456789"));
73 UCX_TEST_ASSERT(!strcmp(s2, "012345678901234567890123456789"), "s2 wrong"); 71 CX_TEST_ASSERT(len2 == 30);
74 UCX_TEST_ASSERT(len2 == 30, "len2 wrong"); 72
75 73 CX_TEST_ASSERT(!strcmp(s3, "."));
76 UCX_TEST_ASSERT(!strcmp(s3, "."), "s3 wrong"); 74 CX_TEST_ASSERT(len3 == 1);
77 UCX_TEST_ASSERT(len3 == 1, "len3 wrong"); 75
78 76 CX_TEST_ASSERT(!strcmp(s4, "lol"));
79 UCX_TEST_ASSERT(!strcmp(s4, "lol"), "s4 wrong"); 77 CX_TEST_ASSERT(len4 == 3);
80 UCX_TEST_ASSERT(len4 == 3, "len4 wrong"); 78
81 79 CX_TEST_ASSERT(!strcmp(s5, "abc------3x"));
82 UCX_TEST_ASSERT(!strcmp(s5, "abc------3x"), "s5 wrong"); 80 CX_TEST_ASSERT(len5 == 11);
83 UCX_TEST_ASSERT(len5 == 11, "len5 wrong"); 81
84 82 CX_TEST_ASSERT(!strcmp(s6, "abcdefg...;;0hello"));
85 UCX_TEST_ASSERT(!strcmp(s6, "abcdefg...;;0hello"), "s6 failed"); 83 CX_TEST_ASSERT(len6 == 18);
86 UCX_TEST_ASSERT(len6 == 18, "len6 wrong"); 84 }
87
88 UCX_TEST_END;
89 85
90 free(s1); 86 free(s1);
91 free(s2); 87 free(s2);
92 free(s3); 88 free(s3);
93 free(s4); 89 free(s4);
94 free(s5); 90 free(s5);
95 free(s6); 91 free(s6);
96 } 92 }
97 93
98 UCX_TEST(test_util_base64encode) { 94 CX_TEST(test_util_base64encode) {
99 char *str1 = "hello world"; 95 char *str1 = "hello world";
100 char *str2 = "test string"; 96 char *str2 = "test string";
101 char *str3 = "01234567890123456789012345678901234567890123456789"; 97 char *str3 = "01234567890123456789012345678901234567890123456789";
102 char str4[8]; 98 char str4[8];
103 str4[0] = 0; 99 str4[0] = 0;
125 char *b7 = util_base64encode(str7, strlen(str7)); 121 char *b7 = util_base64encode(str7, strlen(str7));
126 char *b8 = util_base64encode(str8, strlen(str8)); 122 char *b8 = util_base64encode(str8, strlen(str8));
127 char *b9 = util_base64encode(str9, strlen(str9)); 123 char *b9 = util_base64encode(str9, strlen(str9));
128 char *b10 = util_base64encode(str10, strlen(str10)); 124 char *b10 = util_base64encode(str10, strlen(str10));
129 125
130 UCX_TEST_BEGIN; 126 CX_TEST_DO {
131 127 CX_TEST_ASSERT(!strcmp(b1, "aGVsbG8gd29ybGQ="));
132 UCX_TEST_ASSERT(!strcmp(b1, "aGVsbG8gd29ybGQ="), "b1 failed"); 128 CX_TEST_ASSERT(!strcmp(b2, "dGVzdCBzdHJpbmc="));
133 UCX_TEST_ASSERT(!strcmp(b2, "dGVzdCBzdHJpbmc="), "b2 failed"); 129 CX_TEST_ASSERT(!strcmp(b3, "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk="));
134 UCX_TEST_ASSERT(!strcmp(b3, "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk="), "b3 failed"); 130 CX_TEST_ASSERT(!strcmp(b4, "AAwDMgJuPwE="));
135 UCX_TEST_ASSERT(!strcmp(b4, "AAwDMgJuPwE="), "b4 failed"); 131
136 132 CX_TEST_ASSERT(!strcmp(b5, "YQ=="));
137 UCX_TEST_ASSERT(!strcmp(b5, "YQ=="), "b5 failed"); 133 CX_TEST_ASSERT(!strcmp(b6, "YWI="));
138 UCX_TEST_ASSERT(!strcmp(b6, "YWI="), "b6 failed"); 134 CX_TEST_ASSERT(!strcmp(b7, "YWJj"));
139 UCX_TEST_ASSERT(!strcmp(b7, "YWJj"), "b7 failed"); 135 CX_TEST_ASSERT(!strcmp(b8, "YWJjZA=="));
140 UCX_TEST_ASSERT(!strcmp(b8, "YWJjZA=="), "b8 failed"); 136 CX_TEST_ASSERT(!strcmp(b9, "YWJjZGU="));
141 UCX_TEST_ASSERT(!strcmp(b9, "YWJjZGU="), "b9 failed"); 137 CX_TEST_ASSERT(!strcmp(b10, "YWJjZGVm"));
142 UCX_TEST_ASSERT(!strcmp(b10, "YWJjZGVm"), "b10 failed"); 138 }
143
144 UCX_TEST_END;
145 139
146 free(b1); 140 free(b1);
147 free(b2); 141 free(b2);
148 free(b3); 142 free(b3);
149 free(b4); 143 free(b4);

mercurial