aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-25 02:57:21 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-25 02:57:21 +0200
commit6ac53d4c783340ae9139c7f4dcfe9bfddace5892 (patch)
tree2637740546dcf002fbfe39eb94d6f722a2c5c7d6 /tests
parent0c916e2c8b56c320fdc81f68d445194559479041 (diff)
downloadblogc-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')
-rw-r--r--tests/blogc-runserver/check_httpd_utils.c163
-rw-r--r--tests/blogc-runserver/check_mime.c92
-rw-r--r--tests/blogc/check_loader.c3
3 files changed, 257 insertions, 1 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);
+}
diff --git a/tests/blogc-runserver/check_mime.c b/tests/blogc-runserver/check_mime.c
new file mode 100644
index 0000000..5d11766
--- /dev/null
+++ b/tests/blogc-runserver/check_mime.c
@@ -0,0 +1,92 @@
+/*
+ * 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 <unistd.h>
+#include "../../src/blogc-runserver/mime.h"
+
+
+int
+__wrap_access(const char *pathname, int mode)
+{
+ assert_string_equal(pathname, mock_type(const char*));
+ assert_int_equal(mode, F_OK);
+ return mock_type(int);
+}
+
+
+static void
+test_guess_content_type(void **state)
+{
+ assert_string_equal(br_mime_guess_content_type("foo.html"), "text/html");
+ assert_string_equal(br_mime_guess_content_type("foo.jpg"), "image/jpeg");
+ assert_string_equal(br_mime_guess_content_type("foo.mp4"), "video/mp4");
+ assert_string_equal(br_mime_guess_content_type("foo.bola"), "application/octet-stream");
+}
+
+
+static void
+test_guess_index(void **state)
+{
+ char *t;
+ will_return(__wrap_access, "dir/index.html");
+ will_return(__wrap_access, 0);
+ t = br_mime_guess_index("dir");
+ assert_string_equal(t, "dir/index.html");
+ free(t);
+ will_return(__wrap_access, "dir/index.html");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.htm");
+ will_return(__wrap_access, 0);
+ t = br_mime_guess_index("dir");
+ assert_string_equal(t, "dir/index.htm");
+ free(t);
+ will_return(__wrap_access, "dir/index.html");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.htm");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.shtml");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.xml");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.txt");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.xhtml");
+ will_return(__wrap_access, 0);
+ t = br_mime_guess_index("dir");
+ assert_string_equal(t, "dir/index.xhtml");
+ free(t);
+ will_return(__wrap_access, "dir/index.html");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.htm");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.shtml");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.xml");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.txt");
+ will_return(__wrap_access, 1);
+ will_return(__wrap_access, "dir/index.xhtml");
+ will_return(__wrap_access, 1);
+ assert_null(br_mime_guess_index("dir"));
+}
+
+
+int
+main(void)
+{
+ const UnitTest tests[] = {
+ unit_test(test_guess_content_type),
+ unit_test(test_guess_index),
+ };
+ return run_tests(tests);
+}
diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c
index 52521e2..36a3471 100644
--- a/tests/blogc/check_loader.c
+++ b/tests/blogc/check_loader.c
@@ -50,8 +50,9 @@ test_get_filename(void **state)
char*
-__wrap_bc_file_get_contents(const char *path, size_t *len, bc_error_t **err)
+__wrap_bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err)
{
+ assert_true(utf8);
assert_null(*err);
const char *_path = mock_type(const char*);
if (_path != NULL)