summaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2024-05-13 20:44:07 +0200
committerVincent Douillet <vincent@vdouillet.fr>2024-05-13 22:01:09 +0200
commit10eb77f9323110c55f88195c5b8207eb524baa73 (patch)
tree940c83fc095bad3e63045bc6fbe9ada8e4276135 /test.c
parent0effd40bad7ae753102e7040159afd76be4e8732 (diff)
add unit tests
Diffstat (limited to 'test.c')
-rw-r--r--test.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..390cbc3
--- /dev/null
+++ b/test.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+
+#include "browse.h"
+#include "http.h"
+
+/* minunit, see https://jera.com/techinfo/jtns/jtn002 */
+#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
+#define mu_run_test(test) do { char* message = test(); tests_run++; \
+ if(message) return message; } while (0)
+
+int tests_run;
+
+static char *
+test_browse_invalid_traversal()
+{
+ struct kreq r;
+ struct http_ret ret;
+
+ /* attempt to traverse out of DATA_DIR... */
+ r = (struct kreq) {
+ .path = "..",
+ .suffix = "",
+ .mime = KMIME_TEXT_HTML
+ };
+ ret = browse(&r);
+
+ /* ...should return an error */
+ mu_assert("error, browse allowed invalid traversal!",
+ ret.code >= KHTTP_400);
+
+ return 0;
+}
+
+static char *
+test_browse_path_too_long()
+{
+ struct kreq r;
+ struct http_ret ret;
+
+ /* a lengthy path should cause URL overflow... */
+ r = (struct kreq) {
+ .path = "this/is/a/very/very/lengthy/path/that/should/overflow/the/maximum/allowed/path/length/of/PATH_MAX/well/of/course/this/limit/is/platform/dependent",
+ .suffix = "",
+ .mime = KMIME_TEXT_HTML
+ };
+ ret = browse(&r);
+
+ mu_assert("error, browse allowed invalid traversal!",
+ ret.code >= KHTTP_400);
+
+ return 0;
+}
+
+static char *
+all_tests()
+{
+ mu_run_test(test_browse_invalid_traversal);
+ mu_run_test(test_browse_path_too_long);
+ return 0;
+}
+
+int
+main(void)
+{
+ char *result = all_tests();
+ if (result != 0) {
+ printf("%s\n", result);
+ } else {
+ printf("ALL TESTS PASSED\n");
+ }
+ printf("Tests run: %d\n", tests_run);
+
+ return result != 0;
+}