/* * Copyright (c) 2009, Stefan Walter * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the * above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or * other materials provided with the distribution. * * The names of contributors to this software may not be * used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * * CONTRIBUTORS * Stef Walter * */ #include "config.h" #include "run-tests.h" #include "module/mod_auth_singleid.h" #include #include /* * 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"