- Lauri
>From b044f15d04dc905ba09ef1737f0f4fc26c2802d4 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen <[email protected]> Date: Wed, 18 Jul 2012 19:35:32 +0300 Subject: [PATCH 2/3] lib: Add the test suite
Signed-off-by: Lauri Kasanen <[email protected]> --- tests/lib/dual-contexts.c | 56 ++++++++++++++++++++++++++ tests/lib/ip-ban.c | 37 +++++++++++++++++ tests/lib/md5_check.h | 25 ++++++++++++ tests/lib/run-tests.sh | 81 ++++++++++++++++++++++++++++++++++++++ tests/lib/simple-defaults.c | 14 +++++++ tests/lib/url-ban.c | 37 +++++++++++++++++ tests/lib/x-start-twice.c | 14 +++++++ tests/lib/x-stop-without-start.c | 13 ++++++ tests/lib/x-wrong-ip.c | 11 +++++ tests/lib/x-wrong-port.c | 11 +++++ tests/lib/zeros-50k.c | 37 +++++++++++++++++ 11 files changed, 336 insertions(+), 0 deletions(-) create mode 100644 tests/lib/dual-contexts.c create mode 100644 tests/lib/ip-ban.c create mode 100644 tests/lib/md5_check.h create mode 100755 tests/lib/run-tests.sh create mode 100644 tests/lib/simple-defaults.c create mode 100644 tests/lib/url-ban.c create mode 100644 tests/lib/x-start-twice.c create mode 100644 tests/lib/x-stop-without-start.c create mode 100644 tests/lib/x-wrong-ip.c create mode 100644 tests/lib/x-wrong-port.c create mode 100644 tests/lib/zeros-50k.c diff --git a/tests/lib/dual-contexts.c b/tests/lib/dual-contexts.c new file mode 100644 index 0000000..d68aa5b --- /dev/null +++ b/tests/lib/dual-contexts.c @@ -0,0 +1,56 @@ +#include <monkey.h> +#include "md5_check.h" + +// Serve on two ports at once. +// This is known to fail, would require deep surgery to handle properly. + +const char data1c[] = "data1"; +const char data2c[] = "data2"; + +int data1(const mklib_session *sr, const char *vhost, const char *url, + const char *get, unsigned long get_len, + const char *post, unsigned long post_len, + unsigned int *status, const char **content, + unsigned long *content_len, char *header) { + + *content_len = 5; + + *content = data1c; + + return MKLIB_TRUE; +} + +int data2(const mklib_session *sr, const char *vhost, const char *url, + const char *get, unsigned long get_len, + const char *post, unsigned long post_len, + unsigned int *status, const char **content, + unsigned long *content_len, char *header) { + + *content_len = 5; + + *content = data2c; + + return MKLIB_TRUE; +} + +int main() { + + mklib_ctx c1 = mklib_init(NULL, 8094, 0, NULL); + if (!c1) return 1; + mklib_ctx c2 = mklib_init(NULL, 8095, 0, NULL); + if (!c2) return 1; + + mklib_callback_set(c1, MKCB_DATA, data1); + mklib_callback_set(c2, MKCB_DATA, data2); + + if (!mklib_start(c1)) return 1; + if (!mklib_start(c2)) return 1; + + if (!md5_check("89d903bc35dede724fd52c51437ff5fd", "localhost:8094")) return 1; + if (!md5_check("ff9cf2d690d888cb337f6bf4526b6130", "localhost:8095")) return 1; + + if (!mklib_stop(c1)) return 1; + if (!mklib_stop(c2)) return 1; + + return 0; +} diff --git a/tests/lib/ip-ban.c b/tests/lib/ip-ban.c new file mode 100644 index 0000000..d560b7b --- /dev/null +++ b/tests/lib/ip-ban.c @@ -0,0 +1,37 @@ +#include <monkey.h> +#include <stdlib.h> + +// Make sure that an IP-ban works + + +int data(const mklib_session *sr, const char *vhost, const char *url, + const char *get, unsigned long get_len, + const char *post, unsigned long post_len, + unsigned int *status, const char **content, + unsigned long *content_len, char *header) { + + // If we got here, the ban failed. + exit(1); +} + +int ipcheck(const char *ip) { + // Ban everyone. + return MKLIB_FALSE; +} + +int main() { + + mklib_ctx c = mklib_init(NULL, 8090, 0, NULL); + if (!c) return 1; + + mklib_callback_set(c, MKCB_IPCHECK, ipcheck); + mklib_callback_set(c, MKCB_DATA, data); + + if (!mklib_start(c)) return 1; + + system("wget -q -t2 -O /dev/null localhost:8090"); + + if (!mklib_stop(c)) return 1; + + return 0; +} diff --git a/tests/lib/md5_check.h b/tests/lib/md5_check.h new file mode 100644 index 0000000..76bff31 --- /dev/null +++ b/tests/lib/md5_check.h @@ -0,0 +1,25 @@ +#ifndef MD5_CHECK_H +#define MD5_CHECK_H + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int md5_check(const char *right, const char *where) { + + char *def, buf[33]; + asprintf(&def, "wget --timeout=2 -t2 -q -O- %s | md5sum -", where); + + FILE *f = popen(def, "r"); + if (!f) return 0; + + fgets(buf, 33, f); + + free(def); + pclose(f); + + return (strcmp(right, buf) == 0); +} + +#endif diff --git a/tests/lib/run-tests.sh b/tests/lib/run-tests.sh new file mode 100755 index 0000000..0e4af58 --- /dev/null +++ b/tests/lib/run-tests.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# Tests starting with x are expected to fail + + +export LANG=C + +GREEN="$(echo -e '\033[1;32m')" +YELLOW="$(echo -e '\033[0;33m')" +RED="$(echo -e '\033[1;31m')" +NORMAL="$(echo -e '\033[0;39m')" + +CFLAGS="$CFLAGS -Wl,-rpath,\$ORIGIN/../../src -I ../../src/include/public" +LDFLAGS="$LDFLAGS ../../src/libmonkey.so*" +[ -z "$CC" ] && CC=gcc + + +# Check that we can run the tests +if [ ! -f ../../src/libmonkey.so* ]; then + echo -e "\n${YELLOW}Please build and install the library first.\n" + + echo "The tests will link against the source dir, but the library" + echo "expects to find the plugins in the plugindir (installed plugins)" + echo $NORMAL + exit +fi + + +# Precompile the header for faster builds +$CC ../../src/include/public/monkey.h + +success=0 +fail=0 + +for src in *.c; do + [ ! -f "$src" ] && exit + + test=${src%.c} + log=${test}.log + + ret=0 + case $test in x*) ret=1 ;; esac + + echo -n "Building test $test... " + $CC $CFLAGS $LDFLAGS -o $test $src + if [ $? -ne 0 ]; then + fail=$((fail + 1)) + echo "${RED}Failed to build $NORMAL" + continue + fi + + ./$test > $log + if [ $? -ne $ret ]; then + fail=$((fail + 1)) + echo "${RED}Failed $NORMAL" + else + success=$((success + 1)) + echo + rm -f $log + fi + + # If empty, remove + [ ! -s "$log" ] && rm -f $log +done + +# Remove the PCH +rm ../../src/include/public/monkey.h.gch + + +echo + +total=$((fail + success)) +percentage=$(awk "BEGIN{print $success/$total * 100}") +percentage=$(printf '%.2f' $percentage) + +num=${percentage//.*/} + +[ $fail -eq 0 ] && echo "$GREEN All tests passed!" +[ $fail -ne 0 -a $num -ge 60 ] && echo "$YELLOW $percentage% passed, $fail/$total fails" +[ $num -lt 60 ] && echo "$RED $percentage% passed, $fail/$total fails" + +echo $NORMAL diff --git a/tests/lib/simple-defaults.c b/tests/lib/simple-defaults.c new file mode 100644 index 0000000..7c8ec72 --- /dev/null +++ b/tests/lib/simple-defaults.c @@ -0,0 +1,14 @@ +#include <monkey.h> + +// All defaults, start, stop + +int main() { + + mklib_ctx c = mklib_init(NULL, 0, 0, NULL); + if (!c) return 1; + + if (!mklib_start(c)) return 1; + if (!mklib_stop(c)) return 1; + + return 0; +} diff --git a/tests/lib/url-ban.c b/tests/lib/url-ban.c new file mode 100644 index 0000000..ba9f1f8 --- /dev/null +++ b/tests/lib/url-ban.c @@ -0,0 +1,37 @@ +#include <monkey.h> +#include <stdlib.h> + +// Make sure that an URL-ban works + + +int data(const mklib_session *sr, const char *vhost, const char *url, + const char *get, unsigned long get_len, + const char *post, unsigned long post_len, + unsigned int *status, const char **content, + unsigned long *content_len, char *header) { + + // If we got here, the ban failed. + exit(1); +} + +int urlcheck(const char *ip) { + // Ban everyone. + return MKLIB_FALSE; +} + +int main() { + + mklib_ctx c = mklib_init(NULL, 8090, 0, NULL); + if (!c) return 1; + + mklib_callback_set(c, MKCB_URLCHECK, urlcheck); + mklib_callback_set(c, MKCB_DATA, data); + + if (!mklib_start(c)) return 1; + + system("wget -q -t2 -O /dev/null localhost:8090"); + + if (!mklib_stop(c)) return 1; + + return 0; +} diff --git a/tests/lib/x-start-twice.c b/tests/lib/x-start-twice.c new file mode 100644 index 0000000..b3d74f4 --- /dev/null +++ b/tests/lib/x-start-twice.c @@ -0,0 +1,14 @@ +#include <monkey.h> + +// Xfail, start twice + +int main() { + + mklib_ctx c = mklib_init(NULL, 0, 0, NULL); + if (!c) return 1; + + if (!mklib_start(c)) return 1; + if (!mklib_start(c)) return 1; + + return 0; +} diff --git a/tests/lib/x-stop-without-start.c b/tests/lib/x-stop-without-start.c new file mode 100644 index 0000000..378154d --- /dev/null +++ b/tests/lib/x-stop-without-start.c @@ -0,0 +1,13 @@ +#include <monkey.h> + +// Xfail, stop without start + +int main() { + + mklib_ctx c = mklib_init(NULL, 0, 0, NULL); + if (!c) return 1; + + if (!mklib_stop(c)) return 1; + + return 0; +} diff --git a/tests/lib/x-wrong-ip.c b/tests/lib/x-wrong-ip.c new file mode 100644 index 0000000..10c9836 --- /dev/null +++ b/tests/lib/x-wrong-ip.c @@ -0,0 +1,11 @@ +#include <monkey.h> + +// Start with a wrong IP. Expected to fail + +int main() { + + mklib_ctx c = mklib_init("lol", 0, 0, NULL); + if (!c) return 1; + + return 0; +} diff --git a/tests/lib/x-wrong-port.c b/tests/lib/x-wrong-port.c new file mode 100644 index 0000000..2020626 --- /dev/null +++ b/tests/lib/x-wrong-port.c @@ -0,0 +1,11 @@ +#include <monkey.h> + +// Start with a wrong port. Expected to fail + +int main() { + + mklib_ctx c = mklib_init(NULL, -1, 0, NULL); + if (!c) return 1; + + return 0; +} diff --git a/tests/lib/zeros-50k.c b/tests/lib/zeros-50k.c new file mode 100644 index 0000000..6306c84 --- /dev/null +++ b/tests/lib/zeros-50k.c @@ -0,0 +1,37 @@ +#include <monkey.h> +#include "md5_check.h" + +// Send 50k of zeros, check that md5sum matches + +enum { + clen = 50000 +}; + +int data(const mklib_session *sr, const char *vhost, const char *url, + const char *get, unsigned long get_len, + const char *post, unsigned long post_len, + unsigned int *status, const char **content, + unsigned long *content_len, char *header) { + + *content_len = clen; + + *content = calloc(clen, 1); + + return MKLIB_TRUE; +} + +int main() { + + mklib_ctx c = mklib_init(NULL, 8090, 0, NULL); + if (!c) return 1; + + mklib_callback_set(c, MKCB_DATA, data); + + if (!mklib_start(c)) return 1; + + if (!md5_check("a68b2482a6c645a9738329909861afb3", "localhost:8090")) return 1; + + if (!mklib_stop(c)) return 1; + + return 0; +} -- 1.7.2.1
_______________________________________________ Monkey mailing list [email protected] http://lists.monkey-project.com/listinfo/monkey
