| 1 | // |
| 2 | // CompressTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "CompressTest.h" |
| 12 | #include "ZipTest.h" |
| 13 | #include "Poco/Buffer.h" |
| 14 | #include "Poco/Zip/Compress.h" |
| 15 | #include "Poco/Zip/ZipManipulator.h" |
| 16 | #include "Poco/File.h" |
| 17 | #include "Poco/FileStream.h" |
| 18 | #include "Poco/CppUnit/TestCaller.h" |
| 19 | #include "Poco/CppUnit/TestSuite.h" |
| 20 | #include <iostream> |
| 21 | #undef min |
| 22 | #include <algorithm> |
| 23 | |
| 24 | |
| 25 | using namespace Poco::Zip; |
| 26 | |
| 27 | |
| 28 | CompressTest::CompressTest(const std::string& name): CppUnit::TestCase(name) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | |
| 33 | CompressTest::~CompressTest() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | |
| 38 | void CompressTest::testSingleFile() |
| 39 | { |
| 40 | Poco::FileOutputStream out(Poco::Path::temp() + "appinf.zip" ); |
| 41 | Poco::Path theFile(ZipTest::getTestFile("data" , "test.zip" )); |
| 42 | Compress c(out, true); |
| 43 | c.addFile(theFile, theFile.getFileName()); |
| 44 | ZipArchive a(c.close()); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void CompressTest::testDirectory() |
| 49 | { |
| 50 | Poco::FileOutputStream out(Poco::Path::temp() + "pocobin.zip" ); |
| 51 | Poco::File aFile("some/" ); |
| 52 | if (aFile.exists()) aFile.remove(true); |
| 53 | Poco::File aDir("some/recursive/dir/" ); |
| 54 | aDir.createDirectories(); |
| 55 | Poco::File aDir2("some/other/recursive/dir/" ); |
| 56 | aDir2.createDirectories(); |
| 57 | Poco::File aF("some/recursive/dir/test.file" ); |
| 58 | aF.createFile(); |
| 59 | Poco::FileOutputStream fos(aF.path()); |
| 60 | fos << "just some test data" ; |
| 61 | fos.close(); |
| 62 | |
| 63 | Poco::Path theFile(aFile.path()); |
| 64 | theFile.makeDirectory(); |
| 65 | Compress c(out, true); |
| 66 | c.addRecursive(theFile, ZipCommon::CL_MAXIMUM, false, theFile); |
| 67 | ZipArchive a(c.close()); |
| 68 | Poco::File(aFile).remove(true); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void CompressTest::testManipulator() |
| 73 | { |
| 74 | { |
| 75 | Poco::FileOutputStream out(Poco::Path::temp() + "appinf.zip" ); |
| 76 | Poco::Path theFile(ZipTest::getTestFile("data" , "test.zip" )); |
| 77 | Compress c(out, true); |
| 78 | c.addFile(theFile, theFile.getFileName()); |
| 79 | ZipArchive a(c.close()); |
| 80 | } |
| 81 | ZipManipulator zm(Poco::Path::temp() + "appinf.zip" , true); |
| 82 | zm.renameFile("test.zip" , "renamedtest.zip" ); |
| 83 | zm.addFile("doc/othertest.zip" , ZipTest::getTestFile("data" , "test.zip" )); |
| 84 | ZipArchive archive=zm.commit(); |
| 85 | assertTrue (archive.findHeader("doc/othertest.zip" ) != archive.headerEnd()); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void CompressTest::testManipulatorDel() |
| 90 | { |
| 91 | { |
| 92 | Poco::FileOutputStream out(Poco::Path::temp() + "appinf.zip" ); |
| 93 | Poco::Path theFile(ZipTest::getTestFile("data" , "test.zip" )); |
| 94 | Compress c(out, true); |
| 95 | c.addFile(theFile, theFile.getFileName()); |
| 96 | ZipArchive a(c.close()); |
| 97 | } |
| 98 | ZipManipulator zm(Poco::Path::temp() + "appinf.zip" , true); |
| 99 | zm.deleteFile("test.zip" ); |
| 100 | zm.addFile("doc/data.zip" , ZipTest::getTestFile("data" , "data.zip" )); |
| 101 | ZipArchive archive=zm.commit(); |
| 102 | assertTrue (archive.findHeader("test.zip" ) == archive.headerEnd()); |
| 103 | assertTrue (archive.findHeader("doc/data.zip" ) != archive.headerEnd()); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void CompressTest::testManipulatorReplace() |
| 108 | { |
| 109 | { |
| 110 | Poco::FileOutputStream out(Poco::Path::temp() + "appinf.zip" ); |
| 111 | Poco::Path theFile(ZipTest::getTestFile("data" , "test.zip" )); |
| 112 | Compress c(out, true); |
| 113 | c.addFile(theFile, theFile.getFileName()); |
| 114 | ZipArchive a(c.close()); |
| 115 | } |
| 116 | ZipManipulator zm(Poco::Path::temp() + "appinf.zip" , true); |
| 117 | zm.replaceFile("test.zip" , ZipTest::getTestFile("data" , "doc.zip" )); |
| 118 | |
| 119 | ZipArchive archive=zm.commit(); |
| 120 | assertTrue (archive.findHeader("test.zip" ) != archive.headerEnd()); |
| 121 | assertTrue (archive.findHeader("doc.zip" ) == archive.headerEnd()); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void CompressTest::() |
| 126 | { |
| 127 | std::string ("Testing...123..." ); |
| 128 | Poco::FileOutputStream out(Poco::Path::temp() + "comment.zip" ); |
| 129 | Poco::Path theFile(ZipTest::getTestFile("data" , "test.zip" )); |
| 130 | Compress c(out, true); |
| 131 | c.addFile(theFile, theFile.getFileName()); |
| 132 | c.setZipComment(comment); |
| 133 | ZipArchive a(c.close()); |
| 134 | assertTrue (a.getZipComment() == comment); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void CompressTest::createDataFile(const std::string& path, Poco::UInt64 size) |
| 139 | { |
| 140 | Poco::FileOutputStream out(path.c_str(), std::ios::trunc); |
| 141 | assertTrue ( ! out.fail() ); |
| 142 | Poco::Buffer<char> buffer(MB); |
| 143 | for(int i = 0; size != 0; i++) { |
| 144 | std::memset(buffer.begin(), i, buffer.size()); |
| 145 | Poco::UInt64 bytesToWrite = std::min(size, static_cast<Poco::UInt64>(buffer.size())); |
| 146 | out.write(buffer.begin(), bytesToWrite); |
| 147 | assertTrue ( ! out.fail() ); |
| 148 | size -= bytesToWrite; |
| 149 | } |
| 150 | out.flush(); |
| 151 | assertTrue ( ! out.fail() ); |
| 152 | out.close(); |
| 153 | assertTrue ( ! out.fail() ); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void CompressTest::testZip64() |
| 158 | { |
| 159 | typedef std::map<std::string, Poco::UInt64> FileMap; |
| 160 | std::cout << std::endl; |
| 161 | FileMap files; |
| 162 | files["data1.bin" ] = static_cast<Poco::UInt64>(KB)*4096+1; |
| 163 | files["data2.bin" ] = static_cast<Poco::UInt64>(KB)*16; |
| 164 | files["data3.bin" ] = static_cast<Poco::UInt64>(KB)*4096-1; |
| 165 | |
| 166 | for(FileMap::const_iterator it = files.begin(); it != files.end(); it++) |
| 167 | { |
| 168 | std::cout << '\t' << "createDataFile(" << it->first << ", " << it->second << ");" << std::endl; |
| 169 | createDataFile(it->first, it->second); |
| 170 | } |
| 171 | Poco::FileOutputStream out(Poco::Path::temp() + "zip64.zip" , std::ios::trunc); |
| 172 | Compress c(out, true, true); |
| 173 | for(FileMap::const_iterator it = files.begin(); it != files.end(); it++) |
| 174 | { |
| 175 | const std::string& path = it->first; |
| 176 | std::cout << '\t' << "addFile(" << path << ");" << std::endl; |
| 177 | c.addFile(path, path, ZipCommon::CM_STORE); |
| 178 | } |
| 179 | ZipArchive a(c.close()); |
| 180 | for(FileMap::const_iterator it = files.begin(); it != files.end(); it++) |
| 181 | { |
| 182 | const std::string& path = it->first; |
| 183 | Poco::UInt64 size = it->second; |
| 184 | ZipArchive::FileHeaders::const_iterator it2 = a.findHeader(path); |
| 185 | assertTrue (it2 != a.headerEnd()); |
| 186 | const Poco::Zip::ZipLocalFileHeader& file = it2->second; |
| 187 | assertTrue (file.getUncompressedSize() == size); |
| 188 | assertTrue (file.getCompressedSize() == size); |
| 189 | } |
| 190 | for (FileMap::const_iterator it = files.begin(); it != files.end(); it++) |
| 191 | { |
| 192 | Poco::File(it->first).remove(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void CompressTest::setUp() |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void CompressTest::tearDown() |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | |
| 207 | CppUnit::Test* CompressTest::suite() |
| 208 | { |
| 209 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CompressTest" ); |
| 210 | |
| 211 | CppUnit_addTest(pSuite, CompressTest, testSingleFile); |
| 212 | CppUnit_addTest(pSuite, CompressTest, testDirectory); |
| 213 | CppUnit_addTest(pSuite, CompressTest, testManipulator); |
| 214 | CppUnit_addTest(pSuite, CompressTest, testManipulatorDel); |
| 215 | CppUnit_addTest(pSuite, CompressTest, testManipulatorReplace); |
| 216 | CppUnit_addTest(pSuite, CompressTest, testSetZipComment); |
| 217 | CppUnit_addTest(pSuite, CompressTest, testZip64); |
| 218 | |
| 219 | return pSuite; |
| 220 | } |
| 221 | |