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