Move checksum tests to the test framework

This commit is contained in:
Matthias Clasen 2010-06-21 21:22:09 -04:00
parent 839078ea63
commit 5629366c3d
4 changed files with 56 additions and 53 deletions

View File

@ -84,6 +84,9 @@ utf8_validate_LDADD = $(progs_ldadd)
TEST_PROGS += utf8-misc TEST_PROGS += utf8-misc
utf8_misc_LDADD = $(progs_ldadd) utf8_misc_LDADD = $(progs_ldadd)
TEST_PROGS += checksum
checksum_LDADD = $(progs_ldadd)
if OS_UNIX if OS_UNIX
# some testing of gtester funcitonality # some testing of gtester funcitonality

View File

@ -582,95 +582,98 @@ const char *SHA256_sums[] = {
"df3a0c35d5345d6d792415c1310bd4589cdf68bac96ed599d6bb0c1545ffc86c" "df3a0c35d5345d6d792415c1310bd4589cdf68bac96ed599d6bb0c1545ffc86c"
}; };
typedef struct {
GChecksumType checksum_type;
const gchar *sum;
int length;
} ChecksumTest;
static void static void
test_checksum (GChecksumType checksum_type, test_checksum (gconstpointer d)
const gchar *type,
const gchar *sum,
int length)
{ {
const ChecksumTest *test = d;
GChecksum *checksum; GChecksum *checksum;
const char *p; const char *p;
int chunk_length; int chunk_length;
for (chunk_length = MIN (length, 1); chunk_length < length; chunk_length++) for (chunk_length = MIN (test->length, 1); chunk_length < test->length; chunk_length++)
{ {
checksum = g_checksum_new (checksum_type); checksum = g_checksum_new (test->checksum_type);
for (p = FIXED_STR; p < FIXED_STR + length; p += chunk_length) for (p = FIXED_STR; p < FIXED_STR + test->length; p += chunk_length)
{ {
g_checksum_update (checksum, (const guchar *)p, g_checksum_update (checksum, (const guchar *)p,
MIN (chunk_length, length - (p - FIXED_STR))); MIN (chunk_length, test->length - (p - FIXED_STR)));
}
if (strcmp (g_checksum_get_string (checksum), sum) != 0)
{
g_print ("Invalid %s checksum for `%.*s' (length %d) counting by %d:\n"
"%s (expecting: %s)\n",
type,
length, FIXED_STR,
length, chunk_length,
g_checksum_get_string (checksum),
sum);
exit (1);
} }
g_assert_cmpstr (g_checksum_get_string (checksum), ==, test->sum);
g_checksum_free (checksum); g_checksum_free (checksum);
} }
} }
typedef struct {
GChecksumType checksum_type;
const gchar **sums;
} ChecksumStringTest;
static void static void
test_checksum_string (GChecksumType checksum_type, test_checksum_string (gconstpointer d)
const gchar *type,
const gchar **sums)
{ {
const ChecksumStringTest *test = d;
int length; int length;
for (length = 0; length <= FIXED_LEN; length++) for (length = 0; length <= FIXED_LEN; length++)
{ {
const char *expected = sums[length];
char *checksum; char *checksum;
checksum = g_compute_checksum_for_string (checksum_type, checksum = g_compute_checksum_for_string (test->checksum_type,
FIXED_STR, FIXED_STR,
length); length);
if (strcmp (checksum, expected) != 0) g_assert_cmpstr (checksum, ==, test->sums[length]);
{
g_print ("Invalid %s checksum for `%.*s' (length %d):\n"
"%s (expecting: %s)\n",
type,
length, FIXED_STR, length,
checksum,
expected);
exit (1);
}
g_free (checksum); g_free (checksum);
} }
} }
#define test(type, length) test_checksum (G_CHECKSUM_##type, \ #define test(type, length) { \
#type, \ ChecksumTest *test; \
type##_sums[length], \ gchar *path; \
length) test = g_new0 (ChecksumTest, 1); \
test->checksum_type = G_CHECKSUM_##type; \
test->sum = type##_sums[length]; \
test->length = length; \
path = g_strdup_printf ("/checksum/%s/%d", #type, length); \
g_test_add_data_func (path, test, test_checksum); \
g_free (path); \
}
#define test_string(type) test_checksum_string (G_CHECKSUM_##type, \ #define test_string(type) { \
#type, \ ChecksumStringTest *test; \
type##_sums) gchar *path; \
test = g_new0 (ChecksumStringTest, 1); \
test->checksum_type = G_CHECKSUM_##type; \
test->sums = type##_sums; \
path = g_strdup_printf ("/checksum/%s/string", #type); \
g_test_add_data_func (path, test, test_checksum_string); \
g_free (path); \
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
int length; int length;
for (length = 0; length <= FIXED_LEN; length++) g_test_init (&argc, &argv, NULL);
{
test (MD5, length);
test (SHA1, length);
test (SHA256, length);
}
for (length = 0; length <= FIXED_LEN; length++)
test (MD5, length);
test_string (MD5); test_string (MD5);
for (length = 0; length <= FIXED_LEN; length++)
test (SHA1, length);
test_string (SHA1); test_string (SHA1);
for (length = 0; length <= FIXED_LEN; length++)
test (SHA256, length);
test_string (SHA256); test_string (SHA256);
return EXIT_SUCCESS; return g_test_run ();
} }

1
tests/.gitignore vendored
View File

@ -6,7 +6,6 @@ atomic-test
base64-test base64-test
bit-test bit-test
bookmarkfile-test bookmarkfile-test
checksum-test
child-test child-test
closures closures
collate.out collate.out

View File

@ -87,7 +87,6 @@ test_programs = \
atomic-test \ atomic-test \
bit-test \ bit-test \
$(CXX_TEST) \ $(CXX_TEST) \
checksum-test \
child-test \ child-test \
completion-test \ completion-test \
convert-test \ convert-test \
@ -146,7 +145,6 @@ module_ldadd = $(libgmodule) $(G_MODULE_LIBS) $(progs_ldadd)
atomic_test_LDADD = $(progs_ldadd) atomic_test_LDADD = $(progs_ldadd)
bit_test_LDADD = $(progs_ldadd) bit_test_LDADD = $(progs_ldadd)
bookmarkfile_test_LDADD = $(progs_ldadd) bookmarkfile_test_LDADD = $(progs_ldadd)
checksum_test_LDADD = $(progs_ldadd)
child_test_LDADD = $(thread_ldadd) child_test_LDADD = $(thread_ldadd)
completion_test_LDADD = $(progs_ldadd) completion_test_LDADD = $(progs_ldadd)
convert_test_LDADD = $(progs_ldadd) convert_test_LDADD = $(progs_ldadd)