diff options
author | Stef Walter <stefw@collabora.co.uk> | 2011-02-23 18:55:29 +0100 |
---|---|---|
committer | Stef Walter <stefw@collabora.co.uk> | 2011-02-27 09:34:19 +0100 |
commit | adb9d8dec35cae577a80d0ee558ef1cf681cbd31 (patch) | |
tree | 7c3d0b76486d40c9c3d714caa328e394e1c98b1c /egg | |
parent | e7e9103be95bd3d9f8e894c8c07b343ac669d86e (diff) | |
download | gnome-keyring-adb9d8dec35cae577a80d0ee558ef1cf681cbd31.tar.gz |
Use more modern gtest style for egg tests.
Diffstat (limited to 'egg')
-rw-r--r-- | egg/Makefile.am | 1 | ||||
-rw-r--r-- | egg/egg-hkdf.h | 39 | ||||
-rw-r--r-- | egg/egg-testing.c | 178 | ||||
-rw-r--r-- | egg/egg-testing.h | 54 | ||||
-rw-r--r-- | egg/tests/Makefile.am | 71 | ||||
-rw-r--r-- | egg/tests/files/dh-params.pem (renamed from egg/tests/test-data/dh-params.pem) | 0 | ||||
-rw-r--r-- | egg/tests/files/echo-script.sh (renamed from egg/tests/test-data/echo-script.sh) | 0 | ||||
-rw-r--r-- | egg/tests/files/pem-rsa-enc.key (renamed from egg/tests/test-data/pem-rsa-enc.key) | 0 | ||||
-rw-r--r-- | egg/tests/files/test-certificate-1.der (renamed from egg/tests/test-data/test-certificate-1.der) | bin | 813 -> 813 bytes | |||
-rw-r--r-- | egg/tests/files/test-personalname-1.der (renamed from egg/tests/test-data/test-personalname-1.der) | 0 | ||||
-rw-r--r-- | egg/tests/files/test-pkcs12-1.der (renamed from egg/tests/test-data/test-pkcs12-1.der) | bin | 2824 -> 2824 bytes | |||
-rwxr-xr-x | egg/tests/files/test-pkcs7-1.der (renamed from egg/tests/test-data/test-pkcs7-1.der) | bin | 1002 -> 1002 bytes | |||
-rwxr-xr-x | egg/tests/files/test-pkcs7-2.der (renamed from egg/tests/test-data/test-pkcs7-2.der) | bin | 1002 -> 1002 bytes | |||
-rw-r--r-- | egg/tests/files/test-pkcs8-1.der (renamed from egg/tests/test-data/test-pkcs8-1.der) | bin | 335 -> 335 bytes | |||
-rw-r--r-- | egg/tests/files/test-rsakey-1.der (renamed from egg/tests/test-data/test-rsakey-1.der) | bin | 609 -> 609 bytes | |||
-rw-r--r-- | egg/tests/test-asn1.c | 539 | ||||
-rw-r--r-- | egg/tests/test-asn1x.c | 53 | ||||
-rw-r--r-- | egg/tests/test-cleanup.c (renamed from egg/tests/unit-test-cleanup.c) | 77 | ||||
-rw-r--r-- | egg/tests/test-dh.c (renamed from egg/tests/unit-test-dh.c) | 66 | ||||
-rw-r--r-- | egg/tests/test-dn.c | 70 | ||||
-rw-r--r-- | egg/tests/test-hex.c (renamed from egg/tests/unit-test-hex.c) | 41 | ||||
-rw-r--r-- | egg/tests/test-oid.c (renamed from egg/tests/unit-test-oid.c) | 24 | ||||
-rw-r--r-- | egg/tests/test-openssl.c (renamed from egg/tests/unit-test-openssl.c) | 122 | ||||
-rw-r--r-- | egg/tests/test-padding.c | 134 | ||||
-rw-r--r-- | egg/tests/test-secmem.c (renamed from egg/tests/unit-test-secmem.c) | 98 | ||||
-rw-r--r-- | egg/tests/test-spawn.c (renamed from egg/tests/unit-test-spawn.c) | 64 | ||||
-rw-r--r-- | egg/tests/test-symkey.c (renamed from egg/tests/unit-test-symkey.c) | 168 | ||||
-rw-r--r-- | egg/tests/unit-test-asn1.c | 427 |
28 files changed, 1412 insertions, 814 deletions
diff --git a/egg/Makefile.am b/egg/Makefile.am index a42ce85f..8b3bd242 100644 --- a/egg/Makefile.am +++ b/egg/Makefile.am @@ -39,6 +39,7 @@ libegg_la_SOURCES = \ egg-secure-memory.c egg-secure-memory.h \ egg-spawn.c egg-spawn.h \ egg-symkey.c egg-symkey.h \ + egg-testing.c egg-testing.h \ egg-asn1-defs.h \ $(BUILT_SOURCES) diff --git a/egg/egg-hkdf.h b/egg/egg-hkdf.h new file mode 100644 index 00000000..430d3314 --- /dev/null +++ b/egg/egg-hkdf.h @@ -0,0 +1,39 @@ +/* + * gnome-keyring + * + * Copyright (C) 2011 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Author: Stef Walter <stefw@collabora.co.uk> + */ + +#ifndef EGG_HKDF_H_ +#define EGG_HKDF_H_ + +#include <glib.h> + +gboolean egg_hkdf_perform (const gchar *hash_algo, + gconstpointer input, + gsize n_input, + gconstpointer salt, + gsize n_salt, + gconstpointer info, + gsize n_info, + gpointer output, + gsize n_output); + +#endif /* EGG_HKDF_H_ */ diff --git a/egg/egg-testing.c b/egg/egg-testing.c new file mode 100644 index 00000000..b41ec852 --- /dev/null +++ b/egg/egg-testing.c @@ -0,0 +1,178 @@ +/* + * gnome-keyring + * + * Copyright (C) 2011 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Stef Walter <stefw@collabora.co.uk> + */ + +#include "config.h" + +#include "egg-testing.h" + +#include <errno.h> +#include <unistd.h> + +static GCond *wait_condition = NULL; +static GCond *wait_start = NULL; +static GMutex *wait_mutex = NULL; +static gboolean wait_waiting = FALSE; + +static const char HEXC[] = "0123456789ABCDEF"; + +static gchar* +hex_dump (const guchar *data, gsize n_data) +{ + GString *result; + gsize i; + guchar j; + + g_assert (data); + + result = g_string_sized_new (n_data * 2 + 1); + for (i = 0; i < n_data; ++i) { + g_string_append (result, "\\x"); + + j = data[i] >> 4 & 0xf; + g_string_append_c (result, HEXC[j]); + j = data[i] & 0xf; + g_string_append_c (result, HEXC[j]); + } + + return g_string_free (result, FALSE); +} + +void +egg_assertion_message_cmpmem (const char *domain, + const char *file, + int line, + const char *func, + const char *expr, + gconstpointer arg1, + gsize n_arg1, + const char *cmp, + gconstpointer arg2, + gsize n_arg2) +{ + char *a1, *a2, *s; + a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL"); + a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL"); + s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2); + g_free (a1); + g_free (a2); + g_assertion_message (domain, file, line, func, s); + g_free (s); +} + +void +egg_tests_chdir_base (gchar* argv0) +{ + gchar *dir, *base; + + dir = g_path_get_dirname (argv0); + if (chdir (dir) < 0) + g_warning ("couldn't change directory to: %s: %s", + dir, g_strerror (errno)); + + base = g_path_get_basename (dir); + if (strcmp (base, ".libs") == 0) { + if (chdir ("..") < 0) + g_warning ("couldn't change directory to ..: %s", + g_strerror (errno)); + } + + g_free (base); + g_free (dir); +} + +void +egg_test_wait_stop (void) +{ + GTimeVal tv; + + g_get_current_time (&tv); + g_time_val_add (&tv, 1000); + + g_assert (wait_mutex); + g_assert (wait_condition); + g_mutex_lock (wait_mutex); + if (!wait_waiting) + g_cond_timed_wait (wait_start, wait_mutex, &tv); + g_assert (wait_waiting); + g_cond_broadcast (wait_condition); + g_mutex_unlock (wait_mutex); +} + +gboolean +egg_test_wait_until (int timeout) +{ + GTimeVal tv; + gboolean ret; + + g_get_current_time (&tv); + g_time_val_add (&tv, timeout * 1000); + + g_assert (wait_mutex); + g_assert (wait_condition); + g_mutex_lock (wait_mutex); + g_assert (!wait_waiting); + wait_waiting = TRUE; + g_cond_broadcast (wait_start); + ret = g_cond_timed_wait (wait_condition, wait_mutex, &tv); + g_assert (wait_waiting); + wait_waiting = FALSE; + g_mutex_unlock (wait_mutex); + + return ret; +} + +static gpointer +testing_thread (gpointer loop) +{ + /* Must have been defined by the test including this file */ + gint ret = g_test_run (); + g_main_loop_quit (loop); + return GINT_TO_POINTER (ret); +} + +gint +egg_tests_run_in_thread_with_loop (void) +{ + GThread *thread; + GMainLoop *loop; + gpointer ret; + + g_thread_init (NULL); + + loop = g_main_loop_new (NULL, FALSE); + wait_condition = g_cond_new (); + wait_start = g_cond_new (); + wait_mutex = g_mutex_new (); + + thread = g_thread_create (testing_thread, loop, TRUE, NULL); + g_assert (thread); + + g_main_loop_run (loop); + ret = g_thread_join (thread); + g_main_loop_unref (loop); + + g_cond_free (wait_condition); + g_mutex_free (wait_mutex); + + return GPOINTER_TO_INT (ret); +} diff --git a/egg/egg-testing.h b/egg/egg-testing.h new file mode 100644 index 00000000..0d74d812 --- /dev/null +++ b/egg/egg-testing.h @@ -0,0 +1,54 @@ +/* + * gnome-keyring + * + * Copyright (C) 2011 Stefan Walter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Author: Stef Walter <stefw@collabora.co.uk> + */ + +#ifndef EGG_TESTING_H_ +#define EGG_TESTING_H_ + +#include <glib.h> +#include <string.h> + +#define egg_assert_cmpsize(a, o, b) \ + g_assert_cmpuint ((guint)(a), o, (guint)(b)) + +#define egg_assert_cmpmem(a, na, cmp, b, nb) \ + do { gconstpointer __p1 = (a), __p2 = (b); gsize __n1 = (na), __n2 = (nb); \ + if (__n1 cmp __n2 && memcmp (__p1, __p2, __n1) cmp 0) ; else \ + egg_assertion_message_cmpmem (G_LOG_DOMAIN, __FILE__, __LINE__, \ + G_STRFUNC, #a "[" #na"] " #cmp " " #b "[" #nb "]", \ + __p1, __n1, #cmp, __p2, __n2); } while (0) + +void egg_assertion_message_cmpmem (const char *domain, const char *file, + int line, const char *func, + const char *expr, gconstpointer arg1, + gsize n_arg1, const char *cmp, + gconstpointer arg2, gsize n_arg2); + +void egg_test_wait_stop (void); + +gboolean egg_test_wait_until (int timeout); + +void egg_tests_chdir_base (gchar* argv0); + +gint egg_tests_run_in_thread_with_loop (void); + +#endif /* EGG_DH_H_ */ diff --git a/egg/tests/Makefile.am b/egg/tests/Makefile.am index a408ad2a..4f7fa48b 100644 --- a/egg/tests/Makefile.am +++ b/egg/tests/Makefile.am @@ -1,41 +1,53 @@ + asn1-def-test.c: test.asn $(ASN1PARSER) -o asn1-def-test.c $(srcdir)/test.asn -TESTING_SOURCES = \ - asn1-def-test.c - -# Test files should be listed in order they need to run -TESTING_FILES = \ +INCLUDES = \ + -I$(top_srcdir)/egg \ + -DSRCDIR=$(srcdir) \ + $(GLIB_CFLAGS) + +LDADD = \ + $(top_builddir)/egg/libegg.la \ + $(LIBGCRYPT_LIBS) \ + $(GTHREAD_LIBS) \ + $(GLIB_LIBS) + +TEST_PROGS = \ + test-asn1 \ + test-dn \ + test-cleanup \ + test-hex \ + test-oid \ + test-secmem \ + test-padding \ + test-symkey \ + test-openssl \ + test-dh \ + test-spawn + +test_asn1_SOURCES = \ test-asn1.c \ - unit-test-asn1.c \ - test-dn.c \ - unit-test-cleanup.c \ - unit-test-hex.c \ - unit-test-oid.c \ - test-padding.c \ - unit-test-secmem.c \ - unit-test-symkey.c \ - unit-test-openssl.c \ - unit-test-dh.c \ - unit-test-spawn.c - -UNIT_PROMPT = - -TESTING_LIBS = \ - $(top_builddir)/egg/libegg.la - -include $(top_srcdir)/testing/testing.make - -BUILT_SOURCES += \ asn1-def-test.c -EXTRA_DIST += \ +check_PROGRAMS = $(TEST_PROGS) + +test: $(TEST_PROGS) + SRCDIR='$(srcdir)' gtester -k --verbose ${TEST_PROGS} + +check-local: test + +if WITH_TESTS +all-local: $(check_PROGRAMS) +endif + +EXTRA_DIST = \ test.asn \ - test-data + files # ------------------------------------------------------------------------------ -noinst_PROGRAMS += \ +noinst_PROGRAMS = \ test-asn1x test_asn1x_SOURCES = \ @@ -43,4 +55,5 @@ test_asn1x_SOURCES = \ test_asn1x_LDADD = \ $(top_builddir)/egg/libegg-asn1x.la \ - $(LIBTASN1_LIBS) + $(LIBTASN1_LIBS) \ + $(LDADD) diff --git a/egg/tests/test-data/dh-params.pem b/egg/tests/files/dh-params.pem index cc0afd87..cc0afd87 100644 --- a/egg/tests/test-data/dh-params.pem +++ b/egg/tests/files/dh-params.pem diff --git a/egg/tests/test-data/echo-script.sh b/egg/tests/files/echo-script.sh index aa0f7a93..aa0f7a93 100644 --- a/egg/tests/test-data/echo-script.sh +++ b/egg/tests/files/echo-script.sh diff --git a/egg/tests/test-data/pem-rsa-enc.key b/egg/tests/files/pem-rsa-enc.key index 65439fe6..65439fe6 100644 --- a/egg/tests/test-data/pem-rsa-enc.key +++ b/egg/tests/files/pem-rsa-enc.key diff --git a/egg/tests/test-data/test-certificate-1.der b/egg/tests/files/test-certificate-1.der Binary files differindex 1c084370..1c084370 100644 --- a/egg/tests/test-data/test-certificate-1.der +++ b/egg/tests/files/test-certificate-1.der diff --git a/egg/tests/test-data/test-personalname-1.der b/egg/tests/files/test-personalname-1.der index 60d5d8c4..60d5d8c4 100644 --- a/egg/tests/test-data/test-personalname-1.der +++ b/egg/tests/files/test-personalname-1.der diff --git a/egg/tests/test-data/test-pkcs12-1.der b/egg/tests/files/test-pkcs12-1.der Binary files differindex 285d8dd5..285d8dd5 100644 --- a/egg/tests/test-data/test-pkcs12-1.der +++ b/egg/tests/files/test-pkcs12-1.der diff --git a/egg/tests/test-data/test-pkcs7-1.der b/egg/tests/files/test-pkcs7-1.der Binary files differindex d45b9e0e..d45b9e0e 100755 --- a/egg/tests/test-data/test-pkcs7-1.der +++ b/egg/tests/files/test-pkcs7-1.der diff --git a/egg/tests/test-data/test-pkcs7-2.der b/egg/tests/files/test-pkcs7-2.der Binary files differindex d45b9e0e..d45b9e0e 100755 --- a/egg/tests/test-data/test-pkcs7-2.der +++ b/egg/tests/files/test-pkcs7-2.der diff --git a/egg/tests/test-data/test-pkcs8-1.der b/egg/tests/files/test-pkcs8-1.der Binary files differindex 8b61684e..8b61684e 100644 --- a/egg/tests/test-data/test-pkcs8-1.der +++ b/egg/tests/files/test-pkcs8-1.der diff --git a/egg/tests/test-data/test-rsakey-1.der b/egg/tests/files/test-rsakey-1.der Binary files differindex 878fda5b..878fda5b 100644 --- a/egg/tests/test-data/test-rsakey-1.der +++ b/egg/tests/files/test-rsakey-1.der diff --git a/egg/tests/test-asn1.c b/egg/tests/test-asn1.c index 8e2ac79f..39f188dc 100644 --- a/egg/tests/test-asn1.c +++ b/egg/tests/test-asn1.c @@ -23,9 +23,9 @@ #include "config.h" -#include "test-suite.h" - -#include "egg/egg-asn1x.h" +#include "egg-asn1x.h" +#include "egg-asn1-defs.h" +#include "egg-testing.h" #include <glib.h> #include <libtasn1.h> @@ -55,7 +55,8 @@ const gchar ENUM_THREE[] = "\x0A\x01\x03"; #define XL(x) G_N_ELEMENTS (x) - 1 -TESTING_TEST(asn1_boolean) +static void +test_boolean (void) { GNode *asn; gboolean value; @@ -92,7 +93,8 @@ TESTING_TEST(asn1_boolean) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_integer) +static void +test_integer (void) { GNode *asn; gulong value; @@ -120,7 +122,8 @@ TESTING_TEST(asn1_integer) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_octet_string) +static void +test_octet_string (void) { GNode *asn; gchar *value; @@ -148,7 +151,8 @@ TESTING_TEST(asn1_octet_string) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_generalized_time) +static void +test_generalized_time (void) { GNode *asn; glong value; @@ -175,7 +179,8 @@ TESTING_TEST(asn1_generalized_time) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_implicit) +static void +test_implicit (void) { GNode *asn; gchar *value; @@ -193,7 +198,8 @@ TESTING_TEST(asn1_implicit) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_explicit) +static void +test_explicit (void) { GNode *asn; gchar *value; @@ -211,7 +217,8 @@ TESTING_TEST(asn1_explicit) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_decode) +static void +test_bit_string_decode (void) { GNode *asn; guchar *bits; @@ -235,7 +242,8 @@ TESTING_TEST(asn1_bit_string_decode) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_decode_bad) +static void +test_bit_string_decode_bad (void) { GNode *asn; @@ -249,7 +257,8 @@ TESTING_TEST(asn1_bit_string_decode_bad) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_decode_ulong) +static void +test_bit_string_decode_ulong (void) { GNode *asn; gulong bits; @@ -271,7 +280,8 @@ TESTING_TEST(asn1_bit_string_decode_ulong) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_encode_decode) +static void +test_bit_string_encode_decode (void) { GNode *asn; guchar bits[] = { 0x5d, 0x6e, 0x83 }; @@ -305,7 +315,8 @@ TESTING_TEST(asn1_bit_string_encode_decode) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_encode_decode_ulong) +static void +test_bit_string_encode_decode_ulong (void) { GNode *asn; gulong check, bits = 0x0101b977; @@ -335,7 +346,8 @@ TESTING_TEST(asn1_bit_string_encode_decode_ulong) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_bit_string_encode_decode_zero) +static void +test_bit_string_encode_decode_zero (void) { GNode *asn; gpointer data; @@ -350,14 +362,15 @@ TESTING_TEST(asn1_bit_string_encode_decode_zero) data = egg_asn1x_encode (asn, NULL, &n_data); g_assert (data); - g_assert_cmpsize (n_data, ==, XL (BITS_ZERO)); + egg_assert_cmpsize (n_data, ==, XL (BITS_ZERO)); g_assert (memcmp (data, BITS_ZERO, n_data) == 0); g_free (data); egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_have) +static void +test_have (void) { GNode *asn; guchar *data; @@ -391,7 +404,8 @@ test_is_freed (gpointer unused) is_freed = TRUE; } -TESTING_TEST(asn1_any_set_raw) +static void +test_any_set_raw (void) { GNode *asn, *node; guchar *data; @@ -414,13 +428,13 @@ TESTING_TEST(asn1_any_set_raw) data = egg_asn1x_encode (asn, NULL, &n_data); g_assert (data); - g_assert_cmpsize (n_data, ==, XL (SEQ_ENCODING)); + egg_assert_cmpsize (n_data, ==, XL (SEQ_ENCODING)); g_assert (memcmp (data, SEQ_ENCODING, n_data) == 0); check = egg_asn1x_get_raw_element (node, &n_check); g_assert (check); - g_assert_cmpsize (n_check, ==, XL (SFARNSWORTH)); + egg_assert_cmpsize (n_check, ==, XL (SFARNSWORTH)); g_assert (memcmp (check, SFARNSWORTH, n_check) == 0); g_free (data); @@ -428,7 +442,8 @@ TESTING_TEST(asn1_any_set_raw) g_assert (is_freed); } -TESTING_TEST(asn1_any_set_raw_explicit) +static void +test_any_set_raw_explicit (void) { GNode *asn, *node; guchar *data; @@ -451,7 +466,7 @@ TESTING_TEST(asn1_any_set_raw_explicit) data = egg_asn1x_encode (asn, NULL, &n_data); g_assert (data); - g_assert_cmpsize (n_data, ==, XL (SEQ_ENCODING)); + egg_assert_cmpsize (n_data, ==, XL (SEQ_ENCODING)); g_assert (memcmp (data, SEQ_ENCODING, n_data) == 0); check = egg_asn1x_get_raw_element (node, &n_check); @@ -465,7 +480,8 @@ TESTING_TEST(asn1_any_set_raw_explicit) g_assert (is_freed); } -TESTING_TEST(asn1_choice_not_chosen) +static void +test_choice_not_chosen (void) { GNode *asn, *node; guchar *data; @@ -517,7 +533,7 @@ perform_asn1_any_choice_set_raw (const gchar *choice, const gchar *encoding, gsi } g_assert (data); - g_assert_cmpsize (n_data, ==, n_encoding); + egg_assert_cmpsize (n_data, ==, n_encoding); g_assert (memcmp (data, encoding, n_data) == 0); check = egg_asn1x_get_raw_element (node, &n_check); @@ -531,19 +547,22 @@ perform_asn1_any_choice_set_raw (const gchar *choice, const gchar *encoding, gsi g_assert (is_freed); } -TESTING_TEST(asn1_any_choice_set_raw_short_tag) +static void +test_any_choice_set_raw_short_tag (void) { const gchar ENCODING[] = "\xBE\x0C\x04\x0A""farnsworth"; perform_asn1_any_choice_set_raw ("choiceShortTag", ENCODING, XL (ENCODING)); } -TESTING_TEST(asn1_any_choice_set_raw_long_tag) +static void +test_any_choice_set_raw_long_tag (void) { const gchar ENCODING[] = "\xBF\x1F\x0C\x04\x0A""farnsworth"; perform_asn1_any_choice_set_raw ("choiceLongTag", ENCODING, XL (ENCODING)); } -TESTING_TEST(asn1_append) +static void +test_append (void) { GNode *asn; GNode *child; @@ -576,7 +595,8 @@ TESTING_TEST(asn1_append) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_append_and_clear) +static void +test_append_and_clear (void) { GNode *asn; gpointer data; @@ -606,7 +626,8 @@ TESTING_TEST(asn1_append_and_clear) g_free (data); } -TESTING_TEST(asn1_setof) +static void +test_setof (void) { GNode *asn; gpointer data; @@ -642,7 +663,8 @@ TESTING_TEST(asn1_setof) egg_asn1x_destroy (asn); } -TESTING_TEST(asn1_setof_empty) +static void +test_setof_empty (void) { GNode *asn; gpointer data; @@ -667,7 +689,8 @@ TESTING_TEST(asn1_setof_empty) egg_asn1x_destroy (asn); } -TESTING_TEST (asn1_enumerated) +static void +test_enumerated (void) { GNode *asn; gpointer data; @@ -693,3 +716,457 @@ TESTING_TEST (asn1_enumerated) g_free (data); egg_asn1x_destroy (asn); } + +typedef struct { + GNode *asn1; + guchar *data; + gsize n_data; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + if (!g_file_get_contents ("files/test-certificate-1.der", (gchar**)&test->data, + &test->n_data, NULL)) + g_assert_not_reached (); + + test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate"); + g_assert (test->asn1 != NULL); + + if (!egg_asn1x_decode (test->asn1, test->data, test->n_data)) + g_assert_not_reached (); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + egg_asn1x_destroy (test->asn1); + g_free (test->data); +} + +static void +test_node_name (Test* test, gconstpointer unused) +{ + g_assert_cmpstr (egg_asn1x_name (test->asn1), ==, "Certificate"); +} + +static void +test_asn1_integers (Test* test, gconstpointer unused) +{ + GNode *asn; + guchar *data; + gsize n_data; + gboolean ret; + gulong val; + + asn = egg_asn1x_create (test_asn1_tab, "TestIntegers"); + g_assert ("asn test structure is null" && asn != NULL); + + ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), 35); + g_assert ("couldn't write integer" && ret); + + ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), 23456); + g_assert ("couldn't write integer" && ret); + + ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), 209384022); + g_assert ("couldn't write integer" && ret); + + /* Now encode the whole caboodle */ + data = egg_asn1x_encode (asn, NULL, &n_data); + g_assert ("encoding asn1 didn't work" && data != NULL); + + egg_asn1x_destroy (asn); + + /* Now decode it all nicely */ + asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestIntegers", data, n_data); + g_return_if_fail (asn != NULL); + + /* And get out the values */ + ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), &val); + g_assert ("couldn't read integer from asn1" && ret); + g_assert_cmpuint (val, ==, 35); + + ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), &val); + g_assert ("couldn't read integer from asn1" && ret); + g_assert_cmpuint (val, ==, 23456); + + ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), &val); + g_assert ("couldn't read integer from asn1" && ret); + g_assert_cmpuint (val, ==, 209384022); + + g_free (data); +} + +static void +test_boolean_seq (Test* test, gconstpointer unused) +{ + GNode *asn = NULL; + gboolean value, ret; + gpointer data; + gsize n_data; + + asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq"); + g_assert ("asn test structure is null" && asn != NULL); + + /* Get the default value */ + value = TRUE; + ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); + g_assert (ret == TRUE); + g_assert (value == FALSE); + + ret = egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), TRUE); + g_assert (ret == TRUE); + + data = egg_asn1x_encode (asn, NULL, &n_data); + g_assert (data); + + ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); + g_assert (ret); + g_assert (value == TRUE); + + ret = egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), FALSE); + g_assert (ret == TRUE); + + g_free (data); + data = egg_asn1x_encode (asn, NULL, &n_data); + g_assert (data); + + ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); + g_assert (ret); + g_assert (value == FALSE); + + g_free (data); + egg_asn1x_destroy (asn); +} + +static void +test_write_value (Test* test, gconstpointer unused) +{ + GNode *asn = NULL; + guchar *data; + gsize n_data; + guchar *encoded; + gsize n_encoded; + + asn = egg_asn1x_create (test_asn1_tab, "TestData"); + g_assert ("asn test structure is null" && asn != NULL); + + if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) + g_assert_not_reached (); + + encoded = egg_asn1x_encode (asn, NULL, &n_encoded); + g_assert (encoded); + + data = egg_asn1x_get_string_as_raw (egg_asn1x_node (asn, "data", NULL), NULL, &n_data); + g_assert (data != NULL); + g_assert_cmpuint (n_data, ==, 9); + g_assert (memcmp (data, "SOME DATA", 9) == 0); + g_free (data); + + g_free (encoded); + egg_asn1x_destroy (asn); +} + +static void +test_element_length_content (Test* test, gconstpointer unused) +{ + GNode *asn = NULL; + gchar *buffer; + const guchar *content; + gsize n_content; + gsize n_buffer; + gssize length; + + asn = egg_asn1x_create (test_asn1_tab, "TestData"); + g_assert ("asn test structure is null" && asn != NULL); + + if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) + g_assert_not_reached (); + + buffer = egg_asn1x_encode (asn, NULL, &n_buffer); + g_assert (buffer != NULL); + + /* Now the real test */ + length = egg_asn1x_element_length (buffer, n_buffer + 1024); + g_assert_cmpint (length, ==, 13); + + content = egg_asn1x_element_content (buffer, length, &n_content); + g_assert (content); + g_assert_cmpuint (n_content, ==, 11); + + content = egg_asn1x_element_content (content, n_content, &n_content); + g_assert (content); + g_assert_cmpuint (n_content, ==, 9); + g_assert (memcmp (content, "SOME DATA", 9) == 0); + + const char *BAD_ASN_TAG = "\x00"; + content = egg_asn1x_element_content (BAD_ASN_TAG, 1, &n_content); + g_assert (content == NULL); + + const char *BAD_ASN_LENGTH = "\x30\x80"; + content = egg_asn1x_element_content (BAD_ASN_LENGTH, 2, &n_content); + g_assert (content == NULL); + + egg_asn1x_destroy (asn); + g_free (buffer); +} + +static void +test_read_element (Test* test, gconstpointer unused) +{ + GNode *asn = NULL; + guchar *buffer; + gconstpointer data; + gsize n_data; + gsize n_buffer; + + asn = egg_asn1x_create (test_asn1_tab, "TestData"); + g_assert ("asn test structure is null" && asn != NULL); + + if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) + g_assert_not_reached (); + + buffer = egg_asn1x_encode (asn, NULL, &n_buffer); + g_assert (buffer != NULL); + + /* Now the real test */ + data = egg_asn1x_get_raw_element (egg_asn1x_node (asn, "data", NULL), &n_data); + g_assert (data != NULL); + g_assert_cmpint (n_data, ==, 11); + + data = egg_asn1x_get_raw_value (egg_asn1x_node (asn, "data", NULL), &n_data); + g_assert (data); + g_assert_cmpuint (n_data, ==, 9); + g_assert (memcmp (data, "SOME DATA", 9) == 0); + + egg_asn1x_destroy (asn); + g_free (buffer); +} + +static void +test_oid (Test* test, gconstpointer unused) +{ + GNode *asn = NULL; + GQuark oid, check; + guchar *buffer; + gsize n_buffer; + + asn = egg_asn1x_create (test_asn1_tab, "TestOid"); + g_assert ("asn test structure is null" && asn != NULL); + + if (!egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "oid", NULL), "1.2.34567.89")) + g_assert_not_reached (); + + buffer = egg_asn1x_encode (asn, NULL, &n_buffer); + g_assert (buffer != NULL); + + /* Now a quark has been defined */ + check = g_quark_from_static_string ("1.2.34567.89"); + oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL)); + g_assert (oid); + g_assert (check == oid); + g_assert_cmpstr (g_quark_to_string (oid), ==, "1.2.34567.89"); + + /* Write a different OID */ + if (!egg_asn1x_set_oid_as_quark (egg_asn1x_node (asn, "oid", NULL), g_quark_from_static_string ("5.4.3.2.1678"))) + g_assert_not_reached (); + + g_free (buffer); + buffer = egg_asn1x_encode (asn, NULL, &n_buffer); + g_assert (buffer != NULL); + + oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL)); + g_assert (oid); + g_assert_cmpstr (g_quark_to_string (oid), ==, "5.4.3.2.1678"); + + g_free (buffer); + egg_asn1x_destroy (asn); +} + +typedef struct _TimeTestData { + gchar *value; + time_t ref; +} TimeTestData; + +static const TimeTestData generalized_time_test_data[] = { + { "20070725130528Z", 1185368728 }, + { "20070725130528.2134Z", 1185368728 }, + { "20070725140528-0100", 1185368728 }, + { "20070725040528+0900", 1185368728 }, + { "20070725013528+1130", 1185368728 }, + { "20070725Z", 1185321600 }, + { "20070725+0000", 1185321600 }, + + /* Bad ones */ + { "200707", -1 }, + + { NULL, 0 } +}; + +static const TimeTestData utc_time_test_data[] = { + /* Test the Y2K style wrap arounds */ + { "070725130528Z", 1185368728 }, /* The year 2007 */ + { "020725130528Z", 1027602328 }, /* The year 2002 */ + { "970725130528Z", 869835928 }, /* The year 1997 */ + { "370725130528Z", 2132139928 }, /* The year 2037 */ + + /* Test the time zones and other formats */ + { "070725130528.2134Z", 1185368728 }, + { "070725140528-0100", 1185368728 }, + { "070725040528+0900", 1185368728 }, + { "070725013528+1130", 1185368728 }, + { "070725Z", 1185321600 }, + { "070725+0000", 1185321600 }, + + /* Bad ones */ + { "0707", -1 }, + + { NULL, 0 } +}; + +static void +test_general_time (Test* test, gconstpointer unused) +{ + time_t when; + const TimeTestData *data; + + for (data = generalized_time_test_data; data->value; ++data) { + when = egg_asn1x_parse_time_general (data->value, -1); + if (data->ref != when) { + printf ("%s", data->value); + printf ("%s != ", ctime (&when)); + printf ("%s\n", ctime (&data->ref)); + fflush (stdout); + } + + g_assert ("decoded time doesn't match reference" && data->ref == when); + } +} + +static void +test_utc_time (Test* test, gconstpointer unused) +{ + time_t when; + const TimeTestData *data; + + for (data = utc_time_test_data; data->value; ++data) { + when = egg_asn1x_parse_time_utc (data->value, -1); + if (data->ref != when) { + printf ("%s", data->value); + printf ("%s != ", ctime (&when)); + printf ("%s\n", ctime (&data->ref)); + fflush (stdout); + } + + g_assert ("decoded time doesn't match reference" && data->ref == when); + } +} + +static void +test_read_time (Test* test, gconstpointer unused) +{ + glong time; + + time = egg_asn1x_get_time_as_long (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notBefore", NULL)); + g_assert_cmpint (time, ==, 820454400); +} + +static void +test_read_date (Test* test, gconstpointer unused) +{ + GDate date; + if (!egg_asn1x_get_time_as_date (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notAfter", NULL), &date)) + g_assert_not_reached (); + g_assert_cmpint (date.day, ==, 31); + g_assert_cmpint (date.month, ==, 12); + g_assert_cmpint (date.year, ==, 2020); +} + +static void +test_create_by_oid (Test* test, gconstpointer unused) +{ + /* id-at-initials = X520initials */ + GNode *node = egg_asn1x_create (pkix_asn1_tab, "2.5.4.43"); + g_assert (node != NULL); + g_assert_cmpstr (egg_asn1x_name (node), ==, "X520initials"); + egg_asn1x_destroy (node); +} + +static void +test_create_by_oid_invalid (Test* test, gconstpointer unused) +{ + GNode *node = egg_asn1x_create (pkix_asn1_tab, "23.23.23.23"); + g_assert (node == NULL); +} + +static void +test_create_by_bad_order (Test* test, gconstpointer unused) +{ + /* + * In pkix.asn the definition for parts of this oid + * come in the wrong order. However this should still work. + */ + + /* id-pe-authorityInfoAccess = AuthorityInfoAccessSyntax */ + GNode *node = egg_asn1x_create (pkix_asn1_tab, "1.3.6.1.5.5.7.1.1"); + g_assert (node != NULL); + g_assert_cmpstr (egg_asn1x_name (node), ==, "AuthorityInfoAccessSyntax"); + egg_asn1x_destroy (node); +} + +static void +test_count (Test* test, gconstpointer unused) +{ + GNode *node; + + node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL); + g_assert (node); + g_assert_cmpuint (egg_asn1x_count (node), ==, 7); +} + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/asn1/boolean", test_boolean); + g_test_add_func ("/asn1/integer", test_integer); + g_test_add_func ("/asn1/octet_string", test_octet_string); + g_test_add_func ("/asn1/generalized_time", test_generalized_time); + g_test_add_func ("/asn1/implicit", test_implicit); + g_test_add_func ("/asn1/explicit", test_explicit); + g_test_add_func ("/asn1/bit_string_decode", test_bit_string_decode); + g_test_add_func ("/asn1/bit_string_decode_bad", test_bit_string_decode_bad); + g_test_add_func ("/asn1/bit_string_decode_ulong", test_bit_string_decode_ulong); + g_test_add_func ("/asn1/bit_string_encode_decode", test_bit_string_encode_decode); + g_test_add_func ("/asn1/bit_string_encode_decode_ulong", test_bit_string_encode_decode_ulong); + g_test_add_func ("/asn1/bit_string_encode_decode_zero", test_bit_string_encode_decode_zero); + g_test_add_func ("/asn1/have", test_have); + g_test_add_func ("/asn1/any_set_raw", test_any_set_raw); + g_test_add_func ("/asn1/any_set_raw_explicit", test_any_set_raw_explicit); + g_test_add_func ("/asn1/choice_not_chosen", test_choice_not_chosen); + g_test_add_func ("/asn1/any_choice_set_raw_short_tag", test_any_choice_set_raw_short_tag); + g_test_add_func ("/asn1/any_choice_set_raw_long_tag", test_any_choice_set_raw_long_tag); + g_test_add_func ("/asn1/append", test_append); + g_test_add_func ("/asn1/append_and_clear", test_append_and_clear); + g_test_add_func ("/asn1/setof", test_setof); + g_test_add_func ("/asn1/setof_empty", test_setof_empty); + g_test_add_func ("/asn1/enumerated", test_enumerated); + g_test_add ("/asn1/node_name", Test, NULL, setup, test_node_name, teardown); + g_test_add ("/asn1/asn1_integers", Test, NULL, setup, test_asn1_integers, teardown); + g_test_add ("/asn1/boolean_seq", Test, NULL, setup, test_boolean_seq, teardown); + g_test_add ("/asn1/write_value", Test, NULL, setup, test_write_value, teardown); + g_test_add ("/asn1/element_length_content", Test, NULL, setup, test_element_length_content, teardown); + g_test_add ("/asn1/read_element", Test, NULL, setup, test_read_element, teardown); + g_test_add ("/asn1/oid", Test, NULL, setup, test_oid, teardown); + g_test_add ("/asn1/general_time", Test, NULL, setup, test_general_time, teardown); + g_test_add ("/asn1/utc_time", Test, NULL, setup, test_utc_time, teardown); + g_test_add ("/asn1/read_time", Test, NULL, setup, test_read_time, teardown); + g_test_add ("/asn1/read_date", Test, NULL, setup, test_read_date, teardown); + g_test_add ("/asn1/create_by_oid", Test, NULL, setup, test_create_by_oid, teardown); + g_test_add ("/asn1/create_by_oid_invalid", Test, NULL, setup, test_create_by_oid_invalid, teardown); + g_test_add ("/asn1/create_by_bad_order", Test, NULL, setup, test_create_by_bad_order, teardown); + g_test_add ("/asn1/count", Test, NULL, setup, test_count, teardown); + + return g_test_run (); +} diff --git a/egg/tests/test-asn1x.c b/egg/tests/test-asn1x.c index b3977540..553ad6f9 100644 --- a/egg/tests/test-asn1x.c +++ b/egg/tests/test-asn1x.c @@ -1,12 +1,34 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-asn1.c: Test ASN1 stuf -#include "egg/egg-asn1x.h" -#include "testing/testing.h" + Copyright (C) 2009 Stefan Walter + + The Gnome Keyring Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Keyring Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Gnome Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + Author: Stef Walter <stef@memberwebs.com> +*/ + +#include "egg-asn1x.h" #include <pwd.h> #include <stdlib.h> #include <unistd.h> -#include "egg/egg-asn1-defs.h" +#include "egg-asn1-defs.h" +#include "egg-testing.h" #if 0 static void @@ -47,7 +69,8 @@ test_some_asn1_stuff (const ASN1_ARRAY_TYPE *defs, const gchar *file, const gcha gpointer data, encoded; gsize n_data, n_encoded; - data = testing_data_read (file, &n_data); + if (!g_file_get_contents (file, (gchar**)&data, &n_data, NULL)) + g_assert_not_reached (); asn = egg_asn1x_create (defs, identifier); egg_asn1x_dump (asn); @@ -68,25 +91,23 @@ test_some_asn1_stuff (const ASN1_ARRAY_TYPE *defs, const gchar *file, const gcha g_free (data); } -static int -run (void) +int +main (int argc, char **argv) { - /* Build up a personal name, which is a set */ #if 0 build_personal_name (); #endif + egg_tests_chdir_base (argv[0]); - test_some_asn1_stuff (pkix_asn1_tab, "test-certificate-1.der", "Certificate"); - test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo"); - test_some_asn1_stuff (pk_asn1_tab, "test-rsakey-1.der", "RSAPrivateKey"); - test_some_asn1_stuff (pkix_asn1_tab, "test-personalname-1.der", "PersonalName"); - test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs7-1.der", "pkcs-7-ContentInfo"); - test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs7-2.der", "pkcs-7-ContentInfo"); - test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs12-1.der", "pkcs-12-PFX"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-certificate-1.der", "Certificate"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo"); + test_some_asn1_stuff (pk_asn1_tab, "files/test-rsakey-1.der", "RSAPrivateKey"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-personalname-1.der", "PersonalName"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-1.der", "pkcs-7-ContentInfo"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-2.der", "pkcs-7-ContentInfo"); + test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs12-1.der", "pkcs-12-PFX"); return 0; } - -#include "testing/testing.c" diff --git a/egg/tests/unit-test-cleanup.c b/egg/tests/test-cleanup.c index 45216ce2..90e3477b 100644 --- a/egg/tests/unit-test-cleanup.c +++ b/egg/tests/test-cleanup.c @@ -25,9 +25,7 @@ #include <stdio.h> #include <string.h> -#include "test-suite.h" - -#include "egg/egg-cleanup.h" +#include "egg-cleanup.h" #define DATA "some string" @@ -35,29 +33,30 @@ typedef struct _CleanupParam { gpointer value; } CleanupParam; -static void +static void cleanup_callback (gpointer user_data) -{ +{ CleanupParam *param = (CleanupParam*)user_data; g_assert (param->value && strcmp(param->value, DATA) == 0); param->value = NULL; } -TESTING_TEST(cleanup) +static void +test_cleanup (void) { CleanupParam param; - + param.value = DATA; - + egg_cleanup_register (cleanup_callback, ¶m); - + egg_cleanup_perform (); - + g_assert (param.value == NULL); } /* ----------------------------------------------------------------------------- - * Cleanup handlers are called in the opposite order as installed + * Cleanup handlers are called in the opposite order as installed */ static gint order_value = 0; @@ -66,9 +65,9 @@ typedef struct _OrderParam { gint reference; } OrderParam; -static void +static void order_callback (gpointer user_data) -{ +{ OrderParam *param = (OrderParam*)user_data; /* cleanup handler called out of order */ g_assert_cmpint (order_value, ==, param->reference); @@ -76,24 +75,25 @@ order_callback (gpointer user_data) --order_value; } -TESTING_TEST(order) +static void +test_order (void) { OrderParam param[8]; int i; - + for (i = 0; i < 8; ++i) { - param[i].reference = i; + param[i].reference = i; egg_cleanup_register (order_callback, ¶m[i]); } order_value = i - 1; - + egg_cleanup_perform (); for (i = 0; i < 8; ++i) /* "cleanup handler not called" */ g_assert (param[i].reference == -1); - + /* "not all cleanup handlers called" */ g_assert_cmpint (order_value, ==, -1); } @@ -101,7 +101,7 @@ TESTING_TEST(order) /* ----------------------------------------------------------------------------- * A cleanup handler might cause another to be registered. */ - + static gboolean cleaned_up = FALSE; static void @@ -109,45 +109,60 @@ second_callback (gpointer user_data) { cleaned_up = TRUE; } - + static void reregister_callback (gpointer user_data) { egg_cleanup_register (second_callback, NULL); -} +} -TESTING_TEST(reregister) +static void +test_reregister (void) { cleaned_up = FALSE; - + egg_cleanup_register (reregister_callback, NULL); - + egg_cleanup_perform (); - + /* "second cleanup handler not called" */ g_assert (cleaned_up == TRUE); } /* ----------------------------------------------------------------------------- - * Cleanup handlers can be removed + * Cleanup handlers can be removed */ - + static gboolean test_cleaned_up = FALSE; -static void +static void remove_callback (gpointer user_data) { - test_cleaned_up = TRUE; + test_cleaned_up = TRUE; } -TESTING_TEST(remove) +static void +test_remove (void) { egg_cleanup_register (remove_callback, NULL); egg_cleanup_register (remove_callback, DATA); egg_cleanup_unregister (remove_callback, DATA); egg_cleanup_unregister (remove_callback, NULL); egg_cleanup_perform (); - + /* "removed callback was called" */ g_assert (test_cleaned_up == FALSE); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/cleanup/cleanup", test_cleanup); + g_test_add_func ("/cleanup/order", test_order); + g_test_add_func ("/cleanup/reregister", test_reregister); + g_test_add_func ("/cleanup/remove", test_remove); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-dh.c b/egg/tests/test-dh.c index 583ffb58..6a2b37f5 100644 --- a/egg/tests/unit-test-dh.c +++ b/egg/tests/test-dh.c @@ -21,18 +21,22 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "test-suite.h" +#include "config.h" #include "egg-dh.h" #include "egg-secure-memory.h" +#include "egg-testing.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <gcrypt.h> -TESTING_TEST(dh_perform) +EGG_SECURE_GLIB_DEFINITIONS (); + +static void +test_perform (void) { gcry_mpi_t p, g; gcry_mpi_t x1, X1; @@ -69,7 +73,8 @@ TESTING_TEST(dh_perform) egg_secure_free (k2); } -TESTING_TEST(dh_short_pair) +static void +test_short_pair (void) { gcry_mpi_t p, g; gcry_mpi_t x1, X1; @@ -108,9 +113,9 @@ test_dh_default (const gchar *name, guint bits) ret = egg_dh_default_params_raw (name, &prime, &n_prime, &base, &n_base); g_assert (ret); g_assert (prime != NULL); - g_assert_cmpsize (n_prime, >, 0); + egg_assert_cmpsize (n_prime, >, 0); g_assert (base != NULL); - g_assert_cmpsize (n_base, >, 0); + egg_assert_cmpsize (n_base, >, 0); gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, prime, n_prime, NULL); g_assert (gcry == 0); @@ -126,43 +131,51 @@ test_dh_default (const gchar *name, guint bits) gcry_mpi_release (g); } -TESTING_TEST(dh_default_768) +static void +test_default_768 (void) { test_dh_default ("ietf-ike-grp-modp-768", 768); } -TESTING_TEST(dh_default_1024) +static void +test_default_1024 (void) { test_dh_default ("ietf-ike-grp-modp-1024", 1024); } -TESTING_TEST(dh_default_1536) +static void +test_default_1536 (void) { test_dh_default ("ietf-ike-grp-modp-1536", 1536); } -TESTING_TEST(dh_default_2048) +static void +test_default_2048 (void) { test_dh_default ("ietf-ike-grp-modp-2048", 2048); } -TESTING_TEST(dh_default_3072) +static void +test_default_3072 (void) { test_dh_default ("ietf-ike-grp-modp-3072", 3072); } -TESTING_TEST(dh_default_4096) +static void +test_default_4096 (void) { test_dh_default ("ietf-ike-grp-modp-4096", 4096); } -TESTING_TEST(dh_default_8192) +static void +test_default_8192 (void) { test_dh_default ("ietf-ike-grp-modp-8192", 8192); } -TESTING_TEST(dh_default_bad) +static void +test_default_bad (void) { gboolean ret; gcry_mpi_t p, g; @@ -170,3 +183,22 @@ TESTING_TEST(dh_default_bad) ret = egg_dh_default_params ("bad-name", &p, &g); g_assert (!ret); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/dh/perform", test_perform); + g_test_add_func ("/dh/short_pair", test_short_pair); + g_test_add_func ("/dh/default_768", test_default_768); + g_test_add_func ("/dh/default_1024", test_default_1024); + g_test_add_func ("/dh/default_1536", test_default_1536); + g_test_add_func ("/dh/default_2048", test_default_2048); + g_test_add_func ("/dh/default_3072", test_default_3072); + g_test_add_func ("/dh/default_4096", test_default_4096); + g_test_add_func ("/dh/default_8192", test_default_8192); + g_test_add_func ("/dh/default_bad", test_default_bad); + + return g_test_run (); +} diff --git a/egg/tests/test-dn.c b/egg/tests/test-dn.c index 0ac7a959..eae139fe 100644 --- a/egg/tests/test-dn.c +++ b/egg/tests/test-dn.c @@ -23,12 +23,10 @@ #include "config.h" -#include "test-suite.h" - -#include "egg/egg-asn1-defs.h" -#include "egg/egg-asn1x.h" -#include "egg/egg-dn.h" -#include "egg/egg-oid.h" +#include "egg-asn1-defs.h" +#include "egg-asn1x.h" +#include "egg-dn.h" +#include "egg-oid.h" #include <glib.h> #include <gcrypt.h> @@ -38,40 +36,47 @@ #include <stdio.h> #include <string.h> -static GNode* asn1_cert = NULL; -static guchar *data_cert = NULL; -static gsize n_data_cert = 0; +typedef struct { + GNode* asn1; + guchar *data; + gsize n_data; +} Test; -TESTING_SETUP(dn_cert) +static void +setup (Test *test, gconstpointer unused) { - data_cert = testing_data_read ("test-certificate-1.der", &n_data_cert); + if (!g_file_get_contents ("files/test-certificate-1.der", (gchar**)&test->data, + &test->n_data, NULL)) + g_assert_not_reached (); - asn1_cert = egg_asn1x_create (pkix_asn1_tab, "Certificate"); - g_assert (asn1_cert != NULL); + test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate"); + g_assert (test->asn1 != NULL); - if (!egg_asn1x_decode (asn1_cert, data_cert, n_data_cert)) + if (!egg_asn1x_decode (test->asn1, test->data, test->n_data)) g_assert_not_reached (); } -TESTING_TEARDOWN(dn_cert) +static void +teardown (Test *test, gconstpointer unused) { - egg_asn1x_destroy (asn1_cert); - g_free (data_cert); - data_cert = NULL; + egg_asn1x_destroy (test->asn1); + g_free (test->data); } -TESTING_TEST(read_dn) +static void +test_read_dn (Test* test, gconstpointer unused) { gchar *dn; - dn = egg_dn_read (egg_asn1x_node (asn1_cert, "tbsCertificate", "issuer", "rdnSequence", NULL)); + dn = egg_dn_read (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL)); g_assert (dn != NULL); g_assert_cmpstr (dn, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com"); g_free (dn); } -TESTING_TEST(dn_value) +static void +test_dn_value (Test* test, gconstpointer unused) { const guchar value[] = { 0x13, 0x1a, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x50, 0x72, 0x65, 0x6d, 0x69, 0x75, 0x6d, 0x20, 0x43, 0x41 }; gsize n_value = 28; @@ -118,24 +123,26 @@ concatenate_dn (guint index, GQuark oid, const guchar *value, gsize n_value, gpo g_free (text); } -TESTING_TEST(parse_dn) +static void +test_parse_dn (Test* test, gconstpointer unused) { GString *dn = g_string_new (""); last_index = 1; - if (!egg_dn_parse (egg_asn1x_node (asn1_cert, "tbsCertificate", "issuer", "rdnSequence", NULL), concatenate_dn, dn)) + if (!egg_dn_parse (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL), concatenate_dn, dn)) g_assert_not_reached (); g_assert_cmpstr (dn->str, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com"); g_string_free (dn, TRUE); } -TESTING_TEST(read_dn_part) +static void +test_read_dn_part (Test* test, gconstpointer unused) { GNode *node; gchar *value; - node = egg_asn1x_node (asn1_cert, "tbsCertificate", "issuer", "rdnSequence", NULL); + node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL); value = egg_dn_read_part (node, "CN"); g_assert (value != NULL); @@ -156,3 +163,16 @@ TESTING_TEST(read_dn_part) value = egg_dn_read_part (node, "2.5.4.9"); g_assert (value == NULL); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add ("/dn/read_dn", Test, NULL, setup, test_read_dn, teardown); + g_test_add ("/dn/dn_value", Test, NULL, setup, test_dn_value, teardown); + g_test_add ("/dn/parse_dn", Test, NULL, setup, test_parse_dn, teardown); + g_test_add ("/dn/read_dn_part", Test, NULL, setup, test_read_dn_part, teardown); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-hex.c b/egg/tests/test-hex.c index fd32f473..713bf609 100644 --- a/egg/tests/unit-test-hex.c +++ b/egg/tests/test-hex.c @@ -1,5 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-util.c: Test gck-util.c +/* unit-test-util.c: Test gck-util.c Copyright (C) 2007 Stefan Walter @@ -21,43 +21,46 @@ Author: Stef Walter <stef@memberwebs.com> */ +#include "config.h" + +#include "egg-hex.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "test-suite.h" - -#include "egg-hex.h" - static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 }; static const gchar *TEST_HEX = "05D695961012AE35"; static const gchar *TEST_HEX_DELIM = "05 D6 95 96 10 12 AE 35"; -TESTING_TEST(hex_encode) +static void +test_encode (void) { gchar *hex; - + hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA)); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX); } -TESTING_TEST(hex_encode_spaces) +static void +test_encode_spaces (void) { gchar *hex; - + /* Encode without spaces */ hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX); - + /* Encode with spaces */ hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, ' ', 1); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX_DELIM); } -TESTING_TEST(hex_decode) +static void +test_decode (void) { guchar *data; gsize n_data; @@ -82,7 +85,8 @@ TESTING_TEST(hex_decode) g_free (data); } -TESTING_TEST(hex_decode_fail) +static void +test_decode_fail (void) { guchar *data; gsize n_data; @@ -99,3 +103,16 @@ TESTING_TEST(hex_decode_fail) data = egg_hex_decode_full ("ABABAB", -1, ':', 1, &n_data); g_assert (!data); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/hex/encode", test_encode); + g_test_add_func ("/hex/encode_spaces", test_encode_spaces); + g_test_add_func ("/hex/decode", test_decode); + g_test_add_func ("/hex/decode_fail", test_decode_fail); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-oid.c b/egg/tests/test-oid.c index 4630c7e8..189a2009 100644 --- a/egg/tests/unit-test-oid.c +++ b/egg/tests/test-oid.c @@ -1,6 +1,5 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ /* unit-test-oid.c: Test OID routines - Copyright (C) 2008 Stefan Walter The Gnome Keyring Library is free software; you can redistribute it and/or @@ -23,24 +22,33 @@ #include "config.h" -#include "test-suite.h" - -#include "egg/egg-oid.h" +#include "egg-oid.h" #include <glib.h> -TESTING_TEST(oid_tests) +static void +test_tests (void) { GQuark oid; - + oid = g_quark_from_static_string ("0.9.2342.19200300.100.1.25"); g_assert_cmpstr (egg_oid_get_name (oid), ==, "DC"); g_assert_cmpstr (egg_oid_get_description (oid), ==, "Domain Component"); g_assert_cmpuint (egg_oid_get_flags (oid), ==, EGG_OID_PRINTABLE); - + /* Should return OID for invalid oids */ oid = g_quark_from_static_string ("1.1.1.1.1"); g_assert_cmpstr (egg_oid_get_name (oid), ==, "1.1.1.1.1"); - g_assert_cmpstr (egg_oid_get_description (oid), ==, "1.1.1.1.1"); + g_assert_cmpstr (egg_oid_get_description (oid), ==, "1.1.1.1.1"); g_assert_cmpuint (egg_oid_get_flags (oid), ==, 0); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/oid/tests", test_tests); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-openssl.c b/egg/tests/test-openssl.c index 7b8153b2..bf8134a4 100644 --- a/egg/tests/unit-test-openssl.c +++ b/egg/tests/test-openssl.c @@ -23,10 +23,10 @@ #include "config.h" -#include "test-suite.h" - #include "egg-symkey.h" #include "egg-openssl.h" +#include "egg-secure-memory.h" +#include "egg-testing.h" #include <glib.h> @@ -34,11 +34,33 @@ #include <stdio.h> #include <string.h> -guchar *refenc = NULL; -guchar *refdata = NULL; -gsize n_refenc = 0; -gsize n_refdata = 0; -GHashTable *refheaders = NULL; +EGG_SECURE_GLIB_DEFINITIONS (); + +typedef struct { + guchar *input; + gsize n_input; + guchar *refenc; + guchar *refdata; + gsize n_refenc; + gsize n_refdata; + GHashTable *refheaders; +} Test; + +static void +setup (Test *test, gconstpointer unused) +{ + if (!g_file_get_contents ("files/pem-rsa-enc.key", (gchar**)&test->input, &test->n_input, NULL)) + g_assert_not_reached (); +} + +static void +teardown (Test *test, gconstpointer unused) +{ + g_free (test->input); + g_free (test->refenc); + egg_secure_free (test->refdata); + g_hash_table_destroy (test->refheaders); +} static void copy_each_key_value (gpointer key, gpointer value, gpointer user_data) @@ -50,64 +72,69 @@ static void parse_reference (GQuark type, const guchar *data, gsize n_data, GHashTable *headers, gpointer user_data) { + Test *test = user_data; gboolean res; const gchar *dekinfo; - + g_assert ("no data in PEM callback" && data != NULL); g_assert ("no data in PEM callback" && n_data > 0); - refenc = g_memdup (data, n_data); - n_refenc = n_data; - + test->refenc = g_memdup (data, n_data); + test->n_refenc = n_data; + g_assert ("no headers present in file" && headers != NULL); - refheaders = egg_openssl_headers_new (); - g_hash_table_foreach (headers, copy_each_key_value, refheaders); + g_assert (!test->refheaders); + test->refheaders = egg_openssl_headers_new (); + g_hash_table_foreach (headers, copy_each_key_value, test->refheaders); dekinfo = egg_openssl_get_dekinfo (headers); g_assert ("no dekinfo in headers" && dekinfo != NULL); - - res = egg_openssl_decrypt_block (dekinfo, "booo", 4, data, n_data, &refdata, &n_refdata); + + res = egg_openssl_decrypt_block (dekinfo, "booo", 4, data, n_data, &test->refdata, &test->n_refdata); g_assert ("couldn't openssl decrypt block" && res == TRUE); - g_assert ("no data returned from openssl decrypt" && refdata != NULL); - g_assert ("invalid amount of data returned from openssl decrypt" && n_refdata == n_data); + g_assert ("no data returned from openssl decrypt" && test->refdata != NULL); + g_assert ("invalid amount of data returned from openssl decrypt" && test->n_refdata == n_data); } -TESTING_TEST(parse_reference) +static void +test_parse_reference (Test *test, gconstpointer unused) { - guchar *input; - gsize n_input; guint num; - - input = testing_data_read ("pem-rsa-enc.key", &n_input); - num = egg_openssl_pem_parse (input, n_input, parse_reference, NULL); + num = egg_openssl_pem_parse (test->input, test->n_input, parse_reference, test); g_assert ("couldn't PEM block in reference data" && num == 1); - - g_assert ("parse_reference() wasn't called" && refdata != NULL); + + g_assert ("parse_reference() wasn't called" && test->refdata != NULL); } -TESTING_TEST(write_reference) +static void +test_write_reference (Test *test, gconstpointer unused) { const gchar *dekinfo; guchar *encrypted; gsize n_encrypted; gboolean ret; - - dekinfo = egg_openssl_get_dekinfo (refheaders); + guint num; + + num = egg_openssl_pem_parse (test->input, test->n_input, parse_reference, test); + g_assert ("couldn't PEM block in reference data" && num == 1); + + dekinfo = egg_openssl_get_dekinfo (test->refheaders); g_assert ("no dekinfo in headers" && dekinfo != NULL); - ret = egg_openssl_encrypt_block (dekinfo, "booo", 4, refdata, n_refdata, &encrypted, &n_encrypted); + ret = egg_openssl_encrypt_block (dekinfo, "booo", 4, test->refdata, test->n_refdata, &encrypted, &n_encrypted); g_assert ("couldn't openssl encrypt block" && ret == TRUE); g_assert ("no data returned from openssl encrypt" && encrypted != NULL); - g_assert ("invalid amount of data returned from openssl encrypt" && n_refdata <= n_encrypted); - - g_assert ("data length doesn't match input length" && n_encrypted == n_refenc); - g_assert ("data doesn't match input" && memcmp (encrypted, refenc, n_encrypted) == 0); + g_assert ("invalid amount of data returned from openssl encrypt" && test->n_refdata <= n_encrypted); + + g_assert ("data length doesn't match input length" && n_encrypted == test->n_refenc); + g_assert ("data doesn't match input" && memcmp (encrypted, test->refenc, n_encrypted) == 0); } /* 29 bytes (prime number, so block length has bad chance of matching */ static const guchar *TEST_DATA = (guchar*)"ABCDEFGHIJKLMNOPQRSTUVWXYZ123"; const gsize TEST_DATA_L = 29; - -TESTING_TEST(openssl_roundtrip) + +static void +test_openssl_roundtrip (Test *test, gconstpointer unused) { const gchar *dekinfo; gboolean res; @@ -115,9 +142,13 @@ TESTING_TEST(openssl_roundtrip) guchar *encrypted, *decrypted; gsize n_encrypted, n_decrypted; int i; - - dekinfo = egg_openssl_prep_dekinfo (refheaders); - + guint num; + + num = egg_openssl_pem_parse (test->input, test->n_input, parse_reference, test); + g_assert ("couldn't PEM block in reference data" && num == 1); + + dekinfo = egg_openssl_prep_dekinfo (test->refheaders); + ret = egg_openssl_encrypt_block (dekinfo, "password", -1, TEST_DATA, TEST_DATA_L, &encrypted, &n_encrypted); g_assert ("couldn't openssl encrypt block" && ret == TRUE); g_assert ("no data returned from openssl encrypt" && encrypted != NULL); @@ -130,8 +161,21 @@ TESTING_TEST(openssl_roundtrip) /* Check that the data was decrypted properly */ g_assert ("decrypted data doesn't match length" && n_decrypted >= TEST_DATA_L); g_assert ("decrypted data doesn't match" && memcmp (TEST_DATA, decrypted, TEST_DATA_L) == 0); - + /* Check that the remainder is all zeros */ for (i = TEST_DATA_L; i < n_decrypted; ++i) g_assert ("non null byte in padding" && decrypted[i] == 0); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + egg_tests_chdir_base (argv[0]); + + g_test_add ("/openssl/parse_reference", Test, NULL, setup, test_parse_reference, teardown); + g_test_add ("/openssl/write_reference", Test, NULL, setup, test_write_reference, teardown); + g_test_add ("/openssl/openssl_roundtrip", Test, NULL, setup, test_openssl_roundtrip, teardown); + + return g_test_run (); +} diff --git a/egg/tests/test-padding.c b/egg/tests/test-padding.c index 3a11f467..236d9411 100644 --- a/egg/tests/test-padding.c +++ b/egg/tests/test-padding.c @@ -21,19 +21,20 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> +#include "config.h" -#include "test-suite.h" - -#include "egg/egg-padding.h" +#include "egg-padding.h" +#include "egg-testing.h" #include <gcrypt.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + static void -test_padding (EggPadding padding, gsize block, gconstpointer input, - gsize n_input, gconstpointer output, gsize n_output) +check_padding (EggPadding padding, gsize block, gconstpointer input, + gsize n_input, gconstpointer output, gsize n_output) { gpointer result; gsize n_result; @@ -44,7 +45,7 @@ test_padding (EggPadding padding, gsize block, gconstpointer input, } g_assert (result != NULL); - g_assert_cmpsize (n_output, ==, n_result); + egg_assert_cmpsize (n_output, ==, n_result); g_assert (memcmp (output, result, n_output) == 0); g_free (result); @@ -52,40 +53,45 @@ test_padding (EggPadding padding, gsize block, gconstpointer input, if (!(padding) (NULL, block, input, n_input, NULL, &n_result)) g_assert_not_reached (); - g_assert_cmpsize (n_output, ==, n_result); + egg_assert_cmpsize (n_output, ==, n_result); } -TESTING_TEST(zero_padding) +static void +test_zero_padding (void) { guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 'T', 'E', 'S', 'T' }; gchar raw[] = "TEST"; - test_padding (egg_padding_zero_pad, 8, raw, 4, padded, 8); + check_padding (egg_padding_zero_pad, 8, raw, 4, padded, 8); } -TESTING_TEST(zero_padding_no_data) +static void +test_zero_padding_no_data (void) { guchar padded[] = { }; gchar raw[] = ""; - test_padding (egg_padding_zero_pad, 8, raw, 0, padded, 0); + check_padding (egg_padding_zero_pad, 8, raw, 0, padded, 0); } -TESTING_TEST(pkcs1_one_padding) +static void +test_pkcs1_one_padding (void) { guchar padded[] = { 0x00, 0x01, 0xFF, 0x00, 'T', 'E', 'S', 'T' }; gchar raw[] = "TEST"; - test_padding (egg_padding_pkcs1_pad_01, 8, raw, 4, padded, 8); - test_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 4); + check_padding (egg_padding_pkcs1_pad_01, 8, raw, 4, padded, 8); + check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 4); } -TESTING_TEST(pkcs1_one_padding_no_data) +static void +test_pkcs1_one_padding_no_data (void) { guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }; gchar raw[] = ""; - test_padding (egg_padding_pkcs1_pad_01, 8, raw, 0, padded, 8); - test_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 0); + check_padding (egg_padding_pkcs1_pad_01, 8, raw, 0, padded, 8); + check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 0); } -TESTING_TEST(pkcs1_two_padding) +static void +test_pkcs1_two_padding (void) { guchar padded[] = { 0x00, 0x02, 0x77, 0x66, 0x55, 0x00, 'T', 'E', }; gchar raw[] = "TE"; @@ -93,14 +99,14 @@ TESTING_TEST(pkcs1_two_padding) gpointer vesult; gsize n_result; - test_padding (egg_padding_pkcs1_unpad_02, 8, padded, 8, raw, 2); + check_padding (egg_padding_pkcs1_unpad_02, 8, padded, 8, raw, 2); /* PKCS#1 02 padding is unpredictable */ if (!egg_padding_pkcs1_pad_02 (NULL, 8, raw, 2, &vesult, &n_result)) g_assert_not_reached (); result = vesult; g_assert (result != NULL); - g_assert_cmpsize (n_result, ==, 8); + egg_assert_cmpsize (n_result, ==, 8); g_assert (result[0] == 0x00); g_assert (result[1] == 0x02); g_assert (result[2] != 0x00); @@ -111,72 +117,106 @@ TESTING_TEST(pkcs1_two_padding) g_assert (result[7] == 'E'); } -TESTING_TEST(pkcs1_padding_invalid_prefix) +static void +test_pkcs1_padding_invalid_prefix (void) { guchar padded[] = { 0x01, 0x04, 0x04, 0x04 }; - test_padding (egg_padding_pkcs1_unpad_01, 4, padded, 4, NULL, 0); + check_padding (egg_padding_pkcs1_unpad_01, 4, padded, 4, NULL, 0); } -TESTING_TEST(pkcs1_padding_invalid_type) +static void +test_pkcs1_padding_invalid_type (void) { guchar padded[] = { 0x00, 0x03, 0xFF, 0x00, 'T', 'E', 'S', 'T' }; - test_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0); + check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0); } -TESTING_TEST(pkcs1_padding_invalid_no_zero) +static void +test_pkcs1_padding_invalid_no_zero (void) { guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S', 'T' }; - test_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0); + check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0); } -TESTING_TEST(pkcs1_padding_invalid_length) +static void +test_pkcs1_padding_invalid_length (void) { guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S' }; - test_padding (egg_padding_pkcs1_unpad_01, 8, padded, 7, NULL, 0); + check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 7, NULL, 0); } -TESTING_TEST(pkcs7_padding) +static void +test_pkcs7_padding (void) { guchar padded[] = { 'T', 'E', 'S', 'T', 0x04, 0x04, 0x04, 0x04 }; gchar raw[] = "TEST"; - test_padding (egg_padding_pkcs7_pad, 8, raw, 4, padded, 8); - test_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 4); + check_padding (egg_padding_pkcs7_pad, 8, raw, 4, padded, 8); + check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 4); } -TESTING_TEST(pkcs7_padding_equal_block) +static void +test_pkcs7_padding_equal_block (void) { guchar padded[] = { 'T', 'E', 'S', 'T', 'T', 'E', 'S', 'T', 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }; gchar raw[] = "TESTTEST"; - test_padding (egg_padding_pkcs7_pad, 8, raw, 8, padded, 16); - test_padding (egg_padding_pkcs7_unpad, 8, padded, 16, raw, 8); + check_padding (egg_padding_pkcs7_pad, 8, raw, 8, padded, 16); + check_padding (egg_padding_pkcs7_unpad, 8, padded, 16, raw, 8); } -TESTING_TEST(pkcs7_padding_zero) +static void +test_pkcs7_padding_zero (void) { guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }; gchar raw[] = ""; - test_padding (egg_padding_pkcs7_pad, 8, raw, 0, padded, 8); - test_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 0); + check_padding (egg_padding_pkcs7_pad, 8, raw, 0, padded, 8); + check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 0); } -TESTING_TEST(pkcs7_padding_invalid_zero) +static void +test_pkcs7_padding_invalid_zero (void) { guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - test_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0); + check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0); } -TESTING_TEST(pkcs7_padding_invalid_too_long) +static void +test_pkcs7_padding_invalid_too_long (void) { guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }; - test_padding (egg_padding_pkcs7_unpad, 4, padded, 8, NULL, 0); - test_padding (egg_padding_pkcs7_unpad, 4, padded, 4, NULL, 0); + check_padding (egg_padding_pkcs7_unpad, 4, padded, 8, NULL, 0); + check_padding (egg_padding_pkcs7_unpad, 4, padded, 4, NULL, 0); } -TESTING_TEST(pkcs7_padding_invalid_different) +static void +test_pkcs7_padding_invalid_different (void) { guchar padded[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - test_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0); + check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0); +} + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/padding/zero_padding", test_zero_padding); + g_test_add_func ("/padding/zero_padding_no_data", test_zero_padding_no_data); + g_test_add_func ("/padding/pkcs1_one_padding", test_pkcs1_one_padding); + g_test_add_func ("/padding/pkcs1_one_padding_no_data", test_pkcs1_one_padding_no_data); + g_test_add_func ("/padding/pkcs1_two_padding", test_pkcs1_two_padding); + g_test_add_func ("/padding/pkcs1_padding_invalid_prefix", test_pkcs1_padding_invalid_prefix); + g_test_add_func ("/padding/pkcs1_padding_invalid_type", test_pkcs1_padding_invalid_type); + g_test_add_func ("/padding/pkcs1_padding_invalid_no_zero", test_pkcs1_padding_invalid_no_zero); + g_test_add_func ("/padding/pkcs1_padding_invalid_length", test_pkcs1_padding_invalid_length); + g_test_add_func ("/padding/pkcs7_padding", test_pkcs7_padding); + g_test_add_func ("/padding/pkcs7_padding_equal_block", test_pkcs7_padding_equal_block); + g_test_add_func ("/padding/pkcs7_padding_zero", test_pkcs7_padding_zero); + g_test_add_func ("/padding/pkcs7_padding_invalid_zero", test_pkcs7_padding_invalid_zero); + g_test_add_func ("/padding/pkcs7_padding_invalid_too_long", test_pkcs7_padding_invalid_too_long); + g_test_add_func ("/padding/pkcs7_padding_invalid_different", test_pkcs7_padding_invalid_different); + + return g_test_run (); } diff --git a/egg/tests/unit-test-secmem.c b/egg/tests/test-secmem.c index 84b68beb..ddd74a18 100644 --- a/egg/tests/unit-test-secmem.c +++ b/egg/tests/test-secmem.c @@ -21,27 +21,32 @@ Author: Stef Walter <stef@memberwebs.com> */ +#include "config.h" + +#include "egg-secure-memory.h" + +#include <glib.h> + #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "test-suite.h" -#include "egg/egg-secure-memory.h" +EGG_SECURE_GLIB_DEFINITIONS (); /* Declared in egg-secure-memory.c */ extern int egg_secure_warnings; -/* +/* * Each test looks like (on one line): * void unit_test_xxxxx (CuTest* cu) - * + * * Each setup looks like (on one line): * void unit_setup_xxxxx (void); - * + * * Each teardown looks like (on one line): * void unit_teardown_xxxxx (void); - * + * * Tests be run in the order specified here. */ @@ -54,91 +59,96 @@ find_non_zero (gpointer mem, gsize len) if (*b != 0x00) return sz; } - + return G_MAXSIZE; } -TESTING_TEST(secmem_alloc_free) +static void +test_alloc_free (void) { gpointer p; gboolean ret; - + p = egg_secure_alloc_full (512, 0); g_assert (p != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512)); - + memset (p, 0x67, 512); - + ret = egg_secure_check (p); g_assert (ret == TRUE); - + egg_secure_free_full (p, 0); } -TESTING_TEST(secmem_realloc_across) +static void +test_realloc_across (void) { gpointer p, p2; - + /* Tiny allocation */ p = egg_secure_realloc_full (NULL, 1088, 0); g_assert (p != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088)); - /* Reallocate to a large one, will have to have changed blocks */ + /* Reallocate to a large one, will have to have changed blocks */ p2 = egg_secure_realloc_full (p, 16200, 0); g_assert (p2 != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200)); } -TESTING_TEST(secmem_alloc_two) +static void +test_alloc_two (void) { gpointer p, p2; gboolean ret; - + p2 = egg_secure_alloc_full (4, 0); g_assert (p2 != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4)); memset (p2, 0x67, 4); - + p = egg_secure_alloc_full (16200, 0); g_assert (p != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200)); memset (p, 0x67, 16200); - + ret = egg_secure_check (p); g_assert (ret == TRUE); - + egg_secure_free_full (p2, 0); egg_secure_free_full (p, 0); } -TESTING_TEST(secmem_realloc) +static void +test_realloc (void) { gchar *str = "a test string to see if realloc works properly"; gpointer p, p2; gsize len; - + len = strlen (str) + 1; - + p = egg_secure_realloc_full (NULL, len, 0); g_assert (p != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len)); - + strcpy ((gchar*)p, str); - + p2 = egg_secure_realloc_full (p, 512, 0); g_assert (p2 != NULL); g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len)); - + g_assert (strcmp (p2, str) == 0); - + p = egg_secure_realloc_full (p2, 0, 0); g_assert (p == NULL); } -TESTING_TEST(secmem_multialloc) +static void +test_multialloc (void) { GPtrArray *memory; gpointer data; @@ -148,7 +158,7 @@ TESTING_TEST(secmem_multialloc) /* A predetermined seed to get a predetermined pattern */ g_random_set_seed (15); memory = g_ptr_array_new (); - + /* Don't print "can't allocate" warnings */ egg_secure_warnings = 0; @@ -192,20 +202,21 @@ TESTING_TEST(secmem_multialloc) default: g_assert_not_reached (); } - + egg_secure_validate (); - + if (i > 100000 && !memory->len) break; } - + g_assert (memory->len == 0); g_ptr_array_free (memory, TRUE); - + egg_secure_warnings = 1; } -TESTING_TEST(secmem_clear) +static void +test_clear (void) { gpointer p; @@ -220,7 +231,8 @@ TESTING_TEST(secmem_clear) egg_secure_free_full (p, 0); } -TESTING_TEST(secmem_strclear) +static void +test_strclear (void) { gchar *str; @@ -235,3 +247,19 @@ TESTING_TEST(secmem_strclear) egg_secure_free_full (str, 0); } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/secmem/alloc_free", test_alloc_free); + g_test_add_func ("/secmem/realloc_across", test_realloc_across); + g_test_add_func ("/secmem/alloc_two", test_alloc_two); + g_test_add_func ("/secmem/realloc", test_realloc); + g_test_add_func ("/secmem/multialloc", test_multialloc); + g_test_add_func ("/secmem/clear", test_clear); + g_test_add_func ("/secmem/strclear", test_strclear); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-spawn.c b/egg/tests/test-spawn.c index 23be4cff..07488c01 100644 --- a/egg/tests/unit-test-spawn.c +++ b/egg/tests/test-spawn.c @@ -21,16 +21,19 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "test-suite.h" +#include "config.h" #include "egg-spawn.h" +#include "egg-testing.h" #include <sys/wait.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + typedef struct _EchoData { gint index; gchar *output; @@ -113,7 +116,7 @@ completed_func (gpointer user_data) g_assert (!data->completed); data->completed = TRUE; if (data->is_async) - testing_wait_stop (); + egg_test_wait_stop (); } static void @@ -160,7 +163,8 @@ static EggSpawnCallbacks null_callbacks = { child_setup, }; -TESTING_TEST(test_spawn_sync) +static void +test_sync (void) { GError *error = NULL; gboolean ret; @@ -172,7 +176,7 @@ TESTING_TEST(test_spawn_sync) data.parent_pid = getpid(); data.index = 80; - ret = egg_spawn_sync_with_callbacks (testing_data_directory (), + ret = egg_spawn_sync_with_callbacks ("./files", echo_argv, NULL, 0, &pid, &echo_callbacks, &data, &exit_status, &error); @@ -187,12 +191,13 @@ TESTING_TEST(test_spawn_sync) g_assert_cmpstr (data.error, ==, "1\n2\n3\n4\n5\n"); } -TESTING_TEST(test_spawn_sync_error) +static void +test_sync_error (void) { GError *error = NULL; gboolean ret; - ret = egg_spawn_sync_with_callbacks (testing_data_directory (), + ret = egg_spawn_sync_with_callbacks ("./files", error_argv, NULL, 0, NULL, NULL, NULL, NULL, &error); @@ -202,7 +207,8 @@ TESTING_TEST(test_spawn_sync_error) } -TESTING_TEST(test_spawn_async) +static void +test_async (void) { GError *error = NULL; EchoData data; @@ -214,7 +220,7 @@ TESTING_TEST(test_spawn_async) data.index = 80; data.is_async = TRUE; - ret = egg_spawn_async_with_callbacks (testing_data_directory (), + ret = egg_spawn_async_with_callbacks ("./files", echo_argv, NULL, 0, &pid, &echo_callbacks, &data, NULL, &error); @@ -222,7 +228,7 @@ TESTING_TEST(test_spawn_async) g_assert (error == NULL); g_assert (!data.finalized); - testing_wait_until (2000); + egg_test_wait_until (2000); g_assert (data.finalized); g_assert (data.completed); @@ -230,7 +236,8 @@ TESTING_TEST(test_spawn_async) g_assert_cmpstr (data.error, ==, "1\n2\n3\n4\n5\n"); } -TESTING_TEST(test_spawn_async_none) +static void +test_async_none (void) { GError *error = NULL; EchoData data; @@ -240,7 +247,7 @@ TESTING_TEST(test_spawn_async_none) data.parent_pid = getpid(); data.is_async = TRUE; - ret = egg_spawn_async_with_callbacks (testing_data_directory (), + ret = egg_spawn_async_with_callbacks ("./files", echo_argv, NULL, 0, NULL, &null_callbacks, &data, NULL, &error); @@ -248,19 +255,20 @@ TESTING_TEST(test_spawn_async_none) g_assert (error == NULL); g_assert (!data.finalized); - testing_wait_until (2000); + egg_test_wait_until (2000); g_assert (data.finalized); g_assert (data.completed); g_assert (!data.output); } -TESTING_TEST(test_spawn_async_error) +static void +test_async_error (void) { GError *error = NULL; guint ret; - ret = egg_spawn_async_with_callbacks (testing_data_directory (), + ret = egg_spawn_async_with_callbacks ("./files", error_argv, NULL, 0, NULL, NULL, NULL, NULL, &error); @@ -268,3 +276,23 @@ TESTING_TEST(test_spawn_async_error) g_assert (error != NULL); g_clear_error (&error); } + +int +main (int argc, char **argv) +{ + const gchar *srcdir; + + g_test_init (&argc, &argv, NULL); + + srcdir = g_getenv ("SRCDIR"); + if (srcdir && chdir (srcdir) < 0) + g_error ("couldn't change directory to: %s: %s", srcdir, g_strerror (errno)); + + g_test_add_func ("/spawn/sync", test_sync); + g_test_add_func ("/spawn/sync_error", test_sync_error); + g_test_add_func ("/spawn/async", test_async); + g_test_add_func ("/spawn/async_none", test_async_none); + g_test_add_func ("/spawn/async_error", test_async_error); + + return egg_tests_run_in_thread_with_loop (); +} diff --git a/egg/tests/unit-test-symkey.c b/egg/tests/test-symkey.c index c3cf7226..c2c1b781 100644 --- a/egg/tests/unit-test-symkey.c +++ b/egg/tests/test-symkey.c @@ -21,11 +21,7 @@ Author: Stef Walter <stef@memberwebs.com> */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "test-suite.h" +#include "config.h" #include "egg-libgcrypt.h" #include "egg-secure-memory.h" @@ -33,15 +29,11 @@ #include <gcrypt.h> -TESTING_SETUP(crypto_setup) -{ - egg_libgcrypt_initialize (); -} +#include <stdlib.h> +#include <stdio.h> +#include <string.h> -TESTING_TEARDOWN(crypto_setup) -{ - -} +EGG_SECURE_GLIB_DEFINITIONS (); static const struct { const gchar *password; @@ -49,31 +41,31 @@ static const struct { int hash_algo; int iterations; const gchar *salt; - + const gchar *result_simple; const gchar *result_pkcs12; const gchar *result_pbkdf2; const gchar *result_pbe; } all_generation_tests[] = { - + { /* 24 byte output */ - "booo", GCRY_CIPHER_3DES, GCRY_MD_MD5, 1, - "\x70\x4C\xFF\xD6\x2F\xBA\x03\xE9", - "\x84\x12\xBB\x34\x94\x8C\x40\xAD\x97\x57\x96\x74\x5B\x6A\xFB\xF8\xD6\x61\x33\x51\xEA\x8C\xCF\xD8", + "booo", GCRY_CIPHER_3DES, GCRY_MD_MD5, 1, + "\x70\x4C\xFF\xD6\x2F\xBA\x03\xE9", + "\x84\x12\xBB\x34\x94\x8C\x40\xAD\x97\x57\x96\x74\x5B\x6A\xFB\xF8\xD6\x61\x33\x51\xEA\x8C\xCF\xD8", NULL, NULL, NULL - }, + }, { /* 5 byte output */ - "booo", GCRY_CIPHER_RFC2268_40, GCRY_MD_SHA1, 2048, + "booo", GCRY_CIPHER_RFC2268_40, GCRY_MD_SHA1, 2048, "\x8A\x58\xC2\xE8\x7C\x1D\x80\x11", NULL, "\xD6\xA6\xF0\x76\x66", NULL, NULL }, - + { /* Null Password, 5 byte output */ NULL, GCRY_CIPHER_RFC2268_40, GCRY_MD_SHA1, 2000, "\x04\xE0\x1C\x3E\xF8\xF2\xE9\xFD", @@ -82,7 +74,7 @@ static const struct { NULL, NULL }, - + { /* 24 byte output */ "booo", GCRY_CIPHER_3DES, GCRY_MD_SHA1, 2048, "\xBD\xEE\x0B\xC6\xCF\x43\xAC\x25", @@ -109,11 +101,11 @@ static const struct { NULL, NULL }, - + { /* 8 byte output */ "booo", GCRY_CIPHER_DES, GCRY_MD_MD5, 2048, "\x93\x4C\x3D\x29\xA2\x42\xB0\xF5", - NULL, + NULL, NULL, NULL, "\x8C\x67\x19\x7F\xB9\x23\xE2\x8D" @@ -122,107 +114,125 @@ static const struct { #define N_GENERATION_TESTS (sizeof (all_generation_tests) / sizeof (all_generation_tests[0])) -TESTING_TEST(generate_key_simple) +static void +test_generate_key_simple (void) { int i; gboolean ret; guchar *key; - + for (i = 0; i < N_GENERATION_TESTS; ++i) { - + if (!all_generation_tests[i].result_simple) continue; - - ret = egg_symkey_generate_simple (all_generation_tests[i].cipher_algo, - all_generation_tests[i].hash_algo, - all_generation_tests[i].password, -1, - (guchar*)all_generation_tests[i].salt, 8, - all_generation_tests[i].iterations, - &key, NULL); + + ret = egg_symkey_generate_simple (all_generation_tests[i].cipher_algo, + all_generation_tests[i].hash_algo, + all_generation_tests[i].password, -1, + (guchar*)all_generation_tests[i].salt, 8, + all_generation_tests[i].iterations, + &key, NULL); g_assert (ret && "key generation failed"); - ret = (memcmp (key, all_generation_tests[i].result_simple, + ret = (memcmp (key, all_generation_tests[i].result_simple, gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0); - g_assert (ret && "invalid simple key generated"); + g_assert (ret && "invalid simple key generated"); } } -TESTING_TEST(generate_key_pkcs12) +static void +test_generate_key_pkcs12 (void) { int i; gboolean ret; guchar *key; - + for (i = 0; i < N_GENERATION_TESTS; ++i) { - + if (!all_generation_tests[i].result_pkcs12) continue; - - ret = egg_symkey_generate_pkcs12 (all_generation_tests[i].cipher_algo, - all_generation_tests[i].hash_algo, - all_generation_tests[i].password, -1, - (guchar*)all_generation_tests[i].salt, 8, - all_generation_tests[i].iterations, - &key, NULL); + + ret = egg_symkey_generate_pkcs12 (all_generation_tests[i].cipher_algo, + all_generation_tests[i].hash_algo, + all_generation_tests[i].password, -1, + (guchar*)all_generation_tests[i].salt, 8, + all_generation_tests[i].iterations, + &key, NULL); g_assert ("failed to generate pkcs12 key" && ret); - - ret = (memcmp (key, all_generation_tests[i].result_pkcs12, + + ret = (memcmp (key, all_generation_tests[i].result_pkcs12, gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0); - - g_assert ("invalid pkcs12 key generated" && ret); + + g_assert ("invalid pkcs12 key generated" && ret); } } -TESTING_TEST(generate_key_pbkdf2) +static void +test_generate_key_pbkdf2 (void) { int i; gboolean ret; guchar *key; - + for (i = 0; i < N_GENERATION_TESTS; ++i) { - + if (!all_generation_tests[i].result_pbkdf2) continue; - - ret = egg_symkey_generate_pbkdf2 (all_generation_tests[i].cipher_algo, - all_generation_tests[i].hash_algo, - all_generation_tests[i].password, -1, - (guchar*)all_generation_tests[i].salt, 8, - all_generation_tests[i].iterations, - &key, NULL); + + ret = egg_symkey_generate_pbkdf2 (all_generation_tests[i].cipher_algo, + all_generation_tests[i].hash_algo, + all_generation_tests[i].password, -1, + (guchar*)all_generation_tests[i].salt, 8, + all_generation_tests[i].iterations, + &key, NULL); g_assert ("failed to generate pbkdf2 key" && ret); - - ret = (memcmp (key, all_generation_tests[i].result_pbkdf2, + + ret = (memcmp (key, all_generation_tests[i].result_pbkdf2, gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0); - - g_assert ("invalid pbkdf2 key generated" && ret); + + g_assert ("invalid pbkdf2 key generated" && ret); } } -TESTING_TEST(generate_key_pbe) +static void +test_generate_key_pbe (void) { int i; gboolean ret; guchar *key; - + for (i = 0; i < N_GENERATION_TESTS; ++i) { - + if (!all_generation_tests[i].result_pbe) continue; - - ret = egg_symkey_generate_pbe (all_generation_tests[i].cipher_algo, - all_generation_tests[i].hash_algo, - all_generation_tests[i].password, -1, - (guchar*)all_generation_tests[i].salt, 8, - all_generation_tests[i].iterations, - &key, NULL); + + ret = egg_symkey_generate_pbe (all_generation_tests[i].cipher_algo, + all_generation_tests[i].hash_algo, + all_generation_tests[i].password, -1, + (guchar*)all_generation_tests[i].salt, 8, + all_generation_tests[i].iterations, + &key, NULL); g_assert ("failed to generate pbe key" && ret); - - ret = (memcmp (key, all_generation_tests[i].result_pbe, + + ret = (memcmp (key, all_generation_tests[i].result_pbe, gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0); - - g_assert ("invalid pbe key generated" && ret); - + + g_assert ("invalid pbe key generated" && ret); + } } + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + egg_libgcrypt_initialize (); + + g_test_add_func ("/symkey/generate_key_simple", test_generate_key_simple); + g_test_add_func ("/symkey/generate_key_pkcs12", test_generate_key_pkcs12); + g_test_add_func ("/symkey/generate_key_pbkdf2", test_generate_key_pbkdf2); + g_test_add_func ("/symkey/generate_key_pbe", test_generate_key_pbe); + + return g_test_run (); +} diff --git a/egg/tests/unit-test-asn1.c b/egg/tests/unit-test-asn1.c deleted file mode 100644 index a2845062..00000000 --- a/egg/tests/unit-test-asn1.c +++ /dev/null @@ -1,427 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* unit-test-pkix-parser.c: Test PKIX parser - - Copyright (C) 2007 Stefan Walter - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - - Author: Stef Walter <stef@memberwebs.com> -*/ - -#include "config.h" - -#include "test-suite.h" - -#include "egg/egg-asn1-defs.h" -#include "egg/egg-asn1x.h" -#include "egg/egg-oid.h" - -#include <glib.h> -#include <gcrypt.h> -#include <libtasn1.h> - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -extern const ASN1_ARRAY_TYPE test_asn1_tab[]; - -static GNode *asn1_cert = NULL; -static guchar *data_cert = NULL; -static gsize n_data_cert = 0; - -TESTING_SETUP(asn1_tree) -{ - data_cert = testing_data_read ("test-certificate-1.der", &n_data_cert); - - asn1_cert = egg_asn1x_create (pkix_asn1_tab, "Certificate"); - g_assert (asn1_cert != NULL); - - if (!egg_asn1x_decode (asn1_cert, data_cert, n_data_cert)) - egg_asn1x_assert_not_reached (asn1_cert); -} - -TESTING_TEARDOWN(asn1_tree) -{ - egg_asn1x_destroy (asn1_cert); - g_free (data_cert); - data_cert = NULL; -} - -TESTING_TEST(node_name) -{ - g_assert_cmpstr (egg_asn1x_name (asn1_cert), ==, "Certificate"); -} - -TESTING_TEST(asn1_integers) -{ - GNode *asn; - guchar *data; - gsize n_data; - gboolean ret; - gulong val; - - asn = egg_asn1x_create (test_asn1_tab, "TestIntegers"); - g_assert ("asn test structure is null" && asn != NULL); - - ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), 35); - g_assert ("couldn't write integer" && ret); - - ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), 23456); - g_assert ("couldn't write integer" && ret); - - ret = egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), 209384022); - g_assert ("couldn't write integer" && ret); - - /* Now encode the whole caboodle */ - data = egg_asn1x_encode (asn, NULL, &n_data); - g_assert ("encoding asn1 didn't work" && data != NULL); - - egg_asn1x_destroy (asn); - - /* Now decode it all nicely */ - asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestIntegers", data, n_data); - g_return_if_fail (asn != NULL); - - /* And get out the values */ - ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), &val); - g_assert ("couldn't read integer from asn1" && ret); - g_assert_cmpuint (val, ==, 35); - - ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), &val); - g_assert ("couldn't read integer from asn1" && ret); - g_assert_cmpuint (val, ==, 23456); - - ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), &val); - g_assert ("couldn't read integer from asn1" && ret); - g_assert_cmpuint (val, ==, 209384022); - - g_free (data); -} - -TESTING_TEST(boolean) -{ - GNode *asn = NULL; - gboolean value, ret; - gpointer data; - gsize n_data; - - asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq"); - g_assert ("asn test structure is null" && asn != NULL); - - /* Get the default value */ - value = TRUE; - ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); - g_assert (ret == TRUE); - g_assert (value == FALSE); - - ret = egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), TRUE); - g_assert (ret == TRUE); - - data = egg_asn1x_encode (asn, NULL, &n_data); - g_assert (data); - - ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); - g_assert (ret); - g_assert (value == TRUE); - - ret = egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), FALSE); - g_assert (ret == TRUE); - - g_free (data); - data = egg_asn1x_encode (asn, NULL, &n_data); - g_assert (data); - - ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value); - g_assert (ret); - g_assert (value == FALSE); - - g_free (data); - egg_asn1x_destroy (asn); -} - -TESTING_TEST(write_value) -{ - GNode *asn = NULL; - guchar *data; - gsize n_data; - guchar *encoded; - gsize n_encoded; - - asn = egg_asn1x_create (test_asn1_tab, "TestData"); - g_assert ("asn test structure is null" && asn != NULL); - - if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) - g_assert_not_reached (); - - encoded = egg_asn1x_encode (asn, NULL, &n_encoded); - g_assert (encoded); - - data = egg_asn1x_get_string_as_raw (egg_asn1x_node (asn, "data", NULL), NULL, &n_data); - g_assert (data != NULL); - g_assert_cmpuint (n_data, ==, 9); - g_assert (memcmp (data, "SOME DATA", 9) == 0); - g_free (data); - - g_free (encoded); - egg_asn1x_destroy (asn); -} - -TESTING_TEST(element_length_content) -{ - GNode *asn = NULL; - gchar *buffer; - const guchar *content; - gsize n_content; - gsize n_buffer; - gssize length; - - asn = egg_asn1x_create (test_asn1_tab, "TestData"); - g_assert ("asn test structure is null" && asn != NULL); - - if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) - g_assert_not_reached (); - - buffer = egg_asn1x_encode (asn, NULL, &n_buffer); - g_assert (buffer != NULL); - - /* Now the real test */ - length = egg_asn1x_element_length (buffer, n_buffer + 1024); - g_assert_cmpint (length, ==, 13); - - content = egg_asn1x_element_content (buffer, length, &n_content); - g_assert (content); - g_assert_cmpuint (n_content, ==, 11); - - content = egg_asn1x_element_content (content, n_content, &n_content); - g_assert (content); - g_assert_cmpuint (n_content, ==, 9); - g_assert (memcmp (content, "SOME DATA", 9) == 0); - - const char *BAD_ASN_TAG = "\x00"; - content = egg_asn1x_element_content (BAD_ASN_TAG, 1, &n_content); - g_assert (content == NULL); - - const char *BAD_ASN_LENGTH = "\x30\x80"; - content = egg_asn1x_element_content (BAD_ASN_LENGTH, 2, &n_content); - g_assert (content == NULL); - - egg_asn1x_destroy (asn); - g_free (buffer); -} - -TESTING_TEST(read_element) -{ - GNode *asn = NULL; - guchar *buffer; - gconstpointer data; - gsize n_data; - gsize n_buffer; - - asn = egg_asn1x_create (test_asn1_tab, "TestData"); - g_assert ("asn test structure is null" && asn != NULL); - - if (!egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL)) - g_assert_not_reached (); - - buffer = egg_asn1x_encode (asn, NULL, &n_buffer); - g_assert (buffer != NULL); - - /* Now the real test */ - data = egg_asn1x_get_raw_element (egg_asn1x_node (asn, "data", NULL), &n_data); - g_assert (data != NULL); - g_assert_cmpint (n_data, ==, 11); - - data = egg_asn1x_get_raw_value (egg_asn1x_node (asn, "data", NULL), &n_data); - g_assert (data); - g_assert_cmpuint (n_data, ==, 9); - g_assert (memcmp (data, "SOME DATA", 9) == 0); - - egg_asn1x_destroy (asn); - g_free (buffer); -} - -TESTING_TEST(oid) -{ - GNode *asn = NULL; - GQuark oid, check; - guchar *buffer; - gsize n_buffer; - - asn = egg_asn1x_create (test_asn1_tab, "TestOid"); - g_assert ("asn test structure is null" && asn != NULL); - - if (!egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "oid", NULL), "1.2.34567.89")) - g_assert_not_reached (); - - buffer = egg_asn1x_encode (asn, NULL, &n_buffer); - g_assert (buffer != NULL); - - /* Now a quark has been defined */ - check = g_quark_from_static_string ("1.2.34567.89"); - oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL)); - g_assert (oid); - g_assert (check == oid); - g_assert_cmpstr (g_quark_to_string (oid), ==, "1.2.34567.89"); - - /* Write a different OID */ - if (!egg_asn1x_set_oid_as_quark (egg_asn1x_node (asn, "oid", NULL), g_quark_from_static_string ("5.4.3.2.1678"))) - g_assert_not_reached (); - - g_free (buffer); - buffer = egg_asn1x_encode (asn, NULL, &n_buffer); - g_assert (buffer != NULL); - - oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL)); - g_assert (oid); - g_assert_cmpstr (g_quark_to_string (oid), ==, "5.4.3.2.1678"); - - g_free (buffer); - egg_asn1x_destroy (asn); -} - -typedef struct _TimeTestData { - gchar *value; - time_t ref; -} TimeTestData; - -static const TimeTestData generalized_time_test_data[] = { - { "20070725130528Z", 1185368728 }, - { "20070725130528.2134Z", 1185368728 }, - { "20070725140528-0100", 1185368728 }, - { "20070725040528+0900", 1185368728 }, - { "20070725013528+1130", 1185368728 }, - { "20070725Z", 1185321600 }, - { "20070725+0000", 1185321600 }, - - /* Bad ones */ - { "200707", -1 }, - - { NULL, 0 } -}; - -static const TimeTestData utc_time_test_data[] = { - /* Test the Y2K style wrap arounds */ - { "070725130528Z", 1185368728 }, /* The year 2007 */ - { "020725130528Z", 1027602328 }, /* The year 2002 */ - { "970725130528Z", 869835928 }, /* The year 1997 */ - { "370725130528Z", 2132139928 }, /* The year 2037 */ - - /* Test the time zones and other formats */ - { "070725130528.2134Z", 1185368728 }, - { "070725140528-0100", 1185368728 }, - { "070725040528+0900", 1185368728 }, - { "070725013528+1130", 1185368728 }, - { "070725Z", 1185321600 }, - { "070725+0000", 1185321600 }, - - /* Bad ones */ - { "0707", -1 }, - - { NULL, 0 } -}; - -TESTING_TEST(general_time) -{ - time_t when; - const TimeTestData *data; - - for (data = generalized_time_test_data; data->value; ++data) { - when = egg_asn1x_parse_time_general (data->value, -1); - if (data->ref != when) { - printf ("%s", data->value); - printf ("%s != ", ctime (&when)); - printf ("%s\n", ctime (&data->ref)); - fflush (stdout); - } - - g_assert ("decoded time doesn't match reference" && data->ref == when); - } -} - -TESTING_TEST(utc_time) -{ - time_t when; - const TimeTestData *data; - - for (data = utc_time_test_data; data->value; ++data) { - when = egg_asn1x_parse_time_utc (data->value, -1); - if (data->ref != when) { - printf ("%s", data->value); - printf ("%s != ", ctime (&when)); - printf ("%s\n", ctime (&data->ref)); - fflush (stdout); - } - - g_assert ("decoded time doesn't match reference" && data->ref == when); - } -} - -TESTING_TEST(read_time) -{ - glong time; - - time = egg_asn1x_get_time_as_long (egg_asn1x_node (asn1_cert, "tbsCertificate", "validity", "notBefore", NULL)); - g_assert_cmpint (time, ==, 820454400); -} - -TESTING_TEST(read_date) -{ - GDate date; - if (!egg_asn1x_get_time_as_date (egg_asn1x_node (asn1_cert, "tbsCertificate", "validity", "notAfter", NULL), &date)) - g_assert_not_reached (); - g_assert_cmpint (date.day, ==, 31); - g_assert_cmpint (date.month, ==, 12); - g_assert_cmpint (date.year, ==, 2020); -} - -TESTING_TEST(create_by_oid) -{ - /* id-at-initials = X520initials */ - GNode *node = egg_asn1x_create (pkix_asn1_tab, "2.5.4.43"); - g_assert (node != NULL); - g_assert_cmpstr (egg_asn1x_name (node), ==, "X520initials"); - egg_asn1x_destroy (node); -} - -TESTING_TEST(create_by_oid_invalid) -{ - GNode *node = egg_asn1x_create (pkix_asn1_tab, "23.23.23.23"); - g_assert (node == NULL); -} - -TESTING_TEST(create_by_bad_order) -{ - /* - * In pkix.asn the definition for parts of this oid - * come in the wrong order. However this should still work. - */ - - /* id-pe-authorityInfoAccess = AuthorityInfoAccessSyntax */ - GNode *node = egg_asn1x_create (pkix_asn1_tab, "1.3.6.1.5.5.7.1.1"); - g_assert (node != NULL); - g_assert_cmpstr (egg_asn1x_name (node), ==, "AuthorityInfoAccessSyntax"); - egg_asn1x_destroy (node); -} - -TESTING_TEST(count) -{ - GNode *node; - - node = egg_asn1x_node (asn1_cert, "tbsCertificate", "issuer", "rdnSequence", NULL); - g_assert (node); - g_assert_cmpuint (egg_asn1x_count (node), ==, 7); -} |