test/base64.c

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

mercurial