|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 |
|
33 #include "base64.h" |
|
34 |
|
35 #include <ucx/string.h> |
|
36 #include <libidav/utils.h> |
|
37 |
|
38 UCX_TEST(test_util_base64decode) { |
|
39 char *s1 = util_base64decode("YWJj"); |
|
40 char *s2 = util_base64decode("aGVsbG8gd29ybGQ="); |
|
41 char *s3 = util_base64decode("MA=="); |
|
42 char *s4 = util_base64decode("MHh4MQ=="); |
|
43 |
|
44 UCX_TEST_BEGIN; |
|
45 |
|
46 UCX_TEST_ASSERT(!strcmp(s1, "abc"), "s1 wrong"); |
|
47 UCX_TEST_ASSERT(!strcmp(s2, "hello world"), "s2 wrong"); |
|
48 UCX_TEST_ASSERT(!strcmp(s3, "0"), "s3 wrong"); |
|
49 UCX_TEST_ASSERT(!strcmp(s4, "0xx1"), "s4 wrong"); |
|
50 |
|
51 UCX_TEST_END; |
|
52 |
|
53 free(s1); |
|
54 free(s2); |
|
55 free(s3); |
|
56 free(s4); |
|
57 } |
|
58 |
|
59 UCX_TEST(test_util_base64decode_len) { |
|
60 int len1, len2, len3, len4, len5, len6; |
|
61 char *s1 = util_base64decode_len("aGVsbG8=", &len1); // 5 |
|
62 char *s2 = util_base64decode_len("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5", &len2); // 30 |
|
63 char *s3 = util_base64decode_len("Lg==", &len3); // 1 |
|
64 char *s4 = util_base64decode_len("bG9s", &len4); // 3 |
|
65 char *s5 = util_base64decode_len("YWJjLS0tLS0tM3g=", &len5); // 11 |
|
66 char *s6 = util_base64decode_len("YWJjZGVmZy4uLjs7MGhlbGxv", &len6); // 18 |
|
67 |
|
68 UCX_TEST_BEGIN; |
|
69 |
|
70 UCX_TEST_ASSERT(!strcmp(s1, "hello"), "s1 wrong"); |
|
71 UCX_TEST_ASSERT(len1 == 5, "len1 wrong"); |
|
72 |
|
73 UCX_TEST_ASSERT(!strcmp(s2, "012345678901234567890123456789"), "s2 wrong"); |
|
74 UCX_TEST_ASSERT(len2 == 30, "len2 wrong"); |
|
75 |
|
76 UCX_TEST_ASSERT(!strcmp(s3, "."), "s3 wrong"); |
|
77 UCX_TEST_ASSERT(len3 == 1, "len3 wrong"); |
|
78 |
|
79 UCX_TEST_ASSERT(!strcmp(s4, "lol"), "s4 wrong"); |
|
80 UCX_TEST_ASSERT(len4 == 3, "len4 wrong"); |
|
81 |
|
82 UCX_TEST_ASSERT(!strcmp(s5, "abc------3x"), "s5 wrong"); |
|
83 UCX_TEST_ASSERT(len5 == 11, "len5 wrong"); |
|
84 |
|
85 UCX_TEST_ASSERT(!strcmp(s6, "abcdefg...;;0hello"), "s6 failed"); |
|
86 UCX_TEST_ASSERT(len6 == 18, "len6 wrong"); |
|
87 |
|
88 UCX_TEST_END; |
|
89 |
|
90 free(s1); |
|
91 free(s2); |
|
92 free(s3); |
|
93 free(s4); |
|
94 free(s5); |
|
95 free(s6); |
|
96 } |
|
97 |
|
98 UCX_TEST(test_util_base64encode) { |
|
99 char *str1 = "hello world"; |
|
100 char *str2 = "test string"; |
|
101 char *str3 = "01234567890123456789012345678901234567890123456789"; |
|
102 char str4[8]; |
|
103 str4[0] = 0; |
|
104 str4[1] = 12; |
|
105 str4[2] = 3; |
|
106 str4[3] = 50; |
|
107 str4[4] = 2; |
|
108 str4[5] = 110; |
|
109 str4[6] = 63; |
|
110 str4[7] = 1; |
|
111 |
|
112 char *str5 = "a"; |
|
113 char *str6 = "ab"; |
|
114 char *str7 = "abc"; |
|
115 char *str8 = "abcd"; |
|
116 char *str9 = "abcde"; |
|
117 char *str10 = "abcdef"; |
|
118 |
|
119 char *b1 = util_base64encode(str1, strlen(str1)); |
|
120 char *b2 = util_base64encode(str2, strlen(str2)); |
|
121 char *b3 = util_base64encode(str3, strlen(str3)); |
|
122 char *b4 = util_base64encode(str4, 8); |
|
123 char *b5 = util_base64encode(str5, strlen(str5)); |
|
124 char *b6 = util_base64encode(str6, strlen(str6)); |
|
125 char *b7 = util_base64encode(str7, strlen(str7)); |
|
126 char *b8 = util_base64encode(str8, strlen(str8)); |
|
127 char *b9 = util_base64encode(str9, strlen(str9)); |
|
128 char *b10 = util_base64encode(str10, strlen(str10)); |
|
129 |
|
130 UCX_TEST_BEGIN; |
|
131 |
|
132 UCX_TEST_ASSERT(!strcmp(b1, "aGVsbG8gd29ybGQ="), "b1 failed"); |
|
133 UCX_TEST_ASSERT(!strcmp(b2, "dGVzdCBzdHJpbmc="), "b2 failed"); |
|
134 UCX_TEST_ASSERT(!strcmp(b3, "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk="), "b3 failed"); |
|
135 UCX_TEST_ASSERT(!strcmp(b4, "AAwDMgJuPwE="), "b4 failed"); |
|
136 |
|
137 UCX_TEST_ASSERT(!strcmp(b5, "YQ=="), "b5 failed"); |
|
138 UCX_TEST_ASSERT(!strcmp(b6, "YWI="), "b6 failed"); |
|
139 UCX_TEST_ASSERT(!strcmp(b7, "YWJj"), "b7 failed"); |
|
140 UCX_TEST_ASSERT(!strcmp(b8, "YWJjZA=="), "b8 failed"); |
|
141 UCX_TEST_ASSERT(!strcmp(b9, "YWJjZGU="), "b9 failed"); |
|
142 UCX_TEST_ASSERT(!strcmp(b10, "YWJjZGVm"), "b10 failed"); |
|
143 |
|
144 UCX_TEST_END; |
|
145 |
|
146 free(b1); |
|
147 free(b2); |
|
148 free(b3); |
|
149 free(b4); |
|
150 free(b5); |
|
151 free(b6); |
|
152 free(b7); |
|
153 free(b8); |
|
154 free(b9); |
|
155 free(b10); |
|
156 } |
|
157 |