8 static guchar data[DATA_SIZE];
11 test_incremental (gboolean line_break,
15 gsize len, decoded_len, max, input_len, block_size;
21 data2 = g_malloc (length);
22 text = g_malloc (length * 4);
28 while (input_len < length)
30 block_size = MIN (BLOCK_SIZE, length - input_len);
31 len += g_base64_encode_step (data + input_len, block_size,
32 line_break, text + len, &state, &save);
33 input_len += block_size;
35 len += g_base64_encode_close (line_break, text + len, &state, &save);
38 max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
40 max = length * 4 / 3 + 6;
42 /* Check encoded length */
43 g_assert_cmpint (len, <=, max);
51 int chunk_len = MIN (BLOCK_SIZE, len);
52 decoded_len += g_base64_decode_step (p,
55 &state, &decoder_save);
60 /* Check decoded length */
61 g_assert_cmpint (decoded_len, ==, length);
62 /* Check decoded data */
63 g_assert (memcmp (data, data2, length) == 0);
70 test_incremental_break (gconstpointer d)
72 gint length = GPOINTER_TO_INT (d);
74 test_incremental (TRUE, length);
78 test_incremental_nobreak (gconstpointer d)
80 gint length = GPOINTER_TO_INT (d);
82 test_incremental (FALSE, length);
86 test_full (gconstpointer d)
88 gint length = GPOINTER_TO_INT (d);
93 text = g_base64_encode (data, length);
94 data2 = g_base64_decode (text, &len);
97 /* Check decoded length */
98 g_assert_cmpint (len, ==, length);
99 /* Check decoded base64 data */
100 g_assert (memcmp (data, data2, length) == 0);
107 gint length; /* of data */
108 guchar data[DATA_SIZE];
111 /* 100 pre-encoded string from data[] buffer. Data length from 1..100
113 static const char *ok_100_encode_strs[] = {
126 "AAECAwQFBgcICQoLDA==",
127 "AAECAwQFBgcICQoLDA0=",
128 "AAECAwQFBgcICQoLDA0O",
129 "AAECAwQFBgcICQoLDA0ODw==",
130 "AAECAwQFBgcICQoLDA0ODxA=",
131 "AAECAwQFBgcICQoLDA0ODxAR",
132 "AAECAwQFBgcICQoLDA0ODxAREg==",
133 "AAECAwQFBgcICQoLDA0ODxAREhM=",
134 "AAECAwQFBgcICQoLDA0ODxAREhMU",
135 "AAECAwQFBgcICQoLDA0ODxAREhMUFQ==",
136 "AAECAwQFBgcICQoLDA0ODxAREhMUFRY=",
137 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYX",
138 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGA==",
139 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBk=",
140 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBka",
141 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGw==",
142 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxw=",
143 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd",
144 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHg==",
145 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=",
146 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8g",
147 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gIQ==",
148 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISI=",
149 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj",
150 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJA==",
151 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCU=",
152 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUm",
153 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJw==",
154 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJyg=",
155 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp",
156 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKg==",
157 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKis=",
158 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss",
159 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLQ==",
160 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4=",
161 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v",
162 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMA==",
163 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=",
164 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEy",
165 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMw==",
166 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ=",
167 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1",
168 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Ng==",
169 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc=",
170 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4",
171 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OQ==",
172 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo=",
173 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7",
174 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PA==",
175 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0=",
176 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+",
177 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==",
178 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0A=",
179 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BB",
180 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQg==",
181 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkM=",
182 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNE",
183 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERQ==",
184 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUY=",
185 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH",
186 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSA==",
187 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSEk=",
188 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElK",
189 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKSw==",
190 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0w=",
191 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xN",
192 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTg==",
193 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk8=",
194 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEy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AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFi",
213 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiYw==",
218 generate_databuffer_for_base64 (struct MyRawData *p)
221 for (i = 0; i < DATA_SIZE; i++)
226 test_base64_encode (void)
231 struct MyRawData myraw;
233 generate_databuffer_for_base64 (&myraw);
235 for (i = 0; ok_100_encode_strs[i]; i++)
238 text = g_base64_encode (myraw.data, length);
239 g_assert_cmpstr (text, ==, ok_100_encode_strs[i]);
240 /* printf ("\"%s\",\n",text); */
247 decode_and_compare (const gchar *datap,
248 const struct MyRawData *p)
254 data2 = g_base64_decode (datap, &len);
255 g_assert_cmpint (len, ==, p->length);
256 /* g_print ("length: got %d, expected %d\n",len, length); */
257 memcmp_decode = memcmp (p->data, data2, p->length);
258 g_assert_cmpint (memcmp_decode, ==, 0);
263 decode_inplace_and_compare (const gchar *datap,
264 const struct MyRawData *p)
271 data = g_strdup (datap);
272 data2 = g_base64_decode_inplace (data, &len);
273 g_assert_cmpint (len, ==, p->length);
274 /* g_print ("length: got %d, expected %d\n",len, length); */
275 memcmp_decode = memcmp (p->data, data2, p->length);
276 g_assert_cmpint (memcmp_decode, ==, 0);
281 test_base64_decode (void)
284 struct MyRawData myraw;
286 generate_databuffer_for_base64 (&myraw);
288 for (i = 0; ok_100_encode_strs[i]; i++)
290 myraw.length = i + 1;
291 decode_and_compare (ok_100_encode_strs[i], &myraw);
296 test_base64_decode_inplace (void)
299 struct MyRawData myraw;
301 generate_databuffer_for_base64 (&myraw);
303 for (i = 0; ok_100_encode_strs[i]; i++)
305 myraw.length = i + 1;
306 decode_inplace_and_compare (ok_100_encode_strs[i], &myraw);
311 test_base64_encode_decode (void)
315 struct MyRawData myraw;
317 generate_databuffer_for_base64 (&myraw);
319 for (i = 0; i < DATA_SIZE; i++)
321 myraw.length = i + 1;
322 text = g_base64_encode (myraw.data, myraw.length);
324 decode_and_compare (text, &myraw);
331 test_base64_decode_smallblock (gconstpointer blocksize_p)
333 const guint blocksize = GPOINTER_TO_UINT (blocksize_p);
336 for (i = 0; ok_100_encode_strs[i]; i++)
338 const char *str = ok_100_encode_strs[i];
340 gsize len = strlen (str);
344 gsize decoded_size = 0;
345 guchar *decoded_atonce;
346 gsize decoded_atonce_size = 0;
348 decoded = g_malloc (len / 4 * 3 + 3);
353 int chunk_len = MIN (blocksize, len);
354 gsize size = g_base64_decode_step (p, chunk_len,
355 decoded + decoded_size,
357 decoded_size += size;
362 decoded_atonce = g_base64_decode (str, &decoded_atonce_size);
364 g_assert_cmpint (decoded_size, ==, decoded_atonce_size);
365 g_assert (memcmp (decoded, decoded_atonce, decoded_size) == 0);
368 g_free (decoded_atonce);
374 main (int argc, char *argv[])
378 g_test_init (&argc, &argv, NULL);
380 for (i = 0; i < DATA_SIZE; i++)
383 g_test_add_data_func ("/base64/full/1", GINT_TO_POINTER (DATA_SIZE), test_full);
384 g_test_add_data_func ("/base64/full/2", GINT_TO_POINTER (1), test_full);
385 g_test_add_data_func ("/base64/full/3", GINT_TO_POINTER (2), test_full);
386 g_test_add_data_func ("/base64/full/4", GINT_TO_POINTER (3), test_full);
388 g_test_add_data_func ("/base64/incremental/nobreak/1", GINT_TO_POINTER (DATA_SIZE), test_incremental_nobreak);
389 g_test_add_data_func ("/base64/incremental/break/1", GINT_TO_POINTER (DATA_SIZE), test_incremental_break);
391 g_test_add_data_func ("/base64/incremental/nobreak/2", GINT_TO_POINTER (DATA_SIZE - 1), test_incremental_nobreak);
392 g_test_add_data_func ("/base64/incremental/break/2", GINT_TO_POINTER (DATA_SIZE - 1), test_incremental_break);
394 g_test_add_data_func ("/base64/incremental/nobreak/3", GINT_TO_POINTER (DATA_SIZE - 2), test_incremental_nobreak);
395 g_test_add_data_func ("/base64/incremental/break/3", GINT_TO_POINTER (DATA_SIZE - 2), test_incremental_break);
397 g_test_add_data_func ("/base64/incremental/nobreak/4", GINT_TO_POINTER (1), test_incremental_nobreak);
398 g_test_add_data_func ("/base64/incremental/nobreak/4", GINT_TO_POINTER (2), test_incremental_nobreak);
399 g_test_add_data_func ("/base64/incremental/nobreak/4", GINT_TO_POINTER (3), test_incremental_nobreak);
401 g_test_add_func ("/base64/encode", test_base64_encode);
402 g_test_add_func ("/base64/decode", test_base64_decode);
403 g_test_add_func ("/base64/decode-inplace", test_base64_decode_inplace);
404 g_test_add_func ("/base64/encode-decode", test_base64_encode_decode);
406 g_test_add_data_func ("/base64/incremental/smallblock/1", GINT_TO_POINTER(1),
407 test_base64_decode_smallblock);
408 g_test_add_data_func ("/base64/incremental/smallblock/2", GINT_TO_POINTER(2),
409 test_base64_decode_smallblock);
410 g_test_add_data_func ("/base64/incremental/smallblock/3", GINT_TO_POINTER(3),
411 test_base64_decode_smallblock);
412 g_test_add_data_func ("/base64/incremental/smallblock/4", GINT_TO_POINTER(4),
413 test_base64_decode_smallblock);
415 return g_test_run ();