1 /**
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include <cstdio>
18 #include <cstdlib>
19 #include <fstream>
20 #include <gtest/gtest.h>
21 #include <securec.h>
22 #include <sstream>
23 #include <string>
24
25 #include "zlib.h"
26
27 using namespace std;
28 using namespace testing::ext;
29 namespace {
30 static const char DICTIONARY[] = "hello";
31 static const char GARBAGE[] = "garbage";
32 static const char TESTFILE[] = "foo.gz";
33 static char HELLO[] = "hello, hello!";
34 static unsigned int CALLOC_SIZE = 1;
35 static int ONE = 1;
36 static int FOUR = 4;
37 static int SIX = 6;
38 static int EIGHT = 8;
39 static int GARBAGE_LEN = strlen(GARBAGE);
40 static unsigned BUFFER_SIZE = 8192;
41 }
42
pull(void * desc,unsigned char ** buf)43 static unsigned pull(void *desc, unsigned char **buf)
44 {
45 static unsigned int next = 0;
46 static unsigned char dat[] = {0x63, 0, 2, 0};
47
48 if (desc == nullptr) {
49 next = 0;
50 return 0; /* no input (already provided at next_in) */
51 }
52 return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
53 }
54
push(void * desc,unsigned char * buf,unsigned len)55 static int push(void *desc, unsigned char *buf, unsigned len)
56 {
57 buf += len;
58 return desc != nullptr; /* force error if desc not null */
59 }
60
TestGzPrintf(gzFile file,const char * format,...)61 static int TestGzPrintf(gzFile file, const char *format, ...)
62 {
63 va_list va;
64 int ret;
65
66 va_start(va, format);
67 ret = gzvprintf(file, format, va);
68 va_end(va);
69 return ret;
70 }
71
72 class ActsZlibTest : public testing::Test {
73 protected:
74 ActsZlibTest();
75 ~ActsZlibTest();
76 static void SetUpTestCase();
77 static void TearDownTestCase();
78 };
79
ActsZlibTest()80 ActsZlibTest::ActsZlibTest()
81 {}
82
~ActsZlibTest()83 ActsZlibTest::~ActsZlibTest()
84 {}
85
SetUpTestCase()86 void ActsZlibTest::SetUpTestCase()
87 {}
88
TearDownTestCase()89 void ActsZlibTest::TearDownTestCase()
90 {}
91
92 /* these items are strung together in a linked list, one for each allocation */
93 struct mem_item {
94 void *ptr; /* pointer to allocated memory */
95 size_t size; /* requested size of allocation */
96 struct mem_item *next; /* pointer to next item in list, or NULL */
97 };
98
99 /* this structure is at the root of the linked list, and tracks statistics */
100 struct mem_zone {
101 struct mem_item *first; /* pointer to first item in list, or NULL */
102 size_t total, highwater; /* total allocations, and largest total */
103 size_t limit; /* memory allocation limit, or 0 if no limit */
104 int notlifo, rogue; /* counts of non-LIFO frees and rogue frees */
105 };
106
107 /**
108 * @tc.number : ActsZlibTest_0100
109 * @tc.name : Test compress and uncompress test
110 * @tc.desc : [C- SOFTWARE -0200]
111 */
112 HWTEST_F(ActsZlibTest, ActsZlibTestCompress, Function | MediumTest | Level2)
113 {
114 #ifdef Z_SOLO
115 fprintf(stderr, "*********ActsZlibTestCompress Z_SOLO**********\n");
116 #else
117 Byte *compr, *uncompr;
118 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
119 uLong uncomprLen = comprLen;
120 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
121 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
122 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
123
124 int err = Z_OK;
125 uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
126 err = compress(compr, &comprLen, reinterpret_cast<const Bytef*>(HELLO), len);
127 fprintf(stderr, "compress error: %d\n", err);
128 ASSERT_EQ(err, Z_OK);
129
130 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
131 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
132 fprintf(stderr, "uncompress error: %d\n", err);
133 ASSERT_EQ(err, Z_OK);
134 fprintf(stderr, "uncompress: %s\n", reinterpret_cast<char *>(uncompr));
135 free(compr);
136 free(uncompr);
137 #endif
138 }
139
140 /**
141 * @tc.number : ActsZlibTest_0200
142 * @tc.name : Test gzio
143 * @tc.desc : [C- SOFTWARE -0200]
144 */
145 HWTEST_F(ActsZlibTest, ActsZlibTestGzio, Function | MediumTest | Level2)
146 {
147 #ifdef Z_SOLO
148 fprintf(stderr, "*********ActsZlibTestGzio Z_SOLO**********\n");
149 #else
150 int err = Z_OK;
151 int len = static_cast<int>(strlen(HELLO)) + 1;
152 gzFile file;
153 z_off_t pos;
154 file = gzopen(TESTFILE, "wb");
155 ASSERT_TRUE(file != NULL);
156
157 gzputc(file, 'h');
158 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
159 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
160 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
161 ASSERT_TRUE(false);
162 }
163
164 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
165 gzclose(file);
166 file = gzopen(TESTFILE, "rb");
167 ASSERT_TRUE(file != NULL);
168
169 Byte *compr, *uncompr;
170 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
171 uLong uncomprLen = comprLen;
172 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
173 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
174 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
175
176 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
177 ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
178 ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO));
179
180 pos = gzseek(file, -8L, SEEK_CUR);
181 ASSERT_FALSE(pos != SIX || gztell(file) != pos);
182 ASSERT_FALSE(gzgetc(file) != ' ');
183 ASSERT_FALSE(gzungetc(' ', file) != ' ');
184
185 fprintf(stderr, "gzgets\n");
186 gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
187 ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
188 gzclose(file);
189 free(compr);
190 free(uncompr);
191 #endif
192 }
193
194 /**
195 * @tc.number : ActsZlibTest_0300
196 * @tc.name : Test deflate
197 * @tc.desc : [C- SOFTWARE -0200]
198 */
199 HWTEST_F(ActsZlibTest, ActsZlibTestDeflate, Function | MediumTest | Level2)
200 {
201 Byte *compr;
202 uLong comprLen = 10000 * sizeof(int);
203 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
204 ASSERT_TRUE(compr != Z_NULL);
205
206 z_stream c_stream; /* compression stream */
207 int err = Z_OK;
208 uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
209 c_stream.zalloc = nullptr;
210 c_stream.zfree = nullptr;
211 c_stream.opaque = nullptr;
212 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
213 fprintf(stderr, "deflateInit result: %d\n", err);
214 ASSERT_EQ(err, Z_OK);
215
216 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
217 c_stream.next_out = compr;
218 fprintf(stderr, "deflate\n");
219 while (c_stream.total_in != len && c_stream.total_out < comprLen) {
220 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
221 err = deflate(&c_stream, Z_NO_FLUSH);
222 fprintf(stderr, "deflate result: %d\n", err);
223 ASSERT_EQ(err, Z_OK);
224 }
225
226 for (;;) {
227 c_stream.avail_out = 1;
228 err = deflate(&c_stream, Z_FINISH);
229 fprintf(stderr, "deflate result: %d\n", err);
230 if (err == Z_STREAM_END) {
231 break;
232 }
233 ASSERT_EQ(err, Z_OK);
234 }
235
236 err = deflateEnd(&c_stream);
237 fprintf(stderr, "deflateEnd result: %d\n", err);
238 ASSERT_EQ(err, Z_OK);
239 free(compr);
240 }
241
242 /**
243 * @tc.number : ActsZlibTest_0400
244 * @tc.name : Test inflate
245 * @tc.desc : [C- SOFTWARE -0200]
246 */
247 HWTEST_F(ActsZlibTest, ActsZlibTestInflate, Function | MediumTest | Level2)
248 {
249 Byte *compr, *uncompr;
250 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
251 uLong uncomprLen = comprLen;
252 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
253 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
254 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
255
256 int err = Z_OK;
257 z_stream d_stream; /* decompression stream */
258 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
259 d_stream.zalloc = nullptr;
260 d_stream.zfree = nullptr;
261 d_stream.opaque = nullptr;
262 d_stream.next_in = compr;
263 d_stream.avail_in = 0;
264 d_stream.next_out = uncompr;
265 err = inflateInit(&d_stream);
266 fprintf(stderr, "inflateInit result: %d\n", err);
267 ASSERT_EQ(err, Z_OK);
268
269 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
270 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
271 err = inflate(&d_stream, Z_NO_FLUSH);
272 if (err == Z_STREAM_END || err == Z_DATA_ERROR) {
273 break;
274 }
275 fprintf(stderr, "inflate result: %d\n", err);
276 ASSERT_EQ(err, Z_OK);
277 }
278
279 fprintf(stderr, "inflateEnd result: %d\n", inflateEnd(&d_stream));
280 free(compr);
281 free(uncompr);
282 }
283
284 /**
285 * @tc.number : ActsZlibTest_0500
286 * @tc.name : Test deflate with large buffers and dynamic change of compression level
287 * @tc.desc : [C- SOFTWARE -0200]
288 */
289 HWTEST_F(ActsZlibTest, ActsZlibTestLargeDeflate, Function | MediumTest | Level2)
290 {
291 Byte *compr, *uncompr;
292 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
293 uLong uncomprLen = comprLen;
294 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
295 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
296 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
297
298 z_stream c_stream; /* compression stream */
299 int err = Z_OK;
300 c_stream.zalloc = nullptr;
301 c_stream.zfree = nullptr;
302 c_stream.opaque = nullptr;
303
304 err = deflateInit(&c_stream, Z_BEST_SPEED);
305 fprintf(stderr, "deflateInit result: %d\n", err);
306 ASSERT_EQ(err, Z_OK);
307
308 c_stream.next_out = compr;
309 c_stream.avail_out = static_cast<uInt>(comprLen);
310
311 /* At this point, uncompr is still mostly zeroes, so it should compress
312 * very well:
313 */
314 c_stream.next_in = uncompr;
315 c_stream.avail_in = static_cast<uInt>(uncomprLen);
316 err = deflate(&c_stream, Z_NO_FLUSH);
317 fprintf(stderr, "deflate result: %d\n", err);
318 ASSERT_EQ(err, Z_OK);
319 ASSERT_TRUE(c_stream.avail_in == 0);
320
321 /* Feed in already compressed data and switch to no compression: */
322 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
323 c_stream.next_in = compr;
324 c_stream.avail_in = static_cast<uInt>(comprLen) / 2;
325 err = deflate(&c_stream, Z_NO_FLUSH);
326 fprintf(stderr, "deflate result: %d\n", err);
327 ASSERT_EQ(err, Z_OK);
328
329 /* Switch back to compressing mode: */
330 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
331 c_stream.next_in = uncompr;
332 c_stream.avail_in = static_cast<uInt>(uncomprLen);
333 err = deflate(&c_stream, Z_NO_FLUSH);
334 ASSERT_EQ(err, Z_OK);
335
336 err = deflate(&c_stream, Z_FINISH);
337 ASSERT_EQ(err, Z_STREAM_END);
338
339 err = deflateEnd(&c_stream);
340 ASSERT_EQ(err, Z_OK);
341 free(compr);
342 free(uncompr);
343 }
344
345 /**
346 * @tc.number : ActsZlibTest_0600
347 * @tc.name : Test inflate with large buffers
348 * @tc.desc : [C- SOFTWARE -0200]
349 */
350 HWTEST_F(ActsZlibTest, ActsZlibTestLargeInflate, Function | MediumTest | Level2)
351 {
352 Byte *compr, *uncompr;
353 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
354 uLong uncomprLen = comprLen;
355 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
356 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
357 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
358
359 int err = Z_OK;
360 z_stream d_stream; /* decompression stream */
361 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
362 d_stream.zalloc = nullptr;
363 d_stream.zfree = nullptr;
364 d_stream.opaque = nullptr;
365 d_stream.next_in = compr;
366 d_stream.avail_in = static_cast<uInt>(comprLen);
367 err = inflateInit(&d_stream);
368 ASSERT_EQ(err, Z_OK);
369
370 for (;;) {
371 d_stream.next_out = uncompr; /* discard the output */
372 d_stream.avail_out = static_cast<uInt>(uncomprLen);
373 err = inflate(&d_stream, Z_NO_FLUSH);
374 if (err == Z_STREAM_END || err == Z_DATA_ERROR) {
375 break;
376 }
377 ASSERT_EQ(err, Z_OK);
378 }
379
380 err = inflateEnd(&d_stream);
381 ASSERT_EQ(err, Z_OK);
382 free(compr);
383 free(uncompr);
384 }
385
386 /**
387 * @tc.number : ActsZlibTest_0700
388 * @tc.name : Test deflate with full flush
389 * @tc.desc : [C- SOFTWARE -0200]
390 */
391 HWTEST_F(ActsZlibTest, ActsZlibTestFlush, Function | MediumTest | Level2)
392 {
393 Byte *compr;
394 uLong comprLen = 10000 * sizeof(int);
395 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
396 ASSERT_TRUE(compr != Z_NULL);
397
398 z_stream c_stream; /* compression stream */
399 int err = Z_OK;
400 uInt len = static_cast<uInt>(strlen(HELLO)) + 1;
401 c_stream.zalloc = nullptr;
402 c_stream.zfree = nullptr;
403 c_stream.opaque = nullptr;
404 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
405 ASSERT_EQ(err, Z_OK);
406
407 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
408 c_stream.next_out = compr;
409 c_stream.avail_in = 3;
410 c_stream.avail_out = static_cast<uInt>(comprLen);
411 err = deflate(&c_stream, Z_FULL_FLUSH);
412 ASSERT_EQ(err, Z_OK);
413
414 compr[3]++; /* force an error in first compressed block */
415 c_stream.avail_in = len - 3;
416 err = deflate(&c_stream, Z_FINISH);
417 if (err != Z_STREAM_END) {
418 ASSERT_EQ(err, Z_OK);
419 }
420
421 err = deflateEnd(&c_stream);
422 ASSERT_EQ(err, Z_OK);
423 comprLen = c_stream.total_out;
424 free(compr);
425 }
426
427 /**
428 * @tc.number : ActsZlibTest_0800
429 * @tc.name : Test inflateSync
430 * @tc.desc : [C- SOFTWARE -0200]
431 */
432 HWTEST_F(ActsZlibTest, ActsZlibTestSync, Function | MediumTest | Level2)
433 {
434 Byte *compr, *uncompr;
435 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
436 uLong uncomprLen = comprLen;
437 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
438 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
439 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
440
441 int err = Z_OK;
442 z_stream d_stream; /* decompression stream */
443 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
444 d_stream.zalloc = nullptr;
445 d_stream.zfree = nullptr;
446 d_stream.opaque = nullptr;
447 d_stream.next_in = compr;
448 d_stream.avail_in = 2; /* just read the zlib header */
449 err = inflateInit(&d_stream);
450 ASSERT_EQ(err, Z_OK);
451
452 d_stream.next_out = uncompr;
453 d_stream.avail_out = static_cast<uInt>(uncomprLen);
454
455 inflate(&d_stream, Z_NO_FLUSH);
456 d_stream.avail_in = static_cast<uInt>(comprLen) - 2; /* read all compressed data */
457 inflateSync(&d_stream);
458 inflate(&d_stream, Z_FINISH);
459 inflateEnd(&d_stream);
460 printf("after inflateSync: hel%s\n", reinterpret_cast<char *>(uncompr));
461 free(compr);
462 free(uncompr);
463 }
464
465 /**
466 * @tc.number : ActsZlibTest_0900
467 * @tc.name : Test deflate with preset dictionary
468 * @tc.desc : [C- SOFTWARE -0200]
469 */
470 HWTEST_F(ActsZlibTest, ActsZlibTestDictDeflate, Function | MediumTest | Level2)
471 {
472 Byte *compr, *uncompr;
473 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
474 uLong uncomprLen = comprLen;
475 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
476 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
477 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
478
479 z_stream c_stream; /* compression stream */
480 int err = Z_OK;
481 c_stream.zalloc = nullptr;
482 c_stream.zfree = nullptr;
483 c_stream.opaque = nullptr;
484 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
485 ASSERT_EQ(err, Z_OK);
486
487 err = deflateSetDictionary(&c_stream,
488 reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
489 ASSERT_EQ(err, Z_OK);
490
491 c_stream.next_out = compr;
492 c_stream.avail_out = static_cast<uInt>(comprLen);
493 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
494 c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + 1;
495 err = deflate(&c_stream, Z_FINISH);
496 ASSERT_EQ(err, Z_STREAM_END);
497
498 err = deflateEnd(&c_stream);
499 ASSERT_EQ(err, Z_OK);
500 free(compr);
501 free(uncompr);
502 }
503
504 /**
505 * @tc.number : ActsZlibTest_1000
506 * @tc.name : Test inflate with a preset dictionary
507 * @tc.desc : [C- SOFTWARE -0200]
508 */
509 HWTEST_F(ActsZlibTest, ActsZlibTestDictInflate, Function | MediumTest | Level2)
510 {
511 Byte *compr, *uncompr;
512 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
513 uLong uncomprLen = comprLen;
514 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
515 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
516 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
517
518 int err = Z_OK;
519 z_stream d_stream; /* decompression stream */
520 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
521 d_stream.zalloc = nullptr;
522 d_stream.zfree = nullptr;
523 d_stream.opaque = nullptr;
524 d_stream.next_in = compr;
525 d_stream.avail_in = static_cast<uInt>(comprLen);
526 err = inflateInit(&d_stream);
527 ASSERT_EQ(err, Z_OK);
528 d_stream.next_out = uncompr;
529 d_stream.avail_out = static_cast<uInt>(uncomprLen);
530 for (;;) {
531 err = inflate(&d_stream, Z_NO_FLUSH);
532 if (err == Z_STREAM_END) {
533 break;
534 }
535 if (err == Z_NEED_DICT) {
536 err = inflateSetDictionary(
537 &d_stream, reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
538 }
539 if (err == Z_DATA_ERROR) {
540 break;
541 }
542 ASSERT_EQ(err, Z_OK);
543 }
544
545 err = inflateEnd(&d_stream);
546 ASSERT_EQ(err, Z_OK);
547 if (strcmp(reinterpret_cast<char *>(uncompr), HELLO)) {
548 fprintf(stderr, "bad inflate with dict\n");
549 } else {
550 printf("inflate with dictionary: %s\n", reinterpret_cast<char *>(uncompr));
551 }
552
553 free(compr);
554 free(uncompr);
555 }
556
557 /**
558 * @tc.number : ActsZlibTest_1100
559 * @tc.name : Test compress2 with Z_BEST_COMPRESSION level
560 * @tc.desc : [C- SOFTWARE -0200]
561 */
562 HWTEST_F(ActsZlibTest, ActsZlibTestCompress2, Function | MediumTest | Level2)
563 {
564 #ifdef Z_SOLO
565 fprintf(stderr, "*********ActsZlibTestCompress2 Z_BEST_COMPRESSION Z_SOLO**********\n");
566 #else
567 Byte *compr, *uncompr;
568 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
569 uLong uncomprLen = comprLen;
570 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
571 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
572 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
573
574 int err = Z_OK;
575 uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
576 uLong outLen = compressBound(len);
577 fprintf(stderr, "compressBound result: %lu\n", outLen);
578 err = compress2(compr, &comprLen, reinterpret_cast<const Bytef*>(HELLO), outLen, Z_BEST_COMPRESSION);
579 fprintf(stderr, "compress2 Z_BEST_COMPRESSION result: %d\n", err);
580 ASSERT_EQ(err, Z_OK);
581
582 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
583 err = uncompress2(uncompr, &uncomprLen, compr, &comprLen);
584 fprintf(stderr, "uncompress2 Z_BEST_COMPRESSION result: %d\n", err);
585 ASSERT_EQ(err, Z_OK);
586 fprintf(stderr, "uncompress2: %s\n", reinterpret_cast<char *>(uncompr));
587 free(compr);
588 free(uncompr);
589 #endif
590 }
591
592 /**
593 * @tc.number : ActsZlibTest_1200
594 * @tc.name : Test adler32
595 * @tc.desc : [C- SOFTWARE -0200]
596 */
597 HWTEST_F(ActsZlibTest, ActsZlibTestAdler, Function | MediumTest | Level2)
598 {
599 uLong err = Z_ERRNO;
600 uLong adler1 = 0L;
601 uLong adler2 = 0L;
602 const Bytef *buf = reinterpret_cast<const Bytef*>(DICTIONARY);
603 err = adler32(0L, buf, 0);
604 fprintf(stderr, "adler32 result: %lu\n", err);
605 ASSERT_NE(err, Z_ERRNO);
606
607 err = adler32_z(0L, buf, 0);
608 fprintf(stderr, "adler32_z result: %lu\n", err);
609 ASSERT_NE(err, Z_ERRNO);
610 #ifdef Z_SOLO
611 #ifndef Z_LARGE64
612 err = adler32_combine64(adler1, adler2, 0);
613 fprintf(stderr, "adler32_combine64 result: %lu\n", err);
614 ASSERT_NE(err, Z_ERRNO);
615 #endif
616 #else
617 err = adler32_combine(adler1, adler2, 0);
618 fprintf(stderr, "adler32_combine result: %lu\n", err);
619 ASSERT_NE(err, Z_ERRNO);
620 #endif
621 }
622
623 /**
624 * @tc.number : ActsZlibTest_1300
625 * @tc.name : Test deflate state
626 * @tc.desc : [C- SOFTWARE -0200]
627 */
628 HWTEST_F(ActsZlibTest, ActsZlibTestDeflateState, Function | MediumTest | Level2)
629 {
630 Byte *compr, *uncompr;
631 int *bits = nullptr;
632 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
633 uLong uncomprLen = comprLen;
634 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
635 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
636 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
637
638 gz_headerp headerp = nullptr;
639 z_stream c_stream; /* compression stream */
640 int err = Z_OK;
641 int windowBits = EIGHT;
642 int memLevel = EIGHT;
643 c_stream.zalloc = nullptr;
644 c_stream.zfree = nullptr;
645 c_stream.opaque = nullptr;
646 err = deflateInit2(
647 &c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED);
648 ASSERT_EQ(err, Z_OK);
649 deflateSetHeader(&c_stream, headerp);
650 deflateTune(&c_stream, ONE, FOUR, EIGHT, ONE);
651 memLevel = ONE;
652 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
653 fprintf(stderr, "deflateParams result: %d\n", err);
654 ASSERT_EQ(err, Z_OK);
655
656 err = deflatePending(&c_stream, nullptr, bits);
657 fprintf(stderr, "deflatePending result: %d\n", err);
658 ASSERT_EQ(err, Z_OK);
659
660 err = deflateSetDictionary(&c_stream,
661 reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
662 fprintf(stderr, "deflateGetDictionary result: %d\n", err);
663 ASSERT_EQ(err, Z_OK);
664
665 err = deflateGetDictionary(&c_stream, uncompr, nullptr);
666 fprintf(stderr, "deflateGetDictionary result: %d\n", err);
667 err = deflatePrime(&c_stream, EIGHT, ONE);
668 fprintf(stderr, "deflatePrime result: %d\n", err);
669 c_stream.next_out = compr;
670 c_stream.avail_out = static_cast<uInt>(comprLen);
671 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
672 c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + 1;
673 err = deflate(&c_stream, Z_FINISH);
674 ASSERT_EQ(err, Z_STREAM_END);
675 err = deflateEnd(&c_stream);
676 ASSERT_EQ(err, Z_OK);
677 #ifdef Z_SOLO
678 err = deflateResetKeep(&c_stream);
679 fprintf(stderr, "deflateReset result: %d\n", err);
680 #else
681 err = deflateReset(&c_stream);
682 fprintf(stderr, "deflateReset result: %d\n", err);
683 #endif
684 free(compr);
685 free(uncompr);
686 }
687
688 /**
689 * @tc.number : ActsZlibTest_1400
690 * @tc.name : Test deflateBound
691 * @tc.desc : [C- SOFTWARE -0200]
692 */
693 HWTEST_F(ActsZlibTest, ActsZlibTestDeflateBound, Function | MediumTest | Level2)
694 {
695 #ifdef Z_SOLO
696 z_stream defstream;
697 char *inBuf = reinterpret_cast<char *>(HELLO);
698 uint32_t inLen = strlen(inBuf) + 1;
699 uint8_t *outBuf = nullptr;
700 uint32_t outLen = 0;
701 int err = Z_OK;
702
703 defstream.zalloc = nullptr;
704 defstream.zfree = nullptr;
705 defstream.opaque = nullptr;
706 defstream.avail_in = static_cast<uInt>(inLen);
707 defstream.next_in = reinterpret_cast<Bytef *>(inBuf);
708 defstream.avail_out = static_cast<uInt>(outLen);
709 defstream.next_out = reinterpret_cast<Bytef *>(outBuf);
710 err = deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
711 fprintf(stderr, "deflateInit result: %d\n", err);
712 ASSERT_EQ(err, Z_OK);
713 uint32_t estimateLen = deflateBound(&defstream, inLen);
714 outBuf = reinterpret_cast<uint8_t *>(malloc(estimateLen));
715 defstream.avail_out = static_cast<uInt>(estimateLen);
716 deflate(&defstream, Z_FINISH);
717 deflateEnd(&defstream);
718 z_stream outStream;
719 err = deflateCopy(&defstream, &outStream);
720 fprintf(stderr, "deflateCopy result: %d\n", err);
721 free(inBuf);
722 free(outBuf);
723 #endif
724 }
725
726 /**
727 * @tc.number : ActsZlibTest_1500
728 * @tc.name : Test adler32
729 * @tc.desc : [C- SOFTWARE -0200]
730 */
731 HWTEST_F(ActsZlibTest, ActsZlibTestCRC, Function | MediumTest | Level2)
732 {
733 uLong err = Z_ERRNO;
734 uLong crc1 = 0L;
735 uLong crc2 = 0L;
736 const Bytef *buf = reinterpret_cast<const Bytef*>(DICTIONARY);
737 err = crc32(0L, buf, 0);
738 fprintf(stderr, "crc32 result: %lu\n", err);
739 ASSERT_NE(err, Z_ERRNO);
740
741 err = crc32_z(0L, buf, 0);
742 fprintf(stderr, "crc32_z result: %lu\n", err);
743 ASSERT_NE(err, Z_ERRNO);
744 #ifdef Z_SOLO
745 #ifndef Z_LARGE64
746 err = crc32_combine64(crc1, crc2, 0);
747 fprintf(stderr, "crc32_combine64 result: %lu\n", err);
748 ASSERT_NE(err, Z_ERRNO);
749 #endif
750 #else
751 err = adler32_combine(crc1, crc2, 0);
752 fprintf(stderr, "crc32_combine result: %lu\n", err);
753 ASSERT_NE(err, Z_ERRNO);
754 #endif
755 }
756
757 /**
758 * @tc.number : ActsZlibTest_1600
759 * @tc.name : Test get_crc_table
760 * @tc.desc : [C- SOFTWARE -0200]
761 */
762 HWTEST_F(ActsZlibTest, ActsZlibTestGetCrcTable, Function | MediumTest | Level2)
763 {
764 auto table = get_crc_table();
765 ASSERT_TRUE(table != nullptr);
766 }
767
768 /**
769 * @tc.number : ActsZlibTest_1700
770 * @tc.name : Test gzBuffer
771 * @tc.desc : [C- SOFTWARE -0200]
772 */
773 HWTEST_F(ActsZlibTest, ActsZlibTestGzBuffer, Function | MediumTest | Level2)
774 {
775 #ifdef Z_SOLO
776 fprintf(stderr, "*********ActsZlibTestGzBuffer Z_SOLO**********\n");
777 #else
778 int err = Z_OK;
779 int len = static_cast<int>(strlen(HELLO)) + 1;
780 gzFile file;
781 z_off_t pos;
782 file = gzopen(TESTFILE, "wb");
783 ASSERT_TRUE(file != NULL);
784
785 err = gzbuffer(file, BUFFER_SIZE);
786 ASSERT_EQ(err, Z_OK);
787
788 gzclearerr(file);
789 gzputc(file, 'h');
790 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
791 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
792 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
793 ASSERT_TRUE(false);
794 }
795
796 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
797 gzclearerr(file);
798 gzclose_w(file);
799 file = gzopen(TESTFILE, "rb");
800 ASSERT_TRUE(file != NULL);
801
802 int res = gzdirect(file);
803 fprintf(stderr, "gzdirect result: %d\n", res);
804 Byte *compr, *uncompr;
805 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
806 uLong uncomprLen = comprLen;
807 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
808 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
809 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
810
811 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
812 ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
813 ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO));
814
815 pos = gzseek(file, -8L, SEEK_CUR);
816 ASSERT_FALSE(pos != SIX || gztell(file) != pos);
817 ASSERT_FALSE(gzgetc(file) != ' ');
818 ASSERT_FALSE(gzungetc(' ', file) != ' ');
819
820 fprintf(stderr, "gzgets\n");
821 gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
822 ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
823 gzclose_r(file);
824 free(compr);
825 free(uncompr);
826 #endif
827 }
828
829 /**
830 * @tc.number : ActsZlibTest_1800
831 * @tc.name : Test gzflush
832 * @tc.desc : [C- SOFTWARE -0200]
833 */
834 HWTEST_F(ActsZlibTest, ActsZlibTestGzFlush, Function | MediumTest | Level2)
835 {
836 #ifdef Z_SOLO
837 fprintf(stderr, "*********ActsZlibTestGzFlush Z_SOLO**********\n");
838 #else
839 int err = Z_OK;
840 gzFile file;
841 file = gzopen(TESTFILE, "wb");
842 ASSERT_TRUE(file != NULL);
843
844 gzputc(file, 'h');
845 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
846 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
847 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
848 ASSERT_TRUE(false);
849 }
850 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
851 gzflush(file, Z_FINISH); /* add one zero byte */
852 gzclose(file);
853 #endif
854 }
855
856 /**
857 * @tc.number : ActsZlibTest_1900
858 * @tc.name : Test gzfread
859 * @tc.desc : [C- SOFTWARE -0200]
860 */
861 HWTEST_F(ActsZlibTest, ActsZlibTestGzFread, Function | MediumTest | Level2)
862 {
863 #ifdef Z_SOLO
864 fprintf(stderr, "*********ActsZlibTestGzFread Z_SOLO**********\n");
865 #else
866 int err = Z_OK;
867 int len = static_cast<int>(strlen(HELLO)) + 1;
868 gzFile file;
869 file = gzopen(TESTFILE, "rb");
870 ASSERT_TRUE(file != NULL);
871 err = gzfread(HELLO, len, len, file);
872 ASSERT_EQ(err, 1);
873 gzclose(file);
874
875 #endif
876 }
877
878 /**
879 * @tc.number : ActsZlibTest_2000
880 * @tc.name : Test gzfwrite
881 * @tc.desc : [C- SOFTWARE -0200]
882 */
883 HWTEST_F(ActsZlibTest, ActsZlibTestGzWrite, Function | MediumTest | Level2)
884 {
885 #ifdef Z_SOLO
886 fprintf(stderr, "*********ActsZlibTestGzWrite Z_SOLO**********\n");
887 #else
888 int err = Z_OK;
889 int len = static_cast<int>(strlen(HELLO)) + 1;
890 gzFile file;
891 file = gzopen(TESTFILE, "wb");
892 ASSERT_TRUE(file != NULL);
893 err = gzfwrite(HELLO, len, len, file);
894 ASSERT_EQ(err, len);
895 gzclose(file);
896 #endif
897 }
898
899 /**
900 * @tc.number : ActsZlibTest_2100
901 * @tc.name : Test gzgetc
902 * @tc.desc : [C- SOFTWARE -0200]
903 */
904 HWTEST_F(ActsZlibTest, ActsZlibTestGzGetc, Function | MediumTest | Level2)
905 {
906 #ifdef Z_SOLO
907 fprintf(stderr, "*********ActsZlibTestGzGetc Z_SOLO**********\n");
908 #else
909 int err = Z_OK;
910 gzFile file;
911 file = gzopen(TESTFILE, "rb");
912 ASSERT_TRUE(file != NULL);
913 err = gzgetc(file);
914 ASSERT_TRUE(err == 'h');
915 gzclose(file);
916 #endif
917 }
918
919 /**
920 * @tc.number : ActsZlibTest_2200
921 * @tc.name : Test gzgetc_
922 * @tc.desc : [C- SOFTWARE -0200]
923 */
924 HWTEST_F(ActsZlibTest, ActsZlibTestGzGetc_, Function | MediumTest | Level2)
925 {
926 #ifdef Z_SOLO
927 fprintf(stderr, "*********ActsZlibTestGzGetc_ Z_SOLO**********\n");
928 #else
929 int err = Z_OK;
930 gzFile file;
931 file = gzopen(TESTFILE, "rb");
932 ASSERT_TRUE(file != NULL);
933 err = gzgetc_(file);
934 ASSERT_TRUE(err == 'h');
935 gzclose(file);
936 #endif
937 }
938
939 /**
940 * @tc.number : ActsZlibTest_2300
941 * @tc.name : Test gzgets
942 * @tc.desc : [C- SOFTWARE -0200]
943 */
944 HWTEST_F(ActsZlibTest, ActsZlibTestGzGets, Function | MediumTest | Level2)
945 {
946 #ifdef Z_SOLO
947 fprintf(stderr, "*********ActsZlibTestGzGets Z_SOLO**********\n");
948 #else
949 gzFile file;
950 file = gzopen(TESTFILE, "wb");
951 ASSERT_TRUE(file != NULL);
952 Byte *uncompr;
953 uLong uncomprLen = 10000 * sizeof(int);
954 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
955 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
956 fprintf(stderr, "gzgets\n");
957 gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
958 ASSERT_TRUE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
959 gzclose(file);
960 free(uncompr);
961 #endif
962 }
963
964 /**
965 * @tc.number : ActsZlibTest_2400
966 * @tc.name : Test gzoffset64
967 * @tc.desc : [C- SOFTWARE -0200]
968 */
969 HWTEST_F(ActsZlibTest, ActsZlibTestGzOffset64, Function | MediumTest | Level2)
970 {
971 #ifndef Z_LARGE64
972 fprintf(stderr, "*********ActsZlibTestGzOffset64 Z_LARGE64**********\n");
973 #else
974 int err = Z_OK;
975 int len = static_cast<int>(strlen(HELLO)) + 1;
976 gzFile file;
977 file = gzopen(TESTFILE, "rb");
978 ASSERT_TRUE(file != NULL);
979 err = gzoffset64(file);
980 ASSERT_TRUE(err != Z_OK);
981 gzclose(file);
982 #endif
983 }
984
985 /**
986 * @tc.number : ActsZlibTest_2500
987 * @tc.name : Test gzopen and gzopen64 and gzopen_w
988 * @tc.desc : [C- SOFTWARE -0200]
989 */
990 HWTEST_F(ActsZlibTest, ActsZlibTestGzOpen, Function | MediumTest | Level2)
991 {
992 #ifndef Z_SOLO
993 int err = Z_OK;
994 gzFile file;
995 file = gzopen(TESTFILE, "wb");
996 ASSERT_TRUE(file != NULL);
997 gzputc(file, 'h');
998 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
999 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1000 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1001 ASSERT_TRUE(false);
1002 }
1003 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1004 gzclose(file);
1005 file = gzopen(TESTFILE, "rb");
1006 ASSERT_TRUE(file != NULL);
1007 gzclose(file);
1008 #endif
1009 #ifdef Z_LARGE64
1010 int err = Z_OK;
1011 gzFile file;
1012 file = gzopen64(TESTFILE, "wb");
1013 ASSERT_TRUE(file != NULL);
1014 gzputc(file, 'h');
1015 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1016 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1017 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1018 ASSERT_TRUE(false);
1019 }
1020 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1021 gzclose(file);
1022 file = gzopen64(TESTFILE, "rb");
1023 ASSERT_TRUE(file != NULL);
1024 gzclose(file);
1025 #endif
1026 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO)
1027 gzFile file;
1028 file = gzopen_w(TESTFILE, "wb");
1029 ASSERT_TRUE(file != NULL);
1030 gzclose(file);
1031 #endif
1032 }
1033
1034 /**
1035 * @tc.number : ActsZlibTest_2600
1036 * @tc.name : Test gzprintf
1037 * @tc.desc : [C- SOFTWARE -0200]
1038 */
1039 HWTEST_F(ActsZlibTest, ActsZlibTestGzPrintf, Function | MediumTest | Level2)
1040 {
1041 #ifdef Z_SOLO
1042 fprintf(stderr, "*********ActsZlibTestGzPrintf Z_SOLO**********\n");
1043 #else
1044 gzFile file;
1045 file = gzopen(TESTFILE, "wb");
1046 ASSERT_TRUE(file != NULL);
1047 ASSERT_TRUE(gzprintf(file, ", %s!", "hello") == EIGHT);
1048 gzclose(file);
1049 #endif
1050 }
1051
1052 /**
1053 * @tc.number : ActsZlibTest_2700
1054 * @tc.name : Test gzputc
1055 * @tc.desc : [C- SOFTWARE -0200]
1056 */
1057 HWTEST_F(ActsZlibTest, ActsZlibTestGzPutc, Function | MediumTest | Level2)
1058 {
1059 #ifdef Z_SOLO
1060 fprintf(stderr, "*********ActsZlibTestGzPutc Z_SOLO**********\n");
1061 #else
1062 char err;
1063 gzFile file;
1064 file = gzopen(TESTFILE, "wb");
1065 ASSERT_TRUE(file != NULL);
1066 err = gzputc(file, 'h');
1067 ASSERT_TRUE(err == 'h');
1068 gzclose(file);
1069 #endif
1070 }
1071
1072 /**
1073 * @tc.number : ActsZlibTest_2800
1074 * @tc.name : Test gzputs
1075 * @tc.desc : [C- SOFTWARE -0200]
1076 */
1077 HWTEST_F(ActsZlibTest, ActsZlibTestGzPuts, Function | MediumTest | Level2)
1078 {
1079 #ifdef Z_SOLO
1080 fprintf(stderr, "*********ActsZlibTestGzPuts Z_SOLO**********\n");
1081 #else
1082 gzFile file;
1083 file = gzopen(TESTFILE, "wb");
1084 ASSERT_TRUE(file != NULL);
1085 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1086 gzclose(file);
1087 #endif
1088 }
1089
1090 /**
1091 * @tc.number : ActsZlibTest_2900
1092 * @tc.name : Test gzread
1093 * @tc.desc : [C- SOFTWARE -0200]
1094 */
1095 HWTEST_F(ActsZlibTest, ActsZlibTestGzRead, Function | MediumTest | Level2)
1096 {
1097 #ifdef Z_SOLO
1098 fprintf(stderr, "*********ActsZlibTestGzRead Z_SOLO**********\n");
1099 #else
1100 int err = Z_OK;
1101 int len = static_cast<int>(strlen(HELLO)) + 1;
1102 gzFile file;
1103 file = gzopen(TESTFILE, "wb");
1104 ASSERT_TRUE(file != NULL);
1105
1106 gzputc(file, 'h');
1107 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1108 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1109 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1110 ASSERT_TRUE(false);
1111 }
1112
1113 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1114 gzclose(file);
1115 file = gzopen(TESTFILE, "rb");
1116 ASSERT_TRUE(file != NULL);
1117
1118 Byte *uncompr;
1119 uLong uncomprLen = 10000 * sizeof(int);
1120 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1121 ASSERT_TRUE(uncompr != Z_NULL);
1122
1123 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1124 ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
1125 gzclose(file);
1126 #endif
1127 }
1128
1129 /**
1130 * @tc.number : ActsZlibTest_3000
1131 * @tc.name : Test gzrewind
1132 * @tc.desc : [C- SOFTWARE -0200]
1133 */
1134 HWTEST_F(ActsZlibTest, ActsZlibTestGzRewind, Function | MediumTest | Level2)
1135 {
1136 #ifdef Z_SOLO
1137 fprintf(stderr, "*********ActsZlibTestGzRewind Z_SOLO**********\n");
1138 #else
1139 int err = Z_OK;
1140 gzFile file;
1141 file = gzopen(TESTFILE, "wb");
1142 gzseek(file, 0L, SEEK_SET);
1143 err = gzrewind(file);
1144 ASSERT_TRUE(err == -1);
1145 gzclose(file);
1146 #endif
1147 }
1148
1149 /**
1150 * @tc.number : ActsZlibTest_3100
1151 * @tc.name : Test gzseek and gzseek64
1152 * @tc.desc : [C- SOFTWARE -0200]
1153 */
1154 HWTEST_F(ActsZlibTest, ActsZlibTestGzseek, Function | MediumTest | Level2)
1155 {
1156 long err = 0L;
1157 gzFile file;
1158 file = gzopen(TESTFILE, "wb");
1159 ASSERT_TRUE(file != NULL);
1160 #ifdef Z_SOLO
1161 fprintf(stderr, "*********ActsZlibTestGzseek Z_SOLO**********\n");
1162 #else
1163 err = gzseek(file, 1L, SEEK_CUR);
1164 #endif
1165 #ifdef Z_LARGE64
1166 err = gzseek64(file, 1L, SEEK_CUR);
1167 #endif
1168 ASSERT_TRUE(err == 1L);
1169 gzclose(file);
1170 }
1171
1172 /**
1173 * @tc.number : ActsZlibTest_3200
1174 * @tc.name : Test gzsetparams
1175 * @tc.desc : [C- SOFTWARE -0200]
1176 */
1177 HWTEST_F(ActsZlibTest, ActsZlibTestGzSetParams, Function | MediumTest | Level2)
1178 {
1179 #ifdef Z_SOLO
1180 fprintf(stderr, "*********ActsZlibTestGzSetParams Z_SOLO**********\n");
1181 #else
1182 int err = Z_OK;
1183 gzFile file;
1184 file = gzopen(TESTFILE, "wb");
1185 ASSERT_TRUE(file != NULL);
1186 err = gzsetparams(file, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY);
1187 ASSERT_TRUE(err == Z_OK);
1188 gzclose(file);
1189 #endif
1190 }
1191
1192 /**
1193 * @tc.number : ActsZlibTest_3300
1194 * @tc.name : Test gztell and gztell64
1195 * @tc.desc : [C- SOFTWARE -0200]
1196 */
1197 HWTEST_F(ActsZlibTest, ActsZlibTestGzTell, Function | MediumTest | Level2)
1198 {
1199 # ifndef Z_LARGE64
1200 gzFile file;
1201 file = gzopen(TESTFILE, "wb");
1202 ASSERT_TRUE(file != NULL);
1203 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1204 gzclose(file);
1205 file = gzopen(TESTFILE, "rb");
1206 ASSERT_TRUE(file != NULL);
1207 z_off64_t pos;
1208 pos = gzseek(file, -8L, SEEK_CUR);
1209 ASSERT_FALSE(gztell(file) == pos); /* define gztell gztell64 in zlib.h */
1210 gzclose(file);
1211 #else
1212 gzFile file;
1213 file = gzopen(TESTFILE, "wb");
1214 ASSERT_TRUE(file != NULL);
1215 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1216 gzclose(file);
1217 file = gzopen(TESTFILE, "rb");
1218 ASSERT_TRUE(file != NULL);
1219 z_off_t pos;
1220 pos = gzseek(file, -8L, SEEK_CUR);
1221 ASSERT_FALSE(pos != SIX || gztell(file) != pos);
1222 gzclose(file);
1223 #endif
1224 }
1225
1226 /**
1227 * @tc.number : ActsZlibTest_3400
1228 * @tc.name : Test gzungetc
1229 * @tc.desc : [C- SOFTWARE -0200]
1230 */
1231 HWTEST_F(ActsZlibTest, ActsZlibTestGzUnGetc, Function | MediumTest | Level2)
1232 {
1233 #ifdef Z_SOLO
1234 fprintf(stderr, "*********ActsZlibTestGzUnGetc Z_SOLO**********\n");
1235 #else
1236 gzFile file;
1237 file = gzopen(TESTFILE, "wb");
1238 ASSERT_TRUE(file != NULL);
1239 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1240 gzclose(file);
1241 file = gzopen(TESTFILE, "rb");
1242 ASSERT_TRUE(file != NULL);
1243 ASSERT_FALSE(gzungetc(' ', file) != ' ');
1244 #endif
1245 }
1246
1247 /**
1248 * @tc.number : ActsZlibTest_3500
1249 * @tc.name : Test gzvprintf
1250 * @tc.desc : [C- SOFTWARE -0200]
1251 */
1252 HWTEST_F(ActsZlibTest, ActsZlibTestGzVprintf, Function | MediumTest | Level2)
1253 {
1254 #ifdef Z_SOLO
1255 fprintf(stderr, "*********ActsZlibTestGzVprintf Z_SOLO**********\n");
1256 #else
1257 gzFile file;
1258 file = gzopen(TESTFILE, "wb");
1259 ASSERT_TRUE(file != NULL);
1260
1261 int err = TestGzPrintf(file, ", %s!", "hello");
1262 fprintf(stderr, "gzvprintf result: %d\n", err);
1263 #endif
1264 }
1265
1266 /**
1267 * @tc.number : ActsZlibTest_3600
1268 * @tc.name : Test gzwrite
1269 * @tc.desc : [C- SOFTWARE -0200]
1270 */
1271 HWTEST_F(ActsZlibTest, ActsZlibTestGzwrite, Function | MediumTest | Level2)
1272 {
1273 #ifdef Z_SOLO
1274 fprintf(stderr, "*********ActsZlibTestGzWrite Z_SOLO**********\n");
1275 #else
1276 int len = static_cast<int>(strlen(HELLO)) + 1;
1277 gzFile file;
1278 file = gzopen(TESTFILE, "wb");
1279 ASSERT_TRUE(file != NULL);
1280 int err = gzwrite(file, HELLO, len);
1281 ASSERT_EQ(err, len);
1282 gzclose(file);
1283 #endif
1284 }
1285
1286 /**
1287 * @tc.number : ActsZlibTest_3700
1288 * @tc.name : Test inflateBackInit, inflateBack, inflateBackEnd
1289 * @tc.desc : [C- SOFTWARE -0200]
1290 */
1291 HWTEST_F(ActsZlibTest, ActsZlibTestGzInflateBack, Function | MediumTest | Level2)
1292 {
1293 #ifdef Z_SOLO
1294 fprintf(stderr, "*********ActsZlibTestGzInflateBack Z_SOLO**********\n");
1295 #else
1296 int err = Z_OK;
1297 unsigned char *window;
1298 z_stream strm;
1299 unsigned char match[65280 + 2]; /* buffer for reversed match or gzip 32K sliding window */
1300 Byte *uncompr;
1301 uLong uncomprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1302 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1303 /* initialize inflateBack state for repeated use */
1304 window = match; /* reuse match buffer */
1305 strm.zalloc = nullptr;
1306 strm.zfree = nullptr;
1307 strm.opaque = nullptr;
1308 err = inflateBackInit(&strm, 15, window);
1309 ASSERT_EQ(err, Z_OK);
1310 if (err != Z_OK) {
1311 fprintf(stderr, "gun out of memory error--aborting\n");
1312 ASSERT_TRUE(false);
1313 }
1314 strm.next_in = uncompr;
1315 strm.avail_in = 1;
1316 err = inflateBack(&strm, pull, nullptr, push, &strm);
1317 ASSERT_TRUE(err != Z_OK);
1318 err = inflateBackEnd(&strm);
1319 ASSERT_EQ(err, Z_OK);
1320 #endif
1321 }
1322
1323 /**
1324 * @tc.number : ActsZlibTest_3800
1325 * @tc.name : Test inflateCodesUsed
1326 * @tc.desc : [C- SOFTWARE -0200]
1327 */
1328 HWTEST_F(ActsZlibTest, ActsZlibTestInflateCodesUsed, Function | MediumTest | Level2)
1329 {
1330 Byte *compr, *uncompr;
1331 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1332 uLong uncomprLen = comprLen;
1333 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1334 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1335 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1336 unsigned long err;
1337 z_stream d_stream; /* decompression stream */
1338 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1339 d_stream.zalloc = nullptr;
1340 d_stream.zfree = nullptr;
1341 d_stream.opaque = nullptr;
1342 d_stream.next_in = compr;
1343 d_stream.avail_in = 0;
1344 d_stream.next_out = uncompr;
1345 err = inflateCodesUsed(&d_stream);
1346 ASSERT_TRUE(err != Z_OK);
1347 free(compr);
1348 free(uncompr);
1349 }
1350
1351 /**
1352 * @tc.number : ActsZlibTest_3900
1353 * @tc.name : Test inflateCopy and inflateEnd
1354 * @tc.desc : [C- SOFTWARE -0200]
1355 */
1356 HWTEST_F(ActsZlibTest, ActsZlibTestInflateCopy_END, Function | MediumTest | Level2)
1357 {
1358 int err = Z_OK;
1359 err = inflate(nullptr, 0);
1360 ASSERT_TRUE(err == Z_STREAM_ERROR);
1361 err = inflateEnd(nullptr);
1362 ASSERT_TRUE(err == Z_STREAM_ERROR);
1363 err = inflateCopy(nullptr, nullptr);
1364 ASSERT_TRUE(err == Z_STREAM_ERROR);
1365 }
1366
1367 /**
1368 * @tc.number : ActsZlibTest_4000
1369 * @tc.name : Test inflateGetDictionary
1370 * @tc.desc : [C- SOFTWARE -0200]
1371 */
1372 HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetDictionary, Function | MediumTest | Level2)
1373 {
1374 Byte *compr, *uncompr;
1375 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1376 uLong uncomprLen = comprLen;
1377 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1378 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1379 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1380
1381 int err = Z_OK;
1382 z_stream d_stream; /* decompression stream */
1383 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1384 d_stream.zalloc = nullptr;
1385 d_stream.zfree = nullptr;
1386 d_stream.opaque = nullptr;
1387 d_stream.next_in = compr;
1388 d_stream.avail_in = static_cast<uInt>(comprLen);
1389 err = inflateInit(&d_stream);
1390 ASSERT_EQ(err, Z_OK);
1391 d_stream.next_out = uncompr;
1392 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1393 err = inflate(&d_stream, Z_NO_FLUSH);
1394 err = inflateGetDictionary(&d_stream, uncompr, nullptr);
1395 ASSERT_EQ(err, Z_OK);
1396 err = inflateEnd(&d_stream);
1397 ASSERT_EQ(err, Z_OK);
1398 free(compr);
1399 free(uncompr);
1400 }
1401
1402 /**
1403 * @tc.number : ActsZlibTest_4100
1404 * @tc.name : Test inflateGetHeader
1405 * @tc.desc : [C- SOFTWARE -0200]
1406 */
1407 HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetHeader, Function | MediumTest | Level2)
1408 {
1409 struct mem_zone *zone;
1410 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1411 ASSERT_TRUE(zone != NULL);
1412 zone->first = nullptr;
1413 zone->total = 0;
1414 zone->highwater = 0;
1415 zone->limit = 0;
1416 zone->notlifo = 0;
1417 zone->rogue = 0;
1418 int err = Z_OK;
1419 unsigned len = 1;
1420 unsigned char *out;
1421 z_stream strm;
1422 gz_header head;
1423 strm.opaque = zone;
1424 strm.zalloc = nullptr;
1425 strm.zfree = nullptr;
1426 strm.avail_in = 0;
1427 strm.next_in = nullptr;
1428 err = inflateInit2(&strm, 1);
1429 ASSERT_TRUE(err != Z_OK);
1430 out = (unsigned char *)malloc(len);
1431 ASSERT_TRUE(out != NULL);
1432 head.extra = out;
1433 head.extra_max = len;
1434 head.name = out;
1435 head.name_max = len;
1436 head.comment = out;
1437 head.comm_max = len;
1438 err = inflateGetHeader(&strm, &head);
1439 ASSERT_TRUE(err != Z_DATA_ERROR);
1440 free(out);
1441 free(zone);
1442 }
1443
1444 /**
1445 * @tc.number : ActsZlibTest_4200
1446 * @tc.name : Test inflateInit_ and inflateInit2_
1447 * @tc.desc : [C- SOFTWARE -0200]
1448 */
1449 HWTEST_F(ActsZlibTest, ActsZlibTestInflateInit_, Function | MediumTest | Level2)
1450 {
1451 int err = Z_OK;
1452 int windowBits = 8;
1453 z_stream strm;
1454 struct mem_zone *zone;
1455 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1456 ASSERT_TRUE(zone != NULL);
1457 zone->first = nullptr;
1458 zone->total = 0;
1459 zone->highwater = 0;
1460 zone->limit = 0;
1461 zone->notlifo = 0;
1462 zone->rogue = 0;
1463 strm.opaque = zone;
1464 strm.zalloc = nullptr;
1465 strm.zfree = nullptr;
1466 strm.avail_in = 0;
1467 strm.next_in = nullptr;
1468 err = inflateInit(&strm);
1469 ASSERT_TRUE(err == Z_OK);
1470 err = inflatePrime(&strm, 5, 31);
1471 ASSERT_TRUE(err == Z_OK);
1472 err = inflatePrime(&strm, -1, 0);
1473 ASSERT_TRUE(err == Z_OK);
1474 err = inflateSetDictionary(&strm, nullptr, 0);
1475 ASSERT_TRUE(err == Z_STREAM_ERROR);
1476 err = inflateEnd(&strm);
1477 ASSERT_TRUE(err == Z_OK);
1478 strm.avail_in = 0;
1479 strm.next_in = nullptr;
1480 err = inflateInit_(&strm, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
1481 ASSERT_TRUE(err == Z_VERSION_ERROR);
1482
1483 err = inflateInit2_(&strm, windowBits, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
1484 ASSERT_TRUE(err == Z_VERSION_ERROR);
1485 free(zone);
1486
1487 #ifdef Z_PREFIX
1488 deflate_state state;
1489 _tr_init(&state);
1490 _dist_code distCode = SIX;
1491 printf("_dist_code: %d\n", reinterpret_cast<int>(distCode));
1492 _length_code engthCode = FOUR;
1493 printf("_length_code: %d\n", reinterpret_cast<int>(engthCode));
1494 _tr_align trAlign;
1495 printf("_length_code: %d\n", sizeof(trAlign));
1496 _tr_flush_bits bits;
1497 printf("_length_code: %d\n", sizeof(bits));
1498 _tr_flush_block flushBlock;
1499 printf("_length_code: %d\n", sizeof(flushBlock));
1500 _tr_stored_block storedBlock;
1501 printf("_length_code: %d\n", sizeof(storedBlock));
1502 _tr_tally tally;
1503 printf("_length_code: %d\n", sizeof(tally));
1504 #endif
1505 }
1506
1507 /**
1508 * @tc.number : ActsZlibTest_4300
1509 * @tc.name : Test inflatePrime
1510 * @tc.desc : [C- SOFTWARE -0200]
1511 */
1512 HWTEST_F(ActsZlibTest, ActsZlibTestInflatePrime, Function | MediumTest | Level2)
1513 {
1514 int ret;
1515 z_stream strm;
1516 struct mem_zone *zone;
1517 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1518 ASSERT_TRUE(zone != NULL);
1519 zone->first = nullptr;
1520 zone->total = 0;
1521 zone->highwater = 0;
1522 zone->limit = 0;
1523 zone->notlifo = 0;
1524 zone->rogue = 0;
1525 strm.opaque = zone;
1526 strm.zalloc = nullptr;
1527 strm.zfree = nullptr;
1528 strm.avail_in = 0;
1529 strm.next_in = nullptr;
1530 ret = inflateInit(&strm);
1531 ASSERT_TRUE(ret == Z_OK);
1532 ret = inflatePrime(&strm, 5, 31);
1533 ASSERT_TRUE(ret == Z_OK);
1534 free(zone);
1535 }
1536
1537 /**
1538 * @tc.number : ActsZlibTest_4400
1539 * @tc.name : Test inflateReset, inflateReset2, inflateResetKeep
1540 * @tc.desc : [C- SOFTWARE -0200]
1541 */
1542 HWTEST_F(ActsZlibTest, ActsZlibTestInflateReset, Function | MediumTest | Level2)
1543 {
1544 Byte *compr, *uncompr;
1545 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1546 uLong uncomprLen = comprLen;
1547 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1548 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1549 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1550 int err = Z_OK;
1551 int windowBits = 8;
1552 z_stream d_stream; /* decompression stream */
1553 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1554 d_stream.zalloc = nullptr;
1555 d_stream.zfree = nullptr;
1556 d_stream.opaque = nullptr;
1557 d_stream.next_in = compr;
1558 d_stream.avail_in = 2; /* just read the zlib header */
1559 err = inflateInit(&d_stream);
1560 ASSERT_EQ(err, Z_OK);
1561 d_stream.next_out = uncompr;
1562 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1563 inflate(&d_stream, Z_NO_FLUSH);
1564 err = inflateReset(&d_stream);
1565 ASSERT_TRUE(err == Z_OK);
1566
1567 err = inflateResetKeep(&d_stream);
1568 ASSERT_TRUE(err == Z_OK);
1569 err = inflateInit2(&d_stream, windowBits);
1570 inflate(&d_stream, Z_NO_FLUSH);
1571 err = inflateReset2(&d_stream, windowBits);
1572 ASSERT_TRUE(err == Z_OK);
1573 }
1574
1575 /**
1576 * @tc.number : ActsZlibTest_4500
1577 * @tc.name : Test inflateSetDictionary
1578 * @tc.desc : [C- SOFTWARE -0200]
1579 */
1580 HWTEST_F(ActsZlibTest, ActsZlibTestInflateSetDictionary, Function | MediumTest | Level2)
1581 {
1582 Byte *compr, *uncompr;
1583 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1584 uLong uncomprLen = comprLen;
1585 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1586 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1587 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1588
1589 int err = Z_OK;
1590 z_stream d_stream; /* decompression stream */
1591 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1592 d_stream.zalloc = nullptr;
1593 d_stream.zfree = nullptr;
1594 d_stream.opaque = nullptr;
1595 d_stream.next_in = compr;
1596 d_stream.avail_in = static_cast<uInt>(comprLen);
1597 err = inflateInit(&d_stream);
1598 ASSERT_EQ(err, Z_OK);
1599 d_stream.next_out = uncompr;
1600 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1601 inflate(&d_stream, Z_NO_FLUSH);
1602 inflateSetDictionary(&d_stream, reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
1603 err = inflateEnd(&d_stream);
1604 ASSERT_EQ(err, Z_OK);
1605 free(compr);
1606 free(uncompr);
1607 }
1608
1609 /**
1610 * @tc.number : ActsZlibTest_4600
1611 * @tc.name : Test inflateSyncPoint
1612 * @tc.desc : [C- SOFTWARE -0200]
1613 */
1614 HWTEST_F(ActsZlibTest, ActsZlibTestInflateSyncPoint, Function | MediumTest | Level2)
1615 {
1616 Byte *compr, *uncompr;
1617 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1618 uLong uncomprLen = comprLen;
1619 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1620 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1621 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1622
1623 int err = Z_OK;
1624 z_stream d_stream; /* decompression stream */
1625 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1626 d_stream.zalloc = nullptr;
1627 d_stream.zfree = nullptr;
1628 d_stream.opaque = nullptr;
1629 d_stream.next_in = compr;
1630 d_stream.avail_in = static_cast<uInt>(comprLen);
1631 err = inflateInit(&d_stream);
1632 ASSERT_EQ(err, Z_OK);
1633 d_stream.next_out = uncompr;
1634 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1635 err = inflateSyncPoint(&d_stream);
1636 ASSERT_EQ(err, Z_OK);
1637 free(compr);
1638 free(uncompr);
1639 }
1640
1641 /**
1642 * @tc.number : ActsZlibTest_4700
1643 * @tc.name : Test inflateUndermine
1644 * @tc.desc : [C- SOFTWARE -0200]
1645 */
1646 HWTEST_F(ActsZlibTest, ActsZlibTestInflateUndermine, Function | MediumTest | Level2)
1647 {
1648 Byte *compr, *uncompr;
1649 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1650 uLong uncomprLen = comprLen;
1651 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1652 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1653 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1654
1655 int err = Z_OK;
1656 z_stream d_stream; /* decompression stream */
1657 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1658 d_stream.zalloc = nullptr;
1659 d_stream.zfree = nullptr;
1660 d_stream.opaque = nullptr;
1661 d_stream.next_in = compr;
1662 d_stream.avail_in = static_cast<uInt>(comprLen);
1663 err = inflateInit(&d_stream);
1664 ASSERT_EQ(err, Z_OK);
1665 d_stream.next_out = uncompr;
1666 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1667 err = inflateUndermine(&d_stream, 1);
1668 ASSERT_EQ(err, Z_DATA_ERROR);
1669 free(compr);
1670 free(uncompr);
1671 }
1672
1673 /**
1674 * @tc.number : ActsZlibTest_4800
1675 * @tc.name : Test inflateValidate
1676 * @tc.desc : [C- SOFTWARE -0200]
1677 */
1678 HWTEST_F(ActsZlibTest, ActsZlibTestInflateValidate, Function | MediumTest | Level2)
1679 {
1680 Byte *compr, *uncompr;
1681 uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
1682 uLong uncomprLen = comprLen;
1683 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1684 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1685 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1686
1687 int err = Z_OK;
1688 z_stream d_stream; /* decompression stream */
1689 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1690 d_stream.zalloc = nullptr;
1691 d_stream.zfree = nullptr;
1692 d_stream.opaque = nullptr;
1693 d_stream.next_in = compr;
1694 d_stream.avail_in = static_cast<uInt>(comprLen);
1695 err = inflateInit(&d_stream);
1696 ASSERT_EQ(err, Z_OK);
1697 d_stream.next_out = uncompr;
1698 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1699 err = inflateValidate(&d_stream, 1);
1700 ASSERT_EQ(err, Z_OK);
1701 free(compr);
1702 free(uncompr);
1703 }
1704
1705 /**
1706 * @tc.number : ActsZlibTest_4900
1707 * @tc.name : Test zlibCompileFlags
1708 * @tc.desc : [C- SOFTWARE -0200]
1709 */
1710 HWTEST_F(ActsZlibTest, ActsZlibTestzlibCompileFlags, Function | MediumTest | Level2)
1711 {
1712 static const char* myVersion = ZLIB_VERSION;
1713
1714 if (zlibVersion()[0] != myVersion[0]) {
1715 fprintf(stderr, "incompatible zlib version\n");
1716 ASSERT_TRUE(false);
1717
1718 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
1719 fprintf(stderr, "warning: different zlib version\n");
1720 }
1721
1722 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
1723 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
1724 }
1725
1726 /**
1727 * @tc.number : ActsZlibTest_5000
1728 * @tc.name : Test zError
1729 * @tc.desc : [C- SOFTWARE -0200]
1730 */
1731 HWTEST_F(ActsZlibTest, ActsZlibTestzError, Function | MediumTest | Level2)
1732 {
1733 const char* err;
1734 err = zError(Z_DATA_ERROR);
1735 ASSERT_EQ(err, "data error");
1736 }
1737
1738 /**
1739 * @tc.number : ActsZlibTest_5100
1740 * @tc.name : Test zlibVersion
1741 * @tc.desc : [C- SOFTWARE -0200]
1742 */
1743 HWTEST_F(ActsZlibTest, ActsZlibTestzlibVersion, Function | MediumTest | Level2)
1744 {
1745 static const char* myVersion = ZLIB_VERSION;
1746 static const char* err;
1747 err = zlibVersion();
1748 ASSERT_EQ(err, myVersion);
1749 }