diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-09-25 02:57:21 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-09-25 02:57:21 +0200 |
commit | 6ac53d4c783340ae9139c7f4dcfe9bfddace5892 (patch) | |
tree | 2637740546dcf002fbfe39eb94d6f722a2c5c7d6 /tests/blogc-runserver/check_httpd_utils.c | |
parent | 0c916e2c8b56c320fdc81f68d445194559479041 (diff) | |
download | blogc-6ac53d4c783340ae9139c7f4dcfe9bfddace5892.tar.gz blogc-6ac53d4c783340ae9139c7f4dcfe9bfddace5892.tar.bz2 blogc-6ac53d4c783340ae9139c7f4dcfe9bfddace5892.zip |
runserver: reimplemented http server without libevent
yeah, this patch implements a "complete" http server for static files.
It is not the best code possible, and would be easily DDoS'able if used
in production, as it spawns a thread for each request, without limiting.
I'm sickish and this is the best code I can deliver now. At least it
works! ;)
Diffstat (limited to 'tests/blogc-runserver/check_httpd_utils.c')
-rw-r--r-- | tests/blogc-runserver/check_httpd_utils.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/tests/blogc-runserver/check_httpd_utils.c b/tests/blogc-runserver/check_httpd_utils.c new file mode 100644 index 0000000..18376a2 --- /dev/null +++ b/tests/blogc-runserver/check_httpd_utils.c @@ -0,0 +1,163 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <stdlib.h> +#include <string.h> +#include "../../src/common/utils.h" +#include "../../src/blogc-runserver/httpd-utils.h" + + +ssize_t +__wrap_read(int fd, void *buf, size_t count) +{ + assert_int_equal(fd, mock_type(int)); + const char *mock_buf = mock_type(const char*); + strcpy(buf, mock_buf); + assert_int_equal(count, READLINE_BUFFER_SIZE); + return strlen(mock_buf) > 0 ? strlen(mock_buf) : -1; +} + + +static void +test_readline(void **state) +{ + char *t; + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, ""); + t = br_readline(1234); + assert_string_equal(t, "bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola1\nguda\nxd"); + t = br_readline(1234); + assert_string_equal(t, "bola1"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola2\rguda\rxd"); + t = br_readline(1234); + assert_string_equal(t, "bola2"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola3\r\nguda\r\nxd"); + t = br_readline(1234); + assert_string_equal(t, "bola3"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "guda"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, ""); + t = br_readline(1234); + assert_string_equal(t, "bolaguda"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola1"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\nguda"); + t = br_readline(1234); + assert_string_equal(t, "bola1bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola2"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\rguda"); + t = br_readline(1234); + assert_string_equal(t, "bola2bola"); + free(t); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola3"); + will_return(__wrap_read, 1234); + will_return(__wrap_read, "bola\r\nguda"); + t = br_readline(1234); + assert_string_equal(t, "bola3bola"); + free(t); +} + + +static void +test_hextoi(void **state) +{ + assert_int_equal(br_hextoi('0'), 0); + assert_int_equal(br_hextoi('1'), 1); + assert_int_equal(br_hextoi('2'), 2); + assert_int_equal(br_hextoi('3'), 3); + assert_int_equal(br_hextoi('4'), 4); + assert_int_equal(br_hextoi('5'), 5); + assert_int_equal(br_hextoi('6'), 6); + assert_int_equal(br_hextoi('7'), 7); + assert_int_equal(br_hextoi('8'), 8); + assert_int_equal(br_hextoi('9'), 9); + assert_int_equal(br_hextoi('a'), 10); + assert_int_equal(br_hextoi('b'), 11); + assert_int_equal(br_hextoi('c'), 12); + assert_int_equal(br_hextoi('d'), 13); + assert_int_equal(br_hextoi('e'), 14); + assert_int_equal(br_hextoi('f'), 15); + assert_int_equal(br_hextoi('A'), 10); + assert_int_equal(br_hextoi('B'), 11); + assert_int_equal(br_hextoi('C'), 12); + assert_int_equal(br_hextoi('D'), 13); + assert_int_equal(br_hextoi('E'), 14); + assert_int_equal(br_hextoi('g'), -1); + assert_int_equal(br_hextoi('G'), -1); + assert_int_equal(br_hextoi('-'), -1); +} + + +static void +test_urldecode(void **state) +{ + for (size_t i = 0; i < 128; i++) { + char *t = bc_strdup_printf("%%%02x", i); + char *r = br_urldecode(t); + assert_int_equal(r[0], i); + assert_int_equal(r[1], 0); + free(r); + free(t); + } + char *r = br_urldecode("%Ab"); + assert_string_equal(r, "\xab"); + free(r); + r = br_urldecode("%xb"); + assert_string_equal(r, "%xb"); + free(r); + r = br_urldecode("%C3%BC"); + assert_string_equal(r, "\xc3\xbc"); + free(r); +} + + +static void +test_get_extension(void **state) +{ + assert_null(br_get_extension("bola")); + assert_string_equal(br_get_extension("bola.txt"), "txt"); + assert_string_equal(br_get_extension("bola.txt.jpg"), "jpg"); + assert_null(br_get_extension("bola.txt/foo")); + assert_string_equal(br_get_extension("bola.txt/foo.jpg"), "jpg"); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_readline), + unit_test(test_hextoi), + unit_test(test_urldecode), + unit_test(test_get_extension), + }; + return run_tests(tests); +} |