|
|
#include "unity/unity.h" |
|
|
#include "zlib.h" |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
extern int test_deflateStateCheck(z_streamp strm); |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
static void init_stream(z_stream *strm) { |
|
|
memset(strm, 0, sizeof(*strm)); |
|
|
int ret = deflateInit(strm, Z_DEFAULT_COMPRESSION); |
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_NULL_stream_returns_1(void) { |
|
|
int rc = test_deflateStateCheck(NULL); |
|
|
TEST_ASSERT_EQUAL_INT(1, rc); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_uninitialized_stream_returns_1(void) { |
|
|
z_stream s; |
|
|
memset(&s, 0, sizeof(s)); |
|
|
|
|
|
int rc = test_deflateStateCheck(&s); |
|
|
TEST_ASSERT_EQUAL_INT(1, rc); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_after_deflateInit_is_valid(void) { |
|
|
z_stream strm; |
|
|
init_stream(&strm); |
|
|
|
|
|
int rc = test_deflateStateCheck(&strm); |
|
|
TEST_ASSERT_EQUAL_INT(0, rc); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_in_BUSY_STATE_after_header_is_valid(void) { |
|
|
z_stream strm; |
|
|
init_stream(&strm); |
|
|
|
|
|
unsigned char out[128]; |
|
|
strm.next_out = out; |
|
|
strm.avail_out = (uInt)sizeof(out); |
|
|
|
|
|
int ret = deflate(&strm, Z_NO_FLUSH); |
|
|
TEST_ASSERT(ret == Z_OK || ret == Z_BUF_ERROR ); |
|
|
|
|
|
int rc = test_deflateStateCheck(&strm); |
|
|
TEST_ASSERT_EQUAL_INT(0, rc); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_in_FINISH_STATE_is_valid(void) { |
|
|
z_stream strm; |
|
|
init_stream(&strm); |
|
|
|
|
|
const unsigned char in[] = "Hello, zlib!"; |
|
|
unsigned char out[1024]; |
|
|
|
|
|
strm.next_in = (Bytef *)in; |
|
|
strm.avail_in = (uInt)sizeof(in) - 1; |
|
|
strm.next_out = out; |
|
|
strm.avail_out = (uInt)sizeof(out); |
|
|
|
|
|
int ret; |
|
|
do { |
|
|
ret = deflate(&strm, Z_FINISH); |
|
|
|
|
|
} while (ret == Z_OK); |
|
|
TEST_ASSERT_EQUAL_INT(Z_STREAM_END, ret); |
|
|
|
|
|
int rc = test_deflateStateCheck(&strm); |
|
|
TEST_ASSERT_EQUAL_INT(0, rc); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_with_state_from_different_stream_returns_1(void) { |
|
|
z_stream owner; |
|
|
init_stream(&owner); |
|
|
|
|
|
|
|
|
z_stream imposter; |
|
|
memset(&imposter, 0, sizeof(imposter)); |
|
|
|
|
|
imposter.zalloc = owner.zalloc; |
|
|
imposter.zfree = owner.zfree; |
|
|
imposter.opaque = owner.opaque; |
|
|
|
|
|
imposter.state = owner.state; |
|
|
|
|
|
int rc = test_deflateStateCheck(&imposter); |
|
|
TEST_ASSERT_EQUAL_INT(1, rc); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&owner)); |
|
|
} |
|
|
|
|
|
void test_deflateStateCheck_after_deflateEnd_returns_1(void) { |
|
|
z_stream strm; |
|
|
init_stream(&strm); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(Z_OK, deflateEnd(&strm)); |
|
|
|
|
|
|
|
|
int rc = test_deflateStateCheck(&strm); |
|
|
TEST_ASSERT_EQUAL_INT(1, rc); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_deflateStateCheck_NULL_stream_returns_1); |
|
|
RUN_TEST(test_deflateStateCheck_uninitialized_stream_returns_1); |
|
|
RUN_TEST(test_deflateStateCheck_after_deflateInit_is_valid); |
|
|
RUN_TEST(test_deflateStateCheck_in_BUSY_STATE_after_header_is_valid); |
|
|
RUN_TEST(test_deflateStateCheck_in_FINISH_STATE_is_valid); |
|
|
RUN_TEST(test_deflateStateCheck_with_state_from_different_stream_returns_1); |
|
|
RUN_TEST(test_deflateStateCheck_after_deflateEnd_returns_1); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |