|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Name : tzlibadvanced.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include "tzlib.h" |
|
19 #include "zutil.h" |
|
20 //#include "deflate.h" |
|
21 |
|
22 #define COMPRESSED_DATA_BUFFER_LENGTH 200*sizeof(int) |
|
23 #define FAILURE_RATE_LIMIT 100 |
|
24 #define TEST_MID_MEM_LEVEL 5 |
|
25 |
|
26 // Add this to the windowBits value to use a GZip header instead of a Zlib header |
|
27 #define TEST_GZIP_HEADER 16 |
|
28 |
|
29 // Add this to the windowBits when calling inflateInit2() to use automatic header detection |
|
30 #define TEST_AUTO_HEADER_DETECTION 32 |
|
31 |
|
32 #define CHECK_CONDITION0L(condition, leaveErr, errMessage) \ |
|
33 if (condition) \ |
|
34 { \ |
|
35 ERR_PRINTF1(_L(errMessage)); \ |
|
36 User::Leave(leaveErr); \ |
|
37 } |
|
38 |
|
39 #define CHECK_CONDITION1L(condition, leaveErr, errMessage, p1) \ |
|
40 if (condition) \ |
|
41 { \ |
|
42 ERR_PRINTF2(_L(errMessage), p1); \ |
|
43 User::Leave(leaveErr); \ |
|
44 } |
|
45 |
|
46 _LIT(KParam1, "Param1"); |
|
47 _LIT(KParam2, "Param2"); |
|
48 _LIT(KParam3, "Param3"); |
|
49 _LIT(KParam4, "Param4"); |
|
50 |
|
51 // bufferDescriptor holds extra information required by in_func and out_func to perform their jobs |
|
52 struct bufferDescriptor |
|
53 { |
|
54 Byte *buffer; |
|
55 int next; |
|
56 int length; |
|
57 }; |
|
58 |
|
59 /* |
|
60 * Helper member function that sets up the deflate stream for use. |
|
61 */ |
|
62 void CTestZlib::DeflateInitL(z_stream &aStream, const int aLevel, const int expectedResult) |
|
63 { |
|
64 TInt err = Z_OK; |
|
65 |
|
66 aStream.zalloc = Z_NULL; |
|
67 aStream.zfree = Z_NULL; |
|
68 aStream.opaque = Z_NULL; |
|
69 |
|
70 err = deflateInit(&aStream, aLevel); |
|
71 CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "deflateInit error: %d", err); |
|
72 } |
|
73 |
|
74 /* |
|
75 * Helper member function that sets up the deflate stream for use. |
|
76 */ |
|
77 void CTestZlib::DeflateInit2L(z_stream &aStream, const int aLevel, const int aMethod, const int aWindowBits, const int aMemLevel, const int aStrategy, const int expectedResult) |
|
78 { |
|
79 int err; |
|
80 |
|
81 aStream.zalloc = Z_NULL; |
|
82 aStream.zfree = Z_NULL; |
|
83 aStream.opaque = Z_NULL; |
|
84 |
|
85 err = deflateInit2(&aStream, aLevel, aMethod, aWindowBits, aMemLevel, aStrategy); |
|
86 CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "deflateInit2 error: %d", err); |
|
87 } |
|
88 |
|
89 /* |
|
90 * Helper member function that compresses data using the deflate function. |
|
91 */ |
|
92 TInt CTestZlib::DeflateCompress(z_stream &aStream, Byte *aInputData, int aInputDataLength, Byte *aCompressedDataBuffer, int aCompressedDataBufferLength) |
|
93 { |
|
94 int err; |
|
95 int flush; |
|
96 |
|
97 // Compress data in the input buffer and place compressed data in the output buffer |
|
98 aStream.next_in = aInputData; |
|
99 aStream.next_out = aCompressedDataBuffer; |
|
100 |
|
101 do |
|
102 { |
|
103 if (aStream.total_in < aInputDataLength) |
|
104 { |
|
105 aStream.avail_in = 1; // force small buffer |
|
106 } |
|
107 |
|
108 flush = (aStream.total_in == aInputDataLength) ? Z_FINISH : Z_NO_FLUSH; |
|
109 |
|
110 // Run deflate() on input until output buffer not full |
|
111 // Finish compression if all of input buffer has been read in |
|
112 do |
|
113 { |
|
114 if (aStream.total_out < aCompressedDataBufferLength) |
|
115 { |
|
116 aStream.avail_out = 1; // force small buffer |
|
117 } |
|
118 |
|
119 err = deflate(&aStream, flush); |
|
120 if(err != Z_OK && err != Z_STREAM_END) |
|
121 { |
|
122 return err; |
|
123 } |
|
124 } while(aStream.avail_out == 0 && err == Z_OK); |
|
125 } while(err != Z_STREAM_END); |
|
126 |
|
127 return Z_OK; |
|
128 } |
|
129 |
|
130 /* |
|
131 * Helper member function that cleans up a deflate stream. |
|
132 */ |
|
133 void CTestZlib::DeflateEnd(TAny *aStream) |
|
134 { |
|
135 if(((z_stream *)aStream)->state != NULL) |
|
136 { |
|
137 deflateEnd((z_stream *)aStream); |
|
138 } |
|
139 } |
|
140 |
|
141 /* |
|
142 * Helper member function that sets up the inflate stream for use. |
|
143 */ |
|
144 void CTestZlib::InflateInitL(z_stream &aStream, const int expectedResult) |
|
145 { |
|
146 TInt err = Z_OK; |
|
147 |
|
148 aStream.zalloc = Z_NULL; |
|
149 aStream.zfree = Z_NULL; |
|
150 aStream.opaque = Z_NULL; |
|
151 |
|
152 err = inflateInit(&aStream); |
|
153 CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateInit error: %d", err); |
|
154 } |
|
155 |
|
156 /* |
|
157 * Helper member function that sets up the inflate stream for use. |
|
158 */ |
|
159 void CTestZlib::InflateInit2L(z_stream &aStream, const int aWindowBits, const int expectedResult) |
|
160 { |
|
161 TInt err = Z_OK; |
|
162 |
|
163 aStream.zalloc = Z_NULL; |
|
164 aStream.zfree = Z_NULL; |
|
165 aStream.opaque = Z_NULL; |
|
166 |
|
167 err = inflateInit2(&aStream, aWindowBits); |
|
168 CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateInit2 error: %d", err); |
|
169 } |
|
170 |
|
171 void CTestZlib::InflateBackInitL(z_stream &aStream, const int aWindowBits, Bytef *aWindow, const int expectedResult) |
|
172 { |
|
173 int err; |
|
174 |
|
175 aStream.zalloc = Z_NULL; |
|
176 aStream.zfree = Z_NULL; |
|
177 aStream.opaque = Z_NULL; |
|
178 |
|
179 err = inflateBackInit(&aStream, aWindowBits, aWindow); |
|
180 CHECK_CONDITION1L(err != expectedResult, KErrNoMemory, "inflateBackInit error: %d", err); |
|
181 } |
|
182 |
|
183 /* |
|
184 * Helper member function that decompresses data using the inflate function. |
|
185 */ |
|
186 TInt CTestZlib::InflateDecompress(z_stream &aStream, Byte *aCompressedDataBuffer, int aCompressedDataLength, Byte *aDecompressedDataBuffer, int aDecompressedDataBufferLength) |
|
187 { |
|
188 int err; |
|
189 |
|
190 // Decompress data in the aCompressedDataBuffer and place it in the aDecompressedDataBuffer |
|
191 aStream.next_in = aCompressedDataBuffer; |
|
192 aStream.next_out = aDecompressedDataBuffer; |
|
193 |
|
194 // Keep providing input data and output space for deflate() |
|
195 do |
|
196 { |
|
197 if(aStream.total_in < aCompressedDataLength) |
|
198 { |
|
199 aStream.avail_in = 1; // force small buffer |
|
200 } |
|
201 |
|
202 if (aStream.total_out < aDecompressedDataBufferLength) |
|
203 { |
|
204 aStream.avail_out = 1; // force small buffer |
|
205 } |
|
206 |
|
207 err = inflate(&aStream, Z_NO_FLUSH); |
|
208 if(err != Z_OK && err != Z_STREAM_END) |
|
209 { |
|
210 return err; |
|
211 } |
|
212 } while(err != Z_STREAM_END); |
|
213 |
|
214 return Z_OK; |
|
215 } |
|
216 |
|
217 /* |
|
218 * Helper member function that cleans up a inflate stream. |
|
219 */ |
|
220 void CTestZlib::InflateEnd(TAny *aStream) |
|
221 { |
|
222 if(((z_stream *)aStream)->state != NULL) |
|
223 { |
|
224 inflateEnd((z_stream *)aStream); |
|
225 } |
|
226 } |
|
227 |
|
228 /* |
|
229 * Helper member function that cleans up an inflate back stream. |
|
230 */ |
|
231 void CTestZlib::InflateBackEnd(TAny *aStream) |
|
232 { |
|
233 if(((z_stream *)aStream)->state != NULL) |
|
234 { |
|
235 inflateBackEnd((z_stream *)aStream); |
|
236 } |
|
237 } |
|
238 |
|
239 /* |
|
240 * Helper member function that compares the contents of two GZip headers to make |
|
241 * sure they are the same. |
|
242 */ |
|
243 TBool CTestZlib::GZipHeadersEqual(const gz_header &header1, const gz_header &header2) |
|
244 { |
|
245 TBool headersEqual = true; |
|
246 |
|
247 if(header1.text != header2.text) |
|
248 { |
|
249 INFO_PRINTF3(_L("readGZipHeader.text - Expected %d and got %d"), header1.text, header2.text); |
|
250 headersEqual = false; |
|
251 } |
|
252 |
|
253 if(header1.time != header2.time) |
|
254 { |
|
255 INFO_PRINTF3(_L("readGZipHeader.time - Expected %d and got %d"), header1.time, header2.time); |
|
256 headersEqual = false; |
|
257 } |
|
258 |
|
259 if(header1.xflags != header2.xflags) |
|
260 { |
|
261 INFO_PRINTF3(_L("readGZipHeader.xflags - Expected %d and got %d"), header1.xflags, header2.xflags); |
|
262 headersEqual = false; |
|
263 } |
|
264 |
|
265 if(header1.os != header2.os) |
|
266 { |
|
267 INFO_PRINTF3(_L("readGZipHeader.os - Expected %d and got %d"), header1.os, header2.os); |
|
268 headersEqual = false; |
|
269 } |
|
270 |
|
271 if(header1.extra != NULL & header2.extra != NULL) |
|
272 { |
|
273 if(header1.extra_len != header2.extra_len) |
|
274 { |
|
275 INFO_PRINTF1(_L("readGZipHeader.extra_len - Unexpected value.")); |
|
276 headersEqual = false; |
|
277 } |
|
278 else if(memcmp(header1.extra, header2.extra, header1.extra_len) != 0) |
|
279 { |
|
280 INFO_PRINTF1(_L("readGZipHeader.extra - Unexpected value.")); |
|
281 headersEqual = false; |
|
282 } |
|
283 } |
|
284 else if(header1.extra == NULL && header2.extra != NULL |
|
285 || header1.extra != NULL && header2.extra == NULL) |
|
286 { |
|
287 INFO_PRINTF1(_L("readGZipHeader.extra - Unexpected value.")); |
|
288 headersEqual = false; |
|
289 } |
|
290 |
|
291 if(header1.name != NULL && header2.name != NULL) |
|
292 { |
|
293 if(strcmp((char *)header1.name, (char *)header2.name) != 0) |
|
294 { |
|
295 INFO_PRINTF1(_L("readGZipHeader.name - Unexpected value. The headers contained different strings.")); |
|
296 headersEqual = false; |
|
297 } |
|
298 } |
|
299 else if((header1.name == NULL && header2.name != NULL) |
|
300 || (header1.name != NULL && header2.name == NULL)) |
|
301 { |
|
302 INFO_PRINTF1(_L("readGZipHeader.name - Unexpected value. One of the headers contained a NULL value.")); |
|
303 headersEqual = false; |
|
304 } |
|
305 |
|
306 if(header1.comment != NULL && header2.comment != NULL) |
|
307 { |
|
308 if(strcmp((char *)header1.comment, (char *)header2.comment) != 0) |
|
309 { |
|
310 INFO_PRINTF1(_L("readGZipHeader.comment - Unexpected value. The headers contained different strings.")); |
|
311 headersEqual = false; |
|
312 } |
|
313 } |
|
314 else if((header1.comment == NULL && header2.comment != NULL) |
|
315 || (header1.comment != NULL && header2.comment == NULL)) |
|
316 { |
|
317 INFO_PRINTF1(_L("readGZipHeader.comment - Unexpected value. One of the headers contained a NULL value.")); |
|
318 headersEqual = false; |
|
319 } |
|
320 |
|
321 if(header1.hcrc != header2.hcrc) |
|
322 { |
|
323 INFO_PRINTF3(_L("readGZipHeader.hcrc - Expected %d and got %d"), header1.hcrc, header2.hcrc); |
|
324 headersEqual = false; |
|
325 } |
|
326 |
|
327 if(header1.done != header2.done) |
|
328 { |
|
329 INFO_PRINTF3(_L("readGZipHeader.done - Expected %d and got %d"), header1.done, header2.done); |
|
330 headersEqual = false; |
|
331 } |
|
332 |
|
333 return headersEqual; |
|
334 } |
|
335 |
|
336 /* |
|
337 * Helper member function that checks to see if the header is the default GZip header |
|
338 */ |
|
339 TBool CTestZlib::IsDefaultGZipHeader(const gz_header &header) |
|
340 { |
|
341 gz_header defaultHeader; |
|
342 |
|
343 // gz_header default values |
|
344 defaultHeader.text = 0; |
|
345 defaultHeader.time = 0; |
|
346 defaultHeader.xflags = 0; |
|
347 defaultHeader.os = OS_CODE; |
|
348 defaultHeader.extra = NULL; |
|
349 defaultHeader.name = NULL; |
|
350 defaultHeader.comment = NULL; |
|
351 defaultHeader.hcrc = 0; |
|
352 defaultHeader.done = 1; |
|
353 |
|
354 return GZipHeadersEqual(defaultHeader, header); |
|
355 } |
|
356 |
|
357 /* |
|
358 * Helper member function that compresses an input buffer and place it in a compressed output buffer |
|
359 */ |
|
360 void CTestZlib::CompressDataL(StreamSettings &aStreamSettings, z_stream &aStream, Byte *aInputData, int aInputDataLength, Byte *aCompressedDataBuffer, int aCompressedDataBufferLength, gz_headerp aHeader) |
|
361 { |
|
362 TInt err = Z_OK; |
|
363 |
|
364 // Initialise the deflate stream |
|
365 if(aStreamSettings.deflateInit2 == false) |
|
366 { |
|
367 this->DeflateInitL(aStream, aStreamSettings.level); |
|
368 } |
|
369 else |
|
370 { |
|
371 this->DeflateInit2L(aStream, aStreamSettings.level, aStreamSettings.method, aStreamSettings.deflateWindowBits, aStreamSettings.memLevel, aStreamSettings.strategy); |
|
372 } |
|
373 CleanupStack::PushL(TCleanupItem(DeflateEnd, &aStream)); |
|
374 |
|
375 // If a GZip header is specified insert the gzip header information to the |
|
376 // start of the output when deflate is called |
|
377 if(aHeader != NULL) |
|
378 { |
|
379 err = deflateSetHeader(&aStream, aHeader); |
|
380 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateSetHeader error: %d", err); |
|
381 } |
|
382 |
|
383 // Compress the input |
|
384 err = this->DeflateCompress(aStream, aInputData, aInputDataLength, aCompressedDataBuffer, aCompressedDataBufferLength); |
|
385 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); |
|
386 |
|
387 // Clean up the deflateStream |
|
388 err = deflateEnd(&aStream); |
|
389 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); |
|
390 CleanupStack::Pop(1); |
|
391 } |
|
392 |
|
393 /* |
|
394 * Helper function that deflates some input data as a gzip stream and then inflates |
|
395 * the compressed data. |
|
396 */ |
|
397 TVerdict CTestZlib::DefInfGZipHeaderL(const TBool aIgnoreHeader, const TBool aAutoDetectHeader, gz_headerp aSpecifiedGZipHeader) |
|
398 { |
|
399 int err = Z_OK; |
|
400 TVerdict verdict = EPass; |
|
401 |
|
402 // Streams |
|
403 z_stream deflateStream; |
|
404 z_stream inflateStream; |
|
405 |
|
406 // Headers |
|
407 gz_header readGZipHeader; |
|
408 readGZipHeader.extra = NULL; |
|
409 readGZipHeader.name = NULL; |
|
410 readGZipHeader.comment = NULL; |
|
411 |
|
412 // Buffers |
|
413 const char inputData[] = "This is a piece of data to compress.\0"; // data to compress |
|
414 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
415 Byte *compressedDataBuffer = NULL; |
|
416 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
417 Byte *decompressedDataBuffer = NULL; |
|
418 uLong decompressedDataBufferLength = inputDataLength; |
|
419 |
|
420 // deflateInit2 and inflateInit2 arguments |
|
421 StreamSettings streamSettings; |
|
422 streamSettings.deflateInit2 = true; |
|
423 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
424 streamSettings.method = Z_DEFLATED; |
|
425 streamSettings.deflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; |
|
426 streamSettings.inflateWindowBits = MAX_WBITS; |
|
427 streamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
428 streamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
429 |
|
430 streamSettings.inflateWindowBits += (aAutoDetectHeader == true) ? TEST_AUTO_HEADER_DETECTION : TEST_GZIP_HEADER; |
|
431 |
|
432 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
433 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
434 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
435 |
|
436 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
437 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
438 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
439 |
|
440 // Allocate memory, if required, for readGZipHeader if header data is being |
|
441 // checked and if a specified GZip header has been given. This is because |
|
442 // extra, name and comment may have been set in the specified GZip header. |
|
443 if(aIgnoreHeader == false && aSpecifiedGZipHeader != NULL) |
|
444 { |
|
445 if(aSpecifiedGZipHeader->extra != NULL) |
|
446 { |
|
447 readGZipHeader.extra_max = aSpecifiedGZipHeader->extra_max; |
|
448 readGZipHeader.extra = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->extra_max * sizeof(Bytef)); |
|
449 CHECK_CONDITION0L(readGZipHeader.extra == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.extra."); |
|
450 CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.extra)); |
|
451 } |
|
452 |
|
453 if(aSpecifiedGZipHeader->name != NULL) |
|
454 { |
|
455 readGZipHeader.name_max = aSpecifiedGZipHeader->name_max; |
|
456 readGZipHeader.name = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->name_max * sizeof(Bytef)); |
|
457 CHECK_CONDITION0L(readGZipHeader.name == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.name."); |
|
458 CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.name)); |
|
459 } |
|
460 |
|
461 if(aSpecifiedGZipHeader->comment != NULL) |
|
462 { |
|
463 readGZipHeader.comm_max = aSpecifiedGZipHeader->comm_max; |
|
464 readGZipHeader.comment = (Bytef *)User::AllocZ(aSpecifiedGZipHeader->comm_max * sizeof(Bytef)); |
|
465 CHECK_CONDITION0L(readGZipHeader.comment == NULL, KErrNoMemory, "Failed to allocate memory for readGZipHeader.comment."); |
|
466 CleanupStack::PushL(TCleanupItem(User::Free, readGZipHeader.comment)); |
|
467 } |
|
468 } |
|
469 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength, aSpecifiedGZipHeader); |
|
470 |
|
471 // Initialise the inflateStream |
|
472 this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); |
|
473 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
474 |
|
475 // If the header information is not to be ignored get the GZip |
|
476 // header information when inflate is called |
|
477 if(aIgnoreHeader == false) |
|
478 { |
|
479 err = inflateGetHeader(&inflateStream, &readGZipHeader); |
|
480 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateGetHeader error: %d", err); |
|
481 } |
|
482 |
|
483 err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); |
|
484 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); |
|
485 |
|
486 // Clean up the inflateStream |
|
487 err = inflateEnd(&inflateStream); |
|
488 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); |
|
489 CleanupStack::Pop(1); |
|
490 |
|
491 // If the header information hasn't been ignored Check the GZip header |
|
492 // contains the expected information |
|
493 if(aIgnoreHeader == false) |
|
494 { |
|
495 if(aSpecifiedGZipHeader != NULL) |
|
496 { |
|
497 verdict = GZipHeadersEqual(*aSpecifiedGZipHeader, readGZipHeader) ? EPass : EFail; |
|
498 } |
|
499 else |
|
500 { |
|
501 verdict = IsDefaultGZipHeader(readGZipHeader) ? EPass : EFail; |
|
502 } |
|
503 } |
|
504 |
|
505 if(readGZipHeader.comment != NULL) |
|
506 { |
|
507 CleanupStack::PopAndDestroy(1); |
|
508 } |
|
509 |
|
510 if(readGZipHeader.name != NULL) |
|
511 { |
|
512 CleanupStack::PopAndDestroy(1); |
|
513 } |
|
514 |
|
515 if(readGZipHeader.extra != NULL) |
|
516 { |
|
517 CleanupStack::PopAndDestroy(1); |
|
518 } |
|
519 |
|
520 // Free buffers |
|
521 CleanupStack::PopAndDestroy(2); |
|
522 |
|
523 return verdict; |
|
524 } |
|
525 |
|
526 /* |
|
527 * Helper function that deflates some input data as a gzip stream with a specified header |
|
528 * and then inflates the compressed data. |
|
529 */ |
|
530 TVerdict CTestZlib::DefInfGZipSpecifiedHeaderL(TBool aIgnoreHeader, TBool aAutoDetectHeader) |
|
531 { |
|
532 gz_header specifiedGZipHeader; |
|
533 |
|
534 Bytef extra[] = "12345"; |
|
535 Bytef name[] = "TestDefInfGZipSpecifiedHeaderManual\0"; |
|
536 Bytef comment[] = "This is a test comment.\0"; |
|
537 |
|
538 // gz_header user specified values. Set the specifiedGZipHeader ready for deflateSetHeader |
|
539 specifiedGZipHeader.text = 1; |
|
540 specifiedGZipHeader.time = 101; |
|
541 specifiedGZipHeader.xflags = 0; // Set so that the value can be compared to the value in the header read from the deflated data |
|
542 specifiedGZipHeader.os = 1; // Amiga |
|
543 specifiedGZipHeader.extra = extra; |
|
544 specifiedGZipHeader.extra_len = TEST_MID_MEM_LEVEL; |
|
545 specifiedGZipHeader.extra_max = specifiedGZipHeader.extra_len + 10; // Add extra 10 to check readGZipHeader.extra_len is set properly |
|
546 specifiedGZipHeader.name = name; |
|
547 specifiedGZipHeader.name_max = strlen((char *)name) + 1; |
|
548 specifiedGZipHeader.comment = comment; |
|
549 specifiedGZipHeader.comm_max = strlen((char *)comment) + 1; |
|
550 specifiedGZipHeader.hcrc = 0; |
|
551 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 |
|
552 |
|
553 return DefInfGZipHeaderL(aIgnoreHeader, aAutoDetectHeader, &specifiedGZipHeader); |
|
554 } |
|
555 |
|
556 /** |
|
557 @SYMTestCaseID SYSLIB-EZLIB2-UT-4259 |
|
558 @SYMTestCaseDesc To ensure deflation works after tuning the deflate stream using |
|
559 deflateTune(). |
|
560 @SYMTestPriority Medium |
|
561 @SYMTestActions 1. Create a stream and initialise it. |
|
562 2. Call deflateTune() passing it the specified parameters. |
|
563 3. Check the state of the stream to make sure that the values |
|
564 are identical to the specified parameters. Also check that |
|
565 Z_OK is returned. |
|
566 4. Deflate the input and cleanup using deflateEnd(), checking |
|
567 Z_OK is returned. |
|
568 |
|
569 Note: The test should be repeated for deflateInit() parameter values: |
|
570 • 3 and 4 for level |
|
571 |
|
572 and for deflateTune() parameter values: |
|
573 • 0 and a high value for good_length |
|
574 • 0 and a high value for max_lazy |
|
575 • 0 and a high value for nice_length |
|
576 • 0 and a high value for max_chain |
|
577 Appropriate high values can be picked based on the global |
|
578 configuration_table array in deflate.cpp. |
|
579 @SYMTestExpectedResults deflateTune() should return Z_OK and the input should be compressed. |
|
580 @SYMDEF REQ7362 |
|
581 */ |
|
582 TVerdict CTestZlib::TestDeflateTuneL() |
|
583 { |
|
584 int err = Z_OK; |
|
585 TBool ret; |
|
586 |
|
587 // Streams |
|
588 z_stream deflateStream; |
|
589 |
|
590 // Buffers |
|
591 const char inputData[] = "This is a piece of data to compress.\0"; |
|
592 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
593 Byte *compressedDataBuffer; |
|
594 |
|
595 // deflateInit arguments |
|
596 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
597 int level = Z_DEFAULT_COMPRESSION; |
|
598 int goodLength = 0; |
|
599 int maxLazy = 0; |
|
600 int niceLength = 0; |
|
601 int maxChain = 0; |
|
602 |
|
603 // Allocate memory for output buffer |
|
604 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
605 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for output buffer."); |
|
606 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
607 |
|
608 // Read in the values for deflate tune from the ini file |
|
609 ret = GetIntFromConfig(ConfigSection(), KParam1, goodLength); |
|
610 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); |
|
611 |
|
612 ret = GetIntFromConfig(ConfigSection(), KParam2, maxLazy); |
|
613 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param2 from ini file"); |
|
614 |
|
615 ret = GetIntFromConfig(ConfigSection(), KParam3, niceLength); |
|
616 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param3 from ini file"); |
|
617 |
|
618 ret = GetIntFromConfig(ConfigSection(), KParam4, maxChain); |
|
619 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param4 from ini file"); |
|
620 |
|
621 // Initialise the stream |
|
622 this->DeflateInitL(deflateStream, level); |
|
623 CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); |
|
624 |
|
625 // Tune the deflate stream |
|
626 err = deflateTune(&deflateStream, goodLength, maxLazy, niceLength, maxChain); |
|
627 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateTune error: %d", err); |
|
628 |
|
629 // Check deflateTune has set the streams state values correctly |
|
630 /* |
|
631 internal_state *s = deflateStream.state; |
|
632 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"); |
|
633 */ |
|
634 |
|
635 err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
636 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); |
|
637 |
|
638 // Clean up the deflate stream |
|
639 err = deflateEnd(&deflateStream); |
|
640 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); |
|
641 CleanupStack::Pop(1); |
|
642 |
|
643 // Free buffer |
|
644 CleanupStack::PopAndDestroy(1); |
|
645 |
|
646 return EPass; |
|
647 } |
|
648 |
|
649 /** |
|
650 @SYMTestCaseID SYSLIB-EZLIB2-UT-4260 |
|
651 @SYMTestCaseDesc Check deflateTune() fails when given an invalid stream. |
|
652 @SYMTestPriority Medium |
|
653 @SYMTestActions 1. Create a stream and initialise it. |
|
654 2. Call deflateTune() passing it parameter values: |
|
655 a. NULL for the stream argument |
|
656 b. a stream whose state is NULL for the stream argument |
|
657 @SYMTestExpectedResults deflateTune() fails returning Z_STREAM_ERROR in both cases. |
|
658 @SYMDEF REQ7362 |
|
659 */ |
|
660 |
|
661 TVerdict CTestZlib::TestDeflateTuneFailL() |
|
662 { |
|
663 int err = Z_OK; |
|
664 |
|
665 // Streams |
|
666 z_stream deflateStream; |
|
667 |
|
668 // deflateInit2 arguments |
|
669 int level = Z_DEFAULT_COMPRESSION; |
|
670 |
|
671 // Initialise the stream |
|
672 this->DeflateInitL(deflateStream, level); |
|
673 CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); |
|
674 |
|
675 // Try tuning a NULL deflate stream |
|
676 err = deflateTune(NULL, 0, 0, 0, 0); |
|
677 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateTune on a NULL stream returned an unexpected value: %d", err); |
|
678 |
|
679 // Keep a pointer to the streams state so we can clean up properly with deflateEnd() |
|
680 internal_state *deflateState = deflateStream.state; |
|
681 |
|
682 // Try tuning a deflate stream that has a NULL state |
|
683 deflateStream.state = NULL; |
|
684 err = deflateTune(&deflateStream, 0, 0, 0, 0); |
|
685 deflateStream.state = deflateState; |
|
686 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateTune on a stream with a NULL state returned an unexpected value: %d", err); |
|
687 |
|
688 // Clean up the deflate stream |
|
689 err = deflateEnd(&deflateStream); |
|
690 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); |
|
691 CleanupStack::Pop(1); |
|
692 |
|
693 return EPass; |
|
694 } |
|
695 |
|
696 /** |
|
697 @SYMTestCaseID SYSLIB-EZLIB2-UT-4261 |
|
698 @SYMTestCaseDesc Check input can be compressed and decompressed with default gzip |
|
699 header not being read. |
|
700 @SYMTestPriority High |
|
701 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
702 adding 16 to windowBits. |
|
703 2. Deflate the input and cleanup using deflateEnd(). |
|
704 3. Create an inflate stream and initialise it using inflateInit2(), |
|
705 adding 16 to windowBits. |
|
706 4. Inflate the input and cleanup using inflateEnd(). |
|
707 @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and |
|
708 decompressed with inflateEnd() returning Z_OK. There should be no |
|
709 header information stored. |
|
710 @SYMDEF REQ7362 |
|
711 */ |
|
712 |
|
713 TVerdict CTestZlib::TestDefInfGZipDefaultHeaderIgnoreL() |
|
714 { |
|
715 return DefInfGZipHeaderL(true, false, NULL); |
|
716 } |
|
717 |
|
718 /** |
|
719 @SYMTestCaseID SYSLIB-EZLIB2-UT-4262 |
|
720 @SYMTestCaseDesc Check input can be compressed and decompressed with default gzip |
|
721 header being read using automatic header detection. |
|
722 @SYMTestPriority High |
|
723 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
724 adding 16 to windowBits. |
|
725 2. Deflate the input and cleanup using deflateEnd(). |
|
726 3. Create an inflate stream and initialise it using inflateInit2(), |
|
727 adding 32 to windowBits. |
|
728 4. Use inflateGetHeader() to get the default header information |
|
729 and make sure the gzip header information is set correctly. |
|
730 5. Inflate the input and cleanup using inflateEnd(). |
|
731 @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and |
|
732 decompressed with inflateEnd() returning Z_OK. There should be |
|
733 correct header information stored. |
|
734 @SYMDEF REQ7362 |
|
735 */ |
|
736 |
|
737 TVerdict CTestZlib::TestDefInfGZipDefaultHeaderAutoL() |
|
738 { |
|
739 return DefInfGZipHeaderL(false, true, NULL); |
|
740 } |
|
741 |
|
742 /** |
|
743 @SYMTestCaseID SYSLIB-EZLIB2-UT-4263 |
|
744 @SYMTestCaseDesc Check input can be compressed and decompressed with specified gzip |
|
745 header being read. |
|
746 @SYMTestPriority High |
|
747 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
748 adding 16 to windowBits. |
|
749 2. Use deflateSetHeader() to specify header information and check |
|
750 that the gzip header information is set correctly. |
|
751 3. Deflate the input and cleanup using deflateEnd(). |
|
752 4. Create an inflate stream and initialise it using inflateInit2(), |
|
753 adding 16 to windowBits. |
|
754 5. Use inflateGetHeader() to get the header information and make |
|
755 sure that it matches the header information originally entered. |
|
756 6. Inflate the input and cleanup using inflateEnd(). |
|
757 @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and |
|
758 the header details being set correctly. The data should also be |
|
759 decompressed with inflateEnd() returning Z_OK and the header details |
|
760 matching the original header information. There should also be header |
|
761 information stored. |
|
762 @SYMDEF REQ7362 |
|
763 */ |
|
764 |
|
765 TVerdict CTestZlib::TestDefInfGZipSpecifiedHeaderManualL() |
|
766 { |
|
767 return DefInfGZipSpecifiedHeaderL(false, false); |
|
768 } |
|
769 |
|
770 /** |
|
771 @SYMTestCaseID SYSLIB-EZLIB2-UT-4264 |
|
772 @SYMTestCaseDesc Check input can be compressed and decompressed with specified gzip |
|
773 header being read using automatic header detection. |
|
774 @SYMTestPriority High |
|
775 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
776 adding 16 to windowBits. |
|
777 2. Use deflateSetHeader() to specify header information and check |
|
778 that the gzip header information is set correctly. |
|
779 3. Deflate the input and cleanup using deflateEnd(). |
|
780 4. Create an inflate stream and initialise it using inflateInit2(), |
|
781 adding 32 to windowBits. |
|
782 5. Use inflateGetHeader() to get the header information and make |
|
783 sure that it matches the header information originally entered. |
|
784 6. Inflate the input and cleanup using inflateEnd(). |
|
785 @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK and |
|
786 the header details being set correctly. The data should also be |
|
787 decompressed with inflateEnd() returning Z_OK and the header details |
|
788 matching the original header information. There should also be header |
|
789 information stored. |
|
790 @SYMDEF REQ7362 |
|
791 */ |
|
792 |
|
793 TVerdict CTestZlib::TestDefInfGZipSpecifiedHeaderAutoL() |
|
794 { |
|
795 return DefInfGZipSpecifiedHeaderL(false, true); |
|
796 } |
|
797 |
|
798 /** |
|
799 @SYMTestCaseID SYSLIB-EZLIB2-UT-4265 |
|
800 @SYMTestCaseDesc Check input can be compressed and decompressed with zlib header |
|
801 using automatic header detection. |
|
802 @SYMTestPriority High |
|
803 @SYMTestActions 1. Create a deflate stream and initialise it. |
|
804 2. Deflate the input and cleanup using deflateEnd(). |
|
805 3. Create an inflate stream and initialise it using inflateInit2(), |
|
806 adding 32 to windowBits. |
|
807 4. Inflate the input and cleanup using inflateEnd(). |
|
808 @SYMTestExpectedResults The data should be compressed with deflateEnd() returning Z_OK |
|
809 and decompressed with inflateEnd returning Z_OK. |
|
810 @SYMDEF REQ7362 |
|
811 */ |
|
812 |
|
813 TVerdict CTestZlib::TestDefInfZlibHeaderAutoL() |
|
814 { |
|
815 int err = Z_OK; |
|
816 |
|
817 // Streams |
|
818 z_stream deflateStream; |
|
819 z_stream inflateStream; |
|
820 |
|
821 // Stream settings |
|
822 StreamSettings streamSettings; |
|
823 streamSettings.deflateInit2 = false; |
|
824 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
825 streamSettings.inflateWindowBits = MAX_WBITS + TEST_AUTO_HEADER_DETECTION; |
|
826 |
|
827 // Buffers |
|
828 const char inputData[] = "This is a piece of data to compress.\0"; // data to compress |
|
829 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
830 Byte *compressedDataBuffer = NULL; |
|
831 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
832 Byte *decompressedDataBuffer = NULL; |
|
833 uLong decompressedDataBufferLength = inputDataLength; |
|
834 |
|
835 // Allocate memory for buffers |
|
836 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
837 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
838 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
839 |
|
840 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
841 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
842 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
843 |
|
844 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
845 |
|
846 // Initialise the inflateStream |
|
847 this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); |
|
848 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
849 |
|
850 err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); |
|
851 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); |
|
852 |
|
853 // Clean up the inflateStream |
|
854 err = inflateEnd(&inflateStream); |
|
855 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); |
|
856 CleanupStack::Pop(1); |
|
857 |
|
858 CleanupStack::PopAndDestroy(2); |
|
859 return EPass; |
|
860 } |
|
861 |
|
862 /** |
|
863 @SYMTestCaseID SYSLIB-EZLIB2-UT-4266 |
|
864 @SYMTestCaseDesc Check deflateSetHeader() fails when given an invalid stream. |
|
865 @SYMTestPriority High |
|
866 @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), adding |
|
867 16 to windowBits. |
|
868 2. Call deflateSetHeader() passing it parameter values: |
|
869 a. NULL for the stream argument |
|
870 b. a stream whose state is set to NULL for the stream argument |
|
871 c. a zlib stream for the stream argument |
|
872 d. a raw stream for the stream argument |
|
873 @SYMTestExpectedResults deflateSetHeader() fails returning Z_STREAM_ERROR in all four cases. |
|
874 @SYMDEF REQ7362 |
|
875 */ |
|
876 |
|
877 TVerdict CTestZlib::TestDeflateSetHeaderFailsL() |
|
878 { |
|
879 int err; |
|
880 |
|
881 // Streams |
|
882 z_stream gZipDeflateStream; |
|
883 z_stream zLibDeflateStream; |
|
884 z_stream rawDeflateStream; |
|
885 |
|
886 // GZip headers |
|
887 gz_header specifiedGZipHeader; |
|
888 |
|
889 // Buffers |
|
890 const char inputData[] = "This is a piece of data to compress.\0"; |
|
891 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
892 Byte *compressedDataBuffer; |
|
893 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
894 |
|
895 // deflateInit2 arguments |
|
896 int level = Z_DEFAULT_COMPRESSION; |
|
897 int method = Z_DEFLATED; |
|
898 int gZipDeflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; |
|
899 int memLevel = TEST_MID_MEM_LEVEL; |
|
900 int strategy = Z_DEFAULT_STRATEGY; |
|
901 |
|
902 // GZip header user specified values |
|
903 Bytef extra[] = "12345"; |
|
904 Bytef name[] = "TestDefInfGZipSpecifiedHeaderManual\0"; |
|
905 Bytef comment[] = "This is a test comment.\0"; |
|
906 |
|
907 // Set the specifiedGZipHeader ready for deflateSetHeader |
|
908 specifiedGZipHeader.text = 1; |
|
909 specifiedGZipHeader.time = 101; |
|
910 specifiedGZipHeader.os = 1; |
|
911 specifiedGZipHeader.extra = extra; |
|
912 specifiedGZipHeader.extra_len = 5; |
|
913 specifiedGZipHeader.extra_max = specifiedGZipHeader.extra_len + 10; // Add extra 10 to check readGZipHeader.extra_len is set properly |
|
914 specifiedGZipHeader.name = name; |
|
915 specifiedGZipHeader.name_max = strlen((char *)name) + 1; |
|
916 specifiedGZipHeader.comment = comment; |
|
917 specifiedGZipHeader.comm_max = strlen((char *)comment) + 1; |
|
918 specifiedGZipHeader.hcrc = 0; |
|
919 |
|
920 // Allocate memory for buffers |
|
921 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
922 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
923 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
924 |
|
925 // Initialise the deflate streams |
|
926 this->DeflateInit2L(gZipDeflateStream, level, method, gZipDeflateWindowBits, memLevel, strategy); |
|
927 CleanupStack::PushL(TCleanupItem(DeflateEnd, &gZipDeflateStream)); |
|
928 |
|
929 this->DeflateInitL(zLibDeflateStream, level); |
|
930 CleanupStack::PushL(TCleanupItem(DeflateEnd, &zLibDeflateStream)); |
|
931 |
|
932 this->DeflateInit2L(rawDeflateStream, level, method, -MAX_WBITS, memLevel, strategy); |
|
933 CleanupStack::PushL(TCleanupItem(DeflateEnd, &rawDeflateStream)); |
|
934 |
|
935 // Try setting a NULL streams header |
|
936 err = deflateSetHeader(NULL, &specifiedGZipHeader); |
|
937 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a NULL stream returned an unexpected value: %d", err); |
|
938 |
|
939 // Try setting the header for a deflate stream that has a NULL state |
|
940 // we must keep a pointer to the streams state so we can clean up properly with deflateEnd |
|
941 internal_state *deflateState = gZipDeflateStream.state; |
|
942 gZipDeflateStream.state = NULL; |
|
943 |
|
944 err = deflateSetHeader(&gZipDeflateStream, &specifiedGZipHeader); |
|
945 |
|
946 gZipDeflateStream.state = deflateState; |
|
947 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a stream with a NULL state returned an unexpected value: %d", err); |
|
948 |
|
949 // Try setting a header for a zlib stream |
|
950 err = deflateSetHeader(&zLibDeflateStream, &specifiedGZipHeader); |
|
951 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a zlib stream returned an unexpected value: %d", err); |
|
952 |
|
953 // Try setting a header for a raw stream |
|
954 err = deflateSetHeader(&rawDeflateStream, &specifiedGZipHeader); |
|
955 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflateSetHeader on a raw stream returned an unexpected value: %d", err); |
|
956 |
|
957 // Clean up the deflate streams |
|
958 err = deflateEnd(&gZipDeflateStream); |
|
959 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "gZipDeflateStream - deflateEnd error: %d", err); |
|
960 |
|
961 err = deflateEnd(&zLibDeflateStream); |
|
962 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "zLibDeflateStream - deflateEnd error: %d", err); |
|
963 |
|
964 // deflateInit2() leaves a raw stream in a busy state. This results in deflateEnd() |
|
965 // returning a Z_DATA_ERROR, unless deflate() has been called on the stream. |
|
966 err = deflateEnd(&rawDeflateStream); |
|
967 CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "rawDeflateStream - deflateEnd error: %d", err); |
|
968 |
|
969 CleanupStack::Pop(3); |
|
970 CleanupStack::PopAndDestroy(1); |
|
971 return EPass; |
|
972 } |
|
973 |
|
974 /** |
|
975 @SYMTestCaseID SYSLIB-EZLIB2-UT-4267 |
|
976 @SYMTestCaseDesc Check inflateGetHeader() fails when given an invalid gzip stream. |
|
977 @SYMTestPriority High |
|
978 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), adding 16 to windowBits. |
|
979 2. Deflate the input and cleanup using deflateEnd(). |
|
980 3. Create an inflate stream and initialise it using inflateInit2(), adding 16 to windowBits. |
|
981 4. Call inflateGetHeader() passing it parameter values: |
|
982 e. NULL for the stream argument |
|
983 f. a stream whose state is set to NULL for the stream argument |
|
984 g. a zlib stream for the stream argument |
|
985 h. a raw stream for the stream argument |
|
986 @SYMTestExpectedResults inflateGetHeader() should fail returning Z_STREAM_ERROR for the |
|
987 first 2 cases and Z_DATA_ERROR for the second 2 case. |
|
988 @SYMDEF REQ7362 |
|
989 */ |
|
990 |
|
991 TVerdict CTestZlib::TestInflateGetHeaderFailsL() |
|
992 { |
|
993 int err = Z_OK; |
|
994 |
|
995 // Streams |
|
996 z_stream gZipDeflateStream; |
|
997 z_stream zLibDeflateStream; |
|
998 z_stream rawDeflateStream; |
|
999 z_stream gZipInflateStream; |
|
1000 z_stream zLibInflateStream; |
|
1001 z_stream rawInflateStream; |
|
1002 |
|
1003 // GZip headers |
|
1004 gz_header gZipHeader; |
|
1005 |
|
1006 // Buffers |
|
1007 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1008 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1009 Byte *gZipCompressedDataBuffer = NULL; |
|
1010 Byte *zLibCompressedDataBuffer = NULL; |
|
1011 Byte *rawCompressedDataBuffer = NULL; |
|
1012 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1013 |
|
1014 // deflateInit2 and inflateInit2 arguments |
|
1015 StreamSettings gZipStreamSettings; |
|
1016 gZipStreamSettings.deflateInit2 = true; |
|
1017 gZipStreamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1018 gZipStreamSettings.method = Z_DEFLATED; |
|
1019 gZipStreamSettings.deflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; |
|
1020 gZipStreamSettings.inflateWindowBits = MAX_WBITS + TEST_GZIP_HEADER; |
|
1021 gZipStreamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
1022 gZipStreamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
1023 |
|
1024 StreamSettings zLibStreamSettings; |
|
1025 zLibStreamSettings.deflateInit2 = false; |
|
1026 zLibStreamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1027 |
|
1028 StreamSettings rawStreamSettings; |
|
1029 rawStreamSettings.deflateInit2 = true; |
|
1030 rawStreamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1031 rawStreamSettings.method = Z_DEFLATED; |
|
1032 rawStreamSettings.deflateWindowBits = -MAX_WBITS; |
|
1033 rawStreamSettings.inflateWindowBits = -MAX_WBITS; |
|
1034 rawStreamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
1035 rawStreamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
1036 |
|
1037 // Allocate memory for buffers |
|
1038 gZipCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1039 CHECK_CONDITION0L(gZipCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for gZipCompressedDataBuffer."); |
|
1040 CleanupStack::PushL(TCleanupItem(User::Free, gZipCompressedDataBuffer)); |
|
1041 |
|
1042 zLibCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1043 CHECK_CONDITION0L(zLibCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for zLibCompressedDataBuffer."); |
|
1044 CleanupStack::PushL(TCleanupItem(User::Free, zLibCompressedDataBuffer)); |
|
1045 |
|
1046 rawCompressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1047 CHECK_CONDITION0L(rawCompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for rawCompressedDataBuffer."); |
|
1048 CleanupStack::PushL(TCleanupItem(User::Free, rawCompressedDataBuffer)); |
|
1049 |
|
1050 // Compress data |
|
1051 INFO_PRINTF1(_L("Compress gzip:")); |
|
1052 this->CompressDataL(gZipStreamSettings, gZipDeflateStream, (Byte *)inputData, inputDataLength, gZipCompressedDataBuffer, compressedDataBufferLength); |
|
1053 INFO_PRINTF1(_L("Compress zlib:")); |
|
1054 this->CompressDataL(zLibStreamSettings, zLibDeflateStream, (Byte *)inputData, inputDataLength, zLibCompressedDataBuffer, compressedDataBufferLength); |
|
1055 INFO_PRINTF1(_L("Compress raw:")); |
|
1056 this->CompressDataL(rawStreamSettings, rawDeflateStream, (Byte *)inputData, inputDataLength, rawCompressedDataBuffer, compressedDataBufferLength); |
|
1057 |
|
1058 // Initialise the inflateStreams |
|
1059 INFO_PRINTF1(_L("gZipInflateStream:")); |
|
1060 this->InflateInit2L(gZipInflateStream, gZipStreamSettings.inflateWindowBits); |
|
1061 CleanupStack::PushL(TCleanupItem(InflateEnd, &gZipInflateStream)); |
|
1062 |
|
1063 INFO_PRINTF1(_L("zLibInflateStream:")); |
|
1064 this->InflateInitL(zLibInflateStream); |
|
1065 CleanupStack::PushL(TCleanupItem(InflateEnd, &zLibInflateStream)); |
|
1066 |
|
1067 INFO_PRINTF1(_L("rawInflateStream:")); |
|
1068 this->InflateInit2L(rawInflateStream, rawStreamSettings.inflateWindowBits); |
|
1069 CleanupStack::PushL(TCleanupItem(InflateEnd, &rawInflateStream)); |
|
1070 |
|
1071 // Try and get header information from a NULL stream |
|
1072 err = inflateGetHeader(NULL, &gZipHeader); |
|
1073 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a NULL stream returned an unexpected value: %d", err); |
|
1074 |
|
1075 // Try getting the header for a deflate stream that has a NULL state |
|
1076 // we must keep a pointer to the streams state so we can clean up properly with deflateEnd |
|
1077 internal_state *deflateState = gZipDeflateStream.state; |
|
1078 gZipDeflateStream.state = NULL; |
|
1079 |
|
1080 err = inflateGetHeader(&gZipDeflateStream, &gZipHeader); |
|
1081 |
|
1082 gZipDeflateStream.state = deflateState; |
|
1083 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a stream with a NULL state returned an unexpected value: %d", err); |
|
1084 |
|
1085 // Try and get header information from a zlib stream |
|
1086 err = inflateGetHeader(&zLibDeflateStream, &gZipHeader); |
|
1087 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a zlib stream returned an unexpected value: %d", err); |
|
1088 |
|
1089 // Try and get header information from a raw stream |
|
1090 err = inflateGetHeader(&rawDeflateStream, &gZipHeader); |
|
1091 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateGetHeader on a raw stream returned an unexpected value: %d", err); |
|
1092 |
|
1093 // Clean up the inflateStreams |
|
1094 err = inflateEnd(&gZipInflateStream); |
|
1095 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "gZipInflateStream - inflateEnd error: %d", err); |
|
1096 |
|
1097 err = inflateEnd(&zLibInflateStream); |
|
1098 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "zLibInflateStream - inflateEnd error: %d", err); |
|
1099 |
|
1100 err = inflateEnd(&rawInflateStream); |
|
1101 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "rawInflateStream - inflateEnd error: %d", err); |
|
1102 |
|
1103 CleanupStack::Pop(3); |
|
1104 CleanupStack::PopAndDestroy(3); |
|
1105 return EPass; |
|
1106 } |
|
1107 |
|
1108 /** |
|
1109 @SYMTestCaseID SYSLIB-EZLIB2-UT-4268 |
|
1110 @SYMTestCaseDesc Check output can be deflated in raw format and inflated. |
|
1111 @SYMTestPriority High |
|
1112 @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), |
|
1113 setting windowBits between -8 and -15. |
|
1114 2. Deflate the input and cleanup using deflateEnd(). |
|
1115 3. Create an inflate stream and initialise it using inflateInit2(), |
|
1116 setting windowBits to be equal or less than the windowBits |
|
1117 value used for deflateInit2(). |
|
1118 4. Inflate the input using inflate() and cleanup using inflateEnd(). |
|
1119 @SYMTestExpectedResults The data should be compressed with deflateEnd returning Z_OK and |
|
1120 decompressed with inflateEnd returning Z_OK. |
|
1121 @SYMDEF REQ7362 |
|
1122 */ |
|
1123 |
|
1124 TVerdict CTestZlib::TestDefInfRawL() |
|
1125 { |
|
1126 int err = Z_OK; |
|
1127 |
|
1128 // Streams |
|
1129 z_stream deflateStream; |
|
1130 z_stream inflateStream; |
|
1131 |
|
1132 // Buffers |
|
1133 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1134 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1135 Byte *compressedDataBuffer = NULL; |
|
1136 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1137 Byte *decompressedDataBuffer = NULL; |
|
1138 uLong decompressedDataBufferLength = inputDataLength; |
|
1139 |
|
1140 // deflateInit2 and inflateInit2 arguments |
|
1141 StreamSettings streamSettings; |
|
1142 streamSettings.deflateInit2 = true; |
|
1143 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1144 streamSettings.method = Z_DEFLATED; |
|
1145 streamSettings.deflateWindowBits = -MAX_WBITS; |
|
1146 streamSettings.inflateWindowBits = streamSettings.deflateWindowBits; |
|
1147 streamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
1148 streamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
1149 |
|
1150 // Allocate memory for buffers |
|
1151 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1152 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1153 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1154 |
|
1155 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
1156 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
1157 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
1158 |
|
1159 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1160 |
|
1161 // Initialise the inflateStream |
|
1162 this->InflateInit2L(inflateStream, streamSettings.inflateWindowBits); |
|
1163 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1164 |
|
1165 err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); |
|
1166 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); |
|
1167 |
|
1168 // Clean up the inflateStream |
|
1169 err = inflateEnd(&inflateStream); |
|
1170 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); |
|
1171 |
|
1172 CleanupStack::Pop(1); |
|
1173 CleanupStack::PopAndDestroy(2); |
|
1174 return EPass; |
|
1175 } |
|
1176 |
|
1177 /** |
|
1178 @SYMTestCaseID SYSLIB-EZLIB2-UT-4269 |
|
1179 @SYMTestCaseDesc Check output is not generated in raw format when windowBits < -15 |
|
1180 or > -8. |
|
1181 @SYMTestPriority High |
|
1182 @SYMTestActions 1. Create a stream and initialise it using deflateInit2() passing |
|
1183 it the specified parameters. |
|
1184 |
|
1185 Note: The test should be repeated for deflateInit2() parameter values: |
|
1186 • < -15 and > -8 for windowBits |
|
1187 @SYMTestExpectedResults deflateInit2() should fail returning Z_STREAM_ERROR. Note: if |
|
1188 windowBits is set between 8-15 and 24-31 there will be no error |
|
1189 as the input will be deflated as a zlib/gzip stream. |
|
1190 @SYMDEF REQ7362 |
|
1191 */ |
|
1192 |
|
1193 TVerdict CTestZlib::TestDefRawFailsL() |
|
1194 { |
|
1195 TBool ret; |
|
1196 |
|
1197 // Streams |
|
1198 z_stream deflateStream; |
|
1199 |
|
1200 // deflateInit2 and inflateInit2 arguments |
|
1201 int level = Z_DEFAULT_COMPRESSION; |
|
1202 int method = Z_DEFLATED; |
|
1203 int deflateWindowBits; |
|
1204 int memLevel = TEST_MID_MEM_LEVEL; |
|
1205 int strategy = Z_DEFAULT_STRATEGY; |
|
1206 |
|
1207 // Read in the value for windowBits from the ini file |
|
1208 ret = GetIntFromConfig(ConfigSection(), KParam1, deflateWindowBits); |
|
1209 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); |
|
1210 |
|
1211 // Initialise the deflate stream |
|
1212 this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy, Z_STREAM_ERROR); |
|
1213 |
|
1214 return EPass; |
|
1215 } |
|
1216 |
|
1217 /** |
|
1218 @SYMTestCaseID SYSLIB-EZLIB2-UT-4270 |
|
1219 @SYMTestCaseDesc Check raw input is not inflated when windowBits < -15 or > -8. |
|
1220 @SYMTestPriority High |
|
1221 @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), |
|
1222 setting windowBits between -8 and -15. |
|
1223 2. Deflate the input and cleanup using deflateEnd(). |
|
1224 3. Create an inflate stream and initialise it using inflateInit2() |
|
1225 passing it the specified parameters. |
|
1226 |
|
1227 Note: The test should be repeated for inflateInit2() parameter values: |
|
1228 • < -15, > -8 & < 8, >= 8 & <= 15 and >= 24 & <= 31 for windowBits |
|
1229 For cases >= 8 & <= 15 and >= 24 & <= 31 inflate the compressed data |
|
1230 and call inflateEnd() to clean up. |
|
1231 @SYMTestExpectedResults inflateInit2() should fail returning Z_STREAM_ERROR. Note: if |
|
1232 windowBits is set between 8-15 and 24-31 a Z_DATA_ERROR will be |
|
1233 given as it will inflate as a zlib/gzip stream. |
|
1234 @SYMDEF REQ7362 |
|
1235 */ |
|
1236 |
|
1237 TVerdict CTestZlib::TestDefInfRawFailsL() |
|
1238 { |
|
1239 int err = Z_OK; |
|
1240 TBool ret; |
|
1241 |
|
1242 // Streams |
|
1243 z_stream deflateStream; |
|
1244 z_stream inflateStream; |
|
1245 |
|
1246 // Buffers |
|
1247 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1248 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1249 Byte *compressedDataBuffer = NULL; |
|
1250 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1251 Byte *decompressedDataBuffer = NULL; |
|
1252 uLong decompressedDataBufferLength = inputDataLength; |
|
1253 |
|
1254 // deflateInit2 and inflateInit2 arguments |
|
1255 int level = Z_DEFAULT_COMPRESSION; |
|
1256 int method = Z_DEFLATED; |
|
1257 int deflateWindowBits = -MAX_WBITS; |
|
1258 int inflateWindowBits; |
|
1259 int memLevel = TEST_MID_MEM_LEVEL; |
|
1260 int strategy = Z_DEFAULT_STRATEGY; |
|
1261 |
|
1262 // Allocate memory for buffers |
|
1263 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1264 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1265 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1266 |
|
1267 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
1268 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
1269 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
1270 |
|
1271 // Read in the value for windowBits from the ini file |
|
1272 ret = GetIntFromConfig(ConfigSection(), KParam1, inflateWindowBits); |
|
1273 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file"); |
|
1274 |
|
1275 // Initialise the deflate stream |
|
1276 this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); |
|
1277 CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); |
|
1278 |
|
1279 // Compress the input |
|
1280 err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1281 CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "deflate error: %d", err); |
|
1282 |
|
1283 // Clean up the deflate stream |
|
1284 err = deflateEnd(&deflateStream); |
|
1285 CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "deflateEnd error: %d", err); |
|
1286 CleanupStack::Pop(1); |
|
1287 |
|
1288 // Initialise the inflateStream |
|
1289 // If windowBits are used that specify a zlib or gzip stream we get a different error |
|
1290 if((inflateWindowBits >= 8 && inflateWindowBits <= 15) || (inflateWindowBits >= 24 && inflateWindowBits <= 31)) |
|
1291 { |
|
1292 this->InflateInit2L(inflateStream, inflateWindowBits); |
|
1293 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1294 |
|
1295 err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); |
|
1296 CHECK_CONDITION1L(err!= Z_DATA_ERROR, KErrGeneral, "inflate error: %d", err); |
|
1297 |
|
1298 // Clean up the inflateStream |
|
1299 err = inflateEnd(&inflateStream); |
|
1300 CHECK_CONDITION1L(err!= Z_OK, KErrGeneral, "inflateEnd error: %d", err); |
|
1301 CleanupStack::Pop(1); |
|
1302 } |
|
1303 else |
|
1304 { |
|
1305 this->InflateInit2L(inflateStream, inflateWindowBits, Z_STREAM_ERROR); |
|
1306 } |
|
1307 |
|
1308 CleanupStack::PopAndDestroy(2); |
|
1309 return EPass; |
|
1310 } |
|
1311 |
|
1312 /** |
|
1313 @SYMTestCaseID SYSLIB-EZLIB2-UT-4271 |
|
1314 @SYMTestCaseDesc To check the specified bits are added to the start of a raw stream |
|
1315 correctly using deflatePrime(). |
|
1316 @SYMTestPriority Low |
|
1317 @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), setting |
|
1318 windowBits between -8 and -15. |
|
1319 2. Call deflatePrime() passing it the specified parameters. |
|
1320 3. Deflate the input and cleanup using deflateEnd(). |
|
1321 4. Check the bits at the front of the output are the expected ones. |
|
1322 |
|
1323 Note: The test should be repeated for deflatePrime() parameter values: |
|
1324 • 0, 8 and 16 for bits |
|
1325 • 0, 216 and a number between 0 - 216 for value |
|
1326 @SYMTestExpectedResults deflatePrime() should return Z_OK and the bits at the front of the |
|
1327 output are the expected ones. |
|
1328 @SYMDEF REQ7362 |
|
1329 */ |
|
1330 |
|
1331 TVerdict CTestZlib::TestDeflatePrimeL() |
|
1332 { |
|
1333 int err = Z_OK; |
|
1334 TBool ret; |
|
1335 |
|
1336 // Streams |
|
1337 z_stream deflateStream; |
|
1338 |
|
1339 // Buffers |
|
1340 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1341 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1342 Byte *compressedDataBuffer; |
|
1343 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1344 |
|
1345 // deflateInit2 and inflateInit2 arguments |
|
1346 int level = Z_DEFAULT_COMPRESSION; |
|
1347 int method = Z_DEFLATED; |
|
1348 int deflateWindowBits = -MAX_WBITS; |
|
1349 int memLevel = TEST_MID_MEM_LEVEL; |
|
1350 int strategy = Z_DEFAULT_STRATEGY; |
|
1351 |
|
1352 // deflatePrime arguments |
|
1353 int bits; |
|
1354 int value; |
|
1355 |
|
1356 // Bits added to the start of the compressed stream |
|
1357 Byte bit1 = 0; |
|
1358 Byte bit2 = 0; |
|
1359 |
|
1360 // Allocate memory for buffers |
|
1361 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1362 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1363 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1364 |
|
1365 // Read in the values for bits and value from the ini file |
|
1366 ret = GetIntFromConfig(ConfigSection(), KParam1, bits); |
|
1367 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param1 from ini file."); |
|
1368 |
|
1369 ret = GetIntFromConfig(ConfigSection(), KParam2, value); |
|
1370 CHECK_CONDITION0L(!ret, KErrGeneral, "Failed to read Param2 from ini file."); |
|
1371 |
|
1372 // Initialise the deflate stream |
|
1373 this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); |
|
1374 CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); |
|
1375 |
|
1376 // Call deflatePrime on the stream |
|
1377 err = deflatePrime(&deflateStream, bits, value); |
|
1378 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflatePrime error: %d", err); |
|
1379 |
|
1380 // Compress the input |
|
1381 err = this->DeflateCompress(deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1382 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflate error: %d", err); |
|
1383 |
|
1384 // Clean up the deflate stream |
|
1385 err = deflateEnd(&deflateStream); |
|
1386 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "deflateEnd error: %d", err); |
|
1387 CleanupStack::Pop(1); |
|
1388 |
|
1389 // Check the bits at the start of the compressed data are the same as intended |
|
1390 // when using deflatePrime() |
|
1391 switch(bits) |
|
1392 { |
|
1393 case 16: |
|
1394 bit1 = value & 0xFF; |
|
1395 bit2 = (value >> 8) & 0xFF; |
|
1396 break; |
|
1397 case 8: |
|
1398 bit1 = value & 0xFF; |
|
1399 bit2 = compressedDataBuffer[1]; |
|
1400 break; |
|
1401 case 0: |
|
1402 bit1 = compressedDataBuffer[0]; |
|
1403 bit2 = compressedDataBuffer[1]; |
|
1404 break; |
|
1405 default: |
|
1406 INFO_PRINTF1(_L("The test only works with bits set to 0, 8 or 16.")); |
|
1407 User::Leave(KErrGeneral); |
|
1408 } |
|
1409 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."); |
|
1410 |
|
1411 CleanupStack::PopAndDestroy(1); |
|
1412 return EPass; |
|
1413 } |
|
1414 |
|
1415 /** |
|
1416 @SYMTestCaseID SYSLIB-EZLIB2-UT-4272 |
|
1417 @SYMTestCaseDesc Check deflatePrime() fails when given an invalid stream. |
|
1418 @SYMTestPriority Medium |
|
1419 @SYMTestActions 1. Create a stream and initialise it using deflateInit2(), |
|
1420 setting windowBits between -8 and -15. |
|
1421 2. Call deflatePrime() passing it parameter values: |
|
1422 a. NULL for the stream argument |
|
1423 b. a stream whose state is NULL for the stream argument |
|
1424 @SYMTestExpectedResults deflatePrime() fails returning Z_STREAM_ERROR in both cases. |
|
1425 @SYMDEF REQ7362 |
|
1426 */ |
|
1427 |
|
1428 TVerdict CTestZlib::TestDeflatePrimeFailsL() |
|
1429 { |
|
1430 int err = Z_OK; |
|
1431 |
|
1432 // Streams |
|
1433 z_stream deflateStream; |
|
1434 |
|
1435 // deflateInit2 and inflateInit2 arguments |
|
1436 int level = Z_DEFAULT_COMPRESSION; |
|
1437 int method = Z_DEFLATED; |
|
1438 int deflateWindowBits = -MAX_WBITS; |
|
1439 int memLevel = TEST_MID_MEM_LEVEL; |
|
1440 int strategy = Z_DEFAULT_STRATEGY; |
|
1441 |
|
1442 // deflatePrime arguments |
|
1443 int bits = 0; |
|
1444 int value = 0; |
|
1445 |
|
1446 // Initialise the deflate stream |
|
1447 this->DeflateInit2L(deflateStream, level, method, deflateWindowBits, memLevel, strategy); |
|
1448 CleanupStack::PushL(TCleanupItem(DeflateEnd, &deflateStream)); |
|
1449 |
|
1450 // Call deflatePrime on a NULL stream |
|
1451 err = deflatePrime(NULL, bits, value); |
|
1452 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflatePrime on a NULL stream returned an unexpected value: %d", err); |
|
1453 |
|
1454 // Try calling deflatePrime on a deflate stream that has a NULL state |
|
1455 // We must keep a pointer to the streams state so we can clean up properly with deflateEnd |
|
1456 internal_state *deflateState = deflateStream.state; |
|
1457 deflateStream.state = NULL; |
|
1458 |
|
1459 err = deflatePrime(&deflateStream, bits, value); |
|
1460 |
|
1461 deflateStream.state = deflateState; |
|
1462 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "deflatePrime on a stream with a NULL state returned an unexpected value: %d", err); |
|
1463 |
|
1464 // deflateInit2() leaves a raw stream in a busy state. This results in deflateEnd() |
|
1465 // returning a Z_DATA_ERROR, unless deflate() has been called on the stream, |
|
1466 // meaning we have to set err back to Z_OK for the test to pass |
|
1467 err = deflateEnd(&deflateStream); |
|
1468 CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "deflateEnd error: %d", err); |
|
1469 CleanupStack::Pop(1); |
|
1470 |
|
1471 return EPass; |
|
1472 } |
|
1473 |
|
1474 /** |
|
1475 @SYMTestCaseID SYSLIB-EZLIB2-UT-4274 |
|
1476 @SYMTestCaseDesc Check inflatePrime() fails when given an invalid stream or parameters. |
|
1477 @SYMTestPriority Medium |
|
1478 @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit2(), |
|
1479 setting windowBits to be equal or less than the windowBits value |
|
1480 used for deflateInit2(). |
|
1481 2. Call inflatePrime() passing it parameter values: |
|
1482 a. NULL for the stream argument |
|
1483 b. a stream whose state is NULL for the stream argument |
|
1484 c. > 16 for bits |
|
1485 d. (32 – stream->bits) for bits |
|
1486 @SYMTestExpectedResults inflatePrime() fails returning Z_STREAM_ERROR in all cases. |
|
1487 @SYMDEF REQ7362 |
|
1488 */ |
|
1489 |
|
1490 TVerdict CTestZlib::TestInflatePrimeFailsL() |
|
1491 { |
|
1492 int err = Z_OK; |
|
1493 |
|
1494 // Streams |
|
1495 z_stream inflateStream; |
|
1496 |
|
1497 // inflateInit2 argument |
|
1498 int inflateWindowBits = -MAX_WBITS; |
|
1499 |
|
1500 // deflatePrime arguments |
|
1501 int bits = 0; |
|
1502 int value = 0; |
|
1503 |
|
1504 // Initialise the inflate stream |
|
1505 this->InflateInit2L(inflateStream, inflateWindowBits); |
|
1506 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1507 |
|
1508 // Call inflatePrime on a NULL stream |
|
1509 err = inflatePrime(NULL, bits, value); |
|
1510 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime on a NULL stream returned an unexpected value: %d", err); |
|
1511 |
|
1512 // Try calling inflatePrime on a deflate stream that has a NULL state |
|
1513 // We must keep a pointer to the streams state so we can clean up properly with deflateEnd |
|
1514 struct internal_state FAR *inflateState = inflateStream.state; |
|
1515 inflateStream.state = NULL; |
|
1516 |
|
1517 err = inflatePrime(&inflateStream, bits, value); |
|
1518 |
|
1519 inflateStream.state = inflateState; |
|
1520 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime on a stream with a NULL state returned an unexpected value: %d", err); |
|
1521 |
|
1522 // Call inflatePrime with bits > 16 |
|
1523 err = inflatePrime(&inflateStream, 17, value); |
|
1524 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime using bits > 16 returned an unexpected value: %d", err); |
|
1525 |
|
1526 // Call inflatePrime with inflateStream->state->bits + bits > 32 |
|
1527 // This test cannot be implemented due to inflate_state requiring the definition for |
|
1528 // the code struct which is in a header file that is included by one of the zlib |
|
1529 // cpp files. |
|
1530 /* |
|
1531 inflateState = inflateStream.state; |
|
1532 inflateState->bits(33 - bits); |
|
1533 err = inflatePrime(&inflateStream, bits, value); |
|
1534 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflatePrime, setting inflateStream->state->bits, returned an unexpected value: %d", err); |
|
1535 */ |
|
1536 |
|
1537 // Clean up the inflate stream |
|
1538 err = inflateEnd(&inflateStream); |
|
1539 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateEnd error: %d", err); |
|
1540 CleanupStack::Pop(1); |
|
1541 |
|
1542 return EPass; |
|
1543 } |
|
1544 |
|
1545 /** |
|
1546 @SYMTestCaseID SYSLIB-EZLIB2-UT-4275 |
|
1547 @SYMTestCaseDesc Check that a stream is duplicated correctly when using inflateCopy(). |
|
1548 @SYMTestPriority High |
|
1549 @SYMTestActions 1. Create a stream and initialise it using deflateInit(). |
|
1550 2. Deflate the input and cleanup using deflateEnd(). |
|
1551 3. Create an inflate stream and initialise it using inflateInit(). |
|
1552 4. Create a second inflate stream and copy the first stream to the |
|
1553 second stream using inflateCopy(). |
|
1554 5. Call inflate() and inflateEnd() on both streams. |
|
1555 6. Compare the output of both streams. |
|
1556 @SYMTestExpectedResults inflateCopy() should return Z_OK and the output of both streams will |
|
1557 be identical. |
|
1558 @SYMDEF REQ7362 |
|
1559 */ |
|
1560 |
|
1561 TVerdict CTestZlib::TestInflateCopyL() |
|
1562 { |
|
1563 int err = Z_OK; |
|
1564 |
|
1565 // Streams |
|
1566 z_stream deflateStream; |
|
1567 z_stream inflateStream; |
|
1568 z_stream inflateStreamCopy; |
|
1569 |
|
1570 // Buffers |
|
1571 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1572 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1573 Byte *compressedDataBuffer = NULL; |
|
1574 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1575 Byte *decompressedDataBuffer = NULL; |
|
1576 uLong decompressedDataBufferLength = inputDataLength; |
|
1577 Byte *copiedDecompressedDataBuffer = NULL; |
|
1578 uLong copiedDecompressedDataBufferLength = decompressedDataBufferLength; |
|
1579 |
|
1580 // deflateInit argument |
|
1581 StreamSettings streamSettings; |
|
1582 streamSettings.deflateInit2 = false; |
|
1583 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1584 |
|
1585 // Allocate memory for buffers |
|
1586 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1587 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1588 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1589 |
|
1590 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
1591 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
1592 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
1593 |
|
1594 copiedDecompressedDataBuffer = (Byte *)User::AllocZ(copiedDecompressedDataBufferLength * sizeof(Byte)); |
|
1595 CHECK_CONDITION0L(copiedDecompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for copiedDecompressedDataBuffer."); |
|
1596 CleanupStack::PushL(TCleanupItem(User::Free, copiedDecompressedDataBuffer)); |
|
1597 |
|
1598 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1599 |
|
1600 // Initialise the inflate streams |
|
1601 this->InflateInitL(inflateStream); |
|
1602 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1603 |
|
1604 // Copy the inflateStream |
|
1605 err = inflateCopy(&inflateStreamCopy, &inflateStream); |
|
1606 CHECK_CONDITION1L(err != Z_OK, KErrNoMemory, "inflateCopy error: %d", err); |
|
1607 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStreamCopy)); |
|
1608 |
|
1609 // Decompress the data in the compressedDataBuffer |
|
1610 err = this->InflateDecompress(inflateStream, compressedDataBuffer, deflateStream.total_out, decompressedDataBuffer, decompressedDataBufferLength); |
|
1611 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); |
|
1612 |
|
1613 // Decompress the data in the copiedDecompressedDataBuffer |
|
1614 err = this->InflateDecompress(inflateStreamCopy, compressedDataBuffer, deflateStream.total_out, copiedDecompressedDataBuffer, copiedDecompressedDataBufferLength); |
|
1615 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflate error: %d", err); |
|
1616 |
|
1617 // Clean up the inflate streams |
|
1618 err = inflateEnd(&inflateStream); |
|
1619 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStream - inflateEnd error: %d", err); |
|
1620 |
|
1621 err = inflateEnd(&inflateStreamCopy); |
|
1622 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStreamCopy - inflateEnd error: %d", err); |
|
1623 CleanupStack::Pop(2); |
|
1624 |
|
1625 // Check that both inflate streams produced identical output |
|
1626 CHECK_CONDITION0L(memcmp((char *)copiedDecompressedDataBuffer, (char *)decompressedDataBuffer, decompressedDataBufferLength) != 0, KErrGeneral, "The copied stream did not produce the same decompressed output as the original stream."); |
|
1627 |
|
1628 CleanupStack::PopAndDestroy(3); |
|
1629 return EPass; |
|
1630 } |
|
1631 |
|
1632 /** |
|
1633 @SYMTestCaseID SYSLIB-EZLIB2-UT-4276 |
|
1634 @SYMTestCaseDesc Check inflateCopy() fails when given an invalid stream or parameters. |
|
1635 @SYMTestPriority High |
|
1636 @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit(). |
|
1637 2. Create a second inflate stream and call inflateCopy() passing |
|
1638 it parameter values: |
|
1639 a. NULL for the dest stream argument |
|
1640 b. NULL for the source stream argument |
|
1641 c. a stream whose state is NULL for the source stream argument |
|
1642 d. a stream whose zalloc is Z_NULL for the source stream argument |
|
1643 e. a stream whose zfree is Z_NULL for the source stream argument |
|
1644 @SYMTestExpectedResults inflateCopy() fails returning Z_STREAM_ERROR in all cases. |
|
1645 @SYMDEF REQ7362 |
|
1646 */ |
|
1647 |
|
1648 TVerdict CTestZlib::TestInflateCopyFailsParamsL() |
|
1649 { |
|
1650 int err = Z_OK; |
|
1651 |
|
1652 // Streams |
|
1653 z_stream inflateStream; |
|
1654 z_stream inflateStreamCopy; |
|
1655 |
|
1656 // Initialise the inflate streams |
|
1657 this->InflateInitL(inflateStream); |
|
1658 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1659 |
|
1660 // Try to copy from a NULL stream |
|
1661 err = inflateCopy(&inflateStreamCopy, NULL); |
|
1662 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a NULL stream returned an unexpected value: %d", err); |
|
1663 |
|
1664 // Try to copy to a NULL stream |
|
1665 err = inflateCopy(NULL, &inflateStream); |
|
1666 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy to a NULL stream returned an unexpected value: %d", err); |
|
1667 |
|
1668 // Try calling deflateCopy on a deflate stream that has a NULL state |
|
1669 // We must keep a pointer to the streams state so we can clean up properly with deflateEnd |
|
1670 struct internal_state FAR *state = inflateStream.state; |
|
1671 inflateStream.state = NULL; |
|
1672 |
|
1673 err = inflateCopy(&inflateStreamCopy, &inflateStream); |
|
1674 |
|
1675 inflateStream.state = state; |
|
1676 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream whose state is NULL returned an unexpected value: %d", err); |
|
1677 |
|
1678 // Try to copy from a stream with zalloc set to Z_NULL |
|
1679 inflateStream.zalloc = Z_NULL; |
|
1680 err = inflateCopy(&inflateStreamCopy, &inflateStream); |
|
1681 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream with zalloc set to NULL returned an unexpected value: %d", err); |
|
1682 |
|
1683 // Try to copy from a stream with zfree set to Z_NULL |
|
1684 free_func zfree = inflateStream.zfree; |
|
1685 inflateStream.zfree = Z_NULL; |
|
1686 |
|
1687 err = inflateCopy(&inflateStreamCopy, &inflateStream); |
|
1688 |
|
1689 inflateStream.zfree = zfree; |
|
1690 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "inflateCopy from a stream with zfree set to NULL returned an unexpected value: %d", err); |
|
1691 |
|
1692 // Clean up the inflate streams |
|
1693 err = inflateEnd(&inflateStream); |
|
1694 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateStream - inflateEnd error: %d", err); |
|
1695 CleanupStack::Pop(1); |
|
1696 |
|
1697 return EPass; |
|
1698 } |
|
1699 |
|
1700 /** |
|
1701 @SYMTestCaseID SYSLIB-EZLIB2-UT-4277 |
|
1702 @SYMTestCaseDesc Check inflateCopy() fails when there is not enough memory to copy |
|
1703 the stream. |
|
1704 @SYMTestPriority High |
|
1705 @SYMTestActions 1. Create an inflate stream and initialise it using inflateInit(). |
|
1706 2. Create a second inflate stream and set memory allocation to |
|
1707 fail on the first attempt. |
|
1708 3. Copy the first stream to the second stream using inflateCopy(). |
|
1709 4. Check that the memory is the same before and after calling |
|
1710 inflateCopy(). Note: If Z_OK is returned it will be necessary |
|
1711 to call inflateEnd() before checking the amount of memory. |
|
1712 5. Repeat this process until inflateCopy() returns Z_OK, increasing |
|
1713 the number of allocations that can be performed before failing. |
|
1714 @SYMTestExpectedResults inflateCopy() fails at first, returning Z_MEM_ERROR, and then it |
|
1715 will succeed with Z_OK. No memory should be leaked. |
|
1716 @SYMDEF REQ7362 |
|
1717 */ |
|
1718 |
|
1719 TVerdict CTestZlib::TestInflateCopyFailsMemL() |
|
1720 { |
|
1721 int err; |
|
1722 TVerdict verdict = EPass; |
|
1723 |
|
1724 // Streams |
|
1725 z_stream inflateStream; |
|
1726 z_stream inflateStreamCopy; |
|
1727 |
|
1728 // Initialise the inflate streams |
|
1729 this->InflateInitL(inflateStream); |
|
1730 CleanupStack::PushL(TCleanupItem(InflateEnd, &inflateStream)); |
|
1731 |
|
1732 TInt failureRate; |
|
1733 for(failureRate = 1, err = Z_MEM_ERROR; err != Z_OK && failureRate <= FAILURE_RATE_LIMIT; failureRate++) |
|
1734 { |
|
1735 __UHEAP_SETFAIL(RHeap::EDeterministic, failureRate); |
|
1736 __UHEAP_MARK; |
|
1737 |
|
1738 // Copy the inflateStream |
|
1739 err = inflateCopy(&inflateStreamCopy, &inflateStream); |
|
1740 |
|
1741 // Memory has been allocated so we need to clean up |
|
1742 if(err == Z_OK) |
|
1743 { |
|
1744 err = inflateEnd(&inflateStreamCopy); |
|
1745 if(err != Z_OK) |
|
1746 { |
|
1747 INFO_PRINTF2(_L("inflateStreamCopy - inflateEnd error: %d"), err); |
|
1748 verdict = EAbort; |
|
1749 break; |
|
1750 } |
|
1751 } |
|
1752 else if(err != Z_MEM_ERROR) |
|
1753 { |
|
1754 INFO_PRINTF2(_L("inflateStreamCopy - unexpected error: %d"), err); |
|
1755 verdict = EFail; |
|
1756 break; |
|
1757 } |
|
1758 |
|
1759 __UHEAP_MARKEND; |
|
1760 __UHEAP_RESET; |
|
1761 } |
|
1762 |
|
1763 if(err == Z_OK) |
|
1764 { |
|
1765 INFO_PRINTF2(_L("The test succeeded at heap failure rate = %d."), --failureRate); |
|
1766 } |
|
1767 else if(failureRate > FAILURE_RATE_LIMIT) |
|
1768 { |
|
1769 INFO_PRINTF1(_L("Exceeded FAILURE_RATE_LIMIT. Either the test has failed or the limit needs increasing.")); |
|
1770 verdict = EFail; |
|
1771 } |
|
1772 |
|
1773 CleanupStack::PopAndDestroy(1); |
|
1774 |
|
1775 return verdict; |
|
1776 } |
|
1777 |
|
1778 /* |
|
1779 * in function required by inflateBack. |
|
1780 * Provides a pointer to the next piece of input data and returns the length of the data |
|
1781 */ |
|
1782 unsigned in OF((void FAR *in_desc, unsigned char FAR * FAR *in_buf)) |
|
1783 { |
|
1784 struct bufferDescriptor *compressedDataBufferDescriptor = (struct bufferDescriptor *)in_desc; |
|
1785 *in_buf = &compressedDataBufferDescriptor->buffer[compressedDataBufferDescriptor->next]; |
|
1786 |
|
1787 // Check that there is more input |
|
1788 if(compressedDataBufferDescriptor->next + 1 < compressedDataBufferDescriptor->length) |
|
1789 { |
|
1790 compressedDataBufferDescriptor->next++; |
|
1791 return 1; |
|
1792 } |
|
1793 |
|
1794 return 0; |
|
1795 } |
|
1796 |
|
1797 /* |
|
1798 * out function required by inflateBack. |
|
1799 * Provides a pointer to which the next space in memory data can be output and returns the length of this space |
|
1800 */ |
|
1801 int out OF((void FAR *out_desc, unsigned char FAR *out_buf, unsigned len)) |
|
1802 { |
|
1803 struct bufferDescriptor *decompressedDataBufferDescriptor = (struct bufferDescriptor *)out_desc; |
|
1804 |
|
1805 // Make sure there is output space |
|
1806 if(decompressedDataBufferDescriptor->next + len <= decompressedDataBufferDescriptor->length) |
|
1807 { |
|
1808 memcpy(decompressedDataBufferDescriptor->buffer + decompressedDataBufferDescriptor->next, out_buf, len); |
|
1809 decompressedDataBufferDescriptor->next += len; |
|
1810 } |
|
1811 else |
|
1812 { |
|
1813 int leftOver = decompressedDataBufferDescriptor->length - decompressedDataBufferDescriptor->next; |
|
1814 memcpy(decompressedDataBufferDescriptor->buffer + decompressedDataBufferDescriptor->next, out_buf, leftOver); |
|
1815 } |
|
1816 |
|
1817 return 0; |
|
1818 } |
|
1819 |
|
1820 /** |
|
1821 @SYMTestCaseID SYSLIB-EZLIB2-UT-4278 |
|
1822 @SYMTestCaseDesc Check a stream can be inflated using inflateBackInit(), |
|
1823 inflateBack() and inflateBackEnd(). |
|
1824 @SYMTestPriority High |
|
1825 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
1826 setting windowBits between -8 and -15. |
|
1827 2. Deflate the input and cleanup using deflateEnd(). |
|
1828 3. Create an inflate stream and initialise it using |
|
1829 inflateBackInit() setting windowBits to be equal to or greater |
|
1830 than the windowBits value used for deflateInit2(). |
|
1831 4. Call inflateBack() to uncompress the stream and call |
|
1832 inflateBackEnd() to clean up. |
|
1833 5. Compare the original stream with the uncompressed stream. |
|
1834 @SYMTestExpectedResults inflateBackInit() and inflateBackEnd() should both return Z_OK. |
|
1835 inflateBack() should return Z_STREAM_END when it has finished |
|
1836 inflating the input stream. The original stream should be |
|
1837 identical to the final uncompressed stream. |
|
1838 @SYMDEF REQ7362 |
|
1839 */ |
|
1840 |
|
1841 TVerdict CTestZlib::TestInflateBackL() |
|
1842 { |
|
1843 int err = Z_OK; |
|
1844 |
|
1845 // Streams |
|
1846 z_stream deflateStream; |
|
1847 z_stream inflateStream; |
|
1848 |
|
1849 // Buffers |
|
1850 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1851 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1852 Byte *compressedDataBuffer = NULL; |
|
1853 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1854 Byte *decompressedDataBuffer = NULL; |
|
1855 uLong decompressedDataBufferLength = inputDataLength; |
|
1856 |
|
1857 // deflateInit2 and inflateBackInit arguments |
|
1858 StreamSettings streamSettings; |
|
1859 streamSettings.deflateInit2 = true; |
|
1860 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1861 streamSettings.method = Z_DEFLATED; |
|
1862 streamSettings.deflateWindowBits = -MAX_WBITS; |
|
1863 streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; |
|
1864 streamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
1865 streamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
1866 Bytef inflateWindow[32 * 1024]; |
|
1867 |
|
1868 // Allocate memory for buffers |
|
1869 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1870 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1871 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1872 |
|
1873 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
1874 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
1875 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
1876 |
|
1877 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1878 |
|
1879 // Initialise the inflate stream |
|
1880 InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); |
|
1881 CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); |
|
1882 |
|
1883 // This must be set to NULL otherwise inflateBack will assume there is input and try to decompress it |
|
1884 inflateStream.next_in = NULL; |
|
1885 |
|
1886 struct bufferDescriptor compressedDataBufferDescriptor; |
|
1887 compressedDataBufferDescriptor.buffer = compressedDataBuffer; |
|
1888 compressedDataBufferDescriptor.next = 0; |
|
1889 compressedDataBufferDescriptor.length = compressedDataBufferLength; |
|
1890 |
|
1891 struct bufferDescriptor decompressedDataBufferDescriptor; |
|
1892 decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; |
|
1893 decompressedDataBufferDescriptor.next = 0; |
|
1894 decompressedDataBufferDescriptor.length = decompressedDataBufferLength; |
|
1895 |
|
1896 // Decompress the input |
|
1897 err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
1898 CHECK_CONDITION1L(err != Z_STREAM_END, KErrGeneral, "inflateBack error: %d", err); |
|
1899 |
|
1900 // Clean up the inflate stream |
|
1901 err = inflateBackEnd(&inflateStream); |
|
1902 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); |
|
1903 CleanupStack::Pop(1); |
|
1904 |
|
1905 // Check that the decompressed data is identical to the original data |
|
1906 CHECK_CONDITION0L(strcmp(inputData, (char *)decompressedDataBuffer) != 0, KErrGeneral, "The deflated data did no match the original data."); |
|
1907 |
|
1908 CleanupStack::PopAndDestroy(2); |
|
1909 return EPass; |
|
1910 } |
|
1911 |
|
1912 /** |
|
1913 @SYMTestCaseID SYSLIB-EZLIB2-UT-4279 |
|
1914 @SYMTestCaseDesc Check inflateBackEnd() fails when given an invalid stream or parameters. |
|
1915 @SYMTestPriority High |
|
1916 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
1917 setting windowBits between -8 and -15. |
|
1918 2. Deflate the input and cleanup using deflateEnd(). |
|
1919 3. Create an inflate stream and initialise it using |
|
1920 nflateBackInit() setting windowBits to be equal to or greater |
|
1921 than the windowBits value used for deflateInit2(). |
|
1922 4. Call inflateBack() to uncompress the stream. |
|
1923 5. Call inflateBackEnd() passing it parameter values: |
|
1924 a. NULL for the stream argument |
|
1925 b. a stream whose state is NULL for the stream argument |
|
1926 c. a stream whose zfree is Z_NULL for the stream argument |
|
1927 @SYMTestExpectedResults inflateBackEnd() fails returning Z_STREAM_ERROR in all cases. |
|
1928 @SYMDEF REQ7362 |
|
1929 */ |
|
1930 |
|
1931 TVerdict CTestZlib::TestInflateBackEndFailsL() |
|
1932 { |
|
1933 int err = Z_OK; |
|
1934 |
|
1935 // Streams |
|
1936 z_stream deflateStream; |
|
1937 z_stream inflateStream; |
|
1938 |
|
1939 // Buffers |
|
1940 const char inputData[] = "This is a piece of data to compress.\0"; |
|
1941 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
1942 Byte *compressedDataBuffer = NULL; |
|
1943 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
1944 Byte *decompressedDataBuffer = NULL; |
|
1945 uLong decompressedDataBufferLength = inputDataLength; |
|
1946 |
|
1947 // deflateInit2 and inflateBackInit arguments |
|
1948 StreamSettings streamSettings; |
|
1949 streamSettings.deflateInit2 = true; |
|
1950 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
1951 streamSettings.method = Z_DEFLATED; |
|
1952 streamSettings.deflateWindowBits = -MAX_WBITS; |
|
1953 streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; |
|
1954 streamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
1955 streamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
1956 Bytef inflateWindow[32 * 1024]; |
|
1957 |
|
1958 // Allocate memory for buffers |
|
1959 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
1960 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
1961 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
1962 |
|
1963 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
1964 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
1965 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
1966 |
|
1967 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
1968 |
|
1969 // Initialise the inflate stream |
|
1970 InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); |
|
1971 CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); |
|
1972 |
|
1973 inflateStream.next_in = (unsigned char *)compressedDataBuffer; |
|
1974 inflateStream.avail_in = compressedDataBufferLength; |
|
1975 inflateStream.next_out = (unsigned char *)decompressedDataBuffer; |
|
1976 inflateStream.avail_out = decompressedDataBufferLength; |
|
1977 |
|
1978 struct bufferDescriptor compressedDataBufferDescriptor; |
|
1979 compressedDataBufferDescriptor.buffer = compressedDataBuffer; |
|
1980 compressedDataBufferDescriptor.next = 0; |
|
1981 compressedDataBufferDescriptor.length = compressedDataBufferLength; |
|
1982 |
|
1983 struct bufferDescriptor decompressedDataBufferDescriptor; |
|
1984 decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; |
|
1985 decompressedDataBufferDescriptor.next = 0; |
|
1986 decompressedDataBufferDescriptor.length = decompressedDataBufferLength; |
|
1987 |
|
1988 // Decompress the input |
|
1989 err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
1990 CHECK_CONDITION1L(err != Z_STREAM_END, KErrGeneral, "inflateBack error: %d", err); |
|
1991 |
|
1992 // Try cleaning up a NULL stream |
|
1993 err = inflateBackEnd(NULL); |
|
1994 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a NULL stream returned an unexpected value: %d", err); |
|
1995 |
|
1996 // Keep a pointer to the streams state so we can clean up properly with inflateBackEnd() |
|
1997 struct internal_state FAR *inflateState = inflateStream.state; |
|
1998 |
|
1999 // Try cleaning up an inflate stream with a NULL state |
|
2000 inflateStream.state = NULL; |
|
2001 err = inflateBackEnd(&inflateStream); |
|
2002 inflateStream.state = inflateState; |
|
2003 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a stream with a NULL state returned an unexpected value: %d", err); |
|
2004 |
|
2005 // Try cleaning up an inflate stream with no free function |
|
2006 free_func zfree = inflateStream.zfree; |
|
2007 inflateStream.zfree = Z_NULL; |
|
2008 err = inflateBackEnd(NULL); |
|
2009 inflateStream.zfree = zfree; |
|
2010 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackEnd on a stream with zfree set to NULL returned an unexpected value: %d", err); |
|
2011 |
|
2012 // Clean up the inflate stream |
|
2013 err = inflateBackEnd(&inflateStream); |
|
2014 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); |
|
2015 CleanupStack::Pop(1); |
|
2016 |
|
2017 CleanupStack::PopAndDestroy(2); |
|
2018 return EPass; |
|
2019 } |
|
2020 |
|
2021 /* |
|
2022 * in function required by inflateBack. |
|
2023 * Returning 0 indicates failure. |
|
2024 */ |
|
2025 unsigned inNoInput OF((void FAR *, unsigned char FAR * FAR *)) |
|
2026 { |
|
2027 return 0; |
|
2028 } |
|
2029 |
|
2030 /* |
|
2031 * out function required by inflateBack. |
|
2032 * Returning non 0 indicates failure. |
|
2033 */ |
|
2034 int outNoOutput OF((void FAR *, unsigned char FAR *, unsigned)) |
|
2035 { |
|
2036 return 1; |
|
2037 } |
|
2038 |
|
2039 /** |
|
2040 @SYMTestCaseID SYSLIB-EZLIB2-UT-4280 |
|
2041 @SYMTestCaseDesc Check inflateBack() fails when given an invalid stream or parameters. |
|
2042 @SYMTestPriority High |
|
2043 @SYMTestActions 1. Create a deflate stream and initialise it using deflateInit2(), |
|
2044 setting windowBits between -8 and-15. |
|
2045 2. Deflate the input and cleanup using deflateEnd(). |
|
2046 3. Create an inflate stream and initialise it using |
|
2047 inflateBackInit() setting windowBits to be equal to or greater |
|
2048 than the windowBits value used for deflateInit2(). |
|
2049 4. Call inflateBack() passing it parameter values: |
|
2050 a. NULL for the stream argument |
|
2051 b. a stream whose state is NULL for the stream argument |
|
2052 c. an input function that returns 0 for the in argument |
|
2053 d. an output function that doesn’t return 0 for the out argument |
|
2054 e. corrupted deflate output |
|
2055 5. Call inflateBackEnd() to clean up |
|
2056 @SYMTestExpectedResults inflateBack() fails returning Z_STREAM_ERROR for the first two |
|
2057 cases, Z_BUF_ERROR for the second two cases and Z_DATA_ERROR |
|
2058 for the final case. |
|
2059 @SYMDEF REQ7362 |
|
2060 */ |
|
2061 |
|
2062 TVerdict CTestZlib::TestInflateBackFailsL() |
|
2063 { |
|
2064 int err = Z_OK; |
|
2065 |
|
2066 // Streams |
|
2067 z_stream deflateStream; |
|
2068 z_stream inflateStream; |
|
2069 |
|
2070 // Buffers |
|
2071 const char inputData[] = "This is a piece of data to compress.\0"; |
|
2072 uLong inputDataLength = (uLong)strlen(inputData) + 1; |
|
2073 Byte *compressedDataBuffer = NULL; |
|
2074 uLong compressedDataBufferLength = COMPRESSED_DATA_BUFFER_LENGTH; |
|
2075 Byte *decompressedDataBuffer = NULL; |
|
2076 uLong decompressedDataBufferLength = inputDataLength; |
|
2077 |
|
2078 // deflateInit2 and inflateBackInit arguments |
|
2079 StreamSettings streamSettings; |
|
2080 streamSettings.deflateInit2 = true; |
|
2081 streamSettings.level = Z_DEFAULT_COMPRESSION; |
|
2082 streamSettings.method = Z_DEFLATED; |
|
2083 streamSettings.deflateWindowBits = -MAX_WBITS; |
|
2084 streamSettings.inflateWindowBits = -streamSettings.deflateWindowBits; // inflateBackInit expects the positive equivalent of the windowBits used to make a raw stream |
|
2085 streamSettings.memLevel = TEST_MID_MEM_LEVEL; |
|
2086 streamSettings.strategy = Z_DEFAULT_STRATEGY; |
|
2087 Bytef inflateWindow[32 * 1024]; |
|
2088 |
|
2089 // Allocate memory for buffers |
|
2090 compressedDataBuffer = (Byte *)User::AllocZ(compressedDataBufferLength * sizeof(Byte)); |
|
2091 CHECK_CONDITION0L(compressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for compressedDataBuffer."); |
|
2092 CleanupStack::PushL(TCleanupItem(User::Free, compressedDataBuffer)); |
|
2093 |
|
2094 decompressedDataBuffer = (Byte *)User::AllocZ(decompressedDataBufferLength * sizeof(Byte)); |
|
2095 CHECK_CONDITION0L(decompressedDataBuffer == NULL, KErrNoMemory, "Failed to allocate memory for decompressedDataBuffer."); |
|
2096 CleanupStack::PushL(TCleanupItem(User::Free, decompressedDataBuffer)); |
|
2097 |
|
2098 this->CompressDataL(streamSettings, deflateStream, (Byte *)inputData, inputDataLength, compressedDataBuffer, compressedDataBufferLength); |
|
2099 |
|
2100 // Initialise the inflate stream |
|
2101 InflateBackInitL(inflateStream, streamSettings.inflateWindowBits, inflateWindow); |
|
2102 CleanupStack::PushL(TCleanupItem(InflateBackEnd, &inflateStream)); |
|
2103 |
|
2104 // This must be set to NULL otherwise inflateBack will assume there is input and try to decompress it |
|
2105 inflateStream.next_in = NULL; |
|
2106 |
|
2107 struct bufferDescriptor compressedDataBufferDescriptor; |
|
2108 compressedDataBufferDescriptor.buffer = compressedDataBuffer; |
|
2109 compressedDataBufferDescriptor.next = 0; |
|
2110 compressedDataBufferDescriptor.length = compressedDataBufferLength; |
|
2111 |
|
2112 struct bufferDescriptor decompressedDataBufferDescriptor; |
|
2113 decompressedDataBufferDescriptor.buffer = decompressedDataBuffer; |
|
2114 decompressedDataBufferDescriptor.next = 0; |
|
2115 decompressedDataBufferDescriptor.length = decompressedDataBufferLength; |
|
2116 |
|
2117 // Try inflating a NULL stream |
|
2118 err = inflateBack(NULL, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
2119 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBack on a NULL stream returned an unexpected value: %d", err); |
|
2120 |
|
2121 // Try calling inflateBack on a deflate stream that has a NULL state |
|
2122 // We must keep a pointer to the streams state so we can clean up properly with deflateBackEnd |
|
2123 struct internal_state FAR *inflateState = inflateStream.state; |
|
2124 inflateStream.state = NULL; |
|
2125 |
|
2126 err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
2127 |
|
2128 inflateStream.state = inflateState; |
|
2129 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBack on a stream with a NULL state returned an unexpected value: %d", err); |
|
2130 |
|
2131 // Try inflating a stream with an in_func that returns no input data |
|
2132 err = inflateBack(&inflateStream, &inNoInput, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
2133 CHECK_CONDITION1L(err != Z_BUF_ERROR, KErrGeneral, "Calling inflateBack with an in function that returns no input returned an unexpected value: %d", err); |
|
2134 |
|
2135 // Try inflating a stream with an out_func that does not output any data |
|
2136 err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &outNoOutput, &decompressedDataBufferDescriptor); |
|
2137 CHECK_CONDITION1L(err != Z_BUF_ERROR, KErrGeneral, "Calling inflateBack with an in function that returns no input returned an unexpected value: %d", err); |
|
2138 |
|
2139 // Try inflating a corrupt stream |
|
2140 compressedDataBuffer[1] = 'a'; |
|
2141 compressedDataBuffer[2] = 'a'; |
|
2142 compressedDataBuffer[3] = 'a'; |
|
2143 err = inflateBack(&inflateStream, &in, &compressedDataBufferDescriptor, &out, &decompressedDataBufferDescriptor); |
|
2144 CHECK_CONDITION1L(err != Z_DATA_ERROR, KErrGeneral, "Calling inflateBack on a corrupt stream returned an unexpected value: %d", err); |
|
2145 |
|
2146 // Clean up the inflate stream |
|
2147 err = inflateBackEnd(&inflateStream); |
|
2148 CHECK_CONDITION1L(err != Z_OK, KErrGeneral, "inflateBackEnd error: %d", err); |
|
2149 CleanupStack::Pop(1); |
|
2150 |
|
2151 CleanupStack::PopAndDestroy(2); |
|
2152 return EPass; |
|
2153 } |
|
2154 |
|
2155 /** |
|
2156 @SYMTestCaseID SYSLIB-EZLIB2-UT-4281 |
|
2157 @SYMTestCaseDesc Check inflateBackInit() fails when given an invalid stream or parameters. |
|
2158 @SYMTestPriority High |
|
2159 @SYMTestActions 1. Create an inflate stream and initialise it using |
|
2160 inflateBackInit() passing it parameter values: |
|
2161 a. NULL for the stream argument |
|
2162 b. a stream whose window is NULL for the stream argument |
|
2163 c. < 8 for the windowBits argument |
|
2164 d. > 5 for the windowBits argument |
|
2165 @SYMTestExpectedResults inflateBackInit() should fail returning Z_STREAM_ERROR for all cases. |
|
2166 @SYMDEF REQ7362 |
|
2167 */ |
|
2168 |
|
2169 TVerdict CTestZlib::TestInflateBackInitFailsParamsL() |
|
2170 { |
|
2171 int err = Z_OK; |
|
2172 |
|
2173 // Streams |
|
2174 z_stream inflateStream; |
|
2175 |
|
2176 // inflateBackInit arguments |
|
2177 int inflateBackWindowBits = MAX_WBITS; |
|
2178 Bytef inflateWindow[32 * 1024]; |
|
2179 |
|
2180 // Try initialising a NULL stream |
|
2181 inflateStream.zalloc = Z_NULL; |
|
2182 inflateStream.zfree = Z_NULL; |
|
2183 inflateStream.opaque = Z_NULL; |
|
2184 |
|
2185 err = inflateBackInit(NULL, inflateBackWindowBits, inflateWindow); |
|
2186 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a NULL stream returned an unexpected value: %d", err); |
|
2187 |
|
2188 // Try initialising a stream with a NULL inflate window |
|
2189 err = inflateBackInit(&inflateStream, inflateBackWindowBits, NULL); |
|
2190 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with a NULL window returned an unexpected value: %d", err); |
|
2191 |
|
2192 // Try initialising a stream with window bits < 8 |
|
2193 err = inflateBackInit(&inflateStream, 7, inflateWindow); |
|
2194 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with windowBits < 8 returned an unexpected value: %d", err); |
|
2195 |
|
2196 // Try initialising a stream with window bits > 15 |
|
2197 err = inflateBackInit(&inflateStream, 16, inflateWindow); |
|
2198 CHECK_CONDITION1L(err != Z_STREAM_ERROR, KErrGeneral, "Calling inflateBackInit on a stream with windowBits > 15 returned an unexpected value: %d", err); |
|
2199 |
|
2200 return EPass; |
|
2201 } |
|
2202 |
|
2203 /** |
|
2204 @SYMTestCaseID SYSLIB-EZLIB2-UT-4282 |
|
2205 @SYMTestCaseDesc Check inflateBackInit() fails when there is not enough memory. |
|
2206 @SYMTestPriority High |
|
2207 @SYMTestActions 1. Create an inflate stream and set the next memory allocation to fail. |
|
2208 2. Initialise the stream using inflateBackInit() setting |
|
2209 windowBits to be between 8 and 15. |
|
2210 3. Check that the memory is the same before and after calling |
|
2211 inflateBackInit(). Note: If Z_OK is returned it will be |
|
2212 necessary to call inflateEnd() before checking the amount of memory. |
|
2213 4. Repeat this process until inflateBackInit() returns Z_OK, |
|
2214 increasing the number of allocations that can be performed before failing. |
|
2215 @SYMTestExpectedResults inflateBackInit () fails at first, returning Z_MEM_ERROR, and then |
|
2216 it will succeed with Z_OK. No memory should be leaked. |
|
2217 @SYMDEF REQ7362 |
|
2218 */ |
|
2219 |
|
2220 TVerdict CTestZlib::TestInflateBackInitFailsMem() |
|
2221 { |
|
2222 TInt err; |
|
2223 TVerdict verdict = EPass; |
|
2224 |
|
2225 // Streams |
|
2226 z_stream inflateStream; |
|
2227 |
|
2228 // inflateBackInit arguments |
|
2229 int inflateBackWindowBits = MAX_WBITS; |
|
2230 Bytef inflateWindow[32 * 1024]; |
|
2231 |
|
2232 inflateStream.zalloc = Z_NULL; |
|
2233 inflateStream.zfree = Z_NULL; |
|
2234 inflateStream.opaque = Z_NULL; |
|
2235 |
|
2236 TInt failureRate; |
|
2237 for(failureRate = 1, err = Z_MEM_ERROR; err != Z_OK && failureRate <= FAILURE_RATE_LIMIT; failureRate++) |
|
2238 { |
|
2239 __UHEAP_SETFAIL(RHeap::EDeterministic, failureRate); |
|
2240 __UHEAP_MARK; |
|
2241 |
|
2242 err = inflateBackInit(&inflateStream, inflateBackWindowBits, inflateWindow); |
|
2243 |
|
2244 // Memory has been allocated so we need to clean up |
|
2245 if(err == Z_OK) |
|
2246 { |
|
2247 err = inflateBackEnd(&inflateStream); |
|
2248 if(err != Z_OK) |
|
2249 { |
|
2250 INFO_PRINTF2(_L("inflateBackInit error: %d"), err); |
|
2251 verdict = EAbort; |
|
2252 break; |
|
2253 } |
|
2254 } |
|
2255 else if(err != Z_MEM_ERROR) |
|
2256 { |
|
2257 INFO_PRINTF2(_L("inflateBackInit unexpected error: %d"), err); |
|
2258 verdict = EFail; |
|
2259 break; |
|
2260 } |
|
2261 |
|
2262 __UHEAP_MARKEND; |
|
2263 __UHEAP_RESET; |
|
2264 } |
|
2265 |
|
2266 if(err == Z_OK) |
|
2267 { |
|
2268 INFO_PRINTF2(_L("The test succeeded at heap failure rate = %d."), --failureRate); |
|
2269 } |
|
2270 else if(failureRate > FAILURE_RATE_LIMIT) |
|
2271 { |
|
2272 INFO_PRINTF1(_L("Exceeded FAILURE_RATE_LIMIT. Either the test has failed or the limit needs increasing.")); |
|
2273 verdict = EFail; |
|
2274 } |
|
2275 |
|
2276 return verdict; |
|
2277 } |
|
2278 |
|
2279 /** |
|
2280 @SYMTestCaseID SYSLIB-EZLIB2-UT-4283 |
|
2281 @SYMTestCaseDesc Check adler32_combine() generates the correct adler32 checksum. |
|
2282 @SYMTestPriority High |
|
2283 @SYMTestActions 1. Create two byte buffers, both containing different data, and |
|
2284 use these to create two checksums using adler32(). |
|
2285 2. Create a third checksum using adler32_combine(), providing it |
|
2286 with the first two checksums and the length of the second checksum. |
|
2287 3. Concatenate the two byte buffers and create a fourth checksum |
|
2288 using adler32() and the concatenated buffer as input. |
|
2289 4. Compare the third and fourth checksums. |
|
2290 @SYMTestExpectedResults The third and fourth checksums should be identical. |
|
2291 @SYMDEF REQ7362 |
|
2292 */ |
|
2293 |
|
2294 TVerdict CTestZlib::TestAdler32CombineL() |
|
2295 { |
|
2296 // Byte buffers from which the adler32 checksums will be generated |
|
2297 const Byte *string1 = (Byte *)"Hello"; |
|
2298 const Byte *string2 = (Byte *)"World"; |
|
2299 const int string1Length = 5; |
|
2300 const int string2Length = 5; |
|
2301 const int string3Length = 10; |
|
2302 |
|
2303 // Initialise the adler32 variables |
|
2304 uLong adlerString1 = adler32(0L, NULL, 0); |
|
2305 uLong adlerString2 = adler32(0L, NULL, 0); |
|
2306 uLong adlerString3 = adler32(0L, NULL, 0); |
|
2307 uLong adlerCombined; |
|
2308 |
|
2309 // Generate the checksums from the byte buffers |
|
2310 adlerString1 = adler32(adlerString1, string1, string1Length); |
|
2311 adlerString2 = adler32(adlerString2, string2, string2Length); |
|
2312 |
|
2313 // Generate the checksum from combining adlerString1 and adlerString2 |
|
2314 adlerCombined = adler32_combine(adlerString1, adlerString2, string2Length); |
|
2315 |
|
2316 // Concatenate the byte buffers so that a checksum can be generated |
|
2317 Byte string3[string3Length]; |
|
2318 memcpy((char *)string3, (char *)string1, string1Length); |
|
2319 memcpy((char *)string3 + string1Length, (char *)string2, string2Length); |
|
2320 |
|
2321 // Generate checksum |
|
2322 adlerString3 = adler32(adlerString3, string3, string3Length); |
|
2323 |
|
2324 // Compare the checksums to see if they are the same |
|
2325 CHECK_CONDITION0L(adlerString3 != adlerCombined, KErrGeneral, "The combined checksum is not correct."); |
|
2326 |
|
2327 return EPass; |
|
2328 } |
|
2329 |
|
2330 /** |
|
2331 @SYMTestCaseID SYSLIB-EZLIB2-UT-4284 |
|
2332 @SYMTestCaseDesc Check crc32_combine() generates the correct crc32 checksum. |
|
2333 @SYMTestPriority High |
|
2334 @SYMTestActions 1. Create two byte buffers, both containing different data, and |
|
2335 use these to create two checksums using crc32(). |
|
2336 2. Create a third checksum using crc32_combine(), providing it |
|
2337 with the first two checksums and the length of the second checksum. |
|
2338 3. Concatenate the two byte buffers and create a fourth checksum |
|
2339 using crc32() and the concatenated buffer as input. |
|
2340 4. Compare the third and fourth checksums. |
|
2341 @SYMTestExpectedResults The third and fourth checksums should be identical. |
|
2342 @SYMDEF REQ7362 |
|
2343 */ |
|
2344 |
|
2345 TVerdict CTestZlib::TestCrc32CombineL() |
|
2346 { |
|
2347 // Byte buffers from which the crc32 checksums will be generated |
|
2348 const Byte *string1 = (Byte *)"Hello"; |
|
2349 const Byte *string2 = (Byte *)"World"; |
|
2350 const int string1Length = 5; |
|
2351 const int string2Length = 5; |
|
2352 const int string3Length = 10; |
|
2353 |
|
2354 // Initialise the crc32 variables |
|
2355 uLong crcString1 = crc32(0L, NULL, 0); |
|
2356 uLong crcString2 = crc32(0L, NULL, 0); |
|
2357 uLong crcString3 = crc32(0L, NULL, 0); |
|
2358 uLong crcCombined; |
|
2359 |
|
2360 // Generate the checksums from the byte buffers |
|
2361 crcString1 = crc32(crcString1, string1, string1Length); |
|
2362 crcString2 = crc32(crcString2, string2, string2Length); |
|
2363 |
|
2364 // Generate the checksum from combining adlerString1 and adlerString2 |
|
2365 crcCombined = crc32_combine(crcString1, crcString2, string2Length); |
|
2366 |
|
2367 // Concatenate the byte buffers so that a checksum can be generated |
|
2368 Byte string3[string3Length]; |
|
2369 memcpy((char *)string3, (char *)string1, string1Length); |
|
2370 memcpy((char *)string3 + string1Length, (char *)string2, string2Length); |
|
2371 |
|
2372 // Generate checksum |
|
2373 crcString3 = crc32(crcString3, string3, string3Length); |
|
2374 |
|
2375 // Compare the checksums to see if they are the same |
|
2376 CHECK_CONDITION0L(crcString3 != crcCombined, KErrGeneral, "The combined checksum is not correct."); |
|
2377 |
|
2378 return EPass; |
|
2379 } |
|
2380 |
|
2381 /** |
|
2382 @SYMTestCaseID SYSLIB-EZLIB2-UT-4284 |
|
2383 @SYMTestCaseDesc Check zlibCompileFlags() returns a uLong with the correct |
|
2384 compile flags set. |
|
2385 @SYMTestPriority Medium |
|
2386 @SYMTestActions 1. Call zlibCompileFlags() and compare the first 2 bits with the |
|
2387 size of uInt, the second 2 bits with the size of uLong and |
|
2388 bits 16 and 17 with 0 (GZip APIs are being used). |
|
2389 @SYMTestExpectedResults zlibCompileFlags() will return a uLong and all the comparisons |
|
2390 will return true. |
|
2391 @SYMDEF REQ7362 |
|
2392 */ |
|
2393 |
|
2394 TVerdict CTestZlib::TestZlibCompileFlagsL() |
|
2395 { |
|
2396 // Get the compilerFlags |
|
2397 uLong compileFlags = zlibCompileFlags(); |
|
2398 int compileFlagsUInt = compileFlags & 0x3; |
|
2399 int compileFlagsULong = (compileFlags >> 2) & 0x3; |
|
2400 int compileFlagsGZip = (compileFlags >> 15) & 0x3; |
|
2401 int machineUInt; |
|
2402 int machineULong; |
|
2403 |
|
2404 // Get the size of uInt |
|
2405 switch(sizeof(uInt)) |
|
2406 { |
|
2407 case 2: machineUInt = 0; |
|
2408 break; |
|
2409 case 4: machineUInt = 1; |
|
2410 break; |
|
2411 case 8: machineUInt = 2; |
|
2412 break; |
|
2413 default: machineUInt = 3; |
|
2414 } |
|
2415 |
|
2416 // Check the compiler flag for uInt is correct |
|
2417 CHECK_CONDITION0L(machineUInt != compileFlagsUInt, KErrGeneral, "zlibCompileFlags reports an incorrect size for uInt."); |
|
2418 |
|
2419 // Get the size of uLong |
|
2420 switch(sizeof(uLong)) |
|
2421 { |
|
2422 case 2: machineULong = 0; |
|
2423 break; |
|
2424 case 4: machineULong = 1; |
|
2425 break; |
|
2426 case 8: machineULong = 2; |
|
2427 break; |
|
2428 default: machineULong = 3; |
|
2429 } |
|
2430 |
|
2431 // Check the compiler flag for uLong is correct |
|
2432 CHECK_CONDITION0L(machineULong != compileFlagsULong, KErrGeneral, "zlibCompileFlags reports an incorrect size for uLong."); |
|
2433 |
|
2434 // Check the compiler flags for GZip compression are correct |
|
2435 CHECK_CONDITION0L(compileFlagsGZip != 0, KErrGeneral, "zlibCompileFlags reports GZip functionality is disabled."); |
|
2436 |
|
2437 return EPass; |
|
2438 } |