sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Name : tzlibadvanced.cpp sl@0: // sl@0: // sl@0: sl@0: #include "tzlib.h" sl@0: #include "zutil.h" sl@0: //#include "deflate.h" sl@0: sl@0: #define COMPRESSED_DATA_BUFFER_LENGTH 200*sizeof(int) sl@0: #define FAILURE_RATE_LIMIT 100 sl@0: #define TEST_MID_MEM_LEVEL 5 sl@0: sl@0: // Add this to the windowBits value to use a GZip header instead of a Zlib header sl@0: #define TEST_GZIP_HEADER 16 sl@0: sl@0: // Add this to the windowBits when calling inflateInit2() to use automatic header detection sl@0: #define TEST_AUTO_HEADER_DETECTION 32 sl@0: sl@0: #define CHECK_CONDITION0L(condition, leaveErr, errMessage) \ sl@0: if (condition) \ sl@0: { \ sl@0: ERR_PRINTF1(_L(errMessage)); \ sl@0: User::Leave(leaveErr); \ sl@0: } sl@0: sl@0: #define CHECK_CONDITION1L(condition, leaveErr, errMessage, p1) \ sl@0: if (condition) \ sl@0: { \ sl@0: ERR_PRINTF2(_L(errMessage), p1); \ sl@0: User::Leave(leaveErr); \ sl@0: } sl@0: sl@0: _LIT(KParam1, "Param1"); sl@0: _LIT(KParam2, "Param2"); sl@0: _LIT(KParam3, "Param3"); sl@0: _LIT(KParam4, "Param4"); sl@0: sl@0: // bufferDescriptor holds extra information required by in_func and out_func to perform their jobs sl@0: struct bufferDescriptor sl@0: { sl@0: Byte *buffer; sl@0: int next; sl@0: int length; sl@0: }; sl@0: sl@0: /* sl@0: * Helper member function that sets up the deflate stream for use. sl@0: */ sl@0: void CTestZlib::DeflateInitL(z_stream &aStream, const int aLevel, const int expectedResult) sl@0: { sl@0: TInt err = Z_OK; sl@0: sl@0: aStream.zalloc = Z_NULL; sl@0: aStream.zfree = Z_NULL; sl@0: aStream.opaque = Z_NULL; sl@0: sl@0: err = deflateInit(&aStream, aLevel); sl@0: CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "deflateInit error: %d", err); sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that sets up the deflate stream for use. sl@0: */ sl@0: void CTestZlib::DeflateInit2L(z_stream &aStream, const int aLevel, const int aMethod, const int aWindowBits, const int aMemLevel, const int aStrategy, const int expectedResult) sl@0: { sl@0: int err; sl@0: sl@0: aStream.zalloc = Z_NULL; sl@0: aStream.zfree = Z_NULL; sl@0: aStream.opaque = Z_NULL; sl@0: sl@0: err = deflateInit2(&aStream, aLevel, aMethod, aWindowBits, aMemLevel, aStrategy); sl@0: CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "deflateInit2 error: %d", err); sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that compresses data using the deflate function. sl@0: */ sl@0: TInt CTestZlib::DeflateCompress(z_stream &aStream, Byte *aInputData, int aInputDataLength, Byte *aCompressedDataBuffer, int aCompressedDataBufferLength) sl@0: { sl@0: int err; sl@0: int flush; sl@0: sl@0: // Compress data in the input buffer and place compressed data in the output buffer sl@0: aStream.next_in = aInputData; sl@0: aStream.next_out = aCompressedDataBuffer; sl@0: sl@0: do sl@0: { sl@0: if (aStream.total_in < aInputDataLength) sl@0: { sl@0: aStream.avail_in = 1; // force small buffer sl@0: } sl@0: sl@0: flush = (aStream.total_in == aInputDataLength) ? Z_FINISH : Z_NO_FLUSH; sl@0: sl@0: // Run deflate() on input until output buffer not full sl@0: // Finish compression if all of input buffer has been read in sl@0: do sl@0: { sl@0: if (aStream.total_out < aCompressedDataBufferLength) sl@0: { sl@0: aStream.avail_out = 1; // force small buffer sl@0: } sl@0: sl@0: err = deflate(&aStream, flush); sl@0: if(err != Z_OK && err != Z_STREAM_END) sl@0: { sl@0: return err; sl@0: } sl@0: } while(aStream.avail_out == 0 && err == Z_OK); sl@0: } while(err != Z_STREAM_END); sl@0: sl@0: return Z_OK; sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that cleans up a deflate stream. sl@0: */ sl@0: void CTestZlib::DeflateEnd(TAny *aStream) sl@0: { sl@0: if(((z_stream *)aStream)->state != NULL) sl@0: { sl@0: deflateEnd((z_stream *)aStream); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that sets up the inflate stream for use. sl@0: */ sl@0: void CTestZlib::InflateInitL(z_stream &aStream, const int expectedResult) sl@0: { sl@0: TInt err = Z_OK; sl@0: sl@0: aStream.zalloc = Z_NULL; sl@0: aStream.zfree = Z_NULL; sl@0: aStream.opaque = Z_NULL; sl@0: sl@0: err = inflateInit(&aStream); sl@0: CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateInit error: %d", err); sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that sets up the inflate stream for use. sl@0: */ sl@0: void CTestZlib::InflateInit2L(z_stream &aStream, const int aWindowBits, const int expectedResult) sl@0: { sl@0: TInt err = Z_OK; sl@0: sl@0: aStream.zalloc = Z_NULL; sl@0: aStream.zfree = Z_NULL; sl@0: aStream.opaque = Z_NULL; sl@0: sl@0: err = inflateInit2(&aStream, aWindowBits); sl@0: CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateInit2 error: %d", err); sl@0: } sl@0: sl@0: void CTestZlib::InflateBackInitL(z_stream &aStream, const int aWindowBits, Bytef *aWindow, const int expectedResult) sl@0: { sl@0: int err; sl@0: sl@0: aStream.zalloc = Z_NULL; sl@0: aStream.zfree = Z_NULL; sl@0: aStream.opaque = Z_NULL; sl@0: sl@0: err = inflateBackInit(&aStream, aWindowBits, aWindow); sl@0: CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateBackInit error: %d", err); sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that decompresses data using the inflate function. sl@0: */ sl@0: TInt CTestZlib::InflateDecompress(z_stream &aStream, Byte *aCompressedDataBuffer, int aCompressedDataLength, Byte *aDecompressedDataBuffer, int aDecompressedDataBufferLength) sl@0: { sl@0: int err; sl@0: sl@0: // Decompress data in the aCompressedDataBuffer and place it in the aDecompressedDataBuffer sl@0: aStream.next_in = aCompressedDataBuffer; sl@0: aStream.next_out = aDecompressedDataBuffer; sl@0: sl@0: // Keep providing input data and output space for deflate() sl@0: do sl@0: { sl@0: if(aStream.total_in < aCompressedDataLength) sl@0: { sl@0: aStream.avail_in = 1; // force small buffer sl@0: } sl@0: sl@0: if (aStream.total_out < aDecompressedDataBufferLength) sl@0: { sl@0: aStream.avail_out = 1; // force small buffer sl@0: } sl@0: sl@0: err = inflate(&aStream, Z_NO_FLUSH); sl@0: if(err != Z_OK && err != Z_STREAM_END) sl@0: { sl@0: return err; sl@0: } sl@0: } while(err != Z_STREAM_END); sl@0: sl@0: return Z_OK; sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that cleans up a inflate stream. sl@0: */ sl@0: void CTestZlib::InflateEnd(TAny *aStream) sl@0: { sl@0: if(((z_stream *)aStream)->state != NULL) sl@0: { sl@0: inflateEnd((z_stream *)aStream); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that cleans up an inflate back stream. sl@0: */ sl@0: void CTestZlib::InflateBackEnd(TAny *aStream) sl@0: { sl@0: if(((z_stream *)aStream)->state != NULL) sl@0: { sl@0: inflateBackEnd((z_stream *)aStream); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that compares the contents of two GZip headers to make sl@0: * sure they are the same. sl@0: */ sl@0: TBool CTestZlib::GZipHeadersEqual(const gz_header &header1, const gz_header &header2) sl@0: { sl@0: TBool headersEqual = true; sl@0: sl@0: if(header1.text != header2.text) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.text - Expected %d and got %d"), header1.text, header2.text); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.time != header2.time) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.time - Expected %d and got %d"), header1.time, header2.time); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.xflags != header2.xflags) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.xflags - Expected %d and got %d"), header1.xflags, header2.xflags); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.os != header2.os) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.os - Expected %d and got %d"), header1.os, header2.os); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.extra != NULL & header2.extra != NULL) sl@0: { sl@0: if(header1.extra_len != header2.extra_len) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.extra_len - Unexpected value.")); sl@0: headersEqual = false; sl@0: } sl@0: else if(memcmp(header1.extra, header2.extra, header1.extra_len) != 0) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.extra - Unexpected value.")); sl@0: headersEqual = false; sl@0: } sl@0: } sl@0: else if(header1.extra == NULL && header2.extra != NULL sl@0: || header1.extra != NULL && header2.extra == NULL) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.extra - Unexpected value.")); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.name != NULL && header2.name != NULL) sl@0: { sl@0: if(strcmp((char *)header1.name, (char *)header2.name) != 0) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.name - Unexpected value. The headers contained different strings.")); sl@0: headersEqual = false; sl@0: } sl@0: } sl@0: else if((header1.name == NULL && header2.name != NULL) sl@0: || (header1.name != NULL && header2.name == NULL)) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.name - Unexpected value. One of the headers contained a NULL value.")); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.comment != NULL && header2.comment != NULL) sl@0: { sl@0: if(strcmp((char *)header1.comment, (char *)header2.comment) != 0) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.comment - Unexpected value. The headers contained different strings.")); sl@0: headersEqual = false; sl@0: } sl@0: } sl@0: else if((header1.comment == NULL && header2.comment != NULL) sl@0: || (header1.comment != NULL && header2.comment == NULL)) sl@0: { sl@0: INFO_PRINTF1(_L("readGZipHeader.comment - Unexpected value. One of the headers contained a NULL value.")); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.hcrc != header2.hcrc) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.hcrc - Expected %d and got %d"), header1.hcrc, header2.hcrc); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: if(header1.done != header2.done) sl@0: { sl@0: INFO_PRINTF3(_L("readGZipHeader.done - Expected %d and got %d"), header1.done, header2.done); sl@0: headersEqual = false; sl@0: } sl@0: sl@0: return headersEqual; sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that checks to see if the header is the default GZip header sl@0: */ sl@0: TBool CTestZlib::IsDefaultGZipHeader(const gz_header &header) sl@0: { sl@0: gz_header defaultHeader; sl@0: sl@0: // gz_header default values sl@0: defaultHeader.text = 0; sl@0: defaultHeader.time = 0; sl@0: defaultHeader.xflags = 0; sl@0: defaultHeader.os = OS_CODE; sl@0: defaultHeader.extra = NULL; sl@0: defaultHeader.name = NULL; sl@0: defaultHeader.comment = NULL; sl@0: defaultHeader.hcrc = 0; sl@0: defaultHeader.done = 1; sl@0: sl@0: return GZipHeadersEqual(defaultHeader, header); sl@0: } sl@0: sl@0: /* sl@0: * Helper member function that compresses an input buffer and place it in a compressed output buffer sl@0: */ sl@0: void CTestZlib::CompressDataL(StreamSettings &aStreamSettings, z_stream &aStream, Byte *aInputData, int aInputDataLength, Byte *aCompressedDataBuffer, int aCompressedDataBufferLength, gz_headerp aHeader) sl@0: { sl@0: TInt err = Z_OK; sl@0: sl@0: // Initialise the deflate stream sl@0: if(aStreamSettings.deflateInit2 == false) sl@0: { sl@0: this->DeflateInitL(aStream, aStreamSettings.level); sl@0: } sl@0: else sl@0: { sl@0: this->DeflateInit2L(aStream, aStreamSettings.level, aStreamSettings.method, aStreamSettings.deflateWindowBits, aStreamSettings.memLevel, aStreamSettings.strategy); sl@0: } sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &aStream)); sl@0: sl@0: // If a GZip header is specified insert the gzip header information to the sl@0: // start of the output when deflate is called sl@0: if(aHeader != NULL) sl@0: { sl@0: err = deflateSetHeader(&aStream, aHeader); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateSetHeader error: %d", err); sl@0: } sl@0: sl@0: // Compress the input sl@0: err = this->DeflateCompress(aStream, aInputData, aInputDataLength, aCompressedDataBuffer, aCompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); sl@0: sl@0: // Clean up the deflateStream sl@0: err = deflateEnd(&aStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: } sl@0: sl@0: /* sl@0: * Helper function that deflates some input data as a gzip stream and then inflates sl@0: * the compressed data. sl@0: */ sl@0: TVerdict CTestZlib::DefInfGZipHeaderL(const TBool aIgnoreHeader, const TBool aAutoDetectHeader, gz_headerp aSpecifiedGZipHeader) sl@0: { sl@0: int err = Z_OK; sl@0: TVerdict verdict = EPass; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Headers sl@0: gz_header readGZipHeader; sl@0: readGZipHeader.extra = NULL; sl@0: readGZipHeader.name = NULL; sl@0: readGZipHeader.comment = NULL; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; // data to compress sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = true; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.method = Z_DEFLATED; sl@0: streamSettings.deflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; sl@0: streamSettings.inflateWindowBits = MAX_WBITS; sl@0: streamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: streamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: streamSettings.inflateWindowBits += (aAutoDetectHeader == true) ? TEST_AUTO_HEADER_DETECTION : TEST_GZIP_HEADER; sl@0: sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: // Allocate memory, if required, for readGZipHeader if header data is being sl@0: // checked and if a specified GZip header has been given. This is because sl@0: // extra, name and comment may have been set in the specified GZip header. sl@0: if(aIgnoreHeader == false && aSpecifiedGZipHeader != NULL) sl@0: { sl@0: if(aSpecifiedGZipHeader->extra != NULL) sl@0: { sl@0: readGZipHeader.extra_max = aSpecifiedGZipHeader->extra_max; sl@0: readGZipHeader.extra = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->extra_max * sizeof(Bytef)); sl@0: CHECK_CONDITION0L(readGZipHeader.extra == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.extra."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.extra)); sl@0: } sl@0: sl@0: if(aSpecifiedGZipHeader->name != NULL) sl@0: { sl@0: readGZipHeader.name_max = aSpecifiedGZipHeader->name_max; sl@0: readGZipHeader.name = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->name_max * sizeof(Bytef)); sl@0: CHECK_CONDITION0L(readGZipHeader.name == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.name."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.name)); sl@0: } sl@0: sl@0: if(aSpecifiedGZipHeader->comment != NULL) sl@0: { sl@0: readGZipHeader.comm_max = aSpecifiedGZipHeader->comm_max; sl@0: readGZipHeader.comment = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->comm_max * sizeof(Bytef)); sl@0: CHECK_CONDITION0L(readGZipHeader.comment == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.comment."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.comment)); sl@0: } sl@0: } sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength, aSpecifiedGZipHeader); sl@0: sl@0: // Initialise the inflateStream sl@0: this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: // If the header information is not to be ignored get the GZip sl@0: // header information when inflate is called sl@0: if(aIgnoreHeader == false) sl@0: { sl@0: err = inflateGetHeader(&inflateStream, &readGZipHeader); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateGetHeader error: %d", err); sl@0: } sl@0: sl@0: err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Clean up the inflateStream sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: // If the header information hasn't been ignored Check the GZip header sl@0: // contains the expected information sl@0: if(aIgnoreHeader == false) sl@0: { sl@0: if(aSpecifiedGZipHeader != NULL) sl@0: { sl@0: verdict = GZipHeadersEqual(*aSpecifiedGZipHeader, readGZipHeader) ? EPass : EFail; sl@0: } sl@0: else sl@0: { sl@0: verdict = IsDefaultGZipHeader(readGZipHeader) ? EPass : EFail; sl@0: } sl@0: } sl@0: sl@0: if(readGZipHeader.comment != NULL) sl@0: { sl@0: CleanupStack::PopAndDestroy(1); sl@0: } sl@0: sl@0: if(readGZipHeader.name != NULL) sl@0: { sl@0: CleanupStack::PopAndDestroy(1); sl@0: } sl@0: sl@0: if(readGZipHeader.extra != NULL) sl@0: { sl@0: CleanupStack::PopAndDestroy(1); sl@0: } sl@0: sl@0: // Free buffers sl@0: CleanupStack::PopAndDestroy(2); sl@0: sl@0: return verdict; sl@0: } sl@0: sl@0: /* sl@0: * Helper function that deflates some input data as a gzip stream with a specified header sl@0: * and then inflates the compressed data. sl@0: */ sl@0: TVerdict CTestZlib::DefInfGZipSpecifiedHeaderL(TBool aIgnoreHeader, TBool aAutoDetectHeader) sl@0: { sl@0: gz_header specifiedGZipHeader; sl@0: sl@0: Bytef extra[] = "12345"; sl@0: Bytef name[] = "TestDefInfGZipSpecifiedHeaderManual\0"; sl@0: Bytef comment[] = "This is a test comment.\0"; sl@0: sl@0: // gz_header user specified values. Set the specifiedGZipHeader ready for deflateSetHeader sl@0: specifiedGZipHeader.text = 1; sl@0: specifiedGZipHeader.time = 101; sl@0: specifiedGZipHeader.xflags = 0; // Set so that the value can be compared to the value in the header read from the deflated data sl@0: specifiedGZipHeader.os = 1; // Amiga sl@0: specifiedGZipHeader.extra = extra; sl@0: specifiedGZipHeader.extra_len = TEST_MID_MEM_LEVEL; sl@0: specifiedGZipHeader.extra_max = specifiedGZipHeader.extra_len + 10; // Add extra 10 to check readGZipHeader.extra_len is set properly sl@0: specifiedGZipHeader.name = name; sl@0: specifiedGZipHeader.name_max = strlen((char *)name) + 1; sl@0: specifiedGZipHeader.comment = comment; sl@0: specifiedGZipHeader.comm_max = strlen((char *)comment) + 1; sl@0: specifiedGZipHeader.hcrc = 0; sl@0: specifiedGZipHeader.done = 1; // Finished reading header. Set so that the value can be compared to the value in the header read from the deflated data sl@0: sl@0: return DefInfGZipHeaderL(aIgnoreHeader, aAutoDetectHeader, &specifiedGZipHeader); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4259 sl@0: @SYMTestCaseDesc To ensure deflation works after tuning the deflate stream using sl@0: deflateTune(). sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions 1. Create a stream and initialise it. sl@0: 2. Call deflateTune() passing it the specified parameters. sl@0: 3. Check the state of the stream to make sure that the values sl@0: are identical to the specified parameters. Also check that sl@0: Z_OK is returned. sl@0: 4. Deflate the input and cleanup using deflateEnd(), checking sl@0: Z_OK is returned. sl@0: sl@0: Note: The test should be repeated for deflateInit() parameter values: sl@0: • 3 and 4 for level sl@0: sl@0: and for deflateTune() parameter values: sl@0: • 0 and a high value for good_length sl@0: • 0 and a high value for max_lazy sl@0: • 0 and a high value for nice_length sl@0: • 0 and a high value for max_chain sl@0: Appropriate high values can be picked based on the global sl@0: configuration_table array in deflate.cpp. sl@0: @SYMTestExpectedResults deflateTune() should return Z_OK and the input should be compressed. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: TVerdict CTestZlib::TestDeflateTuneL() sl@0: { sl@0: int err = Z_OK; sl@0: TBool ret; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer; sl@0: sl@0: // deflateInit arguments sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int goodLength = 0; sl@0: int maxLazy = 0; sl@0: int niceLength = 0; sl@0: int maxChain = 0; sl@0: sl@0: // Allocate memory for output buffer sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for output buffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: // Read in the values for deflate tune from the ini file sl@0: ret = GetIntFromConfig(ConfigSection(), KParam1, goodLength); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); sl@0: sl@0: ret = GetIntFromConfig(ConfigSection(), KParam2, maxLazy); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param2 from ini file"); sl@0: sl@0: ret = GetIntFromConfig(ConfigSection(), KParam3, niceLength); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param3 from ini file"); sl@0: sl@0: ret = GetIntFromConfig(ConfigSection(), KParam4, maxChain); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param4 from ini file"); sl@0: sl@0: // Initialise the stream sl@0: this->DeflateInitL(deflateStream, level); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); sl@0: sl@0: // Tune the deflate stream sl@0: err = deflateTune(&deflateStream, goodLength, maxLazy, niceLength, maxChain); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateTune error: %d", err); sl@0: sl@0: // Check deflateTune has set the streams state values correctly sl@0: /* sl@0: internal_state *s = deflateStream.state; sl@0: CHECK_CONDITION0L(s->good_match != goodLength || s->max_lazy_match != maxLazy || s->nice_match != niceLength || s->max_chain_length != maxChain, KErrGeneral, "deflateTune did not set the stream->state values correctly"); sl@0: */ sl@0: sl@0: err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); sl@0: sl@0: // Clean up the deflate stream sl@0: err = deflateEnd(&deflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: // Free buffer sl@0: CleanupStack::PopAndDestroy(1); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4260 sl@0: @SYMTestCaseDesc Check deflateTune() fails when given an invalid stream. sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions 1. Create a stream and initialise it. sl@0: 2. Call deflateTune() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is NULL for the stream argument sl@0: @SYMTestExpectedResults deflateTune() fails returning Z_STREAM_ERROR in both cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDeflateTuneFailL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: sl@0: // deflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: sl@0: // Initialise the stream sl@0: this->DeflateInitL(deflateStream, level); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); sl@0: sl@0: // Try tuning a NULL deflate stream sl@0: err = deflateTune(NULL, 0, 0, 0, 0); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateTune on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Keep a pointer to the streams state so we can clean up properly with deflateEnd() sl@0: internal_state *deflateState = deflateStream.state; sl@0: sl@0: // Try tuning a deflate stream that has a NULL state sl@0: deflateStream.state = NULL; sl@0: err = deflateTune(&deflateStream, 0, 0, 0, 0); sl@0: deflateStream.state = deflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateTune on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the deflate stream sl@0: err = deflateEnd(&deflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4261 sl@0: @SYMTestCaseDesc Check input can be compressed and decompressed with default gzip sl@0: header not being read. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: adding 16 to windowBits. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2(), sl@0: adding 16 to windowBits. sl@0: 4. Inflate the input and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and sl@0: decompressed with inflateEnd() returning Z_OK. There should be no sl@0: header information stored. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfGZipDefaultHeaderIgnoreL() sl@0: { sl@0: return DefInfGZipHeaderL(true, false, NULL); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4262 sl@0: @SYMTestCaseDesc Check input can be compressed and decompressed with default gzip sl@0: header being read using automatic header detection. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: adding 16 to windowBits. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2(), sl@0: adding 32 to windowBits. sl@0: 4. Use inflateGetHeader() to get the default header information sl@0: and make sure the gzip header information is set correctly. sl@0: 5. Inflate the input and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and sl@0: decompressed with inflateEnd() returning Z_OK. There should be sl@0: correct header information stored. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfGZipDefaultHeaderAutoL() sl@0: { sl@0: return DefInfGZipHeaderL(false, true, NULL); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4263 sl@0: @SYMTestCaseDesc Check input can be compressed and decompressed with specified gzip sl@0: header being read. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: adding 16 to windowBits. sl@0: 2. Use deflateSetHeader() to specify header information and check sl@0: that the gzip header information is set correctly. sl@0: 3. Deflate the input and cleanup using deflateEnd(). sl@0: 4. Create an inflate stream and initialise it using inflateInit2(), sl@0: adding 16 to windowBits. sl@0: 5. Use inflateGetHeader() to get the header information and make sl@0: sure that it matches the header information originally entered. sl@0: 6. Inflate the input and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and sl@0: the header details being set correctly. The data should also be sl@0: decompressed with inflateEnd() returning Z_OK and the header details sl@0: matching the original header information. There should also be header sl@0: information stored. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfGZipSpecifiedHeaderManualL() sl@0: { sl@0: return DefInfGZipSpecifiedHeaderL(false, false); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4264 sl@0: @SYMTestCaseDesc Check input can be compressed and decompressed with specified gzip sl@0: header being read using automatic header detection. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: adding 16 to windowBits. sl@0: 2. Use deflateSetHeader() to specify header information and check sl@0: that the gzip header information is set correctly. sl@0: 3. Deflate the input and cleanup using deflateEnd(). sl@0: 4. Create an inflate stream and initialise it using inflateInit2(), sl@0: adding 32 to windowBits. sl@0: 5. Use inflateGetHeader() to get the header information and make sl@0: sure that it matches the header information originally entered. sl@0: 6. Inflate the input and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and sl@0: the header details being set correctly. The data should also be sl@0: decompressed with inflateEnd() returning Z_OK and the header details sl@0: matching the original header information. There should also be header sl@0: information stored. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfGZipSpecifiedHeaderAutoL() sl@0: { sl@0: return DefInfGZipSpecifiedHeaderL(false, true); sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4265 sl@0: @SYMTestCaseDesc Check input can be compressed and decompressed with zlib header sl@0: using automatic header detection. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2(), sl@0: adding 32 to windowBits. sl@0: 4. Inflate the input and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK sl@0: and decompressed with inflateEnd returning Z_OK. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfZlibHeaderAutoL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Stream settings sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = false; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.inflateWindowBits = MAX_WBITS + TEST_AUTO_HEADER_DETECTION; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; // data to compress sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflateStream sl@0: this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Clean up the inflateStream sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4266 sl@0: @SYMTestCaseDesc Check deflateSetHeader() fails when given an invalid stream. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), adding sl@0: 16 to windowBits. sl@0: 2. Call deflateSetHeader() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is set to NULL for the stream argument sl@0: c. a zlib stream for the stream argument sl@0: d. a raw stream for the stream argument sl@0: @SYMTestExpectedResults deflateSetHeader() fails returning Z_STREAM_ERROR in all four cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDeflateSetHeaderFailsL() sl@0: { sl@0: int err; sl@0: sl@0: // Streams sl@0: z_stream gZipDeflateStream; sl@0: z_stream zLibDeflateStream; sl@0: z_stream rawDeflateStream; sl@0: sl@0: // GZip headers sl@0: gz_header specifiedGZipHeader; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: sl@0: // deflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int method = Z_DEFLATED; sl@0: int gZipDeflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; sl@0: int memLevel = TEST_MID_MEM_LEVEL; sl@0: int strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // GZip header user specified values sl@0: Bytef extra[] = "12345"; sl@0: Bytef name[] = "TestDefInfGZipSpecifiedHeaderManual\0"; sl@0: Bytef comment[] = "This is a test comment.\0"; sl@0: sl@0: // Set the specifiedGZipHeader ready for deflateSetHeader sl@0: specifiedGZipHeader.text = 1; sl@0: specifiedGZipHeader.time = 101; sl@0: specifiedGZipHeader.os = 1; sl@0: specifiedGZipHeader.extra = extra; sl@0: specifiedGZipHeader.extra_len = 5; sl@0: specifiedGZipHeader.extra_max = specifiedGZipHeader.extra_len + 10; // Add extra 10 to check readGZipHeader.extra_len is set properly sl@0: specifiedGZipHeader.name = name; sl@0: specifiedGZipHeader.name_max = strlen((char *)name) + 1; sl@0: specifiedGZipHeader.comment = comment; sl@0: specifiedGZipHeader.comm_max = strlen((char *)comment) + 1; sl@0: specifiedGZipHeader.hcrc = 0; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: // Initialise the deflate streams sl@0: this->DeflateInit2L(gZipDeflateStream, level, method, gZipDeflateWindowBits, memLevel, strategy); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &gZipDeflateStream)); sl@0: sl@0: this->DeflateInitL(zLibDeflateStream, level); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &zLibDeflateStream)); sl@0: sl@0: this->DeflateInit2L(rawDeflateStream, level, method, -MAX_WBITS, memLevel, strategy); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &rawDeflateStream)); sl@0: sl@0: // Try setting a NULL streams header sl@0: err = deflateSetHeader(NULL, &specifiedGZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try setting the header for a deflate stream that has a NULL state sl@0: // we must keep a pointer to the streams state so we can clean up properly with deflateEnd sl@0: internal_state *deflateState = gZipDeflateStream.state; sl@0: gZipDeflateStream.state = NULL; sl@0: sl@0: err = deflateSetHeader(&gZipDeflateStream, &specifiedGZipHeader); sl@0: sl@0: gZipDeflateStream.state = deflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Try setting a header for a zlib stream sl@0: err = deflateSetHeader(&zLibDeflateStream, &specifiedGZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a zlib stream returned an unexpected value: %d", err); sl@0: sl@0: // Try setting a header for a raw stream sl@0: err = deflateSetHeader(&rawDeflateStream, &specifiedGZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a raw stream returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the deflate streams sl@0: err = deflateEnd(&gZipDeflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "gZipDeflateStream - deflateEnd error: %d", err); sl@0: sl@0: err = deflateEnd(&zLibDeflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "zLibDeflateStream - deflateEnd error: %d", err); sl@0: sl@0: // deflateInit2() leaves a raw stream in a busy state. This results in deflateEnd() sl@0: // returning a Z_DATA_ERROR, unless deflate() has been called on the stream. sl@0: err = deflateEnd(&rawDeflateStream); sl@0: CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "rawDeflateStream - deflateEnd error: %d", err); sl@0: sl@0: CleanupStack::Pop(3); sl@0: CleanupStack::PopAndDestroy(1); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4267 sl@0: @SYMTestCaseDesc Check inflateGetHeader() fails when given an invalid gzip stream. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), adding 16 to windowBits. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2(), adding 16 to windowBits. sl@0: 4. Call inflateGetHeader() passing it parameter values: sl@0: e. NULL for the stream argument sl@0: f. a stream whose state is set to NULL for the stream argument sl@0: g. a zlib stream for the stream argument sl@0: h. a raw stream for the stream argument sl@0: @SYMTestExpectedResults inflateGetHeader() should fail returning Z_STREAM_ERROR for the sl@0: first 2 cases and Z_DATA_ERROR for the second 2 case. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateGetHeaderFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream gZipDeflateStream; sl@0: z_stream zLibDeflateStream; sl@0: z_stream rawDeflateStream; sl@0: z_stream gZipInflateStream; sl@0: z_stream zLibInflateStream; sl@0: z_stream rawInflateStream; sl@0: sl@0: // GZip headers sl@0: gz_header gZipHeader; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *gZipCompressedDataBuffer = NULL; sl@0: Byte *zLibCompressedDataBuffer = NULL; sl@0: Byte *rawCompressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: StreamSettings gZipStreamSettings; sl@0: gZipStreamSettings.deflateInit2 = true; sl@0: gZipStreamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: gZipStreamSettings.method = Z_DEFLATED; sl@0: gZipStreamSettings.deflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; sl@0: gZipStreamSettings.inflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; sl@0: gZipStreamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: gZipStreamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: StreamSettings zLibStreamSettings; sl@0: zLibStreamSettings.deflateInit2 = false; sl@0: zLibStreamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: sl@0: StreamSettings rawStreamSettings; sl@0: rawStreamSettings.deflateInit2 = true; sl@0: rawStreamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: rawStreamSettings.method = Z_DEFLATED; sl@0: rawStreamSettings.deflateWindowBits = -MAX_WBITS; sl@0: rawStreamSettings.inflateWindowBits = -MAX_WBITS; sl@0: rawStreamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: rawStreamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // Allocate memory for buffers sl@0: gZipCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(gZipCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for gZipCompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, gZipCompressedDataBuffer)); sl@0: sl@0: zLibCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(zLibCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for zLibCompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, zLibCompressedDataBuffer)); sl@0: sl@0: rawCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(rawCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for rawCompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, rawCompressedDataBuffer)); sl@0: sl@0: // Compress data sl@0: INFO_PRINTF1(_L("Compress gzip:")); sl@0: this->CompressDataL(gZipStreamSettings, gZipDeflateStream, (Byte *)inputData, inputDataLength, gZipCompressedDataBuffer, compressedDataBufferLength); sl@0: INFO_PRINTF1(_L("Compress zlib:")); sl@0: this->CompressDataL(zLibStreamSettings, zLibDeflateStream, (Byte *)inputData, inputDataLength, zLibCompressedDataBuffer, compressedDataBufferLength); sl@0: INFO_PRINTF1(_L("Compress raw:")); sl@0: this->CompressDataL(rawStreamSettings, rawDeflateStream, (Byte *)inputData, inputDataLength, rawCompressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflateStreams sl@0: INFO_PRINTF1(_L("gZipInflateStream:")); sl@0: this->InflateInit2L(gZipInflateStream, gZipStreamSettings.inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &gZipInflateStream)); sl@0: sl@0: INFO_PRINTF1(_L("zLibInflateStream:")); sl@0: this->InflateInitL(zLibInflateStream); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &zLibInflateStream)); sl@0: sl@0: INFO_PRINTF1(_L("rawInflateStream:")); sl@0: this->InflateInit2L(rawInflateStream, rawStreamSettings.inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &rawInflateStream)); sl@0: sl@0: // Try and get header information from a NULL stream sl@0: err = inflateGetHeader(NULL, &gZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try getting the header for a deflate stream that has a NULL state sl@0: // we must keep a pointer to the streams state so we can clean up properly with deflateEnd sl@0: internal_state *deflateState = gZipDeflateStream.state; sl@0: gZipDeflateStream.state = NULL; sl@0: sl@0: err = inflateGetHeader(&gZipDeflateStream, &gZipHeader); sl@0: sl@0: gZipDeflateStream.state = deflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Try and get header information from a zlib stream sl@0: err = inflateGetHeader(&zLibDeflateStream, &gZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a zlib stream returned an unexpected value: %d", err); sl@0: sl@0: // Try and get header information from a raw stream sl@0: err = inflateGetHeader(&rawDeflateStream, &gZipHeader); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a raw stream returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the inflateStreams sl@0: err = inflateEnd(&gZipInflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "gZipInflateStream - inflateEnd error: %d", err); sl@0: sl@0: err = inflateEnd(&zLibInflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "zLibInflateStream - inflateEnd error: %d", err); sl@0: sl@0: err = inflateEnd(&rawInflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "rawInflateStream - inflateEnd error: %d", err); sl@0: sl@0: CleanupStack::Pop(3); sl@0: CleanupStack::PopAndDestroy(3); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4268 sl@0: @SYMTestCaseDesc Check output can be deflated in raw format and inflated. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and -15. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2(), sl@0: setting windowBits to be equal or less than the windowBits sl@0: value used for deflateInit2(). sl@0: 4. Inflate the input using inflate() and cleanup using inflateEnd(). sl@0: @SYMTestExpectedResults The data should be compressed with deflateEnd returning Z_OK and sl@0: decompressed with inflateEnd returning Z_OK. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfRawL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = true; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.method = Z_DEFLATED; sl@0: streamSettings.deflateWindowBits = -MAX_WBITS; sl@0: streamSettings.inflateWindowBits = streamSettings.deflateWindowBits; sl@0: streamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: streamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflateStream sl@0: this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Clean up the inflateStream sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); sl@0: sl@0: CleanupStack::Pop(1); sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4269 sl@0: @SYMTestCaseDesc Check output is not generated in raw format when windowBits < -15 sl@0: or > -8. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2() passing sl@0: it the specified parameters. sl@0: sl@0: Note: The test should be repeated for deflateInit2() parameter values: sl@0: • < -15 and > -8 for windowBits sl@0: @SYMTestExpectedResults deflateInit2() should fail returning Z_STREAM_ERROR. Note: if sl@0: windowBits is set between 8-15 and 24-31 there will be no error sl@0: as the input will be deflated as a zlib/gzip stream. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefRawFailsL() sl@0: { sl@0: TBool ret; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int method = Z_DEFLATED; sl@0: int deflateWindowBits; sl@0: int memLevel = TEST_MID_MEM_LEVEL; sl@0: int strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // Read in the value for windowBits from the ini file sl@0: ret = GetIntFromConfig(ConfigSection(), KParam1, deflateWindowBits); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); sl@0: sl@0: // Initialise the deflate stream sl@0: this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy, Z_STREAM_ERROR); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4270 sl@0: @SYMTestCaseDesc Check raw input is not inflated when windowBits < -15 or > -8. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and -15. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit2() sl@0: passing it the specified parameters. sl@0: sl@0: Note: The test should be repeated for inflateInit2() parameter values: sl@0: • < -15, > -8 & < 8, >= 8 & <= 15 and >= 24 & <= 31 for windowBits sl@0: For cases >= 8 & <= 15 and >= 24 & <= 31 inflate the compressed data sl@0: and call inflateEnd() to clean up. sl@0: @SYMTestExpectedResults inflateInit2() should fail returning Z_STREAM_ERROR. Note: if sl@0: windowBits is set between 8-15 and 24-31 a Z_DATA_ERROR will be sl@0: given as it will inflate as a zlib/gzip stream. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDefInfRawFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: TBool ret; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int method = Z_DEFLATED; sl@0: int deflateWindowBits = -MAX_WBITS; sl@0: int inflateWindowBits; sl@0: int memLevel = TEST_MID_MEM_LEVEL; sl@0: int strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: // Read in the value for windowBits from the ini file sl@0: ret = GetIntFromConfig(ConfigSection(), KParam1, inflateWindowBits); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); sl@0: sl@0: // Initialise the deflate stream sl@0: this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); sl@0: sl@0: // Compress the input sl@0: err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "deflate error: %d", err); sl@0: sl@0: // Clean up the deflate stream sl@0: err = deflateEnd(&deflateStream); sl@0: CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: // Initialise the inflateStream sl@0: // If windowBits are used that specify a zlib or gzip stream we get a different error sl@0: if((inflateWindowBits >= 8 && inflateWindowBits <= 15) || (inflateWindowBits >= 24 && inflateWindowBits <= 31)) sl@0: { sl@0: this->InflateInit2L(inflateStream, inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err!= Z_DATA_ERROR, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Clean up the inflateStream sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "inflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: } sl@0: else sl@0: { sl@0: this->InflateInit2L(inflateStream, inflateWindowBits, Z_STREAM_ERROR); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4271 sl@0: @SYMTestCaseDesc To check the specified bits are added to the start of a raw stream sl@0: correctly using deflatePrime(). sl@0: @SYMTestPriority Low sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), setting sl@0: windowBits between -8 and -15. sl@0: 2. Call deflatePrime() passing it the specified parameters. sl@0: 3. Deflate the input and cleanup using deflateEnd(). sl@0: 4. Check the bits at the front of the output are the expected ones. sl@0: sl@0: Note: The test should be repeated for deflatePrime() parameter values: sl@0: • 0, 8 and 16 for bits sl@0: • 0, 216 and a number between 0 - 216 for value sl@0: @SYMTestExpectedResults deflatePrime() should return Z_OK and the bits at the front of the sl@0: output are the expected ones. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDeflatePrimeL() sl@0: { sl@0: int err = Z_OK; sl@0: TBool ret; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int method = Z_DEFLATED; sl@0: int deflateWindowBits = -MAX_WBITS; sl@0: int memLevel = TEST_MID_MEM_LEVEL; sl@0: int strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // deflatePrime arguments sl@0: int bits; sl@0: int value; sl@0: sl@0: // Bits added to the start of the compressed stream sl@0: Byte bit1 = 0; sl@0: Byte bit2 = 0; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: // Read in the values for bits and value from the ini file sl@0: ret = GetIntFromConfig(ConfigSection(), KParam1, bits); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file."); sl@0: sl@0: ret = GetIntFromConfig(ConfigSection(), KParam2, value); sl@0: CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param2 from ini file."); sl@0: sl@0: // Initialise the deflate stream sl@0: this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); sl@0: sl@0: // Call deflatePrime on the stream sl@0: err = deflatePrime(&deflateStream, bits, value); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflatePrime error: %d", err); sl@0: sl@0: // Compress the input sl@0: err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); sl@0: sl@0: // Clean up the deflate stream sl@0: err = deflateEnd(&deflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: // Check the bits at the start of the compressed data are the same as intended sl@0: // when using deflatePrime() sl@0: switch(bits) sl@0: { sl@0: case 16: sl@0: bit1 = value & 0xFF; sl@0: bit2 = (value >> 8) & 0xFF; sl@0: break; sl@0: case 8: sl@0: bit1 = value & 0xFF; sl@0: bit2 = compressedDataBuffer[1]; sl@0: break; sl@0: case 0: sl@0: bit1 = compressedDataBuffer[0]; sl@0: bit2 = compressedDataBuffer[1]; sl@0: break; sl@0: default: sl@0: INFO_PRINTF1(_L("The test only works with bits set to 0, 8 or 16.")); sl@0: User::Leave(KErrGeneral); sl@0: } sl@0: CHECK_CONDITION0L(compressedDataBuffer[0] != bit1 || compressedDataBuffer[1] != bit2, KErrGeneral, "The bits at the start of the compressed data buffer do not match the specified bits in deflatePrime."); sl@0: sl@0: CleanupStack::PopAndDestroy(1); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4272 sl@0: @SYMTestCaseDesc Check deflatePrime() fails when given an invalid stream. sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and -15. sl@0: 2. Call deflatePrime() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is NULL for the stream argument sl@0: @SYMTestExpectedResults deflatePrime() fails returning Z_STREAM_ERROR in both cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestDeflatePrimeFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: sl@0: // deflateInit2 and inflateInit2 arguments sl@0: int level = Z_DEFAULT_COMPRESSION; sl@0: int method = Z_DEFLATED; sl@0: int deflateWindowBits = -MAX_WBITS; sl@0: int memLevel = TEST_MID_MEM_LEVEL; sl@0: int strategy = Z_DEFAULT_STRATEGY; sl@0: sl@0: // deflatePrime arguments sl@0: int bits = 0; sl@0: int value = 0; sl@0: sl@0: // Initialise the deflate stream sl@0: this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); sl@0: CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); sl@0: sl@0: // Call deflatePrime on a NULL stream sl@0: err = deflatePrime(NULL, bits, value); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflatePrime on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try calling deflatePrime on a deflate stream that has a NULL state sl@0: // We must keep a pointer to the streams state so we can clean up properly with deflateEnd sl@0: internal_state *deflateState = deflateStream.state; sl@0: deflateStream.state = NULL; sl@0: sl@0: err = deflatePrime(&deflateStream, bits, value); sl@0: sl@0: deflateStream.state = deflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflatePrime on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // deflateInit2() leaves a raw stream in a busy state. This results in deflateEnd() sl@0: // returning a Z_DATA_ERROR, unless deflate() has been called on the stream, sl@0: // meaning we have to set err back to Z_OK for the test to pass sl@0: err = deflateEnd(&deflateStream); sl@0: CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "deflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4274 sl@0: @SYMTestCaseDesc Check inflatePrime() fails when given an invalid stream or parameters. sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit2(), sl@0: setting windowBits to be equal or less than the windowBits value sl@0: used for deflateInit2(). sl@0: 2. Call inflatePrime() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is NULL for the stream argument sl@0: c. > 16 for bits sl@0: d. (32 – stream->bits) for bits sl@0: @SYMTestExpectedResults inflatePrime() fails returning Z_STREAM_ERROR in all cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflatePrimeFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream inflateStream; sl@0: sl@0: // inflateInit2 argument sl@0: int inflateWindowBits = -MAX_WBITS; sl@0: sl@0: // deflatePrime arguments sl@0: int bits = 0; sl@0: int value = 0; sl@0: sl@0: // Initialise the inflate stream sl@0: this->InflateInit2L(inflateStream, inflateWindowBits); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: // Call inflatePrime on a NULL stream sl@0: err = inflatePrime(NULL, bits, value); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try calling inflatePrime on a deflate stream that has a NULL state sl@0: // We must keep a pointer to the streams state so we can clean up properly with deflateEnd sl@0: struct internal_state FAR *inflateState = inflateStream.state; sl@0: inflateStream.state = NULL; sl@0: sl@0: err = inflatePrime(&inflateStream, bits, value); sl@0: sl@0: inflateStream.state = inflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Call inflatePrime with bits > 16 sl@0: err = inflatePrime(&inflateStream, 17, value); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime using bits > 16 returned an unexpected value: %d", err); sl@0: sl@0: // Call inflatePrime with inflateStream->state->bits + bits > 32 sl@0: // This test cannot be implemented due to inflate_state requiring the definition for sl@0: // the code struct which is in a header file that is included by one of the zlib sl@0: // cpp files. sl@0: /* sl@0: inflateState = inflateStream.state; sl@0: inflateState->bits(33 - bits); sl@0: err = inflatePrime(&inflateStream, bits, value); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime, setting inflateStream->state->bits, returned an unexpected value: %d", err); sl@0: */ sl@0: sl@0: // Clean up the inflate stream sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4275 sl@0: @SYMTestCaseDesc Check that a stream is duplicated correctly when using inflateCopy(). sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a stream and initialise it using deflateInit(). sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using inflateInit(). sl@0: 4. Create a second inflate stream and copy the first stream to the sl@0: second stream using inflateCopy(). sl@0: 5. Call inflate() and inflateEnd() on both streams. sl@0: 6. Compare the output of both streams. sl@0: @SYMTestExpectedResults inflateCopy() should return Z_OK and the output of both streams will sl@0: be identical. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateCopyL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: z_stream inflateStreamCopy; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: Byte *copiedDecompressedDataBuffer = NULL; sl@0: uLong copiedDecompressedDataBufferLength = decompressedDataBufferLength; sl@0: sl@0: // deflateInit argument sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = false; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: copiedDecompressedDataBuffer = (Byte *)User::AllocZ(copiedDecompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(copiedDecompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for copiedDecompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, copiedDecompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflate streams sl@0: this->InflateInitL(inflateStream); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: // Copy the inflateStream sl@0: err = inflateCopy(&inflateStreamCopy, &inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrNoMemory, "inflateCopy error: %d", err); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStreamCopy)); sl@0: sl@0: // Decompress the data in the compressedDataBuffer sl@0: err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Decompress the data in the copiedDecompressedDataBuffer sl@0: err = this->InflateDecompress(inflateStreamCopy, compressedDataBuffer, deflateStream.total_out, copiedDecompressedDataBuffer, copiedDecompressedDataBufferLength); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); sl@0: sl@0: // Clean up the inflate streams sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStream - inflateEnd error: %d", err); sl@0: sl@0: err = inflateEnd(&inflateStreamCopy); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStreamCopy - inflateEnd error: %d", err); sl@0: CleanupStack::Pop(2); sl@0: sl@0: // Check that both inflate streams produced identical output sl@0: CHECK_CONDITION0L(memcmp((char *)copiedDecompressedDataBuffer, (char *)decompressedDataBuffer, decompressedDataBufferLength) != 0, KErrGeneral, "The copied stream did not produce the same decompressed output as the original stream."); sl@0: sl@0: CleanupStack::PopAndDestroy(3); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4276 sl@0: @SYMTestCaseDesc Check inflateCopy() fails when given an invalid stream or parameters. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit(). sl@0: 2. Create a second inflate stream and call inflateCopy() passing sl@0: it parameter values: sl@0: a. NULL for the dest stream argument sl@0: b. NULL for the source stream argument sl@0: c. a stream whose state is NULL for the source stream argument sl@0: d. a stream whose zalloc is Z_NULL for the source stream argument sl@0: e. a stream whose zfree is Z_NULL for the source stream argument sl@0: @SYMTestExpectedResults inflateCopy() fails returning Z_STREAM_ERROR in all cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateCopyFailsParamsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream inflateStream; sl@0: z_stream inflateStreamCopy; sl@0: sl@0: // Initialise the inflate streams sl@0: this->InflateInitL(inflateStream); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: // Try to copy from a NULL stream sl@0: err = inflateCopy(&inflateStreamCopy, NULL); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try to copy to a NULL stream sl@0: err = inflateCopy(NULL, &inflateStream); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy to a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try calling deflateCopy on a deflate stream that has a NULL state sl@0: // We must keep a pointer to the streams state so we can clean up properly with deflateEnd sl@0: struct internal_state FAR *state = inflateStream.state; sl@0: inflateStream.state = NULL; sl@0: sl@0: err = inflateCopy(&inflateStreamCopy, &inflateStream); sl@0: sl@0: inflateStream.state = state; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream whose state is NULL returned an unexpected value: %d", err); sl@0: sl@0: // Try to copy from a stream with zalloc set to Z_NULL sl@0: inflateStream.zalloc = Z_NULL; sl@0: err = inflateCopy(&inflateStreamCopy, &inflateStream); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream with zalloc set to NULL returned an unexpected value: %d", err); sl@0: sl@0: // Try to copy from a stream with zfree set to Z_NULL sl@0: free_func zfree = inflateStream.zfree; sl@0: inflateStream.zfree = Z_NULL; sl@0: sl@0: err = inflateCopy(&inflateStreamCopy, &inflateStream); sl@0: sl@0: inflateStream.zfree = zfree; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream with zfree set to NULL returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the inflate streams sl@0: err = inflateEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStream - inflateEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4277 sl@0: @SYMTestCaseDesc Check inflateCopy() fails when there is not enough memory to copy sl@0: the stream. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit(). sl@0: 2. Create a second inflate stream and set memory allocation to sl@0: fail on the first attempt. sl@0: 3. Copy the first stream to the second stream using inflateCopy(). sl@0: 4. Check that the memory is the same before and after calling sl@0: inflateCopy(). Note: If Z_OK is returned it will be necessary sl@0: to call inflateEnd() before checking the amount of memory. sl@0: 5. Repeat this process until inflateCopy() returns Z_OK, increasing sl@0: the number of allocations that can be performed before failing. sl@0: @SYMTestExpectedResults inflateCopy() fails at first, returning Z_MEM_ERROR, and then it sl@0: will succeed with Z_OK. No memory should be leaked. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateCopyFailsMemL() sl@0: { sl@0: int err; sl@0: TVerdict verdict = EPass; sl@0: sl@0: // Streams sl@0: z_stream inflateStream; sl@0: z_stream inflateStreamCopy; sl@0: sl@0: // Initialise the inflate streams sl@0: this->InflateInitL(inflateStream); sl@0: CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); sl@0: sl@0: TInt failureRate; sl@0: for(failureRate = 1, err = Z_MEM_ERROR; err != Z_OK && failureRate <= FAILURE_RATE_LIMIT; failureRate++) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic, failureRate); sl@0: __UHEAP_MARK; sl@0: sl@0: // Copy the inflateStream sl@0: err = inflateCopy(&inflateStreamCopy, &inflateStream); sl@0: sl@0: // Memory has been allocated so we need to clean up sl@0: if(err == Z_OK) sl@0: { sl@0: err = inflateEnd(&inflateStreamCopy); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("inflateStreamCopy - inflateEnd error: %d"), err); sl@0: verdict = EAbort; sl@0: break; sl@0: } sl@0: } sl@0: else if(err != Z_MEM_ERROR) sl@0: { sl@0: INFO_PRINTF2(_L("inflateStreamCopy - unexpected error: %d"), err); sl@0: verdict = EFail; sl@0: break; sl@0: } sl@0: sl@0: __UHEAP_MARKEND; sl@0: __UHEAP_RESET; sl@0: } sl@0: sl@0: if(err == Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("The test succeeded at heap failure rate = %d."), --failureRate); sl@0: } sl@0: else if(failureRate > FAILURE_RATE_LIMIT) sl@0: { sl@0: INFO_PRINTF1(_L("Exceeded FAILURE_RATE_LIMIT. Either the test has failed or the limit needs increasing.")); sl@0: verdict = EFail; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(1); sl@0: sl@0: return verdict; sl@0: } sl@0: sl@0: /* sl@0: * in function required by inflateBack. sl@0: * Provides a pointer to the next piece of input data and returns the length of the data sl@0: */ sl@0: unsigned in OF((void FAR *in_desc, unsigned char FAR * FAR *in_buf)) sl@0: { sl@0: struct bufferDescriptor *compressedDataBufferDescriptor = (struct bufferDescriptor *)in_desc; sl@0: *in_buf = &compressedDataBufferDescriptor->buffer[compressedDataBufferDescriptor->next]; sl@0: sl@0: // Check that there is more input sl@0: if(compressedDataBufferDescriptor->next + 1 < compressedDataBufferDescriptor->length) sl@0: { sl@0: compressedDataBufferDescriptor->next++; sl@0: return 1; sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: * out function required by inflateBack. sl@0: * Provides a pointer to which the next space in memory data can be output and returns the length of this space sl@0: */ sl@0: int out OF((void FAR *out_desc, unsigned char FAR *out_buf, unsigned len)) sl@0: { sl@0: struct bufferDescriptor *decompressedDataBufferDescriptor = (struct bufferDescriptor *)out_desc; sl@0: sl@0: // Make sure there is output space sl@0: if(decompressedDataBufferDescriptor->next + len <= decompressedDataBufferDescriptor->length) sl@0: { sl@0: memcpy(decompressedDataBufferDescriptor->buffer + decompressedDataBufferDescriptor->next, out_buf, len); sl@0: decompressedDataBufferDescriptor->next += len; sl@0: } sl@0: else sl@0: { sl@0: int leftOver = decompressedDataBufferDescriptor->length - decompressedDataBufferDescriptor->next; sl@0: memcpy(decompressedDataBufferDescriptor->buffer + decompressedDataBufferDescriptor->next, out_buf, leftOver); sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4278 sl@0: @SYMTestCaseDesc Check a stream can be inflated using inflateBackInit(), sl@0: inflateBack() and inflateBackEnd(). sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and -15. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using sl@0: inflateBackInit() setting windowBits to be equal to or greater sl@0: than the windowBits value used for deflateInit2(). sl@0: 4. Call inflateBack() to uncompress the stream and call sl@0: inflateBackEnd() to clean up. sl@0: 5. Compare the original stream with the uncompressed stream. sl@0: @SYMTestExpectedResults inflateBackInit() and inflateBackEnd() should both return Z_OK. sl@0: inflateBack() should return Z_STREAM_END when it has finished sl@0: inflating the input stream. The original stream should be sl@0: identical to the final uncompressed stream. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateBackL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateBackInit arguments sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = true; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.method = Z_DEFLATED; sl@0: streamSettings.deflateWindowBits = -MAX_WBITS; sl@0: streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; sl@0: streamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: streamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: Bytef inflateWindow[32 * 1024]; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflate stream sl@0: InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); sl@0: CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); sl@0: sl@0: // This must be set to NULL otherwise inflateBack will assume there is input and try to decompress it sl@0: inflateStream.next_in = NULL; sl@0: sl@0: struct bufferDescriptor compressedDataBufferDescriptor; sl@0: compressedDataBufferDescriptor.buffer = compressedDataBuffer; sl@0: compressedDataBufferDescriptor.next = 0; sl@0: compressedDataBufferDescriptor.length = compressedDataBufferLength; sl@0: sl@0: struct bufferDescriptor decompressedDataBufferDescriptor; sl@0: decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; sl@0: decompressedDataBufferDescriptor.next = 0; sl@0: decompressedDataBufferDescriptor.length = decompressedDataBufferLength; sl@0: sl@0: // Decompress the input sl@0: err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_STREAM_END, KErrGeneral, "inflateBack error: %d", err); sl@0: sl@0: // Clean up the inflate stream sl@0: err = inflateBackEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: // Check that the decompressed data is identical to the original data sl@0: CHECK_CONDITION0L(strcmp(inputData, (char *)decompressedDataBuffer) != 0, KErrGeneral, "The deflated data did no match the original data."); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4279 sl@0: @SYMTestCaseDesc Check inflateBackEnd() fails when given an invalid stream or parameters. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and -15. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using sl@0: nflateBackInit() setting windowBits to be equal to or greater sl@0: than the windowBits value used for deflateInit2(). sl@0: 4. Call inflateBack() to uncompress the stream. sl@0: 5. Call inflateBackEnd() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is NULL for the stream argument sl@0: c. a stream whose zfree is Z_NULL for the stream argument sl@0: @SYMTestExpectedResults inflateBackEnd() fails returning Z_STREAM_ERROR in all cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateBackEndFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateBackInit arguments sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = true; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.method = Z_DEFLATED; sl@0: streamSettings.deflateWindowBits = -MAX_WBITS; sl@0: streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; sl@0: streamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: streamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: Bytef inflateWindow[32 * 1024]; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflate stream sl@0: InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); sl@0: CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); sl@0: sl@0: inflateStream.next_in = (unsigned char *)compressedDataBuffer; sl@0: inflateStream.avail_in = compressedDataBufferLength; sl@0: inflateStream.next_out = (unsigned char *)decompressedDataBuffer; sl@0: inflateStream.avail_out = decompressedDataBufferLength; sl@0: sl@0: struct bufferDescriptor compressedDataBufferDescriptor; sl@0: compressedDataBufferDescriptor.buffer = compressedDataBuffer; sl@0: compressedDataBufferDescriptor.next = 0; sl@0: compressedDataBufferDescriptor.length = compressedDataBufferLength; sl@0: sl@0: struct bufferDescriptor decompressedDataBufferDescriptor; sl@0: decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; sl@0: decompressedDataBufferDescriptor.next = 0; sl@0: decompressedDataBufferDescriptor.length = decompressedDataBufferLength; sl@0: sl@0: // Decompress the input sl@0: err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_STREAM_END, KErrGeneral, "inflateBack error: %d", err); sl@0: sl@0: // Try cleaning up a NULL stream sl@0: err = inflateBackEnd(NULL); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Keep a pointer to the streams state so we can clean up properly with inflateBackEnd() sl@0: struct internal_state FAR *inflateState = inflateStream.state; sl@0: sl@0: // Try cleaning up an inflate stream with a NULL state sl@0: inflateStream.state = NULL; sl@0: err = inflateBackEnd(&inflateStream); sl@0: inflateStream.state = inflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Try cleaning up an inflate stream with no free function sl@0: free_func zfree = inflateStream.zfree; sl@0: inflateStream.zfree = Z_NULL; sl@0: err = inflateBackEnd(NULL); sl@0: inflateStream.zfree = zfree; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a stream with zfree set to NULL returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the inflate stream sl@0: err = inflateBackEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /* sl@0: * in function required by inflateBack. sl@0: * Returning 0 indicates failure. sl@0: */ sl@0: unsigned inNoInput OF((void FAR *, unsigned char FAR * FAR *)) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: * out function required by inflateBack. sl@0: * Returning non 0 indicates failure. sl@0: */ sl@0: int outNoOutput OF((void FAR *, unsigned char FAR *, unsigned)) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4280 sl@0: @SYMTestCaseDesc Check inflateBack() fails when given an invalid stream or parameters. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), sl@0: setting windowBits between -8 and-15. sl@0: 2. Deflate the input and cleanup using deflateEnd(). sl@0: 3. Create an inflate stream and initialise it using sl@0: inflateBackInit() setting windowBits to be equal to or greater sl@0: than the windowBits value used for deflateInit2(). sl@0: 4. Call inflateBack() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose state is NULL for the stream argument sl@0: c. an input function that returns 0 for the in argument sl@0: d. an output function that doesn’t return 0 for the out argument sl@0: e. corrupted deflate output sl@0: 5. Call inflateBackEnd() to clean up sl@0: @SYMTestExpectedResults inflateBack() fails returning Z_STREAM_ERROR for the first two sl@0: cases, Z_BUF_ERROR for the second two cases and Z_DATA_ERROR sl@0: for the final case. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateBackFailsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream deflateStream; sl@0: z_stream inflateStream; sl@0: sl@0: // Buffers sl@0: const char inputData[] = "This is a piece of data to compress.\0"; sl@0: uLong inputDataLength = (uLong)strlen(inputData) + 1; sl@0: Byte *compressedDataBuffer = NULL; sl@0: uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; sl@0: Byte *decompressedDataBuffer = NULL; sl@0: uLong decompressedDataBufferLength = inputDataLength; sl@0: sl@0: // deflateInit2 and inflateBackInit arguments sl@0: StreamSettings streamSettings; sl@0: streamSettings.deflateInit2 = true; sl@0: streamSettings.level = Z_DEFAULT_COMPRESSION; sl@0: streamSettings.method = Z_DEFLATED; sl@0: streamSettings.deflateWindowBits = -MAX_WBITS; sl@0: streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; // inflateBackInit expects the positive equivalent of the windowBits used to make a raw stream sl@0: streamSettings.memLevel = TEST_MID_MEM_LEVEL; sl@0: streamSettings.strategy = Z_DEFAULT_STRATEGY; sl@0: Bytef inflateWindow[32 * 1024]; sl@0: sl@0: // Allocate memory for buffers sl@0: compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); sl@0: sl@0: decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); sl@0: CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); sl@0: CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); sl@0: sl@0: this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); sl@0: sl@0: // Initialise the inflate stream sl@0: InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); sl@0: CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); sl@0: sl@0: // This must be set to NULL otherwise inflateBack will assume there is input and try to decompress it sl@0: inflateStream.next_in = NULL; sl@0: sl@0: struct bufferDescriptor compressedDataBufferDescriptor; sl@0: compressedDataBufferDescriptor.buffer = compressedDataBuffer; sl@0: compressedDataBufferDescriptor.next = 0; sl@0: compressedDataBufferDescriptor.length = compressedDataBufferLength; sl@0: sl@0: struct bufferDescriptor decompressedDataBufferDescriptor; sl@0: decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; sl@0: decompressedDataBufferDescriptor.next = 0; sl@0: decompressedDataBufferDescriptor.length = decompressedDataBufferLength; sl@0: sl@0: // Try inflating a NULL stream sl@0: err = inflateBack(NULL, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBack on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try calling inflateBack on a deflate stream that has a NULL state sl@0: // We must keep a pointer to the streams state so we can clean up properly with deflateBackEnd sl@0: struct internal_state FAR *inflateState = inflateStream.state; sl@0: inflateStream.state = NULL; sl@0: sl@0: err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: sl@0: inflateStream.state = inflateState; sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBack on a stream with a NULL state returned an unexpected value: %d", err); sl@0: sl@0: // Try inflating a stream with an in_func that returns no input data sl@0: err = inflateBack(&inflateStream, &inNoInput, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_BUF_ERROR, KErrGeneral, "Calling inflateBack with an in function that returns no input returned an unexpected value: %d", err); sl@0: sl@0: // Try inflating a stream with an out_func that does not output any data sl@0: err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &outNoOutput, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_BUF_ERROR, KErrGeneral, "Calling inflateBack with an in function that returns no input returned an unexpected value: %d", err); sl@0: sl@0: // Try inflating a corrupt stream sl@0: compressedDataBuffer[1] = 'a'; sl@0: compressedDataBuffer[2] = 'a'; sl@0: compressedDataBuffer[3] = 'a'; sl@0: err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); sl@0: CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "Calling inflateBack on a corrupt stream returned an unexpected value: %d", err); sl@0: sl@0: // Clean up the inflate stream sl@0: err = inflateBackEnd(&inflateStream); sl@0: CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); sl@0: CleanupStack::Pop(1); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4281 sl@0: @SYMTestCaseDesc Check inflateBackInit() fails when given an invalid stream or parameters. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create an inflate stream and initialise it using sl@0: inflateBackInit() passing it parameter values: sl@0: a. NULL for the stream argument sl@0: b. a stream whose window is NULL for the stream argument sl@0: c. < 8 for the windowBits argument sl@0: d. > 5 for the windowBits argument sl@0: @SYMTestExpectedResults inflateBackInit() should fail returning Z_STREAM_ERROR for all cases. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateBackInitFailsParamsL() sl@0: { sl@0: int err = Z_OK; sl@0: sl@0: // Streams sl@0: z_stream inflateStream; sl@0: sl@0: // inflateBackInit arguments sl@0: int inflateBackWindowBits = MAX_WBITS; sl@0: Bytef inflateWindow[32 * 1024]; sl@0: sl@0: // Try initialising a NULL stream sl@0: inflateStream.zalloc = Z_NULL; sl@0: inflateStream.zfree = Z_NULL; sl@0: inflateStream.opaque = Z_NULL; sl@0: sl@0: err = inflateBackInit(NULL, inflateBackWindowBits, inflateWindow); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a NULL stream returned an unexpected value: %d", err); sl@0: sl@0: // Try initialising a stream with a NULL inflate window sl@0: err = inflateBackInit(&inflateStream, inflateBackWindowBits, NULL); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with a NULL window returned an unexpected value: %d", err); sl@0: sl@0: // Try initialising a stream with window bits < 8 sl@0: err = inflateBackInit(&inflateStream, 7, inflateWindow); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with windowBits < 8 returned an unexpected value: %d", err); sl@0: sl@0: // Try initialising a stream with window bits > 15 sl@0: err = inflateBackInit(&inflateStream, 16, inflateWindow); sl@0: CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with windowBits > 15 returned an unexpected value: %d", err); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4282 sl@0: @SYMTestCaseDesc Check inflateBackInit() fails when there is not enough memory. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create an inflate stream and set the next memory allocation to fail. sl@0: 2. Initialise the stream using inflateBackInit() setting sl@0: windowBits to be between 8 and 15. sl@0: 3. Check that the memory is the same before and after calling sl@0: inflateBackInit(). Note: If Z_OK is returned it will be sl@0: necessary to call inflateEnd() before checking the amount of memory. sl@0: 4. Repeat this process until inflateBackInit() returns Z_OK, sl@0: increasing the number of allocations that can be performed before failing. sl@0: @SYMTestExpectedResults inflateBackInit () fails at first, returning Z_MEM_ERROR, and then sl@0: it will succeed with Z_OK. No memory should be leaked. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestInflateBackInitFailsMem() sl@0: { sl@0: TInt err; sl@0: TVerdict verdict = EPass; sl@0: sl@0: // Streams sl@0: z_stream inflateStream; sl@0: sl@0: // inflateBackInit arguments sl@0: int inflateBackWindowBits = MAX_WBITS; sl@0: Bytef inflateWindow[32 * 1024]; sl@0: sl@0: inflateStream.zalloc = Z_NULL; sl@0: inflateStream.zfree = Z_NULL; sl@0: inflateStream.opaque = Z_NULL; sl@0: sl@0: TInt failureRate; sl@0: for(failureRate = 1, err = Z_MEM_ERROR; err != Z_OK && failureRate <= FAILURE_RATE_LIMIT; failureRate++) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic, failureRate); sl@0: __UHEAP_MARK; sl@0: sl@0: err = inflateBackInit(&inflateStream, inflateBackWindowBits, inflateWindow); sl@0: sl@0: // Memory has been allocated so we need to clean up sl@0: if(err == Z_OK) sl@0: { sl@0: err = inflateBackEnd(&inflateStream); sl@0: if(err != Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("inflateBackInit error: %d"), err); sl@0: verdict = EAbort; sl@0: break; sl@0: } sl@0: } sl@0: else if(err != Z_MEM_ERROR) sl@0: { sl@0: INFO_PRINTF2(_L("inflateBackInit unexpected error: %d"), err); sl@0: verdict = EFail; sl@0: break; sl@0: } sl@0: sl@0: __UHEAP_MARKEND; sl@0: __UHEAP_RESET; sl@0: } sl@0: sl@0: if(err == Z_OK) sl@0: { sl@0: INFO_PRINTF2(_L("The test succeeded at heap failure rate = %d."), --failureRate); sl@0: } sl@0: else if(failureRate > FAILURE_RATE_LIMIT) sl@0: { sl@0: INFO_PRINTF1(_L("Exceeded FAILURE_RATE_LIMIT. Either the test has failed or the limit needs increasing.")); sl@0: verdict = EFail; sl@0: } sl@0: sl@0: return verdict; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4283 sl@0: @SYMTestCaseDesc Check adler32_combine() generates the correct adler32 checksum. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create two byte buffers, both containing different data, and sl@0: use these to create two checksums using adler32(). sl@0: 2. Create a third checksum using adler32_combine(), providing it sl@0: with the first two checksums and the length of the second checksum. sl@0: 3. Concatenate the two byte buffers and create a fourth checksum sl@0: using adler32() and the concatenated buffer as input. sl@0: 4. Compare the third and fourth checksums. sl@0: @SYMTestExpectedResults The third and fourth checksums should be identical. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestAdler32CombineL() sl@0: { sl@0: // Byte buffers from which the adler32 checksums will be generated sl@0: const Byte *string1 = (Byte *)"Hello"; sl@0: const Byte *string2 = (Byte *)"World"; sl@0: const int string1Length = 5; sl@0: const int string2Length = 5; sl@0: const int string3Length = 10; sl@0: sl@0: // Initialise the adler32 variables sl@0: uLong adlerString1 = adler32(0L, NULL, 0); sl@0: uLong adlerString2 = adler32(0L, NULL, 0); sl@0: uLong adlerString3 = adler32(0L, NULL, 0); sl@0: uLong adlerCombined; sl@0: sl@0: // Generate the checksums from the byte buffers sl@0: adlerString1 = adler32(adlerString1, string1, string1Length); sl@0: adlerString2 = adler32(adlerString2, string2, string2Length); sl@0: sl@0: // Generate the checksum from combining adlerString1 and adlerString2 sl@0: adlerCombined = adler32_combine(adlerString1, adlerString2, string2Length); sl@0: sl@0: // Concatenate the byte buffers so that a checksum can be generated sl@0: Byte string3[string3Length]; sl@0: memcpy((char *)string3, (char *)string1, string1Length); sl@0: memcpy((char *)string3 + string1Length, (char *)string2, string2Length); sl@0: sl@0: // Generate checksum sl@0: adlerString3 = adler32(adlerString3, string3, string3Length); sl@0: sl@0: // Compare the checksums to see if they are the same sl@0: CHECK_CONDITION0L(adlerString3 != adlerCombined, KErrGeneral, "The combined checksum is not correct."); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4284 sl@0: @SYMTestCaseDesc Check crc32_combine() generates the correct crc32 checksum. sl@0: @SYMTestPriority High sl@0: @SYMTestActions 1. Create two byte buffers, both containing different data, and sl@0: use these to create two checksums using crc32(). sl@0: 2. Create a third checksum using crc32_combine(), providing it sl@0: with the first two checksums and the length of the second checksum. sl@0: 3. Concatenate the two byte buffers and create a fourth checksum sl@0: using crc32() and the concatenated buffer as input. sl@0: 4. Compare the third and fourth checksums. sl@0: @SYMTestExpectedResults The third and fourth checksums should be identical. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestCrc32CombineL() sl@0: { sl@0: // Byte buffers from which the crc32 checksums will be generated sl@0: const Byte *string1 = (Byte *)"Hello"; sl@0: const Byte *string2 = (Byte *)"World"; sl@0: const int string1Length = 5; sl@0: const int string2Length = 5; sl@0: const int string3Length = 10; sl@0: sl@0: // Initialise the crc32 variables sl@0: uLong crcString1 = crc32(0L, NULL, 0); sl@0: uLong crcString2 = crc32(0L, NULL, 0); sl@0: uLong crcString3 = crc32(0L, NULL, 0); sl@0: uLong crcCombined; sl@0: sl@0: // Generate the checksums from the byte buffers sl@0: crcString1 = crc32(crcString1, string1, string1Length); sl@0: crcString2 = crc32(crcString2, string2, string2Length); sl@0: sl@0: // Generate the checksum from combining adlerString1 and adlerString2 sl@0: crcCombined = crc32_combine(crcString1, crcString2, string2Length); sl@0: sl@0: // Concatenate the byte buffers so that a checksum can be generated sl@0: Byte string3[string3Length]; sl@0: memcpy((char *)string3, (char *)string1, string1Length); sl@0: memcpy((char *)string3 + string1Length, (char *)string2, string2Length); sl@0: sl@0: // Generate checksum sl@0: crcString3 = crc32(crcString3, string3, string3Length); sl@0: sl@0: // Compare the checksums to see if they are the same sl@0: CHECK_CONDITION0L(crcString3 != crcCombined, KErrGeneral, "The combined checksum is not correct."); sl@0: sl@0: return EPass; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB2-UT-4284 sl@0: @SYMTestCaseDesc Check zlibCompileFlags() returns a uLong with the correct sl@0: compile flags set. sl@0: @SYMTestPriority Medium sl@0: @SYMTestActions 1. Call zlibCompileFlags() and compare the first 2 bits with the sl@0: size of uInt, the second 2 bits with the size of uLong and sl@0: bits 16 and 17 with 0 (GZip APIs are being used). sl@0: @SYMTestExpectedResults zlibCompileFlags() will return a uLong and all the comparisons sl@0: will return true. sl@0: @SYMDEF REQ7362 sl@0: */ sl@0: sl@0: TVerdict CTestZlib::TestZlibCompileFlagsL() sl@0: { sl@0: // Get the compilerFlags sl@0: uLong compileFlags = zlibCompileFlags(); sl@0: int compileFlagsUInt = compileFlags & 0x3; sl@0: int compileFlagsULong = (compileFlags >> 2) & 0x3; sl@0: int compileFlagsGZip = (compileFlags >> 15) & 0x3; sl@0: int machineUInt; sl@0: int machineULong; sl@0: sl@0: // Get the size of uInt sl@0: switch(sizeof(uInt)) sl@0: { sl@0: case 2: machineUInt = 0; sl@0: break; sl@0: case 4: machineUInt = 1; sl@0: break; sl@0: case 8: machineUInt = 2; sl@0: break; sl@0: default: machineUInt = 3; sl@0: } sl@0: sl@0: // Check the compiler flag for uInt is correct sl@0: CHECK_CONDITION0L(machineUInt != compileFlagsUInt, KErrGeneral, "zlibCompileFlags reports an incorrect size for uInt."); sl@0: sl@0: // Get the size of uLong sl@0: switch(sizeof(uLong)) sl@0: { sl@0: case 2: machineULong = 0; sl@0: break; sl@0: case 4: machineULong = 1; sl@0: break; sl@0: case 8: machineULong = 2; sl@0: break; sl@0: default: machineULong = 3; sl@0: } sl@0: sl@0: // Check the compiler flag for uLong is correct sl@0: CHECK_CONDITION0L(machineULong != compileFlagsULong, KErrGeneral, "zlibCompileFlags reports an incorrect size for uLong."); sl@0: sl@0: // Check the compiler flags for GZip compression are correct sl@0: CHECK_CONDITION0L(compileFlagsGZip != 0, KErrGeneral, "zlibCompileFlags reports GZip functionality is disabled."); sl@0: sl@0: return EPass; sl@0: }