zipTestMisc.cpp
Engine/source/core/util/zip/test/zipTestMisc.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23/* 24#include "core/crc.h" 25#include "core/strings/stringFunctions.h" 26#include "core/util/zip/zipArchive.h" 27#include "core/util/zip/unitTests/zipTest.h" 28 29#include "unit/test.h" 30#include "unit/memoryTester.h" 31 32 33using namespace UnitTesting; 34using namespace Zip; 35 36CreateUnitTest(ZipTestMisc, "Zip/Misc") 37{ 38private: 39 StringTableEntry mWriteFilename; 40 StringTableEntry mBaselineFilename; 41 StringTableEntry mWorkingFilename; 42 43public: 44 ZipTestMisc() 45 { 46 mWriteFilename = makeTestPath(ZIPTEST_WRITE_FILENAME); 47 mBaselineFilename = makeTestPath(ZIPTEST_BASELINE_FILENAME); 48 mWorkingFilename = makeTestPath(ZIPTEST_WORKING_FILENAME); 49 } 50 51 void run() 52 { 53 MemoryTester m; 54 m.mark(); 55 56 // Clean up from a previous run 57 cleanup(); 58 59 test(makeTestZip(), "Failed to create test zip"); 60 61 miscTest(mWorkingFilename); 62 63 // [tom, 2/7/2007] extractFile() uses the resource manager so it will 64 // create a resource object that will be detected as a "leak" since it 65 // won't be freed until much later. 66 67 test(m.check(), "Zip misc test leaked memory! (Unless it says it's from the Resource Manager above, see comments in core/zip/unitTests/zipTestMisc.cc)"); 68 } 69 70private: 71 72 //----------------------------------------------------------------------------- 73 74 void cleanup() 75 { 76 if(Platform::isFile(mWriteFilename)) 77 dFileDelete(mWriteFilename); 78 if(Platform::isFile(mWorkingFilename)) 79 dFileDelete(mWorkingFilename); 80 } 81 82 bool makeTestZip() 83 { 84 FileStream source, dest; 85 86 if(! source.open(mBaselineFilename, Torque::FS::File::Read)) 87 { 88 fail("Failed to open baseline zip for read"); 89 return false; 90 } 91 92 // [tom, 2/7/2007] FileStream's d'tor calls close() so we don't really have to do it here 93 94 if(! dest.open(mWorkingFilename, Torque::FS::File::Write)) 95 { 96 fail("Failed to open working zip for write"); 97 return false; 98 } 99 100 if(! dest.copyFrom(&source)) 101 { 102 fail("Failed to copy baseline zip"); 103 return false; 104 } 105 106 dest.close(); 107 source.close(); 108 return true; 109 } 110 111 //----------------------------------------------------------------------------- 112 113 bool miscTest(const char *zipfile) 114 { 115 ZipArchive *zip = new ZipArchive; 116 bool ret = true; 117 118 if(! zip->openArchive(zipfile, ZipArchive::ReadWrite)) 119 { 120 delete zip; 121 fail("Unable to open zip file"); 122 123 return false; 124 } 125 126 // Opening a file in the zip as ReadWrite (should fail) 127 Stream *stream; 128 if((stream = zip->openFile("readWriteTest.txt", ZipArchive::ReadWrite)) != NULL) 129 { 130 zip->closeFile(stream); 131 132 fail("File opened successfully as read/write"); 133 ret = false; 134 } 135 136 // Enumerating, Extracting and Deleting files 137 U32 curSize = zip->mDiskStream->getStreamSize(); 138 139 if(zip->numEntries() > 0) 140 { 141 // Find the biggest file in the zip 142 const CentralDir *del = NULL; 143 for(S32 i = 0;i < zip->numEntries();++i) 144 { 145 const CentralDir &cd = (*zip)[i]; 146 147 if(del == NULL || (del && cd.mUncompressedSize > del->mUncompressedSize)) 148 del = &cd; 149 } 150 151 if(del) 152 { 153 // Extract the file 154 const char *ptr = dStrrchr(del->mFilename, '/'); 155 if(ptr) 156 ++ptr; 157 else 158 ptr = del->mFilename; 159 StringTableEntry fn = makeTestPath(ptr); 160 161 ret = test(zip->extractFile(del->mFilename, fn), "Failed to extract file."); 162 163 // Then delete it 164 ret = test(zip->deleteFile(del->mFilename), "ZipArchive::deleteFile() failed?!"); 165 166 // Close and reopen the zip to force it to rebuild 167 zip->closeArchive(); 168 169 if(! zip->openArchive(zipfile, ZipArchive::ReadWrite)) 170 { 171 delete zip; 172 fail("Unable to re-open zip file?!"); 173 174 return false; 175 } 176 } 177 else 178 { 179 fail("Couldn't find a file to delete?!"); 180 ret = false; 181 } 182 } 183 else 184 { 185 fail("Baseline zip has no files."); 186 ret = false; 187 } 188 189 U32 newSize = zip->mDiskStream->getStreamSize(); 190 test(newSize != curSize, "Zip file size didn't change when deleting a file ?!"); 191 192 zip->closeArchive(); 193 delete zip; 194 195 return ret; 196 } 197}; 198*/ 199