summaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c224
1 files changed, 155 insertions, 69 deletions
diff --git a/test.c b/test.c
index 3738ced..b47a56d 100644
--- a/test.c
+++ b/test.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "browse.h"
@@ -18,41 +19,163 @@
int tests_run;
static char *
-test_browse_invalid_traversal()
+test_build_browse_url()
{
+ size_t s;
+ struct file f;
struct kreq r;
- struct http_ret ret;
- /* attempt to traverse out of DATA_DIR... */
+ /* nominal case */
r = (struct kreq) {
- .path = "..",
+ .pname = "vault",
+ .path = "Alice/Bob",
.suffix = "",
- .mime = KMIME_TEXT_HTML
};
- ret = browse(&r);
+ 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);
- /* ...should return an error */
- mu_assert("error, browse allowed invalid traversal!",
- ret.code >= KHTTP_400);
return 0;
}
static char *
-test_browse_path_too_long()
+test_build_delete_url()
{
+ size_t s;
+ struct file f;
struct kreq r;
- struct http_ret ret;
- /* a lengthy path should cause URL overflow... */
+ /* nominal case */
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",
+ .pname = "vault",
+ .path = "Alice/Bob",
.suffix = "",
- .mime = KMIME_TEXT_HTML
};
- ret = browse(&r);
+ 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);
- mu_assert("error, browse allowed invalid traversal!",
- ret.code >= KHTTP_400);
+ 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;
}
@@ -61,7 +184,6 @@ test_upload_post()
{
char *file_content;
struct kreq r;
- struct http_ret ret;
struct kpair fields[2];
file_content = "text file";
@@ -86,9 +208,7 @@ test_upload_post()
.fieldsz = 2,
.fields = fields
};
- ret = upload(&r);
-
- mu_assert("error, POST upload failed!", ret.code <= KHTTP_400);
+ upload(&r);
return 0;
}
@@ -96,16 +216,12 @@ static char *
test_download()
{
struct kreq r;
- struct http_ret ret;
-
r = (struct kreq) {
.pname = "/vault",
.path = "a",
.suffix = "txt",
};
- ret = download(&r);
-
- mu_assert("error, download failed!", ret.code <= KHTTP_400);
+ download(&r);
return 0;
}
@@ -113,17 +229,13 @@ static char *
test_delete_get()
{
struct kreq r;
- struct http_ret ret;
-
r = (struct kreq) {
.pname = "/vault",
.path = "a",
.suffix = "txt",
.method = KMETHOD_GET,
};
- ret = del(&r);
-
- mu_assert("error, GET delete failed!", ret.code <= KHTTP_400);
+ del(&r);
return 0;
}
@@ -131,60 +243,34 @@ static char *
test_delete_post()
{
struct kreq r;
- struct http_ret ret;
-
r = (struct kreq) {
.pname = "/vault",
.path = "Folder",
.suffix = "",
.method = KMETHOD_POST,
};
- ret = del(&r);
-
- mu_assert("error, POST delete failed!", ret.code <= KHTTP_400);
- 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);
+ del(&r);
return 0;
}
static char *
all_tests()
{
- mu_run_test(test_browse_invalid_traversal);
- mu_run_test(test_browse_path_too_long);
- mu_run_test(test_url_build);
+ /* 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;
}