From 0bb68a88e6dc6a04685825e80b4e3dca1dc097d2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 24 Feb 2017 19:10:28 +0300 Subject: Add support for extract files from zip archives. --- src/fs/zip_unittest.cc | 117 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 4 deletions(-) (limited to 'src/fs/zip_unittest.cc') diff --git a/src/fs/zip_unittest.cc b/src/fs/zip_unittest.cc index 3f0c49cdf..9661b7d19 100644 --- a/src/fs/zip_unittest.cc +++ b/src/fs/zip_unittest.cc @@ -40,7 +40,7 @@ TEST_CASE("Zip readArchiveInfo") if (Files::existsLocal(name) == false) prefix = "../"; - SECTION("test 1") + SECTION("test.zip") { name = prefix + "data/test/test.zip"; @@ -56,7 +56,7 @@ TEST_CASE("Zip readArchiveInfo") REQUIRE(headers[1]->uncompressSize == 1959); } - SECTION("test 2") + SECTION("test2.zip") { name = prefix + "data/test/test2.zip"; @@ -118,7 +118,7 @@ TEST_CASE("Zip readArchiveInfo") REQUIRE(headers[10]->uncompressSize == 306); } - SECTION("test 3") + SECTION("test3.zip") { name = prefix + "data/test/test3.zip"; @@ -134,7 +134,7 @@ TEST_CASE("Zip readArchiveInfo") REQUIRE(headers[1]->uncompressSize == 306); } - SECTION("test 4") + SECTION("test4.zip") { name = prefix + "data/test/test4.zip"; @@ -145,3 +145,112 @@ TEST_CASE("Zip readArchiveInfo") delete_all(headers); delete2(logger); } + +TEST_CASE("Zip readCompressedFile") +{ + logger = new Logger(); + std::string name("data/test/test.zip"); + std::string prefix; + std::vector headers; + if (Files::existsLocal(name) == false) + prefix = "../"; + + SECTION("empty") + { + REQUIRE_THROWS(Zip::readCompressedFile(nullptr)); + } + + SECTION("test2.zip") + { + name = prefix + "data/test/test2.zip"; + + REQUIRE(Zip::readArchiveInfo(name, headers)); + REQUIRE(headers.size() == 11); + // test.txt + uint8_t *const buf = Zip::readCompressedFile(headers[0]); + REQUIRE(buf != nullptr); + delete [] buf; + } + + delete_all(headers); + delete2(logger); +} + +TEST_CASE("Zip readFile") +{ + logger = new Logger(); + std::string name("data/test/test.zip"); + std::string prefix; + std::vector headers; + if (Files::existsLocal(name) == false) + prefix = "../"; + + SECTION("empty") + { + REQUIRE_THROWS(Zip::readFile(nullptr)); + } + + SECTION("test.zip") + { + name = prefix + "data/test/test.zip"; + + REQUIRE(Zip::readArchiveInfo(name, headers)); + REQUIRE(headers.size() == 2); + for (int f = 0; f < 2; f ++) + { + logger->log("test header: %s, %u, %u", + headers[f]->fileName.c_str(), + headers[f]->compressSize, + headers[f]->uncompressSize); + uint8_t *const buf = Zip::readFile(headers[f]); + REQUIRE(buf != nullptr); + delete [] buf; + } + } + + SECTION("test2.zip") + { + name = prefix + "data/test/test2.zip"; + + REQUIRE(Zip::readArchiveInfo(name, headers)); + REQUIRE(headers.size() == 11); + // test.txt + uint8_t *buf = Zip::readFile(headers[0]); + REQUIRE(buf != nullptr); + const std::string str = std::string(reinterpret_cast(buf), + headers[0]->uncompressSize); + REQUIRE(str == "test line 1\ntest line 2"); + delete [] buf; + for (int f = 0; f < 11; f ++) + { + logger->log("test header: %s, %u, %u", + headers[f]->fileName.c_str(), + headers[f]->compressSize, + headers[f]->uncompressSize); + buf = Zip::readFile(headers[f]); + REQUIRE(buf != nullptr); + delete [] buf; + } + } + + SECTION("test3.zip") + { + name = prefix + "data/test/test3.zip"; + + REQUIRE(Zip::readArchiveInfo(name, headers)); + REQUIRE(headers.size() == 2); + for (int f = 0; f < 2; f ++) + { + logger->log("test header: %s, %u, %u", + headers[f]->fileName.c_str(), + headers[f]->compressSize, + headers[f]->uncompressSize); + uint8_t *const buf = Zip::readFile(headers[f]); + REQUIRE(buf != nullptr); + delete [] buf; + } + } + + delete_all(headers); + delete2(logger); +} -- cgit v1.2.3-70-g09d2