From 1c7849b72455180e3aa4e57d60b2ca621b63694b Mon Sep 17 00:00:00 2001 From: Dan Cross Date: Thu, 11 Oct 2018 10:51:06 +0000 Subject: [PATCH] Fix a bug in stralloc_starts() and add a test. strcalloc_starts() should have tested the return value of `memcmp` against 0 for equality. Fixed and added a test case. As an aside, one might wonder how bugs like that are creeping into well-tested code imported from other projects? The answer, specific to stralloc, is that the original code was very specific to qmail, and used a number of additional functions specific to qmail. Rather than import half of qmail, the version imported into Magicka has been reworked to, instead, use standard C functions. The process of modifying the code gave rise to the opportunity for bugs to creep in. Now that a unit testing framework is in place, we can test things in isolation more easily and hopefully catch such things BEFORE they are published to the master repository. Signed-off-by: Dan Cross --- src/GNUmakefile.common | 8 ++++- src/stralloc/stralloc.c | 2 +- src/stralloc/test_stralloc.c | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/stralloc/test_stralloc.c diff --git a/src/GNUmakefile.common b/src/GNUmakefile.common index a040b8f..700fd57 100644 --- a/src/GNUmakefile.common +++ b/src/GNUmakefile.common @@ -11,6 +11,7 @@ ODOORS:= ${DEPSDIR}/odoors/libs-${OS}/libODoors.a JSMN:= ${DEPSDIR}/jsmn/libjsmn.a CDK:= ${DEPSDIR}/cdk-5-20161210/libcdk.a UUID:= ${DEPSDIR}/libuuid/.libs/libuuid.a +CUTEST:= ${DEPSDIR}/cutest-1.5/libcutest.a all: magicka magimail magiedit ticproc mgpost magichat \ filecenter dosbox_shim magiftpd reset_pass @@ -46,6 +47,9 @@ ${UUID}: cd ${DEPSDIR}/libuuid/ && ./configure cd ${DEPSDIR}/libuuid/ && ${MAKE} CC=${CC} +${CUTEST}: + cd ${DEPSDIR}/cutest-1.5 && make + HDRS:= bbs.h OBJS:= inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o \ doors.o bbs_list.o chat_system.o email.o files.o settings.o \ @@ -63,7 +67,7 @@ endif %.o: %.c ${HDRS} ${CC} -c -o $@ $< ${CFLAGS} -magickawww: ${OBJS} ${WWWOBJS} ${LUA} ${ZMODEM} ${B64} ${JAMLIB} ${JSMN} ${UUID} +magickawww: ${OBJS} ${WWWOBJS} ${LUA} ${ZMODEM} ${B64} ${JAMLIB} ${JSMN} ${UUID} ${CC} -o ../magicka $^ ${LIBS} -lmicrohttpd magicka: ${OBJS} ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} ${UUID} @@ -116,3 +120,5 @@ clean: cd ${UTILSDIR}/magiftpd && ${MAKE} clean cd ${UTILSDIR}/reset_pass && ${MAKE} clean cd ${UTILSDIR}/dosbox_shim && ${MAKE} clean + cd ${DEPSDIR}/libuuid && ${MAKE} clean + cd ${DEPSDIR}/cutest-1.5 && ${MAKE} clean diff --git a/src/stralloc/stralloc.c b/src/stralloc/stralloc.c index 10473ed..fcd6cd4 100644 --- a/src/stralloc/stralloc.c +++ b/src/stralloc/stralloc.c @@ -33,7 +33,7 @@ int stralloc_starts(stralloc *sa, const char *s) { assert(sa != NULL); assert(s != NULL); len = strlen(s); - return (sa->len >= len) && memcmp(sa->s, s, len); + return (sa->len >= len) && memcmp(sa->s, s, len) == 0; } int stralloc_copyb(stralloc *sa, const char *s, size_t n) { diff --git a/src/stralloc/test_stralloc.c b/src/stralloc/test_stralloc.c new file mode 100644 index 0000000..87be9d9 --- /dev/null +++ b/src/stralloc/test_stralloc.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "CuTest.h" + +#include "stralloc.h" + +void test_stralloc_starts(CuTest* tc) { + stralloc sa = EMPTY_STRALLOC; + stralloc_copys(&sa, "This is a test"); + CuAssertTrue(tc, stralloc_starts(&sa, "This")); +} + +void test_stralloc_starts_equal(CuTest* tc) { + stralloc sa = EMPTY_STRALLOC; + stralloc_copys(&sa, "This is a test"); + CuAssertTrue(tc, stralloc_starts(&sa, "This is a test")); +} + +void test_stralloc_starts_notequal(CuTest* tc) { + stralloc sa = EMPTY_STRALLOC; + stralloc_copys(&sa, "This is a test"); + CuAssertTrue(tc, !stralloc_starts(&sa, "this")); +} + +void test_stralloc_starts_toolong(CuTest* tc) { + stralloc sa = EMPTY_STRALLOC; + stralloc_copys(&sa, "This is a test"); + CuAssertTrue(tc, !stralloc_starts(&sa, "This is a test!")); +} + +CuSuite* stralloc_suite(void) { + CuSuite* suite = CuSuiteNew(); + + SUITE_ADD_TEST(suite, test_stralloc_starts); + SUITE_ADD_TEST(suite, test_stralloc_starts_equal); + SUITE_ADD_TEST(suite, test_stralloc_starts_notequal); + SUITE_ADD_TEST(suite, test_stralloc_starts_toolong); + + return suite; +} + +void RunAllTests(void) { + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + + CuSuiteAddSuite(suite, stralloc_suite()); + + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); +} + +int main(void) { + RunAllTests(); +}