| #include "unity/unity.h" |
| #include "zlib.h" |
|
|
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <errno.h> |
| #include <time.h> |
|
|
| |
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void gen_temp_gz_path(char *out, size_t siz) { |
| if (out == NULL || siz == 0) return; |
|
|
| |
| char base[L_tmpnam]; |
| out[0] = '\0'; |
|
|
| if (tmpnam(base) != NULL) { |
| |
| (void)snprintf(out, siz, "%s.gz", base); |
| return; |
| } |
|
|
| |
| long t = (long)time(NULL); |
| unsigned r = (unsigned)rand(); |
| (void)snprintf(out, siz, "gzclose_test_%ld_%u.gz", t, r); |
| } |
|
|
| |
| void test_gzclose_null_pointer_returns_stream_error(void) { |
| int ret = gzclose((gzFile)NULL); |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, ret); |
| } |
|
|
| |
| void test_gzclose_write_mode_returns_ok_and_creates_file(void) { |
| char path[L_tmpnam + 32]; |
| gen_temp_gz_path(path, sizeof(path)); |
|
|
| gzFile f = gzopen(path, "wb"); |
| if (f == NULL) { |
| TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported in this build (NO_GZCOMPRESS?)"); |
| return; |
| } |
|
|
| const char *msg = "Hello gzclose write-mode test"; |
| int w = gzwrite(f, msg, (unsigned)strlen(msg)); |
| TEST_ASSERT_TRUE_MESSAGE(w > 0, "gzwrite failed"); |
|
|
| int ret = gzclose(f); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| |
| FILE *fp = fopen(path, "rb"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(fp, "Closed gzip file not found on disk"); |
| if (fp) { |
| if (fseek(fp, 0, SEEK_END) == 0) { |
| long sz = ftell(fp); |
| TEST_ASSERT_TRUE_MESSAGE(sz > 0, "Gzip file size should be > 0 after write and close"); |
| } else { |
| TEST_FAIL_MESSAGE("fseek failed when verifying file size"); |
| } |
| fclose(fp); |
| } |
|
|
| remove(path); |
| } |
|
|
| |
| void test_gzclose_read_mode_returns_ok(void) { |
| char path[L_tmpnam + 32]; |
| gen_temp_gz_path(path, sizeof(path)); |
|
|
| |
| gzFile fw = gzopen(path, "wb"); |
| if (fw == NULL) { |
| TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported; cannot set up file for read-mode test"); |
| return; |
| } |
| const char *msg = "Data for read-mode gzclose test"; |
| int w = gzwrite(fw, msg, (unsigned)strlen(msg)); |
| TEST_ASSERT_TRUE_MESSAGE(w > 0, "gzwrite failed in setup for read-mode test"); |
| TEST_ASSERT_EQUAL_INT(Z_OK, gzclose(fw)); |
|
|
| |
| gzFile fr = gzopen(path, "rb"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(fr, "gzopen(\"rb\") failed on created gzip file"); |
|
|
| int ret = gzclose(fr); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| remove(path); |
| } |
|
|
| |
| void test_gzclose_write_mode_without_writing_returns_ok(void) { |
| char path[L_tmpnam + 32]; |
| gen_temp_gz_path(path, sizeof(path)); |
|
|
| gzFile f = gzopen(path, "wb"); |
| if (f == NULL) { |
| TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported in this build (NO_GZCOMPRESS?)"); |
| return; |
| } |
|
|
| |
| int ret = gzclose(f); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| |
| FILE *fp = fopen(path, "rb"); |
| if (fp) { |
| |
| fclose(fp); |
| remove(path); |
| } else { |
| |
| } |
| } |
|
|
| int main(void) { |
| srand((unsigned)time(NULL)); |
| UNITY_BEGIN(); |
|
|
| RUN_TEST(test_gzclose_null_pointer_returns_stream_error); |
| RUN_TEST(test_gzclose_write_mode_returns_ok_and_creates_file); |
| RUN_TEST(test_gzclose_read_mode_returns_ok); |
| RUN_TEST(test_gzclose_write_mode_without_writing_returns_ok); |
|
|
| return UNITY_END(); |
| } |