X-Git-Url: https://git.distorted.org.uk/~mdw/disorder/blobdiff_plain/65bb0fff13f2099c2746d6b8c9ad2421e9419d07..4a99f7ba7e2e1ecc7a09528618e1e3ca54f5727d:/lib/test.c diff --git a/lib/test.c b/lib/test.c index 71847ff..07d09a7 100644 --- a/lib/test.c +++ b/lib/test.c @@ -49,6 +49,8 @@ #include "filepart.h" #include "hash.h" #include "selection.h" +#include "syscalls.h" +#include "kvp.h" static int tests, errors; static int fail_first; @@ -108,15 +110,31 @@ static const char *format_utf32(const uint32_t *s) { if(w == 0) { \ fprintf(stderr, "%s:%d: %s returned 0\n", \ __FILE__, __LINE__, #GOT); \ - count_error(); \ + count_error(); \ } else if(strcmp(w, g)) { \ fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \ __FILE__, __LINE__, #GOT, format(g), format(w)); \ - count_error(); \ + count_error(); \ } \ ++tests; \ } while(0) +#define check_string_prefix(GOT, WANT) do { \ + const char *g = GOT; \ + const char *w = WANT; \ + \ + if(w == 0) { \ + fprintf(stderr, "%s:%d: %s returned 0\n", \ + __FILE__, __LINE__, #GOT); \ + count_error(); \ + } else if(strncmp(w, g, strlen(w))) { \ + fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \ + __FILE__, __LINE__, #GOT, format(g), format(w)); \ + count_error(); \ + } \ + ++tests; \ + } while(0) + static uint32_t *ucs4parse(const char *s) { struct dynstr_ucs4 d; char *e; @@ -777,6 +795,51 @@ static void test_selection(void) { insist(hash_count(h) == 0); } +static void test_wstat(void) { + pid_t pid; + int w; + + fprintf(stderr, "test_wstat\n"); + if(!(pid = xfork())) { + _exit(1); + } + while(waitpid(pid, &w, 0) < 0 && errno == EINTR) + ; + check_string(wstat(w), "exited with status 1"); + if(!(pid = xfork())) { + kill(getpid(), SIGTERM); + _exit(-1); + } + while(waitpid(pid, &w, 0) < 0 && errno == EINTR) + ; + check_string_prefix(wstat(w), "terminated by signal 15"); +} + +static void test_kvp(void) { + struct kvp *k; + + fprintf(stderr, "test_kvp\n"); +#define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S)) + insist(KVP_URLDECODE("=%zz") == 0); + insist(KVP_URLDECODE("=%0") == 0); + insist(KVP_URLDECODE("=%0z") == 0); + insist(KVP_URLDECODE("=%%") == 0); + insist(KVP_URLDECODE("==%") == 0); + insist(KVP_URLDECODE("wibble") == 0); + insist(KVP_URLDECODE("") == 0); + insist(KVP_URLDECODE("wibble&") == 0); + insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0); + check_string(kvp_get(k, "one"), "blat foo"); + insist(kvp_get(k, "ONE") == 0); + insist(k->next == 0); + insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0); + check_string(kvp_get(k, "wibble"), "splat"); + check_string(kvp_get(k, "bar"), "spong"); + insist(kvp_get(k, "ONE") == 0); + insist(k->next->next == 0); + /* TODO test encoding too */ +} + int main(void) { fail_first = !!getenv("FAIL_FIRST"); insist('\n' == 0x0A); @@ -805,6 +868,7 @@ int main(void) { test_hex(); /* inputline.c */ /* kvp.c */ + test_kvp(); /* log.c */ /* mem.c */ /* mime.c */ @@ -827,6 +891,7 @@ int main(void) { test_casefold(); test_words(); /* wstat.c */ + test_wstat(); /* signame.c */ test_signame(); /* cache.c */