From 0bb0a2db095614be951a744535b1a9b5536aa2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommi=20M=C3=A4klin?= Date: Fri, 22 Apr 2022 21:19:30 +0300 Subject: [PATCH] Add unit tests for z_stream_wrapper. --- CMakeLists.txt | 1 + test/include/z_stream_wrapper_unittest.hpp | 111 +++++++++++++++++++++ test/src/z_stream_wrapper_unittest.cpp | 65 ++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 test/include/z_stream_wrapper_unittest.hpp create mode 100644 test/src/z_stream_wrapper_unittest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index aa20866..dba9ca2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ if(CMAKE_BUILD_TESTS) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test/include ${CMAKE_CURRENT_SOURCE_DIR}/include) enable_testing() add_executable(runTests + ${CMAKE_CURRENT_SOURCE_DIR}/test/src/z_stream_wrapper_unittest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/src/zstd_stream_wrapper_unittest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/src/bxzstr_ofstream_integrationtest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/src/bxzstr_ifstream_integrationtest.cpp) diff --git a/test/include/z_stream_wrapper_unittest.hpp b/test/include/z_stream_wrapper_unittest.hpp new file mode 100644 index 0000000..a03dfad --- /dev/null +++ b/test/include/z_stream_wrapper_unittest.hpp @@ -0,0 +1,111 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * This file is a part of bxzstr (https://github.com/tmaklin/bxzstr) + * Written by Tommi Mäklin (tommi@maklin.fi) */ + +#ifndef BXZSTR_Z_STREAM_WRAPPER_UNITTEST_HPP +#define BXZSTR_Z_STREAM_WRAPPER_UNITTEST_HPP + +#include +#include +#include + +#include "gtest/gtest.h" +#include "zlib.h" + +#include "bxzstr.hpp" + +// Test zException +class ZExceptionTest : public ::testing::Test { + protected: + void SetUp() override { + this->msgConstructorValue = "urpdcjgztzcowdpiucfrhxczlgbbopeg"; + this->msgConstructorExpected = "urpdcjgztzcowdpiucfrhxczlgbbopeg"; + this->errcodeConstructorValue = -1; + this->errcodeInputs = std::vector({ Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR, Z_VERSION_ERROR, Z_BUF_ERROR, -1 }); + this->errcodeExpecteds = std::vector({ + "zlib: Z_STREAM_ERROR: urpdcjgztzcowdpiucfrhxczlgbbopeg", + "zlib: Z_DATA_ERROR: urpdcjgztzcowdpiucfrhxczlgbbopeg", + "zlib: Z_MEM_ERROR: urpdcjgztzcowdpiucfrhxczlgbbopeg", + "zlib: Z_VERSION_ERROR: urpdcjgztzcowdpiucfrhxczlgbbopeg", + "zlib: Z_BUF_ERROR: urpdcjgztzcowdpiucfrhxczlgbbopeg", + "zlib: [-1]: urpdcjgztzcowdpiucfrhxczlgbbopeg"}); + } + void TearDown() override { + } + + // Test inputs + std::vector errcodeInputs; + // Test values + std::string msgConstructorValue; + size_t errcodeConstructorValue; + + // Expecteds + std::string msgConstructorExpected; + std::vector errcodeExpecteds; + +}; + +// Test z_stream_wrapper +class ZStreamWrapperTest : public ::testing::Test { + protected: + void SetUp() override { + this->testTrue = true; + this->testFalse = false; + } + void TearDown() override { + } + // Test values + bool testTrue; + bool testFalse; +}; + +// Common inputs/outputs for compression and decompression testing +class ZCompressAndDecompressTest { + protected: + unsigned char test_vals[34] = {0x1f, 0x8b, 0x08, 0x08, 0xf1, 0x0a, 0x61, 0x62, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x7a, 0x2e, + 0x74, 0x78, 0x74, 0x00, 0x33, 0xe4, 0x32, 0xc4, 0x80, 0x00, 0x4c, 0xd2, 0xca, 0x03, 0x14, 0x00, + 0x00, 0x00 }; + unsigned char output_vals[10] = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' }; + + unsigned char* testIn; + const unsigned char* testOut; + bxz::detail::z_stream_wrapper* wrapper; + + void set_addresses(bxz::detail::z_stream_wrapper* wrapper) { + wrapper->set_next_in(&testIn[0]); + wrapper->set_avail_in(10); + wrapper->set_next_out(&testOut[0]); + wrapper->set_avail_out(10); + } +}; + +// Test decompress +class ZDecompressTest : public ZCompressAndDecompressTest, public ::testing::Test { + protected: + void SetUp() override { + this->testIn = reinterpret_cast(test_vals); + this->testOut = reinterpret_cast(output_vals); + wrapper = new bxz::detail::z_stream_wrapper(); + this->set_addresses(wrapper); + } + void TearDown() override { + } +}; + +// Test compress +class ZCompressTest : public ZCompressAndDecompressTest, public ::testing::Test { + protected: + void SetUp() override { + this->testIn = reinterpret_cast(test_vals); + this->testOut = reinterpret_cast(output_vals); + wrapper = new bxz::detail::z_stream_wrapper(false); + this->set_addresses(wrapper); + } + void TearDown() override { + } +}; + +#endif diff --git a/test/src/z_stream_wrapper_unittest.cpp b/test/src/z_stream_wrapper_unittest.cpp new file mode 100644 index 0000000..8b7d080 --- /dev/null +++ b/test/src/z_stream_wrapper_unittest.cpp @@ -0,0 +1,65 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * This file is a part of bxzstr (https://github.com/tmaklin/bxzstr) + * Written by Tommi Mäklin (tommi@maklin.fi) */ + +#include "z_stream_wrapper_unittest.hpp" + +TEST_F(ZExceptionTest, MsgConstructorWorks) { + bxz::zException e(msgConstructorValue); + const std::string &got = e.what(); + EXPECT_EQ(msgConstructorExpected, got); +} + +TEST_F(ZExceptionTest, ErrcodeConstructorWorks) { + for (size_t i = 0; i < errcodeInputs.size(); ++i) { + bxz::zException e(msgConstructorValue, errcodeInputs.at(i)); + const std::string &got = e.what(); + EXPECT_EQ(errcodeExpecteds.at(i), got); + } +} + +TEST_F(ZStreamWrapperTest, ConstructorDoesNotThrowOnInput) { + EXPECT_NO_THROW(bxz::detail::z_stream_wrapper wrapper(testTrue)); +} + +TEST_F(ZStreamWrapperTest, ConstructorDoesNotThrowOnOutput) { + EXPECT_NO_THROW(bxz::detail::z_stream_wrapper wrapper(testFalse)); +} + +TEST_F(ZDecompressTest, DecompressDoesNotThrowOnValidInput) { + EXPECT_NO_THROW(wrapper->decompress()); +} + +TEST_F(ZDecompressTest, DecompressThrowsOnInvalidInput) { + testIn[0] = 0x1d; + EXPECT_THROW(wrapper->decompress(), bxz::zException); +} + +TEST_F(ZDecompressTest, DecompressUpdatesStreamState) { + wrapper->decompress(); + EXPECT_EQ(wrapper->next_in(), &testIn[10]); + EXPECT_EQ(wrapper->avail_in(), 0); + EXPECT_EQ(wrapper->next_out(), &testOut[0]); + EXPECT_EQ(wrapper->avail_out(), 10); +} + +TEST_F(ZCompressTest, CompressEndsStream) { + wrapper->set_avail_out(0); + wrapper->set_next_out(&testOut[10]); + EXPECT_NO_THROW(wrapper->compress(true)); +} + +TEST_F(ZCompressTest, CompressDoesNotThrowOnValidInput) { + EXPECT_NO_THROW(wrapper->compress(false)); +} + +TEST_F(ZCompressTest, CompressUpdatesStreamState) { + wrapper->compress(false); + EXPECT_EQ(wrapper->next_in(), &testIn[10]); + EXPECT_EQ(wrapper->avail_in(), 0); + EXPECT_EQ(wrapper->next_out(), &testOut[10]); + EXPECT_EQ(wrapper->avail_out(), 0); +}