diff options
| author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-08-26 15:44:54 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-26 15:44:54 -0400 | 
| commit | 97a6697b9fdebda0be31bc03c4db3846d0947281 (patch) | |
| tree | 3ba84a7711f25e57ab1980c70c3d68355e6b0b8f /tests | |
| parent | b91f8c354b60f9db22d4ad65a1e6f72b13c8b693 (diff) | |
| download | stasis-97a6697b9fdebda0be31bc03c4db3846d0947281.tar.gz | |
More tests (#36)
* Add test_copy.c
* Add test_wheel.c
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_copy.c | 44 | ||||
| -rw-r--r-- | tests/test_wheel.c | 89 | 
2 files changed, 133 insertions, 0 deletions
| diff --git a/tests/test_copy.c b/tests/test_copy.c new file mode 100644 index 0000000..14310e4 --- /dev/null +++ b/tests/test_copy.c @@ -0,0 +1,44 @@ +#include "testing.h" + + +void test_copy() { +    struct testcase { +        const char *in_file; +        char *out_file; +        int expect_size; +        int expect_return; +    }; +    struct testcase tc[] = { +        {.in_file = "file_to_copy.txt", .out_file = "file_copied_1000.txt", .expect_size = 0x1000, .expect_return = 0}, +        {.in_file = "file_to_copy.txt", .out_file = "file_copied_4000.txt", .expect_size = 0x4000, .expect_return = 0}, +        {.in_file = "file_to_copy.txt", .out_file = "file_copied_8000.txt", .expect_size = 0x8000, .expect_return = 0}, +        {.in_file = "file_to_copy.txt", .out_file = "file_copied_100000.txt", .expect_size = 0x100000, .expect_return = 0}, +    }; + +    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) { +        struct stat st_a, st_b; +        struct testcase *test = &tc[i]; +        char *mock_data = malloc(test->expect_size * sizeof(*mock_data)); +        memset(mock_data, 'A', test->expect_size); +        mock_data[test->expect_size] = '\0'; +        stasis_testing_write_ascii(test->in_file, mock_data); +        guard_free(mock_data); + +        STASIS_ASSERT(copy2(test->in_file, test->out_file, CT_OWNER | CT_PERM) == test->expect_return, "copy2 failed"); +        STASIS_ASSERT(stat(test->in_file, &st_a) == 0, "source stat failed"); +        STASIS_ASSERT(stat(test->in_file, &st_b) == 0, "destination stat failed"); +        STASIS_ASSERT(st_b.st_size == test->expect_size, "destination file is not the expected size"); +        STASIS_ASSERT(st_a.st_size == st_b.st_size, "source and destination files should be the same size"); +        STASIS_ASSERT(st_a.st_mode == st_b.st_mode, "source and destination files should have the same permissions"); +        STASIS_ASSERT(st_a.st_uid == st_b.st_uid, "source and destination should have the same UID"); +    } +} + +int main(int argc, char *argv[]) { +    STASIS_TEST_BEGIN_MAIN(); +    STASIS_TEST_FUNC *tests[] = { +        test_copy, +    }; +    STASIS_TEST_RUN(tests); +    STASIS_TEST_END_MAIN(); +}
\ No newline at end of file diff --git a/tests/test_wheel.c b/tests/test_wheel.c new file mode 100644 index 0000000..99ac97c --- /dev/null +++ b/tests/test_wheel.c @@ -0,0 +1,89 @@ +#include "testing.h" + +void test_get_wheel_file() { +    struct testcase { +        const char *filename; +        struct Wheel expected; +    }; +    struct testcase tc[] = { +        { +            // Test for "build tags" +            .filename = "btpackage-1.2.3-mytag-py2.py3-none-any.whl", +            .expected = { +                .file_name = "btpackage-1.2.3-mytag-py2.py3-none-any.whl", +                .version = "1.2.3", +                .distribution = "btpackage", +                .build_tag = "mytag", +                .platform_tag = "any", +                .python_tag = "py2.py3", +                .abi_tag = "none", +                .path_name = ".", +            } +        }, +        { +            // Test for universal package format +            .filename = "anypackage-1.2.3-py2.py3-none-any.whl", +            .expected = { +                .file_name = "anypackage-1.2.3-py2.py3-none-any.whl", +                .version = "1.2.3", +                .distribution = "anypackage", +                .build_tag = NULL, +                .platform_tag = "any", +                .python_tag = "py2.py3", +                .abi_tag = "none", +                .path_name = ".", +            } +        }, +        { +            // Test for binary package format +            .filename = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl", +            .expected = { +                .file_name = "binpackage-1.2.3-cp311-cp311-linux_x86_64.whl", +                .version = "1.2.3", +                .distribution = "binpackage", +                .build_tag = NULL, +                .platform_tag = "linux_x86_64", +                .python_tag = "cp311", +                .abi_tag = "cp311", +                .path_name = ".", +            } +        }, +    }; + +    struct Wheel *doesnotexist = get_wheel_file("doesnotexist", "doesnotexist-0.0.1-py2.py3-none-any.whl", (char *[]) {"not", NULL}, WHEEL_MATCH_ANY); +    STASIS_ASSERT(doesnotexist == NULL, "returned non-NULL on error"); + +    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) { +        struct testcase *test = &tc[i]; +        struct Wheel *wheel = get_wheel_file(".", test->expected.distribution, (char *[]) {(char *) test->expected.version, NULL}, WHEEL_MATCH_ANY); +        STASIS_ASSERT(wheel != NULL, "result should not be NULL!"); +        STASIS_ASSERT(wheel->file_name && strcmp(wheel->file_name, test->expected.file_name) == 0, "mismatched file name"); +        STASIS_ASSERT(wheel->version && strcmp(wheel->version, test->expected.version) == 0, "mismatched version"); +        STASIS_ASSERT(wheel->distribution && strcmp(wheel->distribution, test->expected.distribution) == 0, "mismatched distribution (package name)"); +        STASIS_ASSERT(wheel->platform_tag && strcmp(wheel->platform_tag, test->expected.platform_tag) == 0, "mismatched platform tag ([platform]_[architecture])"); +        STASIS_ASSERT(wheel->python_tag && strcmp(wheel->python_tag, test->expected.python_tag) == 0, "mismatched python tag (python version)"); +        STASIS_ASSERT(wheel->abi_tag && strcmp(wheel->abi_tag, test->expected.abi_tag) == 0, "mismatched abi tag (python compatibility version)"); +        if (wheel->build_tag) { +            STASIS_ASSERT(strcmp(wheel->build_tag, test->expected.build_tag) == 0, +                          "mismatched build tag (optional arbitrary string)"); +        } +    } +} + +int main(int argc, char *argv[]) { +    STASIS_TEST_BEGIN_MAIN(); +    STASIS_TEST_FUNC *tests[] = { +        test_get_wheel_file, +    }; + +    // Create mock package directories, and files +    mkdir("binpackage", 0755); +    touch("binpackage/binpackage-1.2.3-cp311-cp311-linux_x86_64.whl"); +    mkdir("anypackage", 0755); +    touch("anypackage/anypackage-1.2.3-py2.py3-none-any.whl"); +    mkdir("btpackage", 0755); +    touch("btpackage/btpackage-1.2.3-mytag-py2.py3-none-any.whl"); + +    STASIS_TEST_RUN(tests); +    STASIS_TEST_END_MAIN(); +}
\ No newline at end of file | 
