File size: 4,694 Bytes
e996a55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "unity/unity.h"
#include "zlib.h"
#include "gzguts.h"

#include <stdlib.h>
#include <string.h>

/* Wrapper for the local function provided in gzlib.c */
extern void test_gz_reset(gz_statep state);

void setUp(void) {
    /* Setup code here, or leave empty */
}

void tearDown(void) {
    /* Cleanup code here, or leave empty */
}

/* Helper to make a duplicated C string safely (returns NULL on failure) */
static char *dup_cstr(const char *s) {
    size_t len = strlen(s) + 1;
    char *p = (char *)malloc(len);
    if (p) memcpy(p, s, len);
    return p;
}

void test_gz_reset_write_mode_resets_general_fields(void) {
    gz_state state;
    memset(&state, 0, sizeof(state));

    /* Initialize fields with non-default values */
    state.mode = GZ_WRITE;
    state.x.have = 17;
    state.seek = 1;
    state.x.pos = 12345;
    state.strm.avail_in = 42;
    state.err = Z_STREAM_ERROR;          /* some non-OK error */
    state.msg = dup_cstr("old msg");     /* should be freed by gz_reset via gz_error */
    state.reset = 99;                    /* should be set to 0 by gz_reset in write mode */
    state.want = 777;                    /* should remain unchanged */
    state.level = 3;                     /* should remain unchanged */
    state.strategy = Z_RLE;              /* should remain unchanged */
    state.direct = 0;                    /* should remain unchanged */
    char *saved_path = dup_cstr("dummy/path");
    state.path = saved_path;             /* gz_reset should not touch path */

    test_gz_reset(&state);

    /* General resets */
    TEST_ASSERT_EQUAL(0u, state.x.have);
    TEST_ASSERT_EQUAL_INT(0, state.seek);
    TEST_ASSERT_EQUAL_UINT64(0, state.x.pos);
    TEST_ASSERT_EQUAL_UINT(0u, state.strm.avail_in);
    TEST_ASSERT_EQUAL_INT(Z_OK, state.err);
    TEST_ASSERT_NULL(state.msg);

    /* Write-mode specific reset */
    TEST_ASSERT_EQUAL_INT(0, state.reset);

    /* Unchanged fields */
    TEST_ASSERT_EQUAL_INT(GZ_WRITE, state.mode);
    TEST_ASSERT_EQUAL_UINT(777u, state.want);
    TEST_ASSERT_EQUAL_INT(3, state.level);
    TEST_ASSERT_EQUAL_INT(Z_RLE, state.strategy);
    TEST_ASSERT_EQUAL_INT(0, state.direct);
    TEST_ASSERT_TRUE(state.path == saved_path);

    /* Cleanup */
    free(saved_path);
}

void test_gz_reset_read_mode_resets_read_fields(void) {
    gz_state state;
    memset(&state, 0, sizeof(state));

    /* Initialize read-mode state */
    state.mode = GZ_READ;
    state.eof = 1;
    state.past = 1;
    state.how = COPY;          /* should become LOOK */
    state.direct = 1;          /* should remain unchanged */
    state.x.have = 5;
    state.seek = 1;
    state.x.pos = 999;
    state.strm.avail_in = 7;
    state.err = Z_BUF_ERROR;   /* acceptable non-fatal state */
    state.msg = NULL;
    state.want = 2048;         /* should remain unchanged */
    char *saved_path = dup_cstr("read/path");
    state.path = saved_path;

    test_gz_reset(&state);

    /* Read-mode specifics */
    TEST_ASSERT_EQUAL_INT(0, state.eof);
    TEST_ASSERT_EQUAL_INT(0, state.past);
    TEST_ASSERT_EQUAL_INT(LOOK, state.how);

    /* General resets */
    TEST_ASSERT_EQUAL(0u, state.x.have);
    TEST_ASSERT_EQUAL_INT(0, state.seek);
    TEST_ASSERT_EQUAL_UINT64(0, state.x.pos);
    TEST_ASSERT_EQUAL_UINT(0u, state.strm.avail_in);
    TEST_ASSERT_EQUAL_INT(Z_OK, state.err);
    TEST_ASSERT_NULL(state.msg);

    /* Unchanged fields */
    TEST_ASSERT_EQUAL_INT(GZ_READ, state.mode);
    TEST_ASSERT_EQUAL_INT(1, state.direct);
    TEST_ASSERT_EQUAL_UINT(2048u, state.want);
    TEST_ASSERT_TRUE(state.path == saved_path);

    /* Cleanup */
    free(saved_path);
}

void test_gz_reset_clears_mem_error_without_free(void) {
    gz_state state;
    memset(&state, 0, sizeof(state));

    state.mode = GZ_WRITE;
    state.err = Z_MEM_ERROR;
    /* Assign a string literal so that any erroneous free would be dangerous.
       gz_error should not free msg when prior err == Z_MEM_ERROR. */
    state.msg = (char *)"no-free";
    state.x.have = 3;
    state.seek = 1;
    state.x.pos = 55;
    state.strm.avail_in = 11;

    test_gz_reset(&state);

    /* Error cleared and message cleared */
    TEST_ASSERT_EQUAL_INT(Z_OK, state.err);
    TEST_ASSERT_NULL(state.msg);

    /* General resets */
    TEST_ASSERT_EQUAL(0u, state.x.have);
    TEST_ASSERT_EQUAL_INT(0, state.seek);
    TEST_ASSERT_EQUAL_UINT64(0, state.x.pos);
    TEST_ASSERT_EQUAL_UINT(0u, state.strm.avail_in);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_gz_reset_write_mode_resets_general_fields);
    RUN_TEST(test_gz_reset_read_mode_resets_read_fields);
    RUN_TEST(test_gz_reset_clears_mem_error_without_free);
    return UNITY_END();
}