From 543b45ab3ed3f4d1a9852ead27becf0f2dee8f6d Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 18 Jun 2009 17:12:25 +0000 Subject: Make tests for storage, and fix problems --- Makefile.am | 2 +- autogen.sh | 2 +- configure.in | 9 +- module/mod_auth_singleid.h | 2 + module/storage.c | 203 +++++++++---- tests/Makefile.am | 36 +++ tests/cu-test/AllTests.c | 25 ++ tests/cu-test/CuTest.c | 309 +++++++++++++++++++ tests/cu-test/CuTest.h | 111 +++++++ tests/cu-test/CuTestTest.c | 709 ++++++++++++++++++++++++++++++++++++++++++++ tests/cu-test/README | 209 +++++++++++++ tests/cu-test/license.txt | 38 +++ tests/cu-test/make-tests.sh | 51 ++++ tests/prep-tests.sh | 114 +++++++ tests/test-helpers.c | 51 ++++ tests/test-helpers.h | 42 +++ tests/unit-test-storage.c | 241 +++++++++++++++ 17 files changed, 2101 insertions(+), 53 deletions(-) create mode 100644 tests/Makefile.am create mode 100644 tests/cu-test/AllTests.c create mode 100644 tests/cu-test/CuTest.c create mode 100644 tests/cu-test/CuTest.h create mode 100644 tests/cu-test/CuTestTest.c create mode 100644 tests/cu-test/README create mode 100644 tests/cu-test/license.txt create mode 100755 tests/cu-test/make-tests.sh create mode 100755 tests/prep-tests.sh create mode 100644 tests/test-helpers.c create mode 100644 tests/test-helpers.h create mode 100644 tests/unit-test-storage.c diff --git a/Makefile.am b/Makefile.am index 4cff35f..13a52bf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ -SUBDIRS = module +SUBDIRS = module tests dist-hook: rm -rf `find $(distdir)/ -name '.??*'` diff --git a/autogen.sh b/autogen.sh index 2995946..81fbba3 100755 --- a/autogen.sh +++ b/autogen.sh @@ -7,5 +7,5 @@ autoheader libtoolize automake -a autoconf -./configure --enable-maintainer-mode "$@" +./configure "$@" diff --git a/configure.in b/configure.in index 7f1c9e6..bcbc312 100644 --- a/configure.in +++ b/configure.in @@ -96,9 +96,15 @@ fi AC_SUBST(APACHECTL) -APACHE_CFLAGS="-I`${APXS} -q INCLUDEDIR` -I`${apr_config} --includedir --cppflags`" +APR_CFLAGS="-I`$apr_config --includedir --cppflags`" +APACHE_CFLAGS="-I`$APXS -q INCLUDEDIR` $APR_CFLAGS" +APR_LIBS="`$apr_config --link-ld --libs` -laprutil-1" + +AC_SUBST(APR_CFLAGS) +AC_SUBST(APR_LIBS) AC_SUBST(APACHE_CFLAGS) + # ------------------------------------------------------------------------------------ # OPKELE @@ -123,6 +129,7 @@ fi AC_CONFIG_FILES([ Makefile module/Makefile + tests/Makefile ]) AC_OUTPUT diff --git a/module/mod_auth_singleid.h b/module/mod_auth_singleid.h index 68b5490..eb6d7f7 100644 --- a/module/mod_auth_singleid.h +++ b/module/mod_auth_singleid.h @@ -52,6 +52,8 @@ int sid_storage_check_nonce (sid_storage_t *storage, const char *server, const char *nonce); +size_t sid_storage_nonce_capacity (sid_storage_t *storage); + int sid_storage_store_assoc (sid_storage_t *storage, const sid_assoc_t *assoc); diff --git a/module/storage.c b/module/storage.c index cb1085d..67863eb 100644 --- a/module/storage.c +++ b/module/storage.c @@ -21,12 +21,33 @@ struct sid_storage { /* Book keeping for the records */ sid_nonce_t *records; - size_t first; + size_t last; size_t total; size_t count; - int wrapped; }; +sid_storage_t* +sid_storage_initialize (void *memory, size_t n_memory) +{ + sid_storage_t *storage; + + if (!memory) + return NULL; + if (n_memory < sizeof (sid_storage_t) + (sizeof (sid_nonce_t) * 3)) + return NULL; + + storage = memory; + memset (storage, 0, sizeof (storage)); + + /* Note that we assume that it's all blank */ + storage->records = (sid_nonce_t*)(storage + 1); + storage->total = (n_memory - sizeof (sid_storage_t)) / sizeof (sid_nonce_t); + storage->last = 0; + storage->count = 1; + + return storage; +} + int sid_storage_store_assoc (sid_storage_t *store, const sid_assoc_t *assoc) { @@ -43,6 +64,7 @@ sid_storage_store_assoc (sid_storage_t *store, const sid_assoc_t *assoc) strcpy (store->server, assoc->server ? assoc->server : ""); strcpy (store->handle, assoc->handle ? assoc->handle : ""); memcpy (store->secret, assoc->secret, assoc->n_secret); + store->n_secret = assoc->n_secret; strcpy (store->type, assoc->type ? assoc->type : ""); store->expires = assoc->expires; return 1; @@ -99,17 +121,18 @@ nonce_put (sid_nonce_t rec, const char *nonce) size_t len = strlen (nonce); char *dst = (char*)rec; + assert (sizeof (sid_nonce_t) == 40); + /* If it's short enough, then just store. Fast */ - if (len < sizeof (rec)) { + if (len < 40) { memcpy (dst, nonce, len); - memset (dst + len, 0, sizeof (rec) - len); + memset (dst + len, 0, 40 - len); /* Too long, need to compress into the record */ } else { apr_sha1_ctx_t ctx; - assert (sizeof (sid_nonce_t) == APR_SHA1_DIGESTSIZE + 20); - assert (len > 20); + assert (APR_SHA1_DIGESTSIZE == 20); /* The date prefix we just copy in */ memcpy (dst, nonce, 20); @@ -122,39 +145,94 @@ nonce_put (sid_nonce_t rec, const char *nonce) } static void -insert_nonce_record (sid_storage_t *store, sid_nonce_t rec, size_t at) +bump_ring_forwards (sid_storage_t *store, size_t beg, size_t end) { - sid_nonce_t *records = store->records; + sid_nonce_t *records; - assert (store->total > 2); - assert (at < store->total); - assert (at != store->first); + assert (store); + assert (end <= store->total); - /* Insertion right after latest, either ancient, more likely top */ - if (at == store->first + 1 % store->total) { - /* We can just copy in at this point */ + if (beg == end) + return; - /* Our ring has empty space in it, so always push forwards, but only until first */ - } else if (!store->wrapped) { - memmove (records + at + 1, records + at, sizeof (rec) * (store->first - at)); - store->first += 1; + records = store->records; + assert (beg < store->total); - /* Move data backwards to make space */ - } else if (store->first < at) { - memmove (records + store->first + 2, records + store->first + 1, - sizeof (rec) * (at - store->first)); + /* Simple memmove forward? */ + if (end < store->total && beg < end) { + memmove (store->records + beg + 1, store->records + beg, + (end - beg) * sizeof (sid_nonce_t)); - /* Move data forwards to make space simply */ + /* We wrap, far more complex */ } else { - memmove (records + at + 1, records + at, sizeof (rec) * (store->first - at - 1)); + if (end < beg) + memmove (store->records + 1, store->records, + end * sizeof (sid_nonce_t)); + memcpy (store->records, store->records + store->total - 1, + sizeof (sid_nonce_t)); + memmove (store->records + beg + 1, store->records + beg, + (store->total - beg - 1) * sizeof (sid_nonce_t)); } +} - memcpy (records[at], rec, sizeof (rec)); - ++store->count; +static void +bump_ring_backwards (sid_storage_t *store, size_t beg, size_t end) +{ + sid_nonce_t *records; + + assert (store); + assert (end <= store->total); + + if (beg == end) + return; + + records = store->records; + assert (beg < store->total); + + /* Simple memmove backwards? */ + if (beg > 0 && beg < end) { + memmove (store->records + beg - 1, store->records + beg, + (end - beg) * sizeof (sid_nonce_t)); - /* Track whether we have a full ring or not. */ - if (!store->wrapped && store->count > store->total) - store->wrapped = 1; + /* We wrap, far more complex */ + } else { + if (end < beg) + memmove (store->records + beg - 1, store->records + beg, + (store->total - beg) * sizeof (sid_nonce_t)); + memcpy (store->records + store->total - 1, store->records, + sizeof (sid_nonce_t)); + memmove (store->records, store->records + 1, + (end - 1) * sizeof (sid_nonce_t)); + } +} + +static void +insert_nonce_ring (sid_storage_t *store, sid_nonce_t rec, size_t idx) +{ + sid_nonce_t *records = store->records; + size_t off, top, bottom; + + assert (store->total > 2); + assert (idx < store->total); + + /* Calculate the distance on either side of last */ + bottom = store->last; + top = store->last + store->total; + off = idx; + if (off <= bottom) + off += store->total; + + /* Backwards move */ + if (off - bottom < top - off) { + bump_ring_backwards (store, (store->last + 2) % store->total, idx); + memcpy (records + idx - 1, rec, sizeof (sid_nonce_t)); + + /* Forwards or no move */ + } else { + bump_ring_forwards (store, idx, (store->last + 1) % store->total); + memcpy (records + idx, rec, sizeof (sid_nonce_t)); + store->last = (store->last + 1) % store->total; + } } int @@ -162,45 +240,70 @@ sid_storage_check_nonce (sid_storage_t *store, const char *server, const char *n { sid_nonce_t *records; sid_nonce_t rec; - size_t at, lower, upper, mid; + size_t idx, lower, upper, mid; int res; assert (store); assert (nonce); assert (store->records); - assert (store->first < store->total); + assert (store->last < store->total); nonce_put (rec, nonce); records = store->records; + /* Best case scenario, new nonce is higher than our latest one */ - res = nonce_compare (rec, records[store->first]); + res = nonce_compare (rec, records[store->last]); /* Was the last nonce */ if (res == 0) { return 1; /* Newer than anything, push on top */ - } else if (res < 0) { - at = (store->first + 1) % store->total; - insert_nonce_record (store, rec, at); - store->first = at; - return 0; - } + } else if (res > 0) { + store->last = (store->last + 1) % store->total; + memcpy (records + store->last, rec, sizeof (sid_nonce_t)); + + } else { - /* Do a binary search for the item */ - for (lower = 0, upper = store->total; lower < upper; ) { - mid = lower + ((upper - lower) / 2); // Note: not (low + high) / 2 !! - at = at + store->first % store->total; - res = nonce_compare (rec, records[at]); - if (res == 0) - return 1; /* Have the nonce */ - else if (res > 0) - lower = mid + 1; - else - upper = mid; + /* Check if it's before our last one, assume expired if so */ + idx = (store->last + 1) % store->total; + res = nonce_compare (rec, records[idx]); + if (res < 0) { + return 1; + + /* Do a binary search for the item */ + } else { + lower = 0; + if (store->count < store->total) + lower = store->total - store->count; + upper = store->total; + while (lower < upper) { + mid = lower + ((upper - lower) / 2); // Note: not (low + high) / 2 !! + idx = (store->last + mid) % store->total; + res = nonce_compare (rec, records[idx]); + if (res == 0) + return 1; /* Have the nonce */ + else if (res > 0) + lower = mid + 1; + else + upper = mid; + } + + idx = (store->last + upper) % store->total; + insert_nonce_ring (store, rec, idx); + } } - insert_nonce_record (store, rec, at); + ++store->count; return 0; /* Didn't find nonce */ } + +size_t +sid_storage_nonce_capacity (sid_storage_t *storage) +{ + assert (storage); + return storage->total; +} + + diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..fc04b47 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,36 @@ + +UNIT_TESTS = \ + unit-test-storage.c + +INCLUDES= \ + -I$(top_srcdir) \ + -I$(top_builddir) \ + $(APR_CFLAGS) \ + $(OPKELE_CFLAGS) + +noinst_PROGRAMS= \ + run-tests + +run-tests.c: $(UNIT_TESTS) prep-tests.sh Makefile.am + sh prep-tests.sh -b run-tests $(UNIT_TESTS) + +run_tests_SOURCES = \ + run-tests.c \ + run-tests.h \ + $(UNIT_TESTS) + +run_tests_LDADD = \ + $(APR_LIBS) + +EXTRA_DIST = \ + prep-tests.sh \ + cu-test \ + test-helpers.c \ + test-helpers.h + +# ------------------------------------------------------------------------------ +# Run the tests + +run: $(noinst_PROGRAMS) + ./run-tests + diff --git a/tests/cu-test/AllTests.c b/tests/cu-test/AllTests.c new file mode 100644 index 0000000..5c849ef --- /dev/null +++ b/tests/cu-test/AllTests.c @@ -0,0 +1,25 @@ +#include + +#include "CuTest.h" + +CuSuite* CuGetSuite(); +CuSuite* CuStringGetSuite(); + +void RunAllTests(void) +{ + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + + CuSuiteAddSuite(suite, CuGetSuite()); + CuSuiteAddSuite(suite, CuStringGetSuite()); + + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); +} + +int main(void) +{ + RunAllTests(); +} diff --git a/tests/cu-test/CuTest.c b/tests/cu-test/CuTest.c new file mode 100644 index 0000000..86fa3ea --- /dev/null +++ b/tests/cu-test/CuTest.c @@ -0,0 +1,309 @@ +#include +#include +#include +#include +#include +#include + +#include "CuTest.h" + +/*-------------------------------------------------------------------------* + * CuStr + *-------------------------------------------------------------------------*/ + +char* CuStrAlloc(int size) +{ + char* newStr = (char*) malloc( sizeof(char) * (size) ); + return newStr; +} + +char* CuStrCopy(const char* old) +{ + int len = strlen(old); + char* newStr = CuStrAlloc(len + 1); + strcpy(newStr, old); + return newStr; +} + +/*-------------------------------------------------------------------------* + * CuString + *-------------------------------------------------------------------------*/ + +void CuStringInit(CuString* str) +{ + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*) malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; +} + +CuString* CuStringNew(void) +{ + CuString* str = (CuString*) malloc(sizeof(CuString)); + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*) malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; + return str; +} + +void CuStringResize(CuString* str, int newSize) +{ + str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize); + str->size = newSize; +} + +void CuStringAppend(CuString* str, const char* text) +{ + int length; + + if (text == NULL) { + text = "NULL"; + } + + length = strlen(text); + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + str->length += length; + strcat(str->buffer, text); +} + +void CuStringAppendChar(CuString* str, char ch) +{ + char text[2]; + text[0] = ch; + text[1] = '\0'; + CuStringAppend(str, text); +} + +void CuStringAppendFormat(CuString* str, const char* format, ...) +{ + va_list argp; + char buf[HUGE_STRING_LEN]; + va_start(argp, format); + vsprintf(buf, format, argp); + va_end(argp); + CuStringAppend(str, buf); +} + +void CuStringInsert(CuString* str, const char* text, int pos) +{ + int length = strlen(text); + if (pos > str->length) + pos = str->length; + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1); + str->length += length; + memcpy(str->buffer + pos, text, length); +} + +/*-------------------------------------------------------------------------* + * CuTest + *-------------------------------------------------------------------------*/ + +void CuTestInit(CuTest* t, const char* name, TestFunction function) +{ + t->name = CuStrCopy(name); + t->failed = 0; + t->ran = 0; + t->message = NULL; + t->function = function; + t->jumpBuf = NULL; +} + +CuTest* CuTestNew(const char* name, TestFunction function) +{ + CuTest* tc = CU_ALLOC(CuTest); + CuTestInit(tc, name, function); + return tc; +} + +void CuTestRun(CuTest* tc) +{ + jmp_buf buf; + tc->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + tc->ran = 1; + (tc->function)(tc); + } + tc->jumpBuf = 0; +} + +static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string) +{ + char buf[HUGE_STRING_LEN]; + + sprintf(buf, "%s:%d: ", file, line); + CuStringInsert(string, buf, 0); + + tc->failed = 1; + tc->message = string->buffer; + if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0); +} + +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message) +{ + CuString string; + + CuStringInit(&string); + if (message2 != NULL) + { + CuStringAppend(&string, message2); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, message); + CuFailInternal(tc, file, line, &string); +} + +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition) +{ + if (condition) return; + CuFail_Line(tc, file, line, NULL, message); +} + +void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + const char* expected, const char* actual) +{ + CuString string; + if ((expected == NULL && actual == NULL) || + (expected != NULL && actual != NULL && + strcmp(expected, actual) == 0)) + { + return; + } + + CuStringInit(&string); + if (message != NULL) + { + CuStringAppend(&string, message); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, "expected <"); + CuStringAppend(&string, expected); + CuStringAppend(&string, "> but was <"); + CuStringAppend(&string, actual); + CuStringAppend(&string, ">"); + CuFailInternal(tc, file, line, &string); +} + +void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + int expected, int actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected <%d> but was <%d>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + double expected, double actual, double delta) +{ + char buf[STRING_MAX]; + if (fabs(expected - actual) <= delta) return; + sprintf(buf, "expected <%lf> but was <%lf>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + void* expected, void* actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + + +/*-------------------------------------------------------------------------* + * CuSuite + *-------------------------------------------------------------------------*/ + +void CuSuiteInit(CuSuite* testSuite) +{ + testSuite->count = 0; + testSuite->failCount = 0; +} + +CuSuite* CuSuiteNew(void) +{ + CuSuite* testSuite = CU_ALLOC(CuSuite); + CuSuiteInit(testSuite); + return testSuite; +} + +void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase) +{ + assert(testSuite->count < MAX_TEST_CASES); + testSuite->list[testSuite->count] = testCase; + testSuite->count++; +} + +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) +{ + int i; + for (i = 0 ; i < testSuite2->count ; ++i) + { + CuTest* testCase = testSuite2->list[i]; + CuSuiteAdd(testSuite, testCase); + } +} + +void CuSuiteRun(CuSuite* testSuite) +{ + int i; + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + CuTestRun(testCase); + if (testCase->failed) { testSuite->failCount += 1; } + } +} + +void CuSuiteSummary(CuSuite* testSuite, CuString* summary) +{ + int i; + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + CuStringAppend(summary, testCase->failed ? "F" : "."); + } + CuStringAppend(summary, "\n\n"); +} + +void CuSuiteDetails(CuSuite* testSuite, CuString* details) +{ + int i; + int failCount = 0; + + if (testSuite->failCount == 0) + { + int passCount = testSuite->count - testSuite->failCount; + const char* testWord = passCount == 1 ? "test" : "tests"; + CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord); + } + else + { + if (testSuite->failCount == 1) + CuStringAppend(details, "There was 1 failure:\n"); + else + CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount); + + for (i = 0 ; i < testSuite->count ; ++i) + { + CuTest* testCase = testSuite->list[i]; + if (testCase->failed) + { + failCount++; + CuStringAppendFormat(details, "%d) %s: %s\n", + failCount, testCase->name, testCase->message); + } + } + CuStringAppend(details, "\n!!!FAILURES!!!\n"); + + CuStringAppendFormat(details, "Runs: %d ", testSuite->count); + CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); + CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); + } +} diff --git a/tests/cu-test/CuTest.h b/tests/cu-test/CuTest.h new file mode 100644 index 0000000..7cd2a4f --- /dev/null +++ b/tests/cu-test/CuTest.h @@ -0,0 +1,111 @@ +#ifndef CU_TEST_H +#define CU_TEST_H + +#include +#include + +/* CuString */ + +char* CuStrAlloc(int size); +char* CuStrCopy(const char* old); + +#define CU_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE))) + +#define HUGE_STRING_LEN 8192 +#define STRING_MAX 256 +#define STRING_INC 256 + +typedef struct +{ + int length; + int size; + char* buffer; +} CuString; + +void CuStringInit(CuString* str); +CuString* CuStringNew(void); +void CuStringRead(CuString* str, const char* path); +void CuStringAppend(CuString* str, const char* text); +void CuStringAppendChar(CuString* str, char ch); +void CuStringAppendFormat(CuString* str, const char* format, ...); +void CuStringInsert(CuString* str, const char* text, int pos); +void CuStringResize(CuString* str, int newSize); + +/* CuTest */ + +typedef struct CuTest CuTest; + +typedef void (*TestFunction)(CuTest *); + +struct CuTest +{ + const char* name; + TestFunction function; + int failed; + int ran; + const char* message; + jmp_buf *jumpBuf; +}; + +void CuTestInit(CuTest* t, const char* name, TestFunction function); +CuTest* CuTestNew(const char* name, TestFunction function); +void CuTestRun(CuTest* tc); + +/* Internal versions of assert functions -- use the public versions */ +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); +void CuAssertStrEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + const char* expected, const char* actual); +void CuAssertIntEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + int expected, int actual); +void CuAssertDblEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + double expected, double actual, double delta); +void CuAssertPtrEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + void* expected, void* actual); + +/* public assert functions */ + +#define CuFail(tc, ms) CuFail_Line( (tc), __FILE__, __LINE__, NULL, (ms)) +#define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) +#define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) + +#define CuAssertStrEquals(tc,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertDblEquals(tc,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl)) +#define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl)) +#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertPtrEquals_Msg(tc,ms,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) + +#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL)) +#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL)) + +/* CuSuite */ + +#define MAX_TEST_CASES 1024 + +#define SUITE_ADD_TEST(SUITE,TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) + +typedef struct +{ + int count; + CuTest* list[MAX_TEST_CASES]; + int failCount; + +} CuSuite; + + +void CuSuiteInit(CuSuite* testSuite); +CuSuite* CuSuiteNew(void); +void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); +void CuSuiteRun(CuSuite* testSuite); +void CuSuiteSummary(CuSuite* testSuite, CuString* summary); +void CuSuiteDetails(CuSuite* testSuite, CuString* details); + +#endif /* CU_TEST_H */ diff --git a/tests/cu-test/CuTestTest.c b/tests/cu-test/CuTestTest.c new file mode 100644 index 0000000..547f119 --- /dev/null +++ b/tests/cu-test/CuTestTest.c @@ -0,0 +1,709 @@ +#include +#include +#include +#include +#include + +#include "CuTest.h" + +/*-------------------------------------------------------------------------* + * Helper functions + *-------------------------------------------------------------------------*/ + +#define CompareAsserts(tc, message, expected, actual) X_CompareAsserts((tc), __FILE__, __LINE__, (message), (expected), (actual)) + +static void X_CompareAsserts(CuTest* tc, const char *file, int line, const char* message, const char* expected, const char* actual) +{ + int mismatch; + if (expected == NULL || actual == NULL) { + mismatch = (expected != NULL || actual != NULL); + } else { + const char *front = __FILE__ ":"; + const size_t frontLen = strlen(front); + const size_t expectedLen = strlen(expected); + + const char *matchStr = actual; + + mismatch = (strncmp(matchStr, front, frontLen) != 0); + if (!mismatch) { + matchStr = strchr(matchStr + frontLen, ':'); + mismatch |= (matchStr == NULL || strncmp(matchStr, ": ", 2)); + if (!mismatch) { + matchStr += 2; + mismatch |= (strncmp(matchStr, expected, expectedLen) != 0); + } + } + } + + CuAssert_Line(tc, file, line, message, !mismatch); +} + +/*-------------------------------------------------------------------------* + * CuString Test + *-------------------------------------------------------------------------*/ + +void TestCuStringNew(CuTest* tc) +{ + CuString* str = CuStringNew(); + CuAssertTrue(tc, 0 == str->length); + CuAssertTrue(tc, 0 != str->size); + CuAssertStrEquals(tc, "", str->buffer); +} + + +void TestCuStringAppend(CuTest* tc) +{ + CuString* str = CuStringNew(); + CuStringAppend(str, "hello"); + CuAssertIntEquals(tc, 5, str->length); + CuAssertStrEquals(tc, "hello", str->buffer); + CuStringAppend(str, " world"); + CuAssertIntEquals(tc, 11, str->length); + CuAssertStrEquals(tc, "hello world", str->buffer); +} + + +void TestCuStringAppendNULL(CuTest* tc) +{ + CuString* str = CuStringNew(); + CuStringAppend(str, NULL); + CuAssertIntEquals(tc, 4, str->length); + CuAssertStrEquals(tc, "NULL", str->buffer); +} + + +void TestCuStringAppendChar(CuTest* tc) +{ + CuString* str = CuStringNew(); + CuStringAppendChar(str, 'a'); + CuStringAppendChar(str, 'b'); + CuStringAppendChar(str, 'c'); + CuStringAppendChar(str, 'd'); + CuAssertIntEquals(tc, 4, str->length); + CuAssertStrEquals(tc, "abcd", str->buffer); +} + + +void TestCuStringInserts(CuTest* tc) +{ + CuString* str = CuStringNew(); + CuStringAppend(str, "world"); + CuAssertIntEquals(tc, 5, str->length); + CuAssertStrEquals(tc, "world", str->buffer); + CuStringInsert(str, "hell", 0); + CuAssertIntEquals(tc, 9, str->length); + CuAssertStrEquals(tc, "hellworld", str->buffer); + CuStringInsert(str, "o ", 4); + CuAssertIntEquals(tc, 11, str->length); + CuAssertStrEquals(tc, "hello world", str->buffer); + CuStringInsert(str, "!", 11); + CuAssertIntEquals(tc, 12, str->length); + CuAssertStrEquals(tc, "hello world!", str->buffer); +} + + +void TestCuStringResizes(CuTest* tc) +{ + CuString* str = CuStringNew(); + int i; + for(i = 0 ; i < STRING_MAX ; ++i) + { + CuStringAppend(str, "aa"); + } + CuAssertTrue(tc, STRING_MAX * 2 == str->length); + CuAssertTrue(tc, STRING_MAX * 2 <= str->size); +} + +CuSuite* CuStringGetSuite(void) +{ + CuSuite* suite = CuSuiteNew(); + + SUITE_ADD_TEST(suite, TestCuStringNew); + SUITE_ADD_TEST(suite, TestCuStringAppend); + SUITE_ADD_TEST(suite, TestCuStringAppendNULL); + SUITE_ADD_TEST(suite, TestCuStringAppendChar); + SUITE_ADD_TEST(suite, TestCuStringInserts); + SUITE_ADD_TEST(suite, TestCuStringResizes); + + return suite; +} + +/*-------------------------------------------------------------------------* + * CuTest Test + *-------------------------------------------------------------------------*/ + +void TestPasses(CuTest* tc) +{ + CuAssert(tc, "test should pass", 1 == 0 + 1); +} + +void zTestFails(CuTest* tc) +{ + CuAssert(tc, "test should fail", 1 == 1 + 1); +} + + +void TestCuTestNew(CuTest* tc) +{ + CuTest* tc2 = CuTestNew("MyTest", TestPasses); + CuAssertStrEquals(tc, "MyTest", tc2->name); + CuAssertTrue(tc, !tc2->failed); + CuAssertTrue(tc, tc2->message == NULL); + CuAssertTrue(tc, tc2->function == TestPasses); + CuAssertTrue(tc, tc2->ran == 0); + CuAssertTrue(tc, tc2->jumpBuf == NULL); +} + + +void TestCuTestInit(CuTest *tc) +{ + CuTest tc2; + CuTestInit(&tc2, "MyTest", TestPasses); + CuAssertStrEquals(tc, "MyTest", tc2.name); + CuAssertTrue(tc, !tc2.failed); + CuAssertTrue(tc, tc2.message == NULL); + CuAssertTrue(tc, tc2.function == TestPasses); + CuAssertTrue(tc, tc2.ran == 0); + CuAssertTrue(tc, tc2.jumpBuf == NULL); +} + +void TestCuAssert(CuTest* tc) +{ + CuTest tc2; + CuTestInit(&tc2, "MyTest", TestPasses); + + CuAssert(&tc2, "test 1", 5 == 4 + 1); + CuAssertTrue(tc, !tc2.failed); + CuAssertTrue(tc, tc2.message == NULL); + + CuAssert(&tc2, "test 2", 0); + CuAssertTrue(tc, tc2.failed); + CompareAsserts(tc, "CuAssert didn't fail", "test 2", tc2.message); + + CuAssert(&tc2, "test 3", 1); + CuAssertTrue(tc, tc2.failed); + CompareAsserts(tc, "CuAssert didn't fail", "test 2", tc2.message); + + CuAssert(&tc2, "test 4", 0); + CuAssertTrue(tc, tc2.failed); + CompareAsserts(tc, "CuAssert didn't fail", "test 4", tc2.message); + +} + +void TestCuAssertPtrEquals_Success(CuTest* tc) +{ + CuTest tc2; + int x; + + CuTestInit(&tc2, "MyTest", TestPasses); + + /* test success case */ + CuAssertPtrEquals(&tc2, &x, &x); + CuAssertTrue(tc, ! tc2.failed); + CuAssertTrue(tc, NULL == tc2.message); +} + +void TestCuAssertPtrEquals_Failure(CuTest* tc) +{ + CuTest tc2; + int x; + int* nullPtr = NULL; + char expected_message[STRING_MAX]; + + CuTestInit(&tc2, "MyTest", TestPasses); + + /* test failing case */ + sprintf(expected_message, "expected pointer <0x%p> but was <0x%p>", nullPtr, &x); + CuAssertPtrEquals(&tc2, NULL, &x); + CuAssertTrue(tc, tc2.failed); + CompareAsserts(tc, "CuAssertPtrEquals failed", expected_message, tc2.message); +} + +void TestCuAssertPtrNotNull_Success(CuTest* tc) +{ + CuTest tc2; + int x; + + CuTestInit(&tc2, "MyTest", TestPasses); + + /* test success case */ + CuAssertPtrNotNull(&tc2, &x); + CuAssertTrue(tc, ! tc2.failed); + CuAssertTrue(tc, NULL == tc2.message); +} + +void TestCuAssertPtrNotNull_Failure(CuTest* tc) +{ + CuTest tc2; + + CuTestInit(&tc2, "MyTest", TestPasses); + + /* test failing case */ + CuAssertPtrNotNull(&tc2, NULL); + CuAssertTrue(tc, tc2.failed); + CompareAsserts(tc, "CuAssertPtrNotNull failed", "null pointer unexpected", tc2.message); +} + +void TestCuTestRun(CuTest* tc) +{ + CuTest tc2; + CuTestInit(&tc2, "MyTest", zTestFails); + CuTestRun(&tc2); + + CuAssertStrEquals(tc, "MyTest", tc2.name); + CuAssertTrue(tc, tc2.failed); + CuAssertTrue(tc, tc2.ran); + CompareAsserts(tc, "TestRun failed", "test should fail", tc2.message); +} + +/*-------------------------------------------------------------------------* + * CuSuite Test + *-------------------------------------------------------------------------*/ + +void TestCuSuiteInit(CuTest* tc) +{ + CuSuite ts; + CuSuiteInit(&ts); + CuAssertTrue(tc, ts.count == 0); + CuAssertTrue(tc, ts.failCount == 0); +} + +void TestCuSuiteNew(CuTest* tc) +{ + CuSuite* ts = CuSuiteNew(); + CuAssertTrue(tc, ts->count == 0); + CuAssertTrue(tc, ts->failCount == 0); +} + +void TestCuSuiteAddTest(CuTest* tc) +{ + CuSuite ts; + CuTest tc2; + + CuSuiteInit(&ts); + CuTestInit(&tc2, "MyTest", zTestFails); + + CuSuiteAdd(&ts, &tc2); + CuAssertTrue(tc, ts.count == 1); + + CuAssertStrEquals(tc, "MyTest", ts.list[0]->name); +} + +void TestCuSuiteAddSuite(CuTest* tc) +{ + CuSuite* ts1 = CuSuiteNew(); + CuSuite* ts2 = CuSuiteNew(); + + CuSuiteAdd(ts1, CuTestNew("TestFails1", zTestFails)); + CuSuiteAdd(ts1, CuTestNew("TestFails2", zTestFails)); + + CuSuiteAdd(ts2, CuTestNew("TestFails3", zTestFails)); + CuSuiteAdd(ts2, CuTestNew("TestFails4", zTestFails)); + + CuSuiteAddSuite(ts1, ts2); + CuAssertIntEquals(tc, 4, ts1->count); + + CuAssertStrEquals(tc, "TestFails1", ts1->list[0]->name); + CuAssertStrEquals(tc, "TestFails2", ts1->list[1]->name); + CuAssertStrEquals(tc, "TestFails3", ts1->list[2]->name); + CuAssertStrEquals(tc, "TestFails4", ts1->list[3]->name); +} + +void TestCuSuiteRun(CuTest* tc) +{ + CuSuite ts; + CuTest tc1, tc2, tc3, tc4; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestPasses", TestPasses); + CuTestInit(&tc2, "TestPasses", TestPasses); + CuTestInit(&tc3, "TestFails", zTestFails); + CuTestInit(&tc4, "TestFails", zTestFails); + + CuSuiteAdd(&ts, &tc1); + CuSuiteAdd(&ts, &tc2); + CuSuiteAdd(&ts, &tc3); + CuSuiteAdd(&ts, &tc4); + CuAssertTrue(tc, ts.count == 4); + + CuSuiteRun(&ts); + CuAssertTrue(tc, ts.count - ts.failCount == 2); + CuAssertTrue(tc, ts.failCount == 2); +} + +void TestCuSuiteSummary(CuTest* tc) +{ + CuSuite ts; + CuTest tc1, tc2; + CuString summary; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestPasses", TestPasses); + CuTestInit(&tc2, "TestFails", zTestFails); + CuStringInit(&summary); + + CuSuiteAdd(&ts, &tc1); + CuSuiteAdd(&ts, &tc2); + CuSuiteRun(&ts); + + CuSuiteSummary(&ts, &summary); + + CuAssertTrue(tc, ts.count == 2); + CuAssertTrue(tc, ts.failCount == 1); + CuAssertStrEquals(tc, ".F\n\n", summary.buffer); +} + + +void TestCuSuiteDetails_SingleFail(CuTest* tc) +{ + CuSuite ts; + CuTest tc1, tc2; + CuString details; + const char* front; + const char* back; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestPasses", TestPasses); + CuTestInit(&tc2, "TestFails", zTestFails); + CuStringInit(&details); + + CuSuiteAdd(&ts, &tc1); + CuSuiteAdd(&ts, &tc2); + CuSuiteRun(&ts); + + CuSuiteDetails(&ts, &details); + + CuAssertTrue(tc, ts.count == 2); + CuAssertTrue(tc, ts.failCount == 1); + + front = "There was 1 failure:\n" + "1) TestFails: "; + back = "test should fail\n" + "\n!!!FAILURES!!!\n" + "Runs: 2 Passes: 1 Fails: 1\n"; + + CuAssertStrEquals(tc, back, details.buffer + strlen(details.buffer) - strlen(back)); + details.buffer[strlen(front)] = 0; + CuAssertStrEquals(tc, front, details.buffer); +} + + +void TestCuSuiteDetails_SinglePass(CuTest* tc) +{ + CuSuite ts; + CuTest tc1; + CuString details; + const char* expected; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestPasses", TestPasses); + CuStringInit(&details); + + CuSuiteAdd(&ts, &tc1); + CuSuiteRun(&ts); + + CuSuiteDetails(&ts, &details); + + CuAssertTrue(tc, ts.count == 1); + CuAssertTrue(tc, ts.failCount == 0); + + expected = + "OK (1 test)\n"; + + CuAssertStrEquals(tc, expected, details.buffer); +} + +void TestCuSuiteDetails_MultiplePasses(CuTest* tc) +{ + CuSuite ts; + CuTest tc1, tc2; + CuString details; + const char* expected; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestPasses", TestPasses); + CuTestInit(&tc2, "TestPasses", TestPasses); + CuStringInit(&details); + + CuSuiteAdd(&ts, &tc1); + CuSuiteAdd(&ts, &tc2); + CuSuiteRun(&ts); + + CuSuiteDetails(&ts, &details); + + CuAssertTrue(tc, ts.count == 2); + CuAssertTrue(tc, ts.failCount == 0); + + expected = + "OK (2 tests)\n"; + + CuAssertStrEquals(tc, expected, details.buffer); +} + +void TestCuSuiteDetails_MultipleFails(CuTest* tc) +{ + CuSuite ts; + CuTest tc1, tc2; + CuString details; + const char* front; + const char* mid; + const char* back; + + CuSuiteInit(&ts); + CuTestInit(&tc1, "TestFails1", zTestFails); + CuTestInit(&tc2, "TestFails2", zTestFails); + CuStringInit(&details); + + CuSuiteAdd(&ts, &tc1); + CuSuiteAdd(&ts, &tc2); + CuSuiteRun(&ts); + + CuSuiteDetails(&ts, &details); + + CuAssertTrue(tc, ts.count == 2); + CuAssertTrue(tc, ts.failCount == 2); + + front = + "There were 2 failures:\n" + "1) TestFails1: "; + mid = "test should fail\n" + "2) TestFails2: "; + back = "test should fail\n" + "\n!!!FAILURES!!!\n" + "Runs: 2 Passes: 0 Fails: 2\n"; + + CuAssertStrEquals(tc, back, details.buffer + strlen(details.buffer) - strlen(back)); + CuAssert(tc, "Couldn't find middle", strstr(details.buffer, mid) != NULL); + details.buffer[strlen(front)] = 0; + CuAssertStrEquals(tc, front, details.buffer); +} + + +/*-------------------------------------------------------------------------* + * Misc Test + *-------------------------------------------------------------------------*/ + +void TestCuStrCopy(CuTest* tc) +{ + const char* old = "hello world"; + const char* newStr = CuStrCopy(old); + CuAssert(tc, "old is new", strcmp(old, newStr) == 0); +} + + +void TestCuStringAppendFormat(CuTest* tc) +{ + int i; + char* text = CuStrAlloc(301); /* long string */ + CuString* str = CuStringNew(); + for (i = 0 ; i < 300 ; ++i) + text[i] = 'a'; + text[300] = '\0'; + CuStringAppendFormat(str, "%s", text); + + /* buffer limit raised to HUGE_STRING_LEN so no overflow */ + + CuAssert(tc, "length of str->buffer is 300", 300 == strlen(str->buffer)); +} + +void TestFail(CuTest* tc) +{ + jmp_buf buf; + int pointReached = 0; + CuTest* tc2 = CuTestNew("TestFails", zTestFails); + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuFail(tc2, "hello world"); + pointReached = 1; + } + CuAssert(tc, "point was not reached", pointReached == 0); +} + +void TestAssertStrEquals(CuTest* tc) +{ + jmp_buf buf; + CuTest *tc2 = CuTestNew("TestAssertStrEquals", zTestFails); + + const char* expected = "expected but was "; + const char *expectedMsg = "some text: expected but was "; + + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertStrEquals(tc2, "hello", "world"); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals failed", expected, tc2->message); + if (setjmp(buf) == 0) + { + CuAssertStrEquals_Msg(tc2, "some text", "hello", "world"); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals failed", expectedMsg, tc2->message); +} + +void TestAssertStrEquals_NULL(CuTest* tc) +{ + jmp_buf buf; + CuTest *tc2 = CuTestNew("TestAssertStrEquals_NULL", zTestFails); + + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertStrEquals(tc2, NULL, NULL); + } + CuAssertTrue(tc, !tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_NULL failed", NULL, tc2->message); + if (setjmp(buf) == 0) + { + CuAssertStrEquals_Msg(tc2, "some text", NULL, NULL); + } + CuAssertTrue(tc, !tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_NULL failed", NULL, tc2->message); +} + +void TestAssertStrEquals_FailNULLStr(CuTest* tc) +{ + jmp_buf buf; + CuTest *tc2 = CuTestNew("TestAssertStrEquals_FailNULLStr", zTestFails); + + const char* expected = "expected but was "; + const char *expectedMsg = "some text: expected but was "; + + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertStrEquals(tc2, "hello", NULL); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_FailNULLStr failed", expected, tc2->message); + if (setjmp(buf) == 0) + { + CuAssertStrEquals_Msg(tc2, "some text", "hello", NULL); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_FailNULLStr failed", expectedMsg, tc2->message); +} + +void TestAssertStrEquals_FailStrNULL(CuTest* tc) +{ + jmp_buf buf; + CuTest *tc2 = CuTestNew("TestAssertStrEquals_FailStrNULL", zTestFails); + + const char* expected = "expected but was "; + const char *expectedMsg = "some text: expected but was "; + + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertStrEquals(tc2, NULL, "hello"); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_FailStrNULL failed", expected, tc2->message); + if (setjmp(buf) == 0) + { + CuAssertStrEquals_Msg(tc2, "some text", NULL, "hello"); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals_FailStrNULL failed", expectedMsg, tc2->message); +} + +void TestAssertIntEquals(CuTest* tc) +{ + jmp_buf buf; + CuTest *tc2 = CuTestNew("TestAssertIntEquals", zTestFails); + const char* expected = "expected <42> but was <32>"; + const char* expectedMsg = "some text: expected <42> but was <32>"; + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertIntEquals(tc2, 42, 32); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertIntEquals failed", expected, tc2->message); + if (setjmp(buf) == 0) + { + CuAssertIntEquals_Msg(tc2, "some text", 42, 32); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertStrEquals failed", expectedMsg, tc2->message); +} + +void TestAssertDblEquals(CuTest* tc) +{ + jmp_buf buf; + double x = 3.33; + double y = 10.0 / 3.0; + CuTest *tc2 = CuTestNew("TestAssertDblEquals", zTestFails); + char expected[STRING_MAX]; + char expectedMsg[STRING_MAX]; + sprintf(expected, "expected <%lf> but was <%lf>", x, y); + sprintf(expectedMsg, "some text: expected <%lf> but was <%lf>", x, y); + + CuTestInit(tc2, "TestAssertDblEquals", TestPasses); + + CuAssertDblEquals(tc2, x, x, 0.0); + CuAssertTrue(tc, ! tc2->failed); + CuAssertTrue(tc, NULL == tc2->message); + + CuAssertDblEquals(tc2, x, y, 0.01); + CuAssertTrue(tc, ! tc2->failed); + CuAssertTrue(tc, NULL == tc2->message); + + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertDblEquals(tc2, x, y, 0.001); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertDblEquals failed", expected, tc2->message); + tc2->jumpBuf = &buf; + if (setjmp(buf) == 0) + { + CuAssertDblEquals_Msg(tc2, "some text", x, y, 0.001); + } + CuAssertTrue(tc, tc2->failed); + CompareAsserts(tc, "CuAssertDblEquals failed", expectedMsg, tc2->message); +} + +/*-------------------------------------------------------------------------* + * main + *-------------------------------------------------------------------------*/ + +CuSuite* CuGetSuite(void) +{ + CuSuite* suite = CuSuiteNew(); + + SUITE_ADD_TEST(suite, TestCuStringAppendFormat); + SUITE_ADD_TEST(suite, TestCuStrCopy); + SUITE_ADD_TEST(suite, TestFail); + SUITE_ADD_TEST(suite, TestAssertStrEquals); + SUITE_ADD_TEST(suite, TestAssertStrEquals_NULL); + SUITE_ADD_TEST(suite, TestAssertStrEquals_FailStrNULL); + SUITE_ADD_TEST(suite, TestAssertStrEquals_FailNULLStr); + SUITE_ADD_TEST(suite, TestAssertIntEquals); + SUITE_ADD_TEST(suite, TestAssertDblEquals); + + SUITE_ADD_TEST(suite, TestCuTestNew); + SUITE_ADD_TEST(suite, TestCuTestInit); + SUITE_ADD_TEST(suite, TestCuAssert); + SUITE_ADD_TEST(suite, TestCuAssertPtrEquals_Success); + SUITE_ADD_TEST(suite, TestCuAssertPtrEquals_Failure); + SUITE_ADD_TEST(suite, TestCuAssertPtrNotNull_Success); + SUITE_ADD_TEST(suite, TestCuAssertPtrNotNull_Failure); + SUITE_ADD_TEST(suite, TestCuTestRun); + + SUITE_ADD_TEST(suite, TestCuSuiteInit); + SUITE_ADD_TEST(suite, TestCuSuiteNew); + SUITE_ADD_TEST(suite, TestCuSuiteAddTest); + SUITE_ADD_TEST(suite, TestCuSuiteAddSuite); + SUITE_ADD_TEST(suite, TestCuSuiteRun); + SUITE_ADD_TEST(suite, TestCuSuiteSummary); + SUITE_ADD_TEST(suite, TestCuSuiteDetails_SingleFail); + SUITE_ADD_TEST(suite, TestCuSuiteDetails_SinglePass); + SUITE_ADD_TEST(suite, TestCuSuiteDetails_MultiplePasses); + SUITE_ADD_TEST(suite, TestCuSuiteDetails_MultipleFails); + + return suite; +} diff --git a/tests/cu-test/README b/tests/cu-test/README new file mode 100644 index 0000000..8bc1540 --- /dev/null +++ b/tests/cu-test/README @@ -0,0 +1,209 @@ +HOW TO USE + +You can use CuTest to create unit tests to drive your development +in the style of Extreme Programming. You can also add unit tests to +existing code to ensure that it works as you suspect. + +Your unit tests are an investment. They let you to change your +code and add new features confidently without worrying about +accidentally breaking earlier features. + + +LICENSING + +For details on licensing see license.txt. + + +GETTING STARTED + +To add unit testing to your C code the only files you need are +CuTest.c and CuTest.h. + +CuTestTest.c and AllTests.c have been included to provide an +example of how to write unit tests and then how to aggregate them +into suites and into a single AllTests.c file. Suites allow you +to put group tests into logical sets. AllTests.c combines all the +suites and runs them. + +You should not have to look inside CuTest.c. Looking in +CuTestTest.c and AllTests.c (for example usage) should be +sufficient. + +After downloading the sources, run your compiler to create an +executable called AllTests.exe. For example, if you are using +Windows with the cl.exe compiler you would type: + + cl.exe AllTests.c CuTest.c CuTestTest.c + AllTests.exe + +This will run all the unit tests associated with CuTest and print +the output on the console. You can replace cl.exe with gcc or +your favorite compiler in the command above. + + +DETAILED EXAMPLE + +Here is a more detailed example. We will work through a simple +test first exercise. The goal is to create a library of string +utilities. First, lets write a function that converts a +null-terminated string to all upper case. + +Ensure that CuTest.c and CuTest.h are accessible from your C +project. Next, create a file called StrUtil.c with these +contents: + + #include "CuTest.h" + + char* StrToUpper(char* str) { + return str; + } + + void TestStrToUpper(CuTest *tc) { + char* input = strdup("hello world"); + char* actual = StrToUpper(input); + char* expected = "HELLO WORLD"; + CuAssertStrEquals(tc, expected, actual); + } + + CuSuite* StrUtilGetSuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, TestStrToUpper); + return suite; + } + +Create another file called AllTests.c with these contents: + + #include "CuTest.h" + + CuSuite* StrUtilGetSuite(); + + void RunAllTests(void) { + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + + CuSuiteAddSuite(suite, StrUtilGetSuite()); + + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); + } + + int main(void) { + RunAllTests(); + } + +Then type this on the command line: + + gcc AllTests.c CuTest.c StrUtil.c + +to compile. You can replace gcc with your favorite compiler. +CuTest should be portable enough to handle all Windows and Unix +compilers. Then to run the tests type: + + a.out + +This will print an error because we haven't implemented the +StrToUpper function correctly. We are just returning the string +without changing it to upper case. + + char* StrToUpper(char* str) { + return str; + } + +Rewrite this as follows: + + char* StrToUpper(char* str) { + char* p; + for (p = str ; *p ; ++p) *p = toupper(*p); + return str; + } + +Recompile and run the tests again. The test should pass this +time. + + +WHAT TO DO NEXT + +At this point you might want to write more tests for the +StrToUpper function. Here are some ideas: + +TestStrToUpper_EmptyString : pass in "" +TestStrToUpper_UpperCase : pass in "HELLO WORLD" +TestStrToUpper_MixedCase : pass in "HELLO world" +TestStrToUpper_Numbers : pass in "1234 hello" + +As you write each one of these tests add it to StrUtilGetSuite +function. If you don't the tests won't be run. Later as you write +other functions and write tests for them be sure to include those +in StrUtilGetSuite also. The StrUtilGetSuite function should +include all the tests in StrUtil.c + +Over time you will create another file called FunkyStuff.c +containing other functions unrelated to StrUtil. Follow the same +pattern. Create a FunkyStuffGetSuite function in FunkyStuff.c. +And add FunkyStuffGetSuite to AllTests.c. + +The framework is designed in the way it is so that it is easy to +organize a lot of tests. + +THE BIG PICTURE + +Each individual test corresponds to a CuTest. These are grouped +to form a CuSuite. CuSuites can hold CuTests or other CuSuites. +AllTests.c collects all the CuSuites in the program into a single +CuSuite which it then runs as a single CuSuite. + +The project is open source so feel free to take a peek under the +hood at the CuTest.c file to see how it works. CuTestTest.c +contains tests for CuTest.c. So CuTest tests itself. + +Since AllTests.c has a main() you will need to exclude this when +you are building your product. Here is a nicer way to do this if +you want to avoid messing with multiple builds. Remove the main() +in AllTests.c. Note that it just calls RunAllTests(). Instead +we'll call this directly from the main program. + +Now in the main() of the actual program check to see if the +command line option "--test" was passed. If it was then I call +RunAllTests() from AllTests.c. Otherwise run the real program. + +Shipping the tests with the code can be useful. If you customers +complain about a problem you can ask them to run the unit tests +and send you the output. This can help you to quickly isolate the +piece of your system that is malfunctioning in the customer's +environment. + +CuTest offers a rich set of CuAssert functions. Here is a list: + +void CuAssert(CuTest* tc, char* message, int condition); +void CuAssertTrue(CuTest* tc, int condition); +void CuAssertStrEquals(CuTest* tc, char* expected, char* actual); +void CuAssertIntEquals(CuTest* tc, int expected, int actual); +void CuAssertPtrEquals(CuTest* tc, void* expected, void* actual); +void CuAssertPtrNotNull(CuTest* tc, void* pointer); + +The project is open source and so you can add other more powerful +asserts to make your tests easier to write and more concise. +Please feel free to send me changes you make so that I can +incorporate them into future releases. + +If you see any errors in this document please contact me at +asimjalis@peakprogramming.com. + + +AUTOMATING TEST SUITE GENERATION + +make-tests.sh will grep through all the .c files in the current +directory and generate the code to run all the tests contained in +them. Using this script you don't have to worry about writing +AllTests.c or dealing with any of the other suite code. + + +CREDITS + +[02.23.2003] Dave Glowacki has added +(1) file name and line numbers to the error messages, (2) +AssertDblEquals for doubles, (3) AssertEquals_Msg version of +all the AssertEquals to pass in optional message which is +printed out on assert failure. diff --git a/tests/cu-test/license.txt b/tests/cu-test/license.txt new file mode 100644 index 0000000..fd81689 --- /dev/null +++ b/tests/cu-test/license.txt @@ -0,0 +1,38 @@ +NOTE + +The license is based on the zlib/libpng license. For more details see +http://www.opensource.org/licenses/zlib-license.html. The intent of the +license is to: + +- keep the license as simple as possible +- encourage the use of CuTest in both free and commercial applications + and libraries +- keep the source code together +- give credit to the CuTest contributors for their work + +If you ship CuTest in source form with your source distribution, the +following license document must be included with it in unaltered form. +If you find CuTest useful we would like to hear about it. + +LICENSE + +Copyright (c) 2003 Asim Jalis + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software in +a product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not +be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. diff --git a/tests/cu-test/make-tests.sh b/tests/cu-test/make-tests.sh new file mode 100755 index 0000000..c651033 --- /dev/null +++ b/tests/cu-test/make-tests.sh @@ -0,0 +1,51 @@ +#!/usr/local/bin/bash + +# Auto generate single AllTests file for CuTest. +# Searches through all *.c files in the current directory. +# Prints to stdout. +# Author: Asim Jalis +# Date: 01/08/2003 + +if test $# -eq 0 ; then FILES=*.c ; else FILES=$* ; fi + +echo ' + +/* This is auto-generated code. Edit at your own peril. */ + +#include "CuTest.h" + +' + +cat $FILES | grep '^unit_test_' | + sed -e 's/(.*$//' \ + -e 's/$/(CuTest*);/' \ + -e 's/^/extern /' + +echo \ +' + +void RunAllTests(void) +{ + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + +' +cat $FILES | grep '^void Test' | + sed -e 's/^void //' \ + -e 's/(.*$//' \ + -e 's/^/ SUITE_ADD_TEST(suite, /' \ + -e 's/$/);/' + +echo \ +' + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); +} + +int main(void) +{ + RunAllTests(); +} +' diff --git a/tests/prep-tests.sh b/tests/prep-tests.sh new file mode 100755 index 0000000..bdceb11 --- /dev/null +++ b/tests/prep-tests.sh @@ -0,0 +1,114 @@ +#!/bin/sh -e + +set -e + +# -------------------------------------------------------------------- +# FUNCTIONS + +usage() +{ + echo "usage: prep-tests.sh -b base-name files.c ..." >&2 + exit 2 +} + +# -------------------------------------------------------------------- +# ARGUMENT PARSING + +BASE=unit-test + +while [ $# -gt 0 ]; do + case "$1" in + -b) + BASE="$2" + shift + ;; + --) + shift + break + ;; + -*) + usage + ;; + *) + break + ;; + esac + shift +done + +FILES=$* + +# -------------------------------------------------------------------- +# HEADER FILE + +( + +# HEADER TOP +cat << END +/* This is auto-generated code. Edit at your own peril. */ +#include "tests/cu-test/CuTest.h" +#include "tests/test-helpers.h" +#include + +END + +# DECLARATIONS + + if [ -n "$FILES" ]; then + cat $FILES | grep '^void unit_setup_' | sed -e 's/$/;/' + cat $FILES | grep '^void unit_test_' | sed -e 's/$/;/' + cat $FILES | grep '^void unit_teardown_' | sed -e 's/$/;/' + fi + +) > $BASE.h + +# -------------------------------------------------------------------- +# SOURCE FILE + +( +# START RUNNER FUNCTION +cat << END +/* This is auto-generated code. Edit at your own peril. */ +#include "$BASE.h" + +static int RunAllTests(void) +{ + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + +END + + if [ -n "$FILES" ]; then + cat $FILES | grep '^void unit_setup_' | \ + sed -e 's/^void //' -e 's/(.*$//' -e 's/$/();/' + cat $FILES | grep '^void unit_test_' | \ + sed -e 's/^void //' -e 's/(.*$//' \ + -e 's/^/SUITE_ADD_TEST(suite, /' -e 's/$/);/' + fi + +# MIDDLE RUNNER FUNCTION +cat << END + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\\n", output->buffer); +END + + if [ -n "$FILES" ]; then + + cat $FILES | grep '^void unit_teardown_' | \ + sed -e 's/^void //' -e 's/(.*$//' -e 's/$/();/' + + fi + +# END RUNNER FUNCTION +cat << END + + return suite->failCount; +} + +#include "tests/test-helpers.c" +#include "tests/cu-test/CuTest.c" +END +) > $BASE.c + diff --git a/tests/test-helpers.c b/tests/test-helpers.c new file mode 100644 index 0000000..d8f5307 --- /dev/null +++ b/tests/test-helpers.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008, 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 + * + */ + +/* This file is included into the main .c file for each unit-test program */ + +#include +#include +#include + +#include "test-helpers.h" + +int +main (int argc, char* argv[]) +{ + return RunAllTests(); +} diff --git a/tests/test-helpers.h b/tests/test-helpers.h new file mode 100644 index 0000000..8cb5a2c --- /dev/null +++ b/tests/test-helpers.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2008, 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 + * + */ + +#ifndef TEST_HELPERS_H_ +#define TEST_HELPERS_H_ + +#endif /*TEST_HELPERS_H_*/ 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 +#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" -- cgit v1.2.3