#include #include #include #include "browse.h" #include "delete.h" #include "download.h" #include "http.h" #include "upload.h" #include "url.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) #define BUF_SZ 1000 int tests_run; static char * test_build_browse_url() { size_t s; struct file f; struct kreq r; /* nominal case */ r = (struct kreq) { .pname = "vault", .path = "Alice/Bob", .suffix = "", }; f = (struct file) { .is_dir = true, .path = "Alice/Bob", .action_url = NULL }; s = build_browse_url(&r, &f); mu_assert("failed to build browse url", s > 0 && f.action_url != NULL); /* refuse to browse files */ if (f.action_url != NULL) free(f.action_url); f.action_url = NULL; f.is_dir = false; s = build_browse_url(&r, &f); mu_assert("allowed to browse a file", s <= 0 && f.action_url == NULL); return 0; } static char * test_build_download_url() { size_t s; struct file f; struct kreq r; /* nominal case */ r = (struct kreq) { .pname = "vault", .path = "Alice/Bob", .suffix = "txt", }; f = (struct file) { .is_dir = false, .path = "Alice/Bob.txt", .action_url = NULL }; s = build_download_url(&r, &f); mu_assert("failed to build download url", s > 0 && f.action_url != NULL); /* refuse to download directories */ if (f.action_url != NULL) free(f.action_url); f.action_url = NULL; f.is_dir = true; s = build_download_url(&r, &f); mu_assert("allowed to download a directory", s <= 0 && f.action_url == NULL); return 0; } static char * test_build_upload_url() { size_t s; struct file f; struct kreq r; /* nominal case */ r = (struct kreq) { .pname = "vault", .path = "Alice/Bob", .suffix = "", }; f = (struct file) { .is_dir = true, .path = "Alice/Bob", .action_url = NULL }; s = build_upload_url(&r, &f); mu_assert("failed to build upload url", s > 0 && f.action_url != NULL); /* refuse to upload to a file */ if (f.action_url != NULL) free(f.action_url); f.action_url = NULL; f.is_dir = false; s = build_upload_url(&r, &f); mu_assert("allowed to upload to a file", s <= 0 && f.action_url == NULL); return 0; } static char * test_build_delete_url() { size_t s; struct file f; struct kreq r; /* nominal case */ r = (struct kreq) { .pname = "vault", .path = "Alice/Bob", .suffix = "", }; f = (struct file) { .is_dir = true, .path = "Alice/Bob", .action_url = NULL }; s = build_delete_url(&r, &f); mu_assert("failed to build delete url", s > 0 && f.action_url != NULL); /* allow also to delete a file */ if (f.action_url != NULL) free(f.action_url); f.action_url = NULL; f.is_dir = false; s = build_delete_url(&r, &f); mu_assert("refused to delete a file", s > 0 && f.action_url != NULL); return 0; } static char * test_url_build() { char dst[BUF_SZ], *expected; size_t dst_len; expected = "/vault/browse"; dst_len = url_build(dst, BUF_SZ, "/vault/browse", "", NULL); mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ); mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0); expected = "/vault/browse/hello/world"; dst_len = url_build(dst, BUF_SZ, "/vault/browse", "hello", "world", NULL); mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ); mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0); expected = "/vault/browse"; dst_len = url_build(dst, BUF_SZ, "/vault/", "browse/", NULL); mu_assert("test_url_build failed", dst_len > 0 && dst_len < BUF_SZ); mu_assert("test_url_build failed", strncmp(expected, dst, BUF_SZ) == 0); dst_len = url_build(dst, BUF_SZ, "", "", NULL); mu_assert("test_url_build failed", dst_len == 0); dst_len = url_build(dst, BUF_SZ, NULL); mu_assert("test_url_build failed", dst_len == 0); return 0; } static char * test_upload_post() { char *file_content; struct kreq r; struct kpair fields[2]; file_content = "text file"; fields[0] = (struct kpair) { .key = "file", .file = "bob.txt", .val = file_content, .valsz = strlen(file_content) }; fields[1] = (struct kpair) { .key = "file", .file = "alice.txt", .val = file_content, .valsz = strlen(file_content) }; r = (struct kreq) { .pname = "/vault", .path = "", .method = KMETHOD_POST, .fieldsz = 2, .fields = fields }; upload(&r); return 0; } static char * test_download() { struct kreq r; r = (struct kreq) { .pname = "/vault", .path = "a", .suffix = "txt", }; download(&r); return 0; } static char * test_delete_get() { struct kreq r; r = (struct kreq) { .pname = "/vault", .path = "a", .suffix = "txt", .method = KMETHOD_GET, }; del(&r); return 0; } static char * test_delete_post() { struct kreq r; r = (struct kreq) { .pname = "/vault", .path = "Folder", .suffix = "", .method = KMETHOD_POST, }; del(&r); return 0; } static char * all_tests() { /* the following tests are for debug only */ /* mu_run_test(test_download); mu_run_test(test_upload_post); mu_run_test(test_delete_get); mu_run_test(test_delete_post); */ /* url building */ mu_run_test(test_url_build); mu_run_test(test_build_browse_url); mu_run_test(test_build_download_url); mu_run_test(test_build_upload_url); mu_run_test(test_build_delete_url); 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; }