sl@0
|
1 |
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <iostream>
|
sl@0
|
17 |
#include <cstdio>
|
sl@0
|
18 |
#include <fstream>
|
sl@0
|
19 |
#include <sstream>
|
sl@0
|
20 |
#include <string>
|
sl@0
|
21 |
#include <zlib.h>
|
sl@0
|
22 |
|
sl@0
|
23 |
using namespace std;
|
sl@0
|
24 |
|
sl@0
|
25 |
const unsigned int bufferSize = 1024;
|
sl@0
|
26 |
const string filePath = "..\\..\\ezlib2_testdata\\";
|
sl@0
|
27 |
|
sl@0
|
28 |
/*
|
sl@0
|
29 |
* deflateInflateTest() constants
|
sl@0
|
30 |
*/
|
sl@0
|
31 |
const int deflateInflateTestDataItems = 24;
|
sl@0
|
32 |
const string deflateInflateTestFiles[deflateInflateTestDataItems] = {"GifImage.gif", "BigSize.txt", "BitmapImage.bmp",
|
sl@0
|
33 |
"BigSize.txt", "JpegImage.jpg", "BigSize.txt",
|
sl@0
|
34 |
"SmallSize.txt", "BigSize.txt", "HolyMoley.jar",
|
sl@0
|
35 |
"BigSize.txt", "ExifHeadFile.png", "BigSize.txt",
|
sl@0
|
36 |
"GifImage.gif", "BigSize.doc", "BitmapImage.bmp",
|
sl@0
|
37 |
"BigSize.doc", "JpegImage.jpg", "BigSize.doc",
|
sl@0
|
38 |
"SmallSize.txt", "BigSize.doc", "HolyMoley.jar",
|
sl@0
|
39 |
"BigSize.doc", "ExifHeadFile.png", "BigSize.doc"};
|
sl@0
|
40 |
|
sl@0
|
41 |
const int deflateInflateTestLevel[deflateInflateTestDataItems] = {Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
|
sl@0
|
42 |
Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
|
sl@0
|
43 |
Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION,
|
sl@0
|
44 |
Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION,
|
sl@0
|
45 |
Z_NO_COMPRESSION, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION,
|
sl@0
|
46 |
Z_NO_COMPRESSION, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION,
|
sl@0
|
47 |
Z_BEST_SPEED, Z_BEST_SPEED, Z_BEST_SPEED,
|
sl@0
|
48 |
Z_BEST_SPEED, Z_BEST_SPEED, Z_BEST_SPEED};
|
sl@0
|
49 |
|
sl@0
|
50 |
const int deflateInflateTestWindowBits[deflateInflateTestDataItems] = {8, MAX_WBITS, 8,
|
sl@0
|
51 |
MAX_WBITS, 8, MAX_WBITS,
|
sl@0
|
52 |
8, 8, MAX_WBITS,
|
sl@0
|
53 |
MAX_WBITS, MAX_WBITS, 8,
|
sl@0
|
54 |
MAX_WBITS, 8, 8,
|
sl@0
|
55 |
8, MAX_WBITS, MAX_WBITS,
|
sl@0
|
56 |
8, MAX_WBITS, 8,
|
sl@0
|
57 |
MAX_WBITS, MAX_WBITS, 8};
|
sl@0
|
58 |
|
sl@0
|
59 |
const int deflateInflateTestMemLevel[deflateInflateTestDataItems] = {1, 1, 1,
|
sl@0
|
60 |
1, 1, 1,
|
sl@0
|
61 |
MAX_MEM_LEVEL, MAX_MEM_LEVEL, MAX_MEM_LEVEL,
|
sl@0
|
62 |
MAX_MEM_LEVEL, MAX_MEM_LEVEL, MAX_MEM_LEVEL,
|
sl@0
|
63 |
8, 8, 8,
|
sl@0
|
64 |
8, 8, 8,
|
sl@0
|
65 |
1, MAX_MEM_LEVEL, 8,
|
sl@0
|
66 |
1, MAX_MEM_LEVEL, 8};
|
sl@0
|
67 |
|
sl@0
|
68 |
const int deflateInflateTestStrategy[deflateInflateTestDataItems] = {Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
|
sl@0
|
69 |
Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
|
sl@0
|
70 |
Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
|
sl@0
|
71 |
Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
|
sl@0
|
72 |
Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
|
sl@0
|
73 |
Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
|
sl@0
|
74 |
Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
|
sl@0
|
75 |
Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY};
|
sl@0
|
76 |
|
sl@0
|
77 |
/*
|
sl@0
|
78 |
* inflateOldZlibFilesTest() constants
|
sl@0
|
79 |
*/
|
sl@0
|
80 |
const int inflateOldZlibFilesTestDataItems = 24;
|
sl@0
|
81 |
const string inflateOldZlibFilesTestFiles[inflateOldZlibFilesTestDataItems] = {"GifImage.gif", "BigSize.txt", "BitmapImage.bmp",
|
sl@0
|
82 |
"BigSize.txt", "JpegImage.jpg", "BigSize.txt",
|
sl@0
|
83 |
"SmallSize.txt", "BigSize.txt", "HolyMoley.jar",
|
sl@0
|
84 |
"BigSize.txt", "ExifHeadFile.png", "BigSize.txt",
|
sl@0
|
85 |
"GifImage.gif", "BigSize.doc", "BitmapImage.bmp",
|
sl@0
|
86 |
"BigSize.doc", "JpegImage.jpg", "BigSize.doc",
|
sl@0
|
87 |
"SmallSize.txt", "BigSize.doc", "HolyMoley.jar",
|
sl@0
|
88 |
"BigSize.doc", "ExifHeadFile.png", "BigSize.doc"};
|
sl@0
|
89 |
|
sl@0
|
90 |
/*
|
sl@0
|
91 |
* Global variables
|
sl@0
|
92 |
*/
|
sl@0
|
93 |
ofstream *outputFile = NULL;
|
sl@0
|
94 |
int totalPassed = 0;
|
sl@0
|
95 |
|
sl@0
|
96 |
/*
|
sl@0
|
97 |
* HELPER FUNCTION DECLARATIONS
|
sl@0
|
98 |
*/
|
sl@0
|
99 |
void closeOutputFile();
|
sl@0
|
100 |
void deleteFile(const string fileLocation);
|
sl@0
|
101 |
int deflateCompress(z_stream &aStream, ifstream &input, ofstream &output);
|
sl@0
|
102 |
bool deflateFile(const string &aInputFileName, const string &aOutputFileName, int aLevel, int aMethod, int aWindowBits, int aMemLevel, int aStrategy);
|
sl@0
|
103 |
bool doFilesMatch(const string &aFileName1, const string &aFileName2);
|
sl@0
|
104 |
unsigned int getFileLength(ifstream &file);
|
sl@0
|
105 |
int inflateDecompress(z_stream &aStream, ifstream &input, ofstream &output);
|
sl@0
|
106 |
bool inflateFile(const string &aInputFileName, const string &aOutputFileName, int aWindowBits = 15);
|
sl@0
|
107 |
bool openOutputFile(const string &aOutputFileName);
|
sl@0
|
108 |
void printTestResults();
|
sl@0
|
109 |
|
sl@0
|
110 |
/*
|
sl@0
|
111 |
* TEST FUNCTION DECLARATIONS
|
sl@0
|
112 |
*/
|
sl@0
|
113 |
bool deflateInflateTest();
|
sl@0
|
114 |
bool inflateOldZlibFilesTest();
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
/*
|
sl@0
|
118 |
* Closes the results output file and frees the memory.
|
sl@0
|
119 |
*/
|
sl@0
|
120 |
void closeOutputFile()
|
sl@0
|
121 |
{
|
sl@0
|
122 |
*outputFile << "\t</body>" << endl;
|
sl@0
|
123 |
*outputFile << "</html>" << endl;
|
sl@0
|
124 |
outputFile->close();
|
sl@0
|
125 |
|
sl@0
|
126 |
delete outputFile;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
void deleteFile(const string fileLocation)
|
sl@0
|
130 |
{
|
sl@0
|
131 |
int err = remove(fileLocation.c_str());
|
sl@0
|
132 |
if(err != 0)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
*outputFile << "\t\t" << err << " - Error deleting file: " << fileLocation << "<br />" << endl;
|
sl@0
|
135 |
cout << "Error deleting file: " << fileLocation << endl;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
}
|
sl@0
|
138 |
|
sl@0
|
139 |
/*
|
sl@0
|
140 |
* Compresses data using the deflate function.
|
sl@0
|
141 |
*/
|
sl@0
|
142 |
int deflateCompress(z_stream &aStream, ifstream &input, ofstream &output)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
int err = Z_OK;
|
sl@0
|
145 |
int flush = Z_NO_FLUSH;
|
sl@0
|
146 |
unsigned char in[bufferSize];
|
sl@0
|
147 |
unsigned char out[bufferSize];
|
sl@0
|
148 |
unsigned int fileSize = getFileLength(input);
|
sl@0
|
149 |
|
sl@0
|
150 |
do
|
sl@0
|
151 |
{
|
sl@0
|
152 |
input.read(reinterpret_cast<char *>(in), bufferSize);
|
sl@0
|
153 |
|
sl@0
|
154 |
aStream.avail_in = ((aStream.total_in + bufferSize) > fileSize) ? fileSize - aStream.total_in : bufferSize;
|
sl@0
|
155 |
aStream.next_in = in;
|
sl@0
|
156 |
|
sl@0
|
157 |
flush = input.eof() ? Z_FINISH : Z_NO_FLUSH;
|
sl@0
|
158 |
|
sl@0
|
159 |
// Call deflate() on input until output buffer not full
|
sl@0
|
160 |
// Finish compression if all of input buffer has been read in
|
sl@0
|
161 |
do
|
sl@0
|
162 |
{
|
sl@0
|
163 |
aStream.avail_out = bufferSize;
|
sl@0
|
164 |
aStream.next_out = out;
|
sl@0
|
165 |
|
sl@0
|
166 |
err = deflate(&aStream, flush);
|
sl@0
|
167 |
if(err != Z_OK && err != Z_STREAM_END)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
return err;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
output.write(reinterpret_cast<char *>(out), bufferSize - aStream.avail_out);
|
sl@0
|
172 |
|
sl@0
|
173 |
} while(aStream.avail_out == 0 && err == Z_OK);
|
sl@0
|
174 |
} while(err != Z_STREAM_END);
|
sl@0
|
175 |
|
sl@0
|
176 |
return Z_OK;
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
/*
|
sl@0
|
180 |
* Reads in the data from the input file, compresses it and creates a new output file for the compressed data
|
sl@0
|
181 |
*/
|
sl@0
|
182 |
bool deflateFile(const string &aInputFileName, const string &aOutputFileName, int aLevel, int aMethod, int aWindowBits, int aMemLevel, int aStrategy)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
int err = Z_OK;
|
sl@0
|
185 |
|
sl@0
|
186 |
ifstream input(aInputFileName.c_str(), ios::in | ios::binary);
|
sl@0
|
187 |
ofstream output(aOutputFileName.c_str(), ios::out | ios::trunc | ios::binary);
|
sl@0
|
188 |
z_stream stream;
|
sl@0
|
189 |
|
sl@0
|
190 |
if(input.is_open() && output.is_open())
|
sl@0
|
191 |
{
|
sl@0
|
192 |
stream.zalloc = Z_NULL;
|
sl@0
|
193 |
stream.zfree = Z_NULL;
|
sl@0
|
194 |
stream.opaque = Z_NULL;
|
sl@0
|
195 |
|
sl@0
|
196 |
deflateInit2(&stream, aLevel, aMethod, aWindowBits, aMemLevel, aStrategy);
|
sl@0
|
197 |
err = deflateCompress(stream, input, output);
|
sl@0
|
198 |
deflateEnd(&stream);
|
sl@0
|
199 |
|
sl@0
|
200 |
if(err != Z_OK)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
*outputFile << "\t\t" << err << " - Error deflating!<br />" << endl;
|
sl@0
|
203 |
output.close();
|
sl@0
|
204 |
input.close();
|
sl@0
|
205 |
return false;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
}
|
sl@0
|
208 |
else
|
sl@0
|
209 |
{
|
sl@0
|
210 |
if(!input.is_open())
|
sl@0
|
211 |
{
|
sl@0
|
212 |
*outputFile << "\t\tDeflate could not complete due to not being able to open the input file!<br />" << endl;
|
sl@0
|
213 |
}
|
sl@0
|
214 |
else
|
sl@0
|
215 |
{
|
sl@0
|
216 |
input.close();
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
if(!output.is_open())
|
sl@0
|
220 |
{
|
sl@0
|
221 |
*outputFile << "\t\tDeflate could not complete due to not being able to open the output file!<br />" << endl;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
else
|
sl@0
|
224 |
{
|
sl@0
|
225 |
output.close();
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
return false;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
|
sl@0
|
231 |
input.close();
|
sl@0
|
232 |
output.close();
|
sl@0
|
233 |
|
sl@0
|
234 |
*outputFile << "\t\tDeflate complete!<br />" << endl;
|
sl@0
|
235 |
return true;
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
/*
|
sl@0
|
239 |
* Checks that the two files are identical
|
sl@0
|
240 |
*/
|
sl@0
|
241 |
bool doFilesMatch(const string &aFileName1, const string &aFileName2)
|
sl@0
|
242 |
{
|
sl@0
|
243 |
ifstream file1(aFileName1.c_str(), ios::in | ios::binary);
|
sl@0
|
244 |
ifstream file2(aFileName2.c_str(), ios::in | ios::binary);
|
sl@0
|
245 |
|
sl@0
|
246 |
if(file1.is_open() && file2.is_open())
|
sl@0
|
247 |
{
|
sl@0
|
248 |
int file1Size = getFileLength(file1);
|
sl@0
|
249 |
int file2Size = getFileLength(file2);
|
sl@0
|
250 |
|
sl@0
|
251 |
if(file1Size != file2Size)
|
sl@0
|
252 |
{
|
sl@0
|
253 |
*outputFile << "\t\t" << aFileName1 << " is not the same as " << aFileName2 << " because they have different file sizes!<br />" << endl;
|
sl@0
|
254 |
return false;
|
sl@0
|
255 |
}
|
sl@0
|
256 |
else
|
sl@0
|
257 |
{
|
sl@0
|
258 |
char fileBuffer1[bufferSize];
|
sl@0
|
259 |
char fileBuffer2[bufferSize];
|
sl@0
|
260 |
|
sl@0
|
261 |
for(int totalSpaceProvided = bufferSize; !file1.eof(); totalSpaceProvided += bufferSize)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
file1.read(fileBuffer1, bufferSize);
|
sl@0
|
264 |
file2.read(fileBuffer2, bufferSize);
|
sl@0
|
265 |
|
sl@0
|
266 |
int read = (totalSpaceProvided > file1Size) ? bufferSize - (totalSpaceProvided - file1Size) : bufferSize;
|
sl@0
|
267 |
if(memcmp(fileBuffer1, fileBuffer2, read) != 0)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
*outputFile << "\t\t" << aFileName1 << " is not the same as " << aFileName2 << "<br />" << endl;
|
sl@0
|
270 |
return false;
|
sl@0
|
271 |
}
|
sl@0
|
272 |
}
|
sl@0
|
273 |
}
|
sl@0
|
274 |
}
|
sl@0
|
275 |
else
|
sl@0
|
276 |
{
|
sl@0
|
277 |
if(!file1.is_open())
|
sl@0
|
278 |
{
|
sl@0
|
279 |
*outputFile << "\t\tCould not check if files matched because " << aFileName1 << " could not be opened!<br />" << endl;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
else
|
sl@0
|
282 |
{
|
sl@0
|
283 |
file1.close();
|
sl@0
|
284 |
}
|
sl@0
|
285 |
|
sl@0
|
286 |
if(!file2.is_open())
|
sl@0
|
287 |
{
|
sl@0
|
288 |
*outputFile << "\t\tCould not check if files matched because " << aFileName2 << " could not be opened!<br />" << endl;
|
sl@0
|
289 |
}
|
sl@0
|
290 |
else
|
sl@0
|
291 |
{
|
sl@0
|
292 |
file2.close();
|
sl@0
|
293 |
}
|
sl@0
|
294 |
|
sl@0
|
295 |
return false;
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|
sl@0
|
298 |
file1.close();
|
sl@0
|
299 |
file2.close();
|
sl@0
|
300 |
|
sl@0
|
301 |
return true;
|
sl@0
|
302 |
}
|
sl@0
|
303 |
|
sl@0
|
304 |
/*
|
sl@0
|
305 |
* Returns a files size in bytes
|
sl@0
|
306 |
*/
|
sl@0
|
307 |
unsigned int getFileLength(ifstream &file)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
file.seekg (0, ios::end);
|
sl@0
|
310 |
unsigned int fileSize = file.tellg();
|
sl@0
|
311 |
file.seekg (0, ios::beg);
|
sl@0
|
312 |
|
sl@0
|
313 |
return fileSize;
|
sl@0
|
314 |
}
|
sl@0
|
315 |
|
sl@0
|
316 |
/*
|
sl@0
|
317 |
* Decompresses data using the inflate function.
|
sl@0
|
318 |
*/
|
sl@0
|
319 |
int inflateDecompress(z_stream &aStream, ifstream &input, ofstream &output)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
int err = Z_OK;
|
sl@0
|
322 |
unsigned char in[bufferSize];
|
sl@0
|
323 |
unsigned char out[bufferSize];
|
sl@0
|
324 |
unsigned int fileSize = getFileLength(input);
|
sl@0
|
325 |
|
sl@0
|
326 |
// Keep providing input data and output space for deflate()
|
sl@0
|
327 |
do
|
sl@0
|
328 |
{
|
sl@0
|
329 |
input.read(reinterpret_cast<char *>(in), bufferSize);
|
sl@0
|
330 |
|
sl@0
|
331 |
aStream.avail_in = ((aStream.total_in + bufferSize) > fileSize) ? fileSize - aStream.total_in : bufferSize;
|
sl@0
|
332 |
aStream.next_in = in;
|
sl@0
|
333 |
|
sl@0
|
334 |
// Call inflate() on input until output buffer not full
|
sl@0
|
335 |
// Finish compression if all of input buffer has been read in
|
sl@0
|
336 |
do
|
sl@0
|
337 |
{
|
sl@0
|
338 |
aStream.avail_out = bufferSize;
|
sl@0
|
339 |
aStream.next_out = out;
|
sl@0
|
340 |
|
sl@0
|
341 |
err = inflate(&aStream, Z_NO_FLUSH);
|
sl@0
|
342 |
if(err != Z_OK && err != Z_STREAM_END && err != Z_BUF_ERROR)
|
sl@0
|
343 |
{
|
sl@0
|
344 |
return err;
|
sl@0
|
345 |
}
|
sl@0
|
346 |
output.write(reinterpret_cast<char *>(out), bufferSize - aStream.avail_out);
|
sl@0
|
347 |
|
sl@0
|
348 |
} while(aStream.avail_out == 0);
|
sl@0
|
349 |
} while(err != Z_STREAM_END);
|
sl@0
|
350 |
|
sl@0
|
351 |
return Z_OK;
|
sl@0
|
352 |
}
|
sl@0
|
353 |
|
sl@0
|
354 |
/*
|
sl@0
|
355 |
* Reads in the data from the input file, decompresses it and creates a new output file for the decompressed data
|
sl@0
|
356 |
*
|
sl@0
|
357 |
* NOTE: Inflate fails when windowBits is set to 8. This is because deflateInit2() changes windowBits from 8 to 9
|
sl@0
|
358 |
* due to an error in zlib. However, inflateInit2() does not make the same change and this results in the
|
sl@0
|
359 |
* inflate failing with a Z_DATA_ERROR.
|
sl@0
|
360 |
*/
|
sl@0
|
361 |
bool inflateFile(const string &aInputFileName, const string &aOutputFileName, int aWindowBits)
|
sl@0
|
362 |
{
|
sl@0
|
363 |
int err = Z_OK;
|
sl@0
|
364 |
|
sl@0
|
365 |
ifstream input(aInputFileName.c_str(), ios::in | ios::binary);
|
sl@0
|
366 |
ofstream output(aOutputFileName.c_str(), ios::out | ios::binary | ios::trunc);
|
sl@0
|
367 |
z_stream stream;
|
sl@0
|
368 |
|
sl@0
|
369 |
if(input.is_open() && output.is_open())
|
sl@0
|
370 |
{
|
sl@0
|
371 |
stream.zalloc = Z_NULL;
|
sl@0
|
372 |
stream.zfree = Z_NULL;
|
sl@0
|
373 |
stream.opaque = Z_NULL;
|
sl@0
|
374 |
|
sl@0
|
375 |
inflateInit2(&stream, aWindowBits);
|
sl@0
|
376 |
err = inflateDecompress(stream, input, output);
|
sl@0
|
377 |
inflateEnd(&stream);
|
sl@0
|
378 |
|
sl@0
|
379 |
if(err != Z_OK)
|
sl@0
|
380 |
{
|
sl@0
|
381 |
*outputFile << "\t\t" << err << " - Error inflating!<br />" << endl;
|
sl@0
|
382 |
output.close();
|
sl@0
|
383 |
input.close();
|
sl@0
|
384 |
return false;
|
sl@0
|
385 |
}
|
sl@0
|
386 |
}
|
sl@0
|
387 |
else
|
sl@0
|
388 |
{
|
sl@0
|
389 |
if(!input.is_open())
|
sl@0
|
390 |
{
|
sl@0
|
391 |
*outputFile << "\t\tInflate could not complete due to not being able to open the input file!<br />" << endl;
|
sl@0
|
392 |
}
|
sl@0
|
393 |
else
|
sl@0
|
394 |
{
|
sl@0
|
395 |
input.close();
|
sl@0
|
396 |
}
|
sl@0
|
397 |
|
sl@0
|
398 |
if(!output.is_open())
|
sl@0
|
399 |
{
|
sl@0
|
400 |
*outputFile << "\t\tInflate could not complete due to not being able to open the output file!<br />" << endl;
|
sl@0
|
401 |
}
|
sl@0
|
402 |
else
|
sl@0
|
403 |
{
|
sl@0
|
404 |
output.close();
|
sl@0
|
405 |
}
|
sl@0
|
406 |
|
sl@0
|
407 |
return false;
|
sl@0
|
408 |
}
|
sl@0
|
409 |
|
sl@0
|
410 |
output.close();
|
sl@0
|
411 |
input.close();
|
sl@0
|
412 |
|
sl@0
|
413 |
*outputFile << "\t\tInflate complete!<br />" << endl;
|
sl@0
|
414 |
return true;
|
sl@0
|
415 |
}
|
sl@0
|
416 |
|
sl@0
|
417 |
/*
|
sl@0
|
418 |
* Creates and opens the results output file.
|
sl@0
|
419 |
*/
|
sl@0
|
420 |
bool openOutputFile(const string &aOutputFileName)
|
sl@0
|
421 |
{
|
sl@0
|
422 |
outputFile = new ofstream((aOutputFileName + ".html").c_str(), ios::out | ios::trunc);
|
sl@0
|
423 |
|
sl@0
|
424 |
if(outputFile != NULL)
|
sl@0
|
425 |
{
|
sl@0
|
426 |
*outputFile << "<html>" << endl;
|
sl@0
|
427 |
*outputFile << "\t<body>" << endl;
|
sl@0
|
428 |
|
sl@0
|
429 |
return true;
|
sl@0
|
430 |
}
|
sl@0
|
431 |
|
sl@0
|
432 |
return false;
|
sl@0
|
433 |
}
|
sl@0
|
434 |
|
sl@0
|
435 |
/*
|
sl@0
|
436 |
* Outputs the test results to the results output file.
|
sl@0
|
437 |
*/
|
sl@0
|
438 |
void printTestResults()
|
sl@0
|
439 |
{
|
sl@0
|
440 |
int totalFailed = deflateInflateTestDataItems + inflateOldZlibFilesTestDataItems - totalPassed;
|
sl@0
|
441 |
|
sl@0
|
442 |
*outputFile << "\t\tTEST RESULTS:<br />" << endl;
|
sl@0
|
443 |
*outputFile << "\t\t<font color=00AF00>Passed = " << totalPassed << "</font><br />" << endl;
|
sl@0
|
444 |
*outputFile << "\t\t<font color=FF0000>Failed = " << totalFailed << "</font><br />" << endl;
|
sl@0
|
445 |
}
|
sl@0
|
446 |
|
sl@0
|
447 |
/**
|
sl@0
|
448 |
@SYMTestCaseID SYSLIB-EZLIB2-CT-4312
|
sl@0
|
449 |
@SYMTestCaseDesc Check files can be deflated and inflated.
|
sl@0
|
450 |
@SYMTestPriority High
|
sl@0
|
451 |
@SYMTestActions 1. Open the input file for reading and create a compressed output
|
sl@0
|
452 |
file for writing.
|
sl@0
|
453 |
2. Create a deflate stream and initialise it using deflateInit2()
|
sl@0
|
454 |
passing it the specified compression parameters.
|
sl@0
|
455 |
3. Deflate the input using deflate(), writing the deflated data
|
sl@0
|
456 |
to the compressed output file.
|
sl@0
|
457 |
4. Cleanup the deflate stream and close the input and compressed
|
sl@0
|
458 |
output files.
|
sl@0
|
459 |
5. Open the compressed file for reading and create a decompressed
|
sl@0
|
460 |
output file for writing
|
sl@0
|
461 |
6. Create an inflate stream and initialise it using inflateInit2().
|
sl@0
|
462 |
7. Inflate the input using inflate(), writing the inflated data to
|
sl@0
|
463 |
the decompressed output file.
|
sl@0
|
464 |
8. Cleanup the inflate stream and close the compressed input and
|
sl@0
|
465 |
decompressed output files.
|
sl@0
|
466 |
9. Open the original file (that was compressed) and the
|
sl@0
|
467 |
decompressed file for reading.
|
sl@0
|
468 |
10. Compare the contents of each file using memcmp().
|
sl@0
|
469 |
11. Close both the files.
|
sl@0
|
470 |
12. Cleanup any files created during the test.
|
sl@0
|
471 |
|
sl@0
|
472 |
Note: The test should be repeated for different types of input
|
sl@0
|
473 |
files as well as different size of input file e.g.:
|
sl@0
|
474 |
• TXT
|
sl@0
|
475 |
• PNG
|
sl@0
|
476 |
• JAR
|
sl@0
|
477 |
• JPG
|
sl@0
|
478 |
• BMP
|
sl@0
|
479 |
• DOC
|
sl@0
|
480 |
• GIF
|
sl@0
|
481 |
• Varying sizes of each input file should be tested starting
|
sl@0
|
482 |
from a couple of kilobytes up to several megabytes.
|
sl@0
|
483 |
|
sl@0
|
484 |
And with compression parameters as:
|
sl@0
|
485 |
• Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION
|
sl@0
|
486 |
and Z_BEST_SPEED for level
|
sl@0
|
487 |
• 8 and MAX_WBITS for windowBits
|
sl@0
|
488 |
• 1, 8 (default memory level) and MAX_MEM_LEVEL for memLevel
|
sl@0
|
489 |
• Z_DEFAULT_STRATEGY, Z_FILTERED and Z_HUFFMAN_ONLY for strategy
|
sl@0
|
490 |
@SYMTestExpectedResults The input file used for deflating should be identical to the
|
sl@0
|
491 |
inflated output file.
|
sl@0
|
492 |
*/
|
sl@0
|
493 |
bool deflateInflateTest()
|
sl@0
|
494 |
{
|
sl@0
|
495 |
bool passed = true;
|
sl@0
|
496 |
|
sl@0
|
497 |
cout << endl << "Starting test case deflateInflateTest:" << endl;
|
sl@0
|
498 |
for(int i = 0; i < deflateInflateTestDataItems ; i++)
|
sl@0
|
499 |
{
|
sl@0
|
500 |
stringstream ss;
|
sl@0
|
501 |
string testNum;
|
sl@0
|
502 |
ss << (i + 1);
|
sl@0
|
503 |
ss >> testNum;
|
sl@0
|
504 |
|
sl@0
|
505 |
cout << "Starting test " << testNum << " - File: " << deflateInflateTestFiles[i];
|
sl@0
|
506 |
|
sl@0
|
507 |
int extBegin = deflateInflateTestFiles[i].find_last_of(".", deflateInflateTestFiles[i].length());
|
sl@0
|
508 |
string fileName = deflateInflateTestFiles[i].substr(0, extBegin);
|
sl@0
|
509 |
string fileExt = deflateInflateTestFiles[i].substr(extBegin + 1);
|
sl@0
|
510 |
|
sl@0
|
511 |
string uncompressedFileName(filePath + deflateInflateTestFiles[i]);
|
sl@0
|
512 |
string compressedFileName(filePath + fileName + testNum + ".zip");
|
sl@0
|
513 |
string decompressedFileName(filePath + "ezlib2_" + fileName + testNum + "_decompressed" + "." + fileExt);
|
sl@0
|
514 |
|
sl@0
|
515 |
if(deflateFile(uncompressedFileName, compressedFileName, deflateInflateTestLevel[i], Z_DEFLATED, deflateInflateTestWindowBits[i], deflateInflateTestMemLevel[i], deflateInflateTestStrategy[i]) == false)
|
sl@0
|
516 |
{
|
sl@0
|
517 |
passed = false;
|
sl@0
|
518 |
*outputFile << "\t\t<font color=FF0000>FAILED for file: " << deflateInflateTestFiles[i] << "</font><p />" << endl << endl;
|
sl@0
|
519 |
cout << " - FAILED" << endl;
|
sl@0
|
520 |
continue;
|
sl@0
|
521 |
}
|
sl@0
|
522 |
|
sl@0
|
523 |
if(inflateFile(compressedFileName, decompressedFileName, deflateInflateTestWindowBits[i]) == false)
|
sl@0
|
524 |
{
|
sl@0
|
525 |
passed = false;
|
sl@0
|
526 |
*outputFile << "\t\t<font color=FF0000>FAILED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
527 |
cout << " - FAILED" << endl;
|
sl@0
|
528 |
|
sl@0
|
529 |
deleteFile(compressedFileName);
|
sl@0
|
530 |
deleteFile(decompressedFileName);
|
sl@0
|
531 |
continue;
|
sl@0
|
532 |
}
|
sl@0
|
533 |
|
sl@0
|
534 |
if(doFilesMatch(uncompressedFileName, decompressedFileName) == false)
|
sl@0
|
535 |
{
|
sl@0
|
536 |
passed = false;
|
sl@0
|
537 |
*outputFile << "\t\t<font color=FF0000>FAILED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
538 |
cout << " - FAILED" << endl;
|
sl@0
|
539 |
|
sl@0
|
540 |
deleteFile(compressedFileName);
|
sl@0
|
541 |
deleteFile(decompressedFileName);
|
sl@0
|
542 |
continue;
|
sl@0
|
543 |
}
|
sl@0
|
544 |
|
sl@0
|
545 |
totalPassed++;
|
sl@0
|
546 |
*outputFile << "\t\t<font color=00AF00>PASSED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
547 |
cout << " - PASSED" << endl;
|
sl@0
|
548 |
|
sl@0
|
549 |
deleteFile(compressedFileName);
|
sl@0
|
550 |
deleteFile(decompressedFileName);
|
sl@0
|
551 |
}
|
sl@0
|
552 |
cout << "Finished test case case deflateInflateTest." << endl;
|
sl@0
|
553 |
|
sl@0
|
554 |
return passed;
|
sl@0
|
555 |
}
|
sl@0
|
556 |
|
sl@0
|
557 |
/**
|
sl@0
|
558 |
@SYMTestCaseID SYSLIB-EZLIB2-CT-4313
|
sl@0
|
559 |
@SYMTestCaseDesc Check files deflated with EZlib can be inflated using EZlib2.
|
sl@0
|
560 |
@SYMTestPriority High
|
sl@0
|
561 |
@SYMTestActions 1. Open the compressed input file, which was compressed using EZlib,
|
sl@0
|
562 |
for reading and create a decompressed output file for writing
|
sl@0
|
563 |
2. Create an inflate stream and initialise it using inflateInit2().
|
sl@0
|
564 |
3. Inflate the input using inflate(), writing the inflated data
|
sl@0
|
565 |
to the decompressed output file.
|
sl@0
|
566 |
4. Cleanup the inflate stream and close the compressed input and
|
sl@0
|
567 |
decompressed output files.
|
sl@0
|
568 |
5. Open the compressed input file and the decompressed output
|
sl@0
|
569 |
file for reading.
|
sl@0
|
570 |
6. Compare the contents of each file using memcmp().
|
sl@0
|
571 |
7. Close both the files.
|
sl@0
|
572 |
8. Cleanup any files created during the test.
|
sl@0
|
573 |
|
sl@0
|
574 |
Note: The test should be repeated for different types of files as
|
sl@0
|
575 |
well as different size of file e.g.:
|
sl@0
|
576 |
• TXT
|
sl@0
|
577 |
• PNG
|
sl@0
|
578 |
• JAR
|
sl@0
|
579 |
• JPG
|
sl@0
|
580 |
• BMP
|
sl@0
|
581 |
• DOC
|
sl@0
|
582 |
• GIF
|
sl@0
|
583 |
• Varying sizes of each input file should be tested starting
|
sl@0
|
584 |
from a couple of kilobytes up to several megabytes.
|
sl@0
|
585 |
|
sl@0
|
586 |
@SYMTestExpectedResults The input file used for deflating should be identical to the
|
sl@0
|
587 |
inflated output file.
|
sl@0
|
588 |
*/
|
sl@0
|
589 |
bool inflateOldZlibFilesTest()
|
sl@0
|
590 |
{
|
sl@0
|
591 |
bool passed = true;
|
sl@0
|
592 |
|
sl@0
|
593 |
cout << endl << "Starting test case inflateOldZlibFilesTest:" << endl;
|
sl@0
|
594 |
for(int i = 0; i < inflateOldZlibFilesTestDataItems ; i++)
|
sl@0
|
595 |
{
|
sl@0
|
596 |
stringstream ss;
|
sl@0
|
597 |
string testNum;
|
sl@0
|
598 |
ss << (i + 1);
|
sl@0
|
599 |
ss >> testNum;
|
sl@0
|
600 |
|
sl@0
|
601 |
cout << "Starting test " << testNum << " - File: " << inflateOldZlibFilesTestFiles[i];
|
sl@0
|
602 |
|
sl@0
|
603 |
int extBegin = inflateOldZlibFilesTestFiles[i].find_last_of(".", inflateOldZlibFilesTestFiles[i].length());
|
sl@0
|
604 |
string fileName = inflateOldZlibFilesTestFiles[i].substr(0, extBegin);
|
sl@0
|
605 |
string fileExt = inflateOldZlibFilesTestFiles[i].substr(extBegin + 1);
|
sl@0
|
606 |
|
sl@0
|
607 |
string uncompressedFileName(filePath + inflateOldZlibFilesTestFiles[i]);
|
sl@0
|
608 |
string compressedFileName(filePath + "ezlib_" + fileName + testNum + ".zip");
|
sl@0
|
609 |
string decompressedFileName(filePath + "ezlib_" + fileName + testNum + "_decompressed" + "." + fileExt);
|
sl@0
|
610 |
|
sl@0
|
611 |
if(inflateFile(compressedFileName, decompressedFileName, 15) == false)
|
sl@0
|
612 |
{
|
sl@0
|
613 |
passed = false;
|
sl@0
|
614 |
*outputFile << "\t\t<font color=FF0000>FAILED for file: " << compressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
615 |
cout << " - FAILED" << endl;
|
sl@0
|
616 |
|
sl@0
|
617 |
deleteFile(decompressedFileName);
|
sl@0
|
618 |
continue;
|
sl@0
|
619 |
}
|
sl@0
|
620 |
|
sl@0
|
621 |
if(doFilesMatch(uncompressedFileName, decompressedFileName) == false)
|
sl@0
|
622 |
{
|
sl@0
|
623 |
passed = false;
|
sl@0
|
624 |
*outputFile << "\t\t<font color=FF0000>FAILED for file: " << compressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
625 |
cout << " - FAILED" << endl;
|
sl@0
|
626 |
|
sl@0
|
627 |
deleteFile(decompressedFileName);
|
sl@0
|
628 |
continue;
|
sl@0
|
629 |
}
|
sl@0
|
630 |
|
sl@0
|
631 |
totalPassed++;
|
sl@0
|
632 |
*outputFile << "\t\t<font color=00AF00>PASSED for file: " << compressedFileName << "</font><p />" << endl << endl;
|
sl@0
|
633 |
cout << " - PASSED" << endl;
|
sl@0
|
634 |
|
sl@0
|
635 |
deleteFile(decompressedFileName);
|
sl@0
|
636 |
}
|
sl@0
|
637 |
cout << "Finished test case inflateOldZlibFilesTest." << endl;
|
sl@0
|
638 |
|
sl@0
|
639 |
return passed;
|
sl@0
|
640 |
}
|
sl@0
|
641 |
|
sl@0
|
642 |
int main()
|
sl@0
|
643 |
{
|
sl@0
|
644 |
if(openOutputFile("basicfunctest"))
|
sl@0
|
645 |
{
|
sl@0
|
646 |
*outputFile << "\t\t<b>START TEST deflateInflateTest()</b><br />" << endl;
|
sl@0
|
647 |
if(deflateInflateTest() == false)
|
sl@0
|
648 |
{
|
sl@0
|
649 |
*outputFile << "\t\t<font color=FF0000>deflateInflateTest() FAILED!</font><br />" << endl;
|
sl@0
|
650 |
}
|
sl@0
|
651 |
else
|
sl@0
|
652 |
{
|
sl@0
|
653 |
*outputFile << "\t\t<font color=00AF00>deflateInflateTest() PASSED!</font><br />" << endl;
|
sl@0
|
654 |
}
|
sl@0
|
655 |
*outputFile << "\t\t<b>END TEST deflateInflateTest()</b><p />" << endl << endl;;
|
sl@0
|
656 |
|
sl@0
|
657 |
*outputFile << "\t\t<b>START TEST inflateOldZlibFilesTest()</b><br />" << endl;
|
sl@0
|
658 |
if(inflateOldZlibFilesTest() == false)
|
sl@0
|
659 |
{
|
sl@0
|
660 |
*outputFile << "\t\t<font color=FF0000>inflateOldZlibFilesTest() FAILED!</font><br />" << endl;
|
sl@0
|
661 |
}
|
sl@0
|
662 |
else
|
sl@0
|
663 |
{
|
sl@0
|
664 |
*outputFile << "\t\t<font color=00AF00>inflateOldZlibFilesTest() PASSED!</font><br />" << endl;
|
sl@0
|
665 |
}
|
sl@0
|
666 |
*outputFile << "\t\t<b>END TEST inflateOldZlibFilesTest()</b><p />" << endl << endl;
|
sl@0
|
667 |
|
sl@0
|
668 |
printTestResults();
|
sl@0
|
669 |
|
sl@0
|
670 |
closeOutputFile();
|
sl@0
|
671 |
}
|
sl@0
|
672 |
|
sl@0
|
673 |
return 0;
|
sl@0
|
674 |
}
|