diff options
author | Stef Walter <stef@memberwebs.com> | 2009-06-18 17:12:25 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2009-06-18 17:12:25 +0000 |
commit | 543b45ab3ed3f4d1a9852ead27becf0f2dee8f6d (patch) | |
tree | 00a56400bbed9e01b569e09f87eb679c5dbc8d86 /tests/unit-test-storage.c | |
parent | 4bf87cb9f4f6b9812c06a8d2c500869c48ebc2dc (diff) |
Make tests for storage, and fix problems
Diffstat (limited to 'tests/unit-test-storage.c')
-rw-r--r-- | tests/unit-test-storage.c | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/tests/unit-test-storage.c b/tests/unit-test-storage.c new file mode 100644 index 0000000..11e9680 --- /dev/null +++ b/tests/unit-test-storage.c @@ -0,0 +1,241 @@ + +#include "config.h" + +#include "run-tests.h" + +#include "module/mod_auth_singleid.h" + +#include <string.h> +#include <time.h> + +/* + * 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. + */ + +static void *memory = NULL; +static sid_storage_t *store = NULL; + +#define LONG_DATA \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" \ + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" + +void unit_setup_storage (void) +{ + memory = malloc (4096); + store = sid_storage_initialize (memory, 4096); +} + +void unit_teardown_storage (void) +{ + free (memory); + store = NULL; + memory = NULL; +} + +void unit_test_storage_initialize_null (CuTest *cu) +{ + sid_storage_t *st = sid_storage_initialize (NULL, 0); + CuAssertPtrEquals (cu, NULL, st); +} + +void unit_test_storage_initialize_short (CuTest *cu) +{ + void *mem = malloc (16); + sid_storage_t *st = sid_storage_initialize (mem, 16); + CuAssertPtrEquals (cu, NULL, st); + free (mem); +} + +void unit_test_storage_store_retrieve (CuTest *cu) +{ + sid_assoc_t assoc = { + "server name", "handle name", "type name", + (unsigned char*)"mysecret", 2, time (NULL) + }; + + sid_assoc_t check; + int res; + + res = sid_storage_store_assoc (store, &assoc); + CuAssertIntEquals (cu, 1, res); + + res = sid_storage_find_assoc (store, "server name", NULL, &check); + CuAssertIntEquals (cu, 1, res); + + CuAssertStrEquals (cu, "server name", check.server); + CuAssertStrEquals (cu, "handle name", check.handle); + CuAssertStrEquals (cu, "type name", check.type); + CuAssertIntEquals (cu, 2, check.n_secret); + CuAssert (cu, "invalid secret", memcmp (check.secret, "my", 2) == 0); + CuAssertIntEquals (cu, assoc.expires, check.expires); +} + +void unit_test_storage_store_too_long (CuTest *cu) +{ + sid_assoc_t assoc = { + "server", LONG_DATA, "type name", + (unsigned char*)"mysecret", 2, time (NULL) + }; + + int res; + + res = sid_storage_store_assoc (store, &assoc); + CuAssertIntEquals (cu, 0, res); + + res = sid_storage_find_assoc (store, "server", NULL, &assoc); + CuAssertIntEquals (cu, 0, res); +} + +void unit_test_storage_find_wrong (CuTest *cu) +{ + sid_assoc_t assoc = { + "server", "handle", "type name", + (unsigned char*)"mysecret", 2, time (NULL) + }; + + int res; + + res = sid_storage_store_assoc (store, &assoc); + CuAssertIntEquals (cu, 1, res); + + res = sid_storage_find_assoc (store, "other server", NULL, &assoc); + CuAssertIntEquals (cu, 0, res); + + res = sid_storage_find_assoc (store, NULL, "other handle", &assoc); + CuAssertIntEquals (cu, 0, res); + + res = sid_storage_find_assoc (store, "server", "handle", &assoc); + CuAssertIntEquals (cu, 1, res); +} + +void unit_test_storage_invalidate (CuTest *cu) +{ + sid_assoc_t assoc = { + "server", "handle", "type", + (unsigned char*)"mysecret", 8, time (NULL) + }; + + int res; + + res = sid_storage_store_assoc (store, &assoc); + CuAssertIntEquals (cu, 1, res); + + res = sid_storage_find_assoc (store, "server", "handle", &assoc); + CuAssertIntEquals (cu, 1, res); + + sid_storage_invalidate_assoc (store, "bad server", NULL); + + /* Should still find */ + res = sid_storage_find_assoc (store, "server", "handle", &assoc); + CuAssertIntEquals (cu, 1, res); + + sid_storage_invalidate_assoc (store, "server", NULL); + + /* Gone now */ + res = sid_storage_find_assoc (store, "server", "handle", &assoc); + CuAssertIntEquals (cu, 0, res); +} + +void unit_test_storage_check_nonce (CuTest *cu) +{ + char nonce[256]; + int capacity; + int i, res; + + capacity = sid_storage_nonce_capacity (store); + CuAssert (cu, "should be greater than two", capacity > 2); + + /* Fill to half capacity, and do some checks */ + for (i = 0; i < capacity / 2; ++i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not be seen", res == 0); + } + + /* All those should be present */ + for (i = 0; i < capacity / 2; ++i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should be seen", res == 1); + } + + /* Fill capacity, and blow first set away */ + for (i = 0; i < capacity; ++i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_B%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not be seen", res == 0); + } + + /* First set should still be seen, even though we blew them away */ + for (i = 0; i < capacity / 2; ++i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_A%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should be seen", res == 1); + } + + /* Stuff never entered should not be seen */ + for (i = 0; i < capacity; ++i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_C%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not be seen", res == 0); + } + + /* Fill odd ones in reverse*/ + for (i = capacity - 1; i >= 0; --i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_D%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not be seen", res == 0); + } + + /* Should all be present */ + for (i = capacity - 1; i >= 0; --i) { + snprintf (nonce, 256, "2009-01-01T00:00:00Z_D%d", i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not seen", res == 1); + } + +} + +void unit_test_storage_long_nonce (CuTest *cu) +{ + char nonce[256]; + int capacity; + int i, res; + + #define SUFFIX "xxxxxxxxx xxxxxxxxx " + capacity = sid_storage_nonce_capacity (store); + + /* Fill to capacity, and do some checks */ + for (i = 0; i < capacity; ++i) { + snprintf (nonce, 256, "2009-06-01T00:00:00Z_X%d" SUFFIX, i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should not be seen", res == 0); + } + + /* All those should be present */ + for (i = 0; i < capacity; ++i) { + snprintf (nonce, 256, "2009-06-01T00:00:00Z_X%d" SUFFIX, i); + res = sid_storage_check_nonce (store, "server", nonce); + CuAssert (cu, "should be seen", res == 1); + } +} + +/* ----------------------------------------------------------------------------- + * Code being tested + */ + +#include "module/storage.c" |