Torque3D Documentation / _generateds / zipTestRead.cpp

zipTestRead.cpp

Engine/source/core/util/zip/test/zipTestRead.cpp

More...

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 "platform/platform.h"
 25
 26#include "unit/test.h"
 27#include "unit/memoryTester.h"
 28
 29#include "core/util/zip/zipArchive.h"
 30#include "core/util/zip/unitTests/zipTest.h"
 31
 32#include "core/stringTable.h"
 33#include "core/crc.h"
 34
 35using namespace UnitTesting;
 36using namespace Zip;
 37
 38CreateUnitTest(ZipTestRead, "Zip/Read")
 39{
 40private:
 41   StringTableEntry mWriteFilename;
 42   StringTableEntry mBaselineFilename;
 43   StringTableEntry mWorkingFilename;
 44
 45public:
 46   ZipTestRead()
 47   {
 48      mWriteFilename = makeTestPath(ZIPTEST_WRITE_FILENAME);
 49      mBaselineFilename = makeTestPath(ZIPTEST_BASELINE_FILENAME);
 50      mWorkingFilename = makeTestPath(ZIPTEST_WORKING_FILENAME);
 51   }
 52
 53   void run()
 54   {
 55      MemoryTester m;
 56      m.mark();
 57
 58      // Clean up from a previous run
 59      cleanup();
 60
 61      // Read mode tests with the baseline zip
 62      readTest(mBaselineFilename);
 63
 64      // Read mode tests with a zip created by us
 65      test(makeTestZip(), "Failed to make test zip");
 66      readTest(mWorkingFilename);
 67
 68      // Do read/write mode tests
 69      readWriteTest(mWriteFilename);
 70
 71      test(m.check(), "Zip read test leaked memory!");
 72   }
 73
 74private:
 75
 76   //-----------------------------------------------------------------------------
 77
 78   void cleanup()
 79   {
 80      if(Platform::isFile(mWriteFilename))
 81         dFileDelete(mWriteFilename);
 82      if(Platform::isFile(mWorkingFilename))
 83         dFileDelete(mWorkingFilename);
 84   }
 85
 86   //-----------------------------------------------------------------------------
 87
 88   bool writeFile(ZipArchive *zip, const char *filename)
 89   {
 90      Stream * stream = zip->openFile(filename, ZipArchive::Write);
 91      if(stream != NULL)
 92      {
 93         stream->writeLine((U8 *)"This is a test file for reading from a zip created with the zip code.\r\nIt is pointless by itself, but needs to be long enough that it gets compressed.\r\n");
 94         zip->closeFile(stream);
 95         return true;
 96      }
 97
 98      return false;
 99   }
100
101   bool makeTestZip()
102   {
103      ZipArchive *zip = new ZipArchive;
104
105      if(!zip->openArchive(mWorkingFilename, ZipArchive::Write))
106      {
107         delete zip;
108         fail("Unable to open zip file");
109
110         return false;
111      }
112
113      test(writeFile(zip, "test.txt"), "Failed to write file into test zip");
114      test(writeFile(zip, "console.log"), "Failed to write file into test zip");
115      test(writeFile(zip, "folder/OpenAL32.dll"), "Failed to write file into test zip");
116
117      zip->closeArchive();
118      delete zip;
119
120      return true;
121   }
122
123   //-----------------------------------------------------------------------------
124
125   bool testReadFile(ZipArchive *zip, const char *filename)
126   {
127      // [tom, 2/5/2007] Try using openFile() first for fairest real-world test
128      Stream *stream;
129      if((stream = zip->openFile(filename, ZipArchive::Read)) == NULL)
130         return false;
131
132      const CentralDir *cd = zip->findFileInfo(filename);
133      if(cd == NULL)
134      {
135         // [tom, 2/5/2007] This shouldn't happen
136         zip->closeFile(stream);
137
138         fail("testReadFile - File opened successfully, but couldn't find central directory. This is bad.");
139
140         return false;
141      }
142
143      U32 crc = CRC::INITIAL_CRC_VALUE;
144      
145      bool ret = true;
146      U8 buffer[1024];
147      U32 numBytes = stream->getStreamSize() - stream->getPosition();
148      while((stream->getStatus() != Stream::EOS) && numBytes > 0)
149      {
150         U32 numRead = numBytes > sizeof(buffer) ? sizeof(buffer) : numBytes;
151         if(! stream->read(numRead, buffer))
152         {
153            ret = false;
154            fail("testReadFile - Couldn't read from stream");
155            break;
156         }
157
158         crc = CRC::calculateCRC(buffer, numRead, crc);
159
160         numBytes -= numRead;
161      }
162
163      if(ret)
164      {
165         // CRC Check
166         crc ^= CRC::CRC_POSTCOND_VALUE;
167         if(crc != cd->mCRC32)
168         {
169            fail("testReadFile - File failed CRC check");
170            ret = false;
171         }
172      }
173
174      zip->closeFile(stream);
175      return ret;
176   }
177
178   //-----------------------------------------------------------------------------
179
180   bool readTest(const char *zipfile)
181   {
182      ZipArchive *zip = new ZipArchive;
183
184      if(!zip->openArchive(zipfile, ZipArchive::Read))
185      {
186         delete zip;
187         fail("Unable to open zip file");
188
189         return false;
190      }
191
192      // Try reading files that exist in various formats
193      testReadFile(zip, "test.txt");
194      testReadFile(zip, "console.log");
195      testReadFile(zip, "folder/OpenAL32.dll");
196
197      // Try reading a file that doesn't exist
198      if(testReadFile(zip, "hopefullyDoesntExist.bla.bla.foo"))
199         fail("Opening a file the didn't exist succeeded");
200
201      zip->closeArchive();
202      delete zip;
203
204      return true;
205   }
206
207   bool readWriteTest(const char *zipfile)
208   {
209      ZipArchive *zip = new ZipArchive;
210      bool ret = true;
211
212      if(!zip->openArchive(zipfile, ZipArchive::ReadWrite))
213      {
214         delete zip;
215         fail("Unable to open zip file");
216
217         return false;
218      }
219
220      // Test writing a file and then reading it back before the zip is rebuilt
221      test(writeFile(zip, "test.txt"), "Failed to write file to test zip");
222      test(testReadFile(zip, "test.txt"), "Failed to read file back from test zip");
223      
224      // Read from file that is already open for write (should fail)
225      Stream *wrStream = zip->openFile("writeTest.txt", ZipArchive::Write);
226      if(wrStream != NULL)
227      {
228         wrStream->writeLine((U8 *)"Test text to make a valid file");
229
230         // This next open should fail
231         Stream * rdStream = zip->openFile("writeTest.txt", ZipArchive::Read);
232         if(rdStream != NULL)
233         {
234            fail("Succeeded in opening a file for read that's already open for write");
235            ret = false;
236
237            zip->closeFile(rdStream);
238         }
239
240         zip->closeFile(wrStream);
241      }
242      else
243      {
244         fail("Could not open file for write");
245         ret = false;
246      }
247
248      zip->closeArchive();
249      delete zip;
250
251      return ret;
252   }
253};
254*/
255