Add some more tests.

2006-05-03  Matthias Clasen  <mclasen@redhat.com>

	* tests/base64-test.c: Add some more tests.
This commit is contained in:
Matthias Clasen
2006-05-03 15:27:40 +00:00
committed by Matthias Clasen
parent e1d80e9f7c
commit 92dc9fe794
3 changed files with 43 additions and 19 deletions

View File

@@ -1,5 +1,11 @@
2006-05-03 Matthias Clasen <mclasen@redhat.com> 2006-05-03 Matthias Clasen <mclasen@redhat.com>
* tests/base64-test.c: Add some more tests.
* glib/gbase64.c (g_base64_decode_step): Avoid writing
beyond the guaranteed lenght of the output buffer, if
there is padding.
* tests/base64-test.c (test_incremental): Use malloced memory * tests/base64-test.c (test_incremental): Use malloced memory
instead of stack-allocated, so that MALLOC_CHECK_=2 catches instead of stack-allocated, so that MALLOC_CHECK_=2 catches
the OOB write... the OOB write...

View File

@@ -1,5 +1,11 @@
2006-05-03 Matthias Clasen <mclasen@redhat.com> 2006-05-03 Matthias Clasen <mclasen@redhat.com>
* tests/base64-test.c: Add some more tests.
* glib/gbase64.c (g_base64_decode_step): Avoid writing
beyond the guaranteed lenght of the output buffer, if
there is padding.
* tests/base64-test.c (test_incremental): Use malloced memory * tests/base64-test.c (test_incremental): Use malloced memory
instead of stack-allocated, so that MALLOC_CHECK_=2 catches instead of stack-allocated, so that MALLOC_CHECK_=2 catches
the OOB write... the OOB write...

View File

@@ -9,34 +9,39 @@
static guchar data[DATA_SIZE]; static guchar data[DATA_SIZE];
static void static void
test_incremental (gboolean line_break) test_incremental (gboolean line_break,
gint length)
{ {
char *p; char *p;
int i; gsize len, decoded_len, max, input_len, block_size;
gsize len, decoded_len, max;
int state, save; int state, save;
guint decoder_save; guint decoder_save;
char *text; char *text;
guchar *data2; guchar *data2;
data2 = g_malloc (DATA_SIZE); data2 = g_malloc (length);
text = g_malloc (DATA_SIZE * 2); text = g_malloc (length * 2);
len = 0; len = 0;
state = 0; state = 0;
save = 0; save = 0;
for (i = 0; i < NUM_BLOCKS; i++) input_len = 0;
len += g_base64_encode_step (data + i * BLOCK_SIZE, BLOCK_SIZE, while (input_len < length)
{
block_size = MIN (BLOCK_SIZE, length - input_len);
len += g_base64_encode_step (data + input_len, block_size,
line_break, text + len, &state, &save); line_break, text + len, &state, &save);
input_len += block_size;
}
len += g_base64_encode_close (line_break, text + len, &state, &save); len += g_base64_encode_close (line_break, text + len, &state, &save);
if (line_break) if (line_break)
max = DATA_SIZE * 4 / 3 + DATA_SIZE * 4 / (3 * 72) + 7; max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
else else
max = DATA_SIZE * 4 / 3 + 6; max = length * 4 / 3 + 6;
if (len > max) if (len > max)
{ {
g_print ("To long encoded length: got %d, expected max %d\n", g_print ("Too long encoded length: got %d, expected max %d\n",
len, max); len, max);
exit (1); exit (1);
} }
@@ -47,7 +52,7 @@ test_incremental (gboolean line_break)
p = text; p = text;
while (len > 0) while (len > 0)
{ {
int chunk_len = MAX (32, len); int chunk_len = MIN (BLOCK_SIZE, len);
decoded_len += g_base64_decode_step (p, decoded_len += g_base64_decode_step (p,
chunk_len, chunk_len,
data2 + decoded_len, data2 + decoded_len,
@@ -56,14 +61,14 @@ test_incremental (gboolean line_break)
len -= chunk_len; len -= chunk_len;
} }
if (decoded_len != DATA_SIZE) if (decoded_len != length)
{ {
g_print ("Wrong decoded length: got %d, expected %d\n", g_print ("Wrong decoded length: got %d, expected %d\n",
decoded_len, DATA_SIZE); decoded_len, length);
exit (1); exit (1);
} }
if (memcmp (data, data2, DATA_SIZE) != 0) if (memcmp (data, data2, length) != 0)
{ {
g_print ("Wrong decoded base64 data\n"); g_print ("Wrong decoded base64 data\n");
exit (1); exit (1);
@@ -108,8 +113,15 @@ main (int argc, char *argv[])
data[i] = (guchar)i; data[i] = (guchar)i;
test_full (); test_full ();
test_incremental (FALSE);
test_incremental (TRUE); test_incremental (FALSE, DATA_SIZE);
test_incremental (TRUE, DATA_SIZE);
test_incremental (FALSE, DATA_SIZE - 1);
test_incremental (TRUE, DATA_SIZE - 1);
test_incremental (FALSE, DATA_SIZE - 2);
test_incremental (TRUE, DATA_SIZE - 2);
return 0; return 0;
} }