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 g_assert_cmpmem (data
, length
, data2
, decoded_len
);
67 test_incremental_break (gconstpointer d
)
69 gint length
= GPOINTER_TO_INT (d
);
71 test_incremental (TRUE
, length
);
75 test_incremental_nobreak (gconstpointer d
)
77 gint length
= GPOINTER_TO_INT (d
);
79 test_incremental (FALSE
, length
);
83 test_full (gconstpointer d
)
85 gint length
= GPOINTER_TO_INT (d
);
90 text
= g_base64_encode (data
, length
);
91 data2
= g_base64_decode (text
, &len
);
94 g_assert_cmpmem (data
, length
, data2
, len
);
101 gint length
; /* of data */
102 guchar data
[DATA_SIZE
];
105 /* 100 pre-encoded string from data[] buffer. Data length from 1..100
107 static const char *ok_100_encode_strs
[] = {
120 "AAECAwQFBgcICQoLDA==",
121 "AAECAwQFBgcICQoLDA0=",
122 "AAECAwQFBgcICQoLDA0O",
123 "AAECAwQFBgcICQoLDA0ODw==",
124 "AAECAwQFBgcICQoLDA0ODxA=",
125 "AAECAwQFBgcICQoLDA0ODxAR",
126 "AAECAwQFBgcICQoLDA0ODxAREg==",
127 "AAECAwQFBgcICQoLDA0ODxAREhM=",
128 "AAECAwQFBgcICQoLDA0ODxAREhMU",
129 "AAECAwQFBgcICQoLDA0ODxAREhMUFQ==",
130 "AAECAwQFBgcICQoLDA0ODxAREhMUFRY=",
131 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYX",
132 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGA==",
133 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBk=",
134 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBka",
135 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGw==",
136 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxw=",
137 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd",
138 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHg==",
139 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=",
140 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8g",
141 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gIQ==",
142 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISI=",
143 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj",
144 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJA==",
145 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCU=",
146 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUm",
147 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJw==",
148 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJyg=",
149 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp",
150 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKg==",
151 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKis=",
152 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss",
153 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLQ==",
154 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4=",
155 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v",
156 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMA==",
157 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=",
158 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEy",
159 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMw==",
160 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ=",
161 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1",
162 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Ng==",
163 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc=",
164 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4",
165 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OQ==",
166 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo=",
167 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7",
168 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PA==",
169 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0=",
170 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+",
171 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==",
172 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0A=",
173 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BB",
174 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQg==",
175 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkM=",
176 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNE",
177 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERQ==",
178 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUY=",
179 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH",
180 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSA==",
181 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSEk=",
182 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElK",
183 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKSw==",
184 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0w=",
185 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xN",
186 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTg==",
187 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk8=",
188 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc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gcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFi",
207 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiYw==",
212 generate_databuffer_for_base64 (struct MyRawData
*p
)
215 for (i
= 0; i
< DATA_SIZE
; i
++)
220 test_base64_encode (void)
225 struct MyRawData myraw
;
227 generate_databuffer_for_base64 (&myraw
);
229 for (i
= 0; ok_100_encode_strs
[i
]; i
++)
232 text
= g_base64_encode (myraw
.data
, length
);
233 g_assert_cmpstr (text
, ==, ok_100_encode_strs
[i
]);
234 /* printf ("\"%s\",\n",text); */
241 decode_and_compare (const gchar
*datap
,
242 const struct MyRawData
*p
)
247 data2
= g_base64_decode (datap
, &len
);
248 g_assert_cmpmem (p
->data
, p
->length
, data2
, len
);
253 decode_inplace_and_compare (const gchar
*datap
,
254 const struct MyRawData
*p
)
260 data
= g_strdup (datap
);
261 data2
= g_base64_decode_inplace (data
, &len
);
262 g_assert_cmpmem (p
->data
, p
->length
, data2
, len
);
267 test_base64_decode (void)
270 struct MyRawData myraw
;
272 generate_databuffer_for_base64 (&myraw
);
274 for (i
= 0; ok_100_encode_strs
[i
]; i
++)
276 myraw
.length
= i
+ 1;
277 decode_and_compare (ok_100_encode_strs
[i
], &myraw
);
282 test_base64_decode_inplace (void)
285 struct MyRawData myraw
;
287 generate_databuffer_for_base64 (&myraw
);
289 for (i
= 0; ok_100_encode_strs
[i
]; i
++)
291 myraw
.length
= i
+ 1;
292 decode_inplace_and_compare (ok_100_encode_strs
[i
], &myraw
);
297 test_base64_encode_decode (void)
301 struct MyRawData myraw
;
303 generate_databuffer_for_base64 (&myraw
);
305 for (i
= 0; i
< DATA_SIZE
; i
++)
307 myraw
.length
= i
+ 1;
308 text
= g_base64_encode (myraw
.data
, myraw
.length
);
310 decode_and_compare (text
, &myraw
);
317 test_base64_decode_smallblock (gconstpointer blocksize_p
)
319 const guint blocksize
= GPOINTER_TO_UINT (blocksize_p
);
322 for (i
= 0; ok_100_encode_strs
[i
]; i
++)
324 const char *str
= ok_100_encode_strs
[i
];
326 gsize len
= strlen (str
);
330 gsize decoded_size
= 0;
331 guchar
*decoded_atonce
;
332 gsize decoded_atonce_size
= 0;
334 decoded
= g_malloc (len
/ 4 * 3 + 3);
339 int chunk_len
= MIN (blocksize
, len
);
340 gsize size
= g_base64_decode_step (p
, chunk_len
,
341 decoded
+ decoded_size
,
343 decoded_size
+= size
;
348 decoded_atonce
= g_base64_decode (str
, &decoded_atonce_size
);
350 g_assert_cmpmem (decoded
, decoded_size
, decoded_atonce
, decoded_atonce_size
);
353 g_free (decoded_atonce
);
359 main (int argc
, char *argv
[])
363 g_test_init (&argc
, &argv
, NULL
);
365 for (i
= 0; i
< DATA_SIZE
; i
++)
368 g_test_add_data_func ("/base64/full/1", GINT_TO_POINTER (DATA_SIZE
), test_full
);
369 g_test_add_data_func ("/base64/full/2", GINT_TO_POINTER (1), test_full
);
370 g_test_add_data_func ("/base64/full/3", GINT_TO_POINTER (2), test_full
);
371 g_test_add_data_func ("/base64/full/4", GINT_TO_POINTER (3), test_full
);
373 g_test_add_data_func ("/base64/incremental/nobreak/1", GINT_TO_POINTER (DATA_SIZE
), test_incremental_nobreak
);
374 g_test_add_data_func ("/base64/incremental/break/1", GINT_TO_POINTER (DATA_SIZE
), test_incremental_break
);
376 g_test_add_data_func ("/base64/incremental/nobreak/2", GINT_TO_POINTER (DATA_SIZE
- 1), test_incremental_nobreak
);
377 g_test_add_data_func ("/base64/incremental/break/2", GINT_TO_POINTER (DATA_SIZE
- 1), test_incremental_break
);
379 g_test_add_data_func ("/base64/incremental/nobreak/3", GINT_TO_POINTER (DATA_SIZE
- 2), test_incremental_nobreak
);
380 g_test_add_data_func ("/base64/incremental/break/3", GINT_TO_POINTER (DATA_SIZE
- 2), test_incremental_break
);
382 g_test_add_data_func ("/base64/incremental/nobreak/4-a", GINT_TO_POINTER (1), test_incremental_nobreak
);
383 g_test_add_data_func ("/base64/incremental/nobreak/4-b", GINT_TO_POINTER (2), test_incremental_nobreak
);
384 g_test_add_data_func ("/base64/incremental/nobreak/4-c", GINT_TO_POINTER (3), test_incremental_nobreak
);
386 g_test_add_func ("/base64/encode", test_base64_encode
);
387 g_test_add_func ("/base64/decode", test_base64_decode
);
388 g_test_add_func ("/base64/decode-inplace", test_base64_decode_inplace
);
389 g_test_add_func ("/base64/encode-decode", test_base64_encode_decode
);
391 g_test_add_data_func ("/base64/incremental/smallblock/1", GINT_TO_POINTER(1),
392 test_base64_decode_smallblock
);
393 g_test_add_data_func ("/base64/incremental/smallblock/2", GINT_TO_POINTER(2),
394 test_base64_decode_smallblock
);
395 g_test_add_data_func ("/base64/incremental/smallblock/3", GINT_TO_POINTER(3),
396 test_base64_decode_smallblock
);
397 g_test_add_data_func ("/base64/incremental/smallblock/4", GINT_TO_POINTER(4),
398 test_base64_decode_smallblock
);
400 return g_test_run ();