char *test_Dir_resolve_file() { Dir *test = Dir_create("tests/", "sample.html", "test/plain"); mu_assert(test != NULL, "Failed to make test dir."); FileRecord *rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/sample.json")); mu_assert(rec != NULL, "Failed to resolve file that should be there."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/")); mu_assert(rec != NULL, "Failed to find default file."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/../../../../../etc/passwd")); mu_assert(rec == NULL, "HACK! should not find this."); Dir_destroy(test); test = Dir_create("foobar/", "sample.html", "test/plan"); mu_assert(test != NULL, "Failed to make the failed dir."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/sample.json")); mu_assert(rec == NULL, "Should not get something from a bad base directory."); Dir_destroy(test); return NULL; }
char *test_Dir_serve_file() { int rc = 0; Request *req = NULL; Dir *test = Dir_create("tests/", "sample.html", "test/plain"); Connection conn = {0}; conn.fd = 1; conn.send = my_send; req = fake_req("GET", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "Should fail to write since it's not a socket."); req = fake_req("HEAD", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "Should fail to write since it's not a socket."); req = fake_req("POST", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "Should fail to write since it's not a socket."); return NULL; }
char *test_Dir_resolve_file() { Dir *test = Dir_create( bfromcstr("tests/"), bfromcstr("sample.html"), bfromcstr("test/plain"), 0); mu_assert(test != NULL, "Failed to make test dir."); FileRecord *rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/sample.json")); mu_assert(rec != NULL, "Failed to resolve file that should be there."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/")); mu_assert(rec != NULL, "Failed to find default file."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/../../../../../etc/passwd")); mu_assert(rec == NULL, "HACK! should not find this."); rec = Dir_resolve_file(test, bfromcstr("/tests/"), bfromcstr("/test")); mu_assert(rec == NULL, "Should NOT find short paths."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/%E2%82%ACtonn%C3%A4nt%2520.tx%C3%BE")); mu_assert(rec != NULL, "Should find file with a percent-encoded name."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/%E2%82%ACtonn%C3%A4nt%2520/present.txt")); mu_assert(rec != NULL, "Should find file inside percent-encoded path."); Dir_destroy(test); test = Dir_create( bfromcstr("foobar/"), bfromcstr("sample.html"), bfromcstr("test/plan"), 0); mu_assert(test != NULL, "Failed to make the failed dir."); rec = Dir_resolve_file(test, bfromcstr("/"), bfromcstr("/sample.json")); mu_assert(rec == NULL, "Should not get something from a bad base directory."); Dir_destroy(test); return NULL; }
char *test_Dir_serve_file() { int rc = 0; Request *req = NULL; Dir *test = Dir_create( bfromcstr("tests/"), bfromcstr("sample.html"), bfromcstr("test/plain"), 0); Connection conn = {.iob = NULL}; int zero_fd = open("/dev/null", O_WRONLY); conn.iob = IOBuf_create(1024, zero_fd, IOBUF_NULL); req = fake_req("GET", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); // TODO: different platforms barf on sendfile for different reasons // mu_assert(req->response_size > -1, "Should serve the /sample.json"); req = fake_req("HEAD", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == 0, "Should serve the HEAD of /sample.json"); req = fake_req("POST", "/", "/sample.json"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "POST should pass through but send an error."); mu_assert(req->status_code == 405, "POST to file should 405."); req = fake_req("GET", "/tests/", "/test"); rc = Dir_serve_file(test, req, &conn); mu_assert(rc == -1, "GET of path shorter than prefix should 404"); mu_assert(req->status_code == 404, "GET shortpath should 404."); return NULL; }