| #include "unity/unity.h" |
| #include "zlib.h" |
| #include <string.h> |
| #include <stdlib.h> |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| void test_deflateEnd_null_stream_returns_stream_error(void) { |
| int ret = deflateEnd(NULL); |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, ret); |
| } |
|
|
| |
| void test_deflateEnd_uninitialized_stream_returns_stream_error(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
| int ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, ret); |
| } |
|
|
| |
| void test_deflateEnd_after_init_returns_ok_and_clears_state(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
|
|
| int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| TEST_ASSERT_NOT_NULL(strm.state); |
|
|
| ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| TEST_ASSERT_NULL(strm.state); |
| } |
|
|
| |
| void test_deflateEnd_after_header_written_returns_data_error(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
|
|
| int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| unsigned char outbuf[128]; |
| strm.next_out = outbuf; |
| strm.avail_out = (uInt)sizeof(outbuf); |
|
|
| |
| ret = deflate(&strm, Z_NO_FLUSH); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| |
| ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_DATA_ERROR, ret); |
| TEST_ASSERT_NULL(strm.state); |
| } |
|
|
| |
| void test_deflateEnd_after_finish_returns_ok(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
|
|
| int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| const char *msg = "hello world, this is a small test payload"; |
| unsigned char inbuf[128]; |
| size_t inlen = strlen(msg); |
| TEST_ASSERT(inlen < sizeof(inbuf)); |
| memcpy(inbuf, msg, inlen); |
|
|
| unsigned char outbuf[512]; |
| memset(outbuf, 0, sizeof(outbuf)); |
|
|
| strm.next_in = (Bytef *)inbuf; |
| strm.avail_in = (uInt)inlen; |
| strm.next_out = outbuf; |
| strm.avail_out = (uInt)sizeof(outbuf); |
|
|
| ret = deflate(&strm, Z_FINISH); |
| |
| while (ret == Z_OK) { |
| if (strm.avail_out == 0) { |
| |
| |
| TEST_FAIL_MESSAGE("Unexpectedly ran out of output buffer"); |
| } |
| ret = deflate(&strm, Z_FINISH); |
| } |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_END, ret); |
|
|
| ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| TEST_ASSERT_NULL(strm.state); |
| } |
|
|
| |
| void test_deflateEnd_called_twice_second_call_errors(void) { |
| z_stream strm; |
| memset(&strm, 0, sizeof(strm)); |
|
|
| int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
|
|
| ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_OK, ret); |
| TEST_ASSERT_NULL(strm.state); |
|
|
| |
| ret = deflateEnd(&strm); |
| TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, ret); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_deflateEnd_null_stream_returns_stream_error); |
| RUN_TEST(test_deflateEnd_uninitialized_stream_returns_stream_error); |
| RUN_TEST(test_deflateEnd_after_init_returns_ok_and_clears_state); |
| RUN_TEST(test_deflateEnd_after_header_written_returns_data_error); |
| RUN_TEST(test_deflateEnd_after_finish_returns_ok); |
| RUN_TEST(test_deflateEnd_called_twice_second_call_errors); |
| return UNITY_END(); |
| } |