86 lines
5.2 KiB
Plaintext
86 lines
5.2 KiB
Plaintext
#include "testlib.h"
|
|
|
|
RENDER(Request& context)
|
|
{
|
|
if(!test_demo_request_allowed(context))
|
|
{
|
|
site_tests_restricted(context, "Filesystem", "write to local files and append server-side test data");
|
|
return;
|
|
}
|
|
|
|
u64 passed = 0;
|
|
u64 failed = 0;
|
|
u64 skipped = 0;
|
|
|
|
auto check = [&](String name, bool ok, String detail)
|
|
{
|
|
site_tests_case(name, ok ? "pass" : "fail", detail);
|
|
if(ok)
|
|
passed++;
|
|
else
|
|
failed++;
|
|
};
|
|
|
|
String file_name = path_join("/tmp", "uce-site-tests-io.txt");
|
|
bool write_ok = file_put_contents(file_name, "alpha");
|
|
file_append(file_name, "|beta");
|
|
String file_text = file_get_contents(file_name);
|
|
|
|
site_tests_page_start("Filesystem", "Local-only file helper coverage using a temporary file under /tmp.");
|
|
|
|
check("path_join()", file_name == "/tmp/uce-site-tests-io.txt", file_name);
|
|
check("basename() / dirname() / expand_path()", basename(file_name) == "uce-site-tests-io.txt" && dirname(file_name) == "/tmp" && expand_path("child", "/tmp/base") == "/tmp/base/child" && contains(expand_path("../sibling", "/tmp/base"), "sibling"), basename(file_name) + " / " + dirname(file_name) + " / " + expand_path("../sibling", "/tmp/base"));
|
|
check("file_put_contents()", write_ok, file_name);
|
|
check("file_append() / file_append_contents()", file_append_contents(file_name, "|gamma") && contains(file_get_contents(file_name), "|gamma"), file_get_contents(file_name));
|
|
file_put_contents(file_name, "alpha|beta");
|
|
check("file_get_contents()", file_text == "alpha|beta", file_text);
|
|
check("file_mtime()", file_mtime(file_name) > 0, std::to_string((u64)file_mtime(file_name)));
|
|
|
|
String real_tmp = path_real("/tmp");
|
|
String real_file = path_real(file_name);
|
|
check("path_real() / path_is_within() canonical containment", real_tmp != "" && real_file != "" && path_is_within(file_name, "/tmp") && path_is_within("/tmp/../tmp/uce-site-tests-io.txt", "/tmp") && !path_is_within("/tmp", file_name) && !path_is_within("/tmp", "/tmp/uce-site-tests-io.txt"), real_tmp + " / " + real_file);
|
|
|
|
String shell_escaped = shell_escape("a'b; echo injected");
|
|
String shell_out = shell_exec("printf %s " + shell_escaped);
|
|
check("shell_escape() / shell_exec()", shell_out == "a'b; echo injected" && contains(shell_escaped, "'\\''"), shell_escaped + " => " + shell_out);
|
|
|
|
String lock_file = "/tmp/uce-site-tests-lock.txt";
|
|
int lock_fd = file_open_locked(lock_file, 66, LOCK_EX, 0644, 3.0, "site tests");
|
|
bool lock_write = file_put_contents_locked_fd(lock_fd, "locked-content");
|
|
String lock_read = file_get_contents_locked_fd(lock_fd);
|
|
file_close_locked(lock_fd);
|
|
check("file_open_locked() / locked fd read-write / close", lock_fd > 0 && lock_write && lock_read == "locked-content" && file_get_contents(lock_file) == "locked-content", "fd=" + std::to_string(lock_fd) + " read=" + lock_read);
|
|
int release_fd = file_open_locked(lock_file, 66, LOCK_EX, 0644, 3.0, "release test");
|
|
file_release_process_locks("site tests release");
|
|
check("file_release_process_locks()", release_fd > 0, "fd=" + std::to_string(release_fd));
|
|
|
|
String start_dir = process_start_directory();
|
|
String old_cwd = cwd_get();
|
|
cwd_set("/tmp");
|
|
String tmp_cwd = cwd_get();
|
|
cwd_set(old_cwd);
|
|
check("cwd_get() / cwd_set() / process_start_directory()", old_cwd != "" && tmp_cwd == "/tmp" && process_start_directory() == start_dir && start_dir != "", "old=" + old_cwd + " tmp=" + tmp_cwd + " start=" + start_dir);
|
|
|
|
StringMap cfg;
|
|
cfg["u64"] = "42";
|
|
cfg["f64"] = "3.5";
|
|
cfg["yes"] = "yes";
|
|
cfg["no"] = "off";
|
|
check("config_map_*() / config_*() helpers", config_map_u64(cfg, "u64", 7) == 42 && config_map_u64(cfg, "bad", 7) == 7 && config_map_f64(cfg, "f64", 1.0) > 3.49 && !config_map_bool(cfg, "no") && config_bool_value("false") == false && config_u64("NO_SUCH_UCE_TEST_KEY", 99) == 99 && config_f64("NO_SUCH_UCE_TEST_KEY", 1.25) == 1.25 && !config_bool("NO_SUCH_UCE_TEST_KEY", false), var_dump(cfg));
|
|
|
|
DValue perf = request_perf();
|
|
check("request_perf()", perf.get_type_name() != "invalid", json_encode(perf));
|
|
|
|
u64 parsed_epoch = time_parse("1970-01-01 00:00:05 UTC");
|
|
String utc_year = time_format_utc("%Y", parsed_epoch);
|
|
String local_year = time_format_local("%Y", parsed_epoch);
|
|
String relative = time_format_relative(time() - 120, "recent %deltaS", 1, "medium %deltaM", 3600, "old %deltaH");
|
|
check("time_format_local() / time_format_relative() / time_parse()", parsed_epoch == 5 && utc_year == "1970" && local_year != "" && contains(relative, "medium"), "parsed=" + std::to_string(parsed_epoch) + " utc=" + utc_year + " local=" + local_year + " rel=" + relative);
|
|
|
|
StringList escaped_keys = memcache_escape_keys({"a b", "line\nkey"});
|
|
check("memcache_escape_key() / memcache_escape_keys()", memcache_escape_key("a b") == "a_b" && escaped_keys.size() == 2 && !contains(escaped_keys[1], "\n"), join(escaped_keys, ","));
|
|
check("signal_name() / runtime_safe_key() / backtrace helpers", signal_name(SIGSEGV) == "SIGSEGV" && runtime_safe_key(" task one ") == gen_sha1("task one") && runtime_safe_key(" ") == "" && capture_backtrace_string() == "" && backtrace_frames_string(0, 0) == "", signal_name(SIGSEGV) + " / " + runtime_safe_key(" task one "));
|
|
|
|
site_tests_summary(passed, failed, skipped, "This page intentionally limits writes to /tmp/uce-site-tests-io.txt so it stays disposable.");
|
|
site_tests_page_end();
|
|
} |