From 7a74628734e70b15a61c492e7c3b12a5fd981c1d Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 23 Jun 2026 10:37:12 -0400 Subject: test_utils.c: add test_is_file_compressed() --- tests/test_utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests/test_utils.c') diff --git a/tests/test_utils.c b/tests/test_utils.c index fc53f53..7116eb2 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -456,6 +456,49 @@ void test_pushd_popd_suggested_workflow() { } } +void test_is_file_compressed() { + const char *filenames[] = { + "zstd", "bz2", "gz", "xz", "zip", + }; + char datadir[PATH_MAX] = {0}; + snprintf(datadir, sizeof(datadir), "%s/compression", TEST_DATA_DIR); + + char filename[PATH_MAX] = {0}; + for (size_t i = 0; i < sizeof(filenames) / sizeof(*filenames); i++) { + snprintf(filename, sizeof(filename), "%s/%s", datadir, filenames[i]); + const int compressed = is_file_compressed(filename); + SYSDEBUG("[%zu] is %s compressed? => %s", i, filename, compressed ? "Yes" : "No"); + STASIS_ASSERT(compressed == true, "compression should have been detected"); + } + + for (size_t i = 0; i < sizeof(filenames) / sizeof(*filenames); i++) { + char bytes[128]; + if (get_random_bytes(bytes, sizeof(bytes))) { + SYSERROR("get_random_bytes failed: %s, %s", bytes, strerror(errno)); + STASIS_ASSERT_FATAL(false, "get_random_bytes failed"); + return; + } + + FILE *fp = fopen(filenames[i], "wb"); + if (!fp) { + SYSERROR("fopen failed: %s, %s", filenames[i], strerror(errno)); + STASIS_ASSERT_FATAL(false, "fopen failed"); + return; + } + + bytes[0] = 'J'; + const size_t bytes_written = fwrite(bytes, 1, sizeof(bytes), fp); + if (bytes_written != sizeof(bytes)) { + SYSERROR("fwrite failed: %s, %s", bytes, strerror(errno)); + STASIS_ASSERT_FATAL(false, "fwrite failed"); + return; + } + fclose(fp); + + STASIS_ASSERT(is_file_compressed(filenames[i]) == false, "random data should not be detected as compressed"); + } +} + int main(int argc, char *argv[]) { STASIS_TEST_BEGIN_MAIN(); @@ -479,6 +522,7 @@ int main(int argc, char *argv[]) { test_dirstack, test_pushd_popd, test_pushd_popd_suggested_workflow, + test_is_file_compressed, }; const char *ws = "workspace"; getcwd(cwd_start, sizeof(cwd_start) - 1); -- cgit From 0f6a1c982c2f417e9f0de968bbb7ce58299a8e8d Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 23 Jun 2026 10:43:51 -0400 Subject: test_utils.c: rename filename to inputfile * Add test for known-uncompressed data file --- tests/test_utils.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tests/test_utils.c') diff --git a/tests/test_utils.c b/tests/test_utils.c index 7116eb2..e1689dd 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -463,14 +463,17 @@ void test_is_file_compressed() { char datadir[PATH_MAX] = {0}; snprintf(datadir, sizeof(datadir), "%s/compression", TEST_DATA_DIR); - char filename[PATH_MAX] = {0}; + char inputfile[PATH_MAX] = {0}; for (size_t i = 0; i < sizeof(filenames) / sizeof(*filenames); i++) { - snprintf(filename, sizeof(filename), "%s/%s", datadir, filenames[i]); - const int compressed = is_file_compressed(filename); - SYSDEBUG("[%zu] is %s compressed? => %s", i, filename, compressed ? "Yes" : "No"); + snprintf(inputfile, sizeof(inputfile), "%s/%s", datadir, filenames[i]); + const int compressed = is_file_compressed(inputfile); + SYSDEBUG("[%zu] is %s compressed? => %s", i, inputfile, compressed ? "Yes" : "No"); STASIS_ASSERT(compressed == true, "compression should have been detected"); } + snprintf(inputfile, sizeof(inputfile), "%s/none", datadir); + STASIS_ASSERT(is_file_compressed(inputfile) == false, "'none' file should not be detected as compressed data"); + for (size_t i = 0; i < sizeof(filenames) / sizeof(*filenames); i++) { char bytes[128]; if (get_random_bytes(bytes, sizeof(bytes))) { -- cgit