Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
25 const unsigned int bufferSize = 1024;
26 const string filePath = "..\\..\\ezlib2_testdata\\";
29 * deflateInflateTest() constants
31 const int deflateInflateTestDataItems = 24;
32 const string deflateInflateTestFiles[deflateInflateTestDataItems] = {"GifImage.gif", "BigSize.txt", "BitmapImage.bmp",
33 "BigSize.txt", "JpegImage.jpg", "BigSize.txt",
34 "SmallSize.txt", "BigSize.txt", "HolyMoley.jar",
35 "BigSize.txt", "ExifHeadFile.png", "BigSize.txt",
36 "GifImage.gif", "BigSize.doc", "BitmapImage.bmp",
37 "BigSize.doc", "JpegImage.jpg", "BigSize.doc",
38 "SmallSize.txt", "BigSize.doc", "HolyMoley.jar",
39 "BigSize.doc", "ExifHeadFile.png", "BigSize.doc"};
41 const int deflateInflateTestLevel[deflateInflateTestDataItems] = {Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
42 Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
43 Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION,
44 Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION,
45 Z_NO_COMPRESSION, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION,
46 Z_NO_COMPRESSION, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION,
47 Z_BEST_SPEED, Z_BEST_SPEED, Z_BEST_SPEED,
48 Z_BEST_SPEED, Z_BEST_SPEED, Z_BEST_SPEED};
50 const int deflateInflateTestWindowBits[deflateInflateTestDataItems] = {8, MAX_WBITS, 8,
51 MAX_WBITS, 8, MAX_WBITS,
53 MAX_WBITS, MAX_WBITS, 8,
55 8, MAX_WBITS, MAX_WBITS,
57 MAX_WBITS, MAX_WBITS, 8};
59 const int deflateInflateTestMemLevel[deflateInflateTestDataItems] = {1, 1, 1,
61 MAX_MEM_LEVEL, MAX_MEM_LEVEL, MAX_MEM_LEVEL,
62 MAX_MEM_LEVEL, MAX_MEM_LEVEL, MAX_MEM_LEVEL,
68 const int deflateInflateTestStrategy[deflateInflateTestDataItems] = {Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
69 Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
70 Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
71 Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
72 Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
73 Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY,
74 Z_DEFAULT_STRATEGY, Z_DEFAULT_STRATEGY, Z_FILTERED,
75 Z_FILTERED, Z_HUFFMAN_ONLY, Z_HUFFMAN_ONLY};
78 * inflateOldZlibFilesTest() constants
80 const int inflateOldZlibFilesTestDataItems = 24;
81 const string inflateOldZlibFilesTestFiles[inflateOldZlibFilesTestDataItems] = {"GifImage.gif", "BigSize.txt", "BitmapImage.bmp",
82 "BigSize.txt", "JpegImage.jpg", "BigSize.txt",
83 "SmallSize.txt", "BigSize.txt", "HolyMoley.jar",
84 "BigSize.txt", "ExifHeadFile.png", "BigSize.txt",
85 "GifImage.gif", "BigSize.doc", "BitmapImage.bmp",
86 "BigSize.doc", "JpegImage.jpg", "BigSize.doc",
87 "SmallSize.txt", "BigSize.doc", "HolyMoley.jar",
88 "BigSize.doc", "ExifHeadFile.png", "BigSize.doc"};
93 ofstream *outputFile = NULL;
97 * HELPER FUNCTION DECLARATIONS
99 void closeOutputFile();
100 void deleteFile(const string fileLocation);
101 int deflateCompress(z_stream &aStream, ifstream &input, ofstream &output);
102 bool deflateFile(const string &aInputFileName, const string &aOutputFileName, int aLevel, int aMethod, int aWindowBits, int aMemLevel, int aStrategy);
103 bool doFilesMatch(const string &aFileName1, const string &aFileName2);
104 unsigned int getFileLength(ifstream &file);
105 int inflateDecompress(z_stream &aStream, ifstream &input, ofstream &output);
106 bool inflateFile(const string &aInputFileName, const string &aOutputFileName, int aWindowBits = 15);
107 bool openOutputFile(const string &aOutputFileName);
108 void printTestResults();
111 * TEST FUNCTION DECLARATIONS
113 bool deflateInflateTest();
114 bool inflateOldZlibFilesTest();
118 * Closes the results output file and frees the memory.
120 void closeOutputFile()
122 *outputFile << "\t</body>" << endl;
123 *outputFile << "</html>" << endl;
129 void deleteFile(const string fileLocation)
131 int err = remove(fileLocation.c_str());
134 *outputFile << "\t\t" << err << " - Error deleting file: " << fileLocation << "<br />" << endl;
135 cout << "Error deleting file: " << fileLocation << endl;
140 * Compresses data using the deflate function.
142 int deflateCompress(z_stream &aStream, ifstream &input, ofstream &output)
145 int flush = Z_NO_FLUSH;
146 unsigned char in[bufferSize];
147 unsigned char out[bufferSize];
148 unsigned int fileSize = getFileLength(input);
152 input.read(reinterpret_cast<char *>(in), bufferSize);
154 aStream.avail_in = ((aStream.total_in + bufferSize) > fileSize) ? fileSize - aStream.total_in : bufferSize;
155 aStream.next_in = in;
157 flush = input.eof() ? Z_FINISH : Z_NO_FLUSH;
159 // Call deflate() on input until output buffer not full
160 // Finish compression if all of input buffer has been read in
163 aStream.avail_out = bufferSize;
164 aStream.next_out = out;
166 err = deflate(&aStream, flush);
167 if(err != Z_OK && err != Z_STREAM_END)
171 output.write(reinterpret_cast<char *>(out), bufferSize - aStream.avail_out);
173 } while(aStream.avail_out == 0 && err == Z_OK);
174 } while(err != Z_STREAM_END);
180 * Reads in the data from the input file, compresses it and creates a new output file for the compressed data
182 bool deflateFile(const string &aInputFileName, const string &aOutputFileName, int aLevel, int aMethod, int aWindowBits, int aMemLevel, int aStrategy)
186 ifstream input(aInputFileName.c_str(), ios::in | ios::binary);
187 ofstream output(aOutputFileName.c_str(), ios::out | ios::trunc | ios::binary);
190 if(input.is_open() && output.is_open())
192 stream.zalloc = Z_NULL;
193 stream.zfree = Z_NULL;
194 stream.opaque = Z_NULL;
196 deflateInit2(&stream, aLevel, aMethod, aWindowBits, aMemLevel, aStrategy);
197 err = deflateCompress(stream, input, output);
202 *outputFile << "\t\t" << err << " - Error deflating!<br />" << endl;
212 *outputFile << "\t\tDeflate could not complete due to not being able to open the input file!<br />" << endl;
219 if(!output.is_open())
221 *outputFile << "\t\tDeflate could not complete due to not being able to open the output file!<br />" << endl;
234 *outputFile << "\t\tDeflate complete!<br />" << endl;
239 * Checks that the two files are identical
241 bool doFilesMatch(const string &aFileName1, const string &aFileName2)
243 ifstream file1(aFileName1.c_str(), ios::in | ios::binary);
244 ifstream file2(aFileName2.c_str(), ios::in | ios::binary);
246 if(file1.is_open() && file2.is_open())
248 int file1Size = getFileLength(file1);
249 int file2Size = getFileLength(file2);
251 if(file1Size != file2Size)
253 *outputFile << "\t\t" << aFileName1 << " is not the same as " << aFileName2 << " because they have different file sizes!<br />" << endl;
258 char fileBuffer1[bufferSize];
259 char fileBuffer2[bufferSize];
261 for(int totalSpaceProvided = bufferSize; !file1.eof(); totalSpaceProvided += bufferSize)
263 file1.read(fileBuffer1, bufferSize);
264 file2.read(fileBuffer2, bufferSize);
266 int read = (totalSpaceProvided > file1Size) ? bufferSize - (totalSpaceProvided - file1Size) : bufferSize;
267 if(memcmp(fileBuffer1, fileBuffer2, read) != 0)
269 *outputFile << "\t\t" << aFileName1 << " is not the same as " << aFileName2 << "<br />" << endl;
279 *outputFile << "\t\tCould not check if files matched because " << aFileName1 << " could not be opened!<br />" << endl;
288 *outputFile << "\t\tCould not check if files matched because " << aFileName2 << " could not be opened!<br />" << endl;
305 * Returns a files size in bytes
307 unsigned int getFileLength(ifstream &file)
309 file.seekg (0, ios::end);
310 unsigned int fileSize = file.tellg();
311 file.seekg (0, ios::beg);
317 * Decompresses data using the inflate function.
319 int inflateDecompress(z_stream &aStream, ifstream &input, ofstream &output)
322 unsigned char in[bufferSize];
323 unsigned char out[bufferSize];
324 unsigned int fileSize = getFileLength(input);
326 // Keep providing input data and output space for deflate()
329 input.read(reinterpret_cast<char *>(in), bufferSize);
331 aStream.avail_in = ((aStream.total_in + bufferSize) > fileSize) ? fileSize - aStream.total_in : bufferSize;
332 aStream.next_in = in;
334 // Call inflate() on input until output buffer not full
335 // Finish compression if all of input buffer has been read in
338 aStream.avail_out = bufferSize;
339 aStream.next_out = out;
341 err = inflate(&aStream, Z_NO_FLUSH);
342 if(err != Z_OK && err != Z_STREAM_END && err != Z_BUF_ERROR)
346 output.write(reinterpret_cast<char *>(out), bufferSize - aStream.avail_out);
348 } while(aStream.avail_out == 0);
349 } while(err != Z_STREAM_END);
355 * Reads in the data from the input file, decompresses it and creates a new output file for the decompressed data
357 * NOTE: Inflate fails when windowBits is set to 8. This is because deflateInit2() changes windowBits from 8 to 9
358 * due to an error in zlib. However, inflateInit2() does not make the same change and this results in the
359 * inflate failing with a Z_DATA_ERROR.
361 bool inflateFile(const string &aInputFileName, const string &aOutputFileName, int aWindowBits)
365 ifstream input(aInputFileName.c_str(), ios::in | ios::binary);
366 ofstream output(aOutputFileName.c_str(), ios::out | ios::binary | ios::trunc);
369 if(input.is_open() && output.is_open())
371 stream.zalloc = Z_NULL;
372 stream.zfree = Z_NULL;
373 stream.opaque = Z_NULL;
375 inflateInit2(&stream, aWindowBits);
376 err = inflateDecompress(stream, input, output);
381 *outputFile << "\t\t" << err << " - Error inflating!<br />" << endl;
391 *outputFile << "\t\tInflate could not complete due to not being able to open the input file!<br />" << endl;
398 if(!output.is_open())
400 *outputFile << "\t\tInflate could not complete due to not being able to open the output file!<br />" << endl;
413 *outputFile << "\t\tInflate complete!<br />" << endl;
418 * Creates and opens the results output file.
420 bool openOutputFile(const string &aOutputFileName)
422 outputFile = new ofstream((aOutputFileName + ".html").c_str(), ios::out | ios::trunc);
424 if(outputFile != NULL)
426 *outputFile << "<html>" << endl;
427 *outputFile << "\t<body>" << endl;
436 * Outputs the test results to the results output file.
438 void printTestResults()
440 int totalFailed = deflateInflateTestDataItems + inflateOldZlibFilesTestDataItems - totalPassed;
442 *outputFile << "\t\tTEST RESULTS:<br />" << endl;
443 *outputFile << "\t\t<font color=00AF00>Passed = " << totalPassed << "</font><br />" << endl;
444 *outputFile << "\t\t<font color=FF0000>Failed = " << totalFailed << "</font><br />" << endl;
448 @SYMTestCaseID SYSLIB-EZLIB2-CT-4312
449 @SYMTestCaseDesc Check files can be deflated and inflated.
450 @SYMTestPriority High
451 @SYMTestActions 1. Open the input file for reading and create a compressed output
453 2. Create a deflate stream and initialise it using deflateInit2()
454 passing it the specified compression parameters.
455 3. Deflate the input using deflate(), writing the deflated data
456 to the compressed output file.
457 4. Cleanup the deflate stream and close the input and compressed
459 5. Open the compressed file for reading and create a decompressed
460 output file for writing
461 6. Create an inflate stream and initialise it using inflateInit2().
462 7. Inflate the input using inflate(), writing the inflated data to
463 the decompressed output file.
464 8. Cleanup the inflate stream and close the compressed input and
465 decompressed output files.
466 9. Open the original file (that was compressed) and the
467 decompressed file for reading.
468 10. Compare the contents of each file using memcmp().
469 11. Close both the files.
470 12. Cleanup any files created during the test.
472 Note: The test should be repeated for different types of input
473 files as well as different size of input file e.g.:
481 • Varying sizes of each input file should be tested starting
482 from a couple of kilobytes up to several megabytes.
484 And with compression parameters as:
485 • Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION
486 and Z_BEST_SPEED for level
487 • 8 and MAX_WBITS for windowBits
488 • 1, 8 (default memory level) and MAX_MEM_LEVEL for memLevel
489 • Z_DEFAULT_STRATEGY, Z_FILTERED and Z_HUFFMAN_ONLY for strategy
490 @SYMTestExpectedResults The input file used for deflating should be identical to the
491 inflated output file.
493 bool deflateInflateTest()
497 cout << endl << "Starting test case deflateInflateTest:" << endl;
498 for(int i = 0; i < deflateInflateTestDataItems ; i++)
505 cout << "Starting test " << testNum << " - File: " << deflateInflateTestFiles[i];
507 int extBegin = deflateInflateTestFiles[i].find_last_of(".", deflateInflateTestFiles[i].length());
508 string fileName = deflateInflateTestFiles[i].substr(0, extBegin);
509 string fileExt = deflateInflateTestFiles[i].substr(extBegin + 1);
511 string uncompressedFileName(filePath + deflateInflateTestFiles[i]);
512 string compressedFileName(filePath + fileName + testNum + ".zip");
513 string decompressedFileName(filePath + "ezlib2_" + fileName + testNum + "_decompressed" + "." + fileExt);
515 if(deflateFile(uncompressedFileName, compressedFileName, deflateInflateTestLevel[i], Z_DEFLATED, deflateInflateTestWindowBits[i], deflateInflateTestMemLevel[i], deflateInflateTestStrategy[i]) == false)
518 *outputFile << "\t\t<font color=FF0000>FAILED for file: " << deflateInflateTestFiles[i] << "</font><p />" << endl << endl;
519 cout << " - FAILED" << endl;
523 if(inflateFile(compressedFileName, decompressedFileName, deflateInflateTestWindowBits[i]) == false)
526 *outputFile << "\t\t<font color=FF0000>FAILED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
527 cout << " - FAILED" << endl;
529 deleteFile(compressedFileName);
530 deleteFile(decompressedFileName);
534 if(doFilesMatch(uncompressedFileName, decompressedFileName) == false)
537 *outputFile << "\t\t<font color=FF0000>FAILED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
538 cout << " - FAILED" << endl;
540 deleteFile(compressedFileName);
541 deleteFile(decompressedFileName);
546 *outputFile << "\t\t<font color=00AF00>PASSED for file: " << uncompressedFileName << "</font><p />" << endl << endl;
547 cout << " - PASSED" << endl;
549 deleteFile(compressedFileName);
550 deleteFile(decompressedFileName);
552 cout << "Finished test case case deflateInflateTest." << endl;
558 @SYMTestCaseID SYSLIB-EZLIB2-CT-4313
559 @SYMTestCaseDesc Check files deflated with EZlib can be inflated using EZlib2.
560 @SYMTestPriority High
561 @SYMTestActions 1. Open the compressed input file, which was compressed using EZlib,
562 for reading and create a decompressed output file for writing
563 2. Create an inflate stream and initialise it using inflateInit2().
564 3. Inflate the input using inflate(), writing the inflated data
565 to the decompressed output file.
566 4. Cleanup the inflate stream and close the compressed input and
567 decompressed output files.
568 5. Open the compressed input file and the decompressed output
570 6. Compare the contents of each file using memcmp().
571 7. Close both the files.
572 8. Cleanup any files created during the test.
574 Note: The test should be repeated for different types of files as
575 well as different size of file e.g.:
583 • Varying sizes of each input file should be tested starting
584 from a couple of kilobytes up to several megabytes.
586 @SYMTestExpectedResults The input file used for deflating should be identical to the
587 inflated output file.
589 bool inflateOldZlibFilesTest()
593 cout << endl << "Starting test case inflateOldZlibFilesTest:" << endl;
594 for(int i = 0; i < inflateOldZlibFilesTestDataItems ; i++)
601 cout << "Starting test " << testNum << " - File: " << inflateOldZlibFilesTestFiles[i];
603 int extBegin = inflateOldZlibFilesTestFiles[i].find_last_of(".", inflateOldZlibFilesTestFiles[i].length());
604 string fileName = inflateOldZlibFilesTestFiles[i].substr(0, extBegin);
605 string fileExt = inflateOldZlibFilesTestFiles[i].substr(extBegin + 1);
607 string uncompressedFileName(filePath + inflateOldZlibFilesTestFiles[i]);
608 string compressedFileName(filePath + "ezlib_" + fileName + testNum + ".zip");
609 string decompressedFileName(filePath + "ezlib_" + fileName + testNum + "_decompressed" + "." + fileExt);
611 if(inflateFile(compressedFileName, decompressedFileName, 15) == false)
614 *outputFile << "\t\t<font color=FF0000>FAILED for file: " << compressedFileName << "</font><p />" << endl << endl;
615 cout << " - FAILED" << endl;
617 deleteFile(decompressedFileName);
621 if(doFilesMatch(uncompressedFileName, decompressedFileName) == false)
624 *outputFile << "\t\t<font color=FF0000>FAILED for file: " << compressedFileName << "</font><p />" << endl << endl;
625 cout << " - FAILED" << endl;
627 deleteFile(decompressedFileName);
632 *outputFile << "\t\t<font color=00AF00>PASSED for file: " << compressedFileName << "</font><p />" << endl << endl;
633 cout << " - PASSED" << endl;
635 deleteFile(decompressedFileName);
637 cout << "Finished test case inflateOldZlibFilesTest." << endl;
644 if(openOutputFile("basicfunctest"))
646 *outputFile << "\t\t<b>START TEST deflateInflateTest()</b><br />" << endl;
647 if(deflateInflateTest() == false)
649 *outputFile << "\t\t<font color=FF0000>deflateInflateTest() FAILED!</font><br />" << endl;
653 *outputFile << "\t\t<font color=00AF00>deflateInflateTest() PASSED!</font><br />" << endl;
655 *outputFile << "\t\t<b>END TEST deflateInflateTest()</b><p />" << endl << endl;;
657 *outputFile << "\t\t<b>START TEST inflateOldZlibFilesTest()</b><br />" << endl;
658 if(inflateOldZlibFilesTest() == false)
660 *outputFile << "\t\t<font color=FF0000>inflateOldZlibFilesTest() FAILED!</font><br />" << endl;
664 *outputFile << "\t\t<font color=00AF00>inflateOldZlibFilesTest() PASSED!</font><br />" << endl;
666 *outputFile << "\t\t<b>END TEST inflateOldZlibFilesTest()</b><p />" << endl << endl;