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_3400
941 * @tc.name : Test gzungetc
942 * @tc.desc : [C- SOFTWARE -0200]
943 */
944 HWTEST_F(ActsZlibTest, ActsZlibTestGzUnGetc, Function | MediumTest | Level2)
945 {
946 #ifdef Z_SOLO
947 fprintf(stderr, "*********ActsZlibTestGzUnGetc Z_SOLO**********\n");
948 #else
949 std::lock_guard<std::mutex> lock(file_mutex);
950 gzFile file;
951 file = gzopen(TESTFILE, "rb");
952 ASSERT_TRUE(file != NULL);
953 int err = gzgetc(file);
954 ASSERT_TRUE(err == 'h');
955 ASSERT_FALSE(gzungetc('h', file) != 'h');
956 char sz_read[5] = {0};
957 gzread(file, sz_read, 1);
958 ASSERT_TRUE(sz_read[0] == 'h');
959 gzclose(file);
960 #endif
961 }
962
963 /**
964 * @tc.number : ActsZlibTest_2200
965 * @tc.name : Test gzgetc_
966 * @tc.desc : [C- SOFTWARE -0200]
967 */
968 HWTEST_F(ActsZlibTest, ActsZlibTestGzGetc_, Function | MediumTest | Level2)
969 {
970 #ifdef Z_SOLO
971 fprintf(stderr, "*********ActsZlibTestGzGetc_ Z_SOLO**********\n");
972 #else
973 std::lock_guard<std::mutex> lock(file_mutex);
974 int err = Z_OK;
975 gzFile file;
976 file = gzopen(TESTFILE, "rb");
977 ASSERT_TRUE(file != NULL);
978 err = gzgetc_(file);
979 ASSERT_TRUE(err == 'h');
980 gzclose(file);
981 #endif
982 }
983
984 /**
985 * @tc.number : ActsZlibTest_2300
986 * @tc.name : Test gzgets
987 * @tc.desc : [C- SOFTWARE -0200]
988 */
989 HWTEST_F(ActsZlibTest, ActsZlibTestGzGets, Function | MediumTest | Level2)
990 {
991 #ifdef Z_SOLO
992 fprintf(stderr, "*********ActsZlibTestGzGets Z_SOLO**********\n");
993 #else
994 std::lock_guard<std::mutex> lock(file_mutex);
995 gzFile file;
996 file = gzopen(TESTFILE, "wb");
997 ASSERT_TRUE(file != NULL);
998 Byte *uncompr;
999 uLong uncomprLen = 100 * sizeof(int);
1000 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1001 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1002 fprintf(stderr, "gzgets\n");
1003 gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
1004 ASSERT_TRUE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
1005 gzclose(file);
1006 free(uncompr);
1007 #endif
1008 }
1009
1010 /**
1011 * @tc.number : ActsZlibTest_2400
1012 * @tc.name : Test gzoffset64
1013 * @tc.desc : [C- SOFTWARE -0200]
1014 */
1015 HWTEST_F(ActsZlibTest, ActsZlibTestGzOffset64, Function | MediumTest | Level2)
1016 {
1017 #ifndef Z_LARGE64
1018 fprintf(stderr, "*********ActsZlibTestGzOffset64 Z_LARGE64**********\n");
1019 #else
1020 std::lock_guard<std::mutex> lock(file_mutex);
1021 int err = Z_OK;
1022 int len = static_cast<int>(strlen(HELLO)) + 1;
1023 gzFile file;
1024 file = gzopen(TESTFILE, "rb");
1025 ASSERT_TRUE(file != NULL);
1026 err = gzoffset64(file);
1027 ASSERT_TRUE(err != Z_OK);
1028 gzclose(file);
1029 #endif
1030 }
1031
1032 /**
1033 * @tc.number : ActsZlibTest_2500
1034 * @tc.name : Test gzopen and gzopen64 and gzopen_w
1035 * @tc.desc : [C- SOFTWARE -0200]
1036 */
1037 HWTEST_F(ActsZlibTest, ActsZlibTestGzOpen, Function | MediumTest | Level2)
1038 {
1039 std::lock_guard<std::mutex> lock(file_mutex);
1040 #ifndef Z_SOLO
1041 int err = Z_OK;
1042 gzFile file;
1043 file = gzopen(TESTFILE, "wb");
1044 ASSERT_TRUE(file != NULL);
1045 gzputc(file, 'h');
1046 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1047 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1048 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1049 ASSERT_TRUE(false);
1050 }
1051 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1052 gzclose(file);
1053 file = gzopen(TESTFILE, "rb");
1054 ASSERT_TRUE(file != NULL);
1055 gzclose(file);
1056 #endif
1057 #ifdef Z_LARGE64
1058 int err = Z_OK;
1059 gzFile file;
1060 file = gzopen64(TESTFILE, "wb");
1061 ASSERT_TRUE(file != NULL);
1062 gzputc(file, 'h');
1063 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1064 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1065 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1066 ASSERT_TRUE(false);
1067 }
1068 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1069 gzclose(file);
1070 file = gzopen64(TESTFILE, "rb");
1071 ASSERT_TRUE(file != NULL);
1072 gzclose(file);
1073 #endif
1074 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO)
1075 gzFile file;
1076 file = gzopen_w(TESTFILE, "wb");
1077 ASSERT_TRUE(file != NULL);
1078 gzclose(file);
1079 #endif
1080 }
1081
1082 /**
1083 * @tc.number : ActsZlibTest_2600
1084 * @tc.name : Test gzprintf
1085 * @tc.desc : [C- SOFTWARE -0200]
1086 */
1087 HWTEST_F(ActsZlibTest, ActsZlibTestGzPrintf, Function | MediumTest | Level2)
1088 {
1089 #ifdef Z_SOLO
1090 fprintf(stderr, "*********ActsZlibTestGzPrintf Z_SOLO**********\n");
1091 #else
1092 std::lock_guard<std::mutex> lock(file_mutex);
1093 gzFile file;
1094 file = gzopen(TESTFILE, "wb");
1095 ASSERT_TRUE(file != NULL);
1096 ASSERT_TRUE(gzprintf(file, ", %s!", "hello") == EIGHT);
1097 gzclose(file);
1098 #endif
1099 }
1100
1101 /**
1102 * @tc.number : ActsZlibTest_2700
1103 * @tc.name : Test gzputc
1104 * @tc.desc : [C- SOFTWARE -0200]
1105 */
1106 HWTEST_F(ActsZlibTest, ActsZlibTestGzPutc, Function | MediumTest | Level2)
1107 {
1108 #ifdef Z_SOLO
1109 fprintf(stderr, "*********ActsZlibTestGzPutc Z_SOLO**********\n");
1110 #else
1111 std::lock_guard<std::mutex> lock(file_mutex);
1112 char err;
1113 gzFile file;
1114 file = gzopen(TESTFILE, "wb");
1115 ASSERT_TRUE(file != NULL);
1116 err = gzputc(file, 'h');
1117 ASSERT_TRUE(err == 'h');
1118 gzclose(file);
1119 #endif
1120 }
1121
1122 /**
1123 * @tc.number : ActsZlibTest_2800
1124 * @tc.name : Test gzputs
1125 * @tc.desc : [C- SOFTWARE -0200]
1126 */
1127 HWTEST_F(ActsZlibTest, ActsZlibTestGzPuts, Function | MediumTest | Level2)
1128 {
1129 #ifdef Z_SOLO
1130 fprintf(stderr, "*********ActsZlibTestGzPuts Z_SOLO**********\n");
1131 #else
1132 std::lock_guard<std::mutex> lock(file_mutex);
1133 gzFile file;
1134 file = gzopen(TESTFILE, "wb");
1135 ASSERT_TRUE(file != NULL);
1136 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1137 gzclose(file);
1138 #endif
1139 }
1140
1141 /**
1142 * @tc.number : ActsZlibTest_2900
1143 * @tc.name : Test gzread
1144 * @tc.desc : [C- SOFTWARE -0200]
1145 */
1146 HWTEST_F(ActsZlibTest, ActsZlibTestGzRead, Function | MediumTest | Level2)
1147 {
1148 #ifdef Z_SOLO
1149 fprintf(stderr, "*********ActsZlibTestGzRead Z_SOLO**********\n");
1150 #else
1151 std::lock_guard<std::mutex> lock(file_mutex);
1152 int err = Z_OK;
1153 int len = static_cast<int>(strlen(HELLO)) + 1;
1154 gzFile file;
1155 file = gzopen(TESTFILE, "wb");
1156 ASSERT_TRUE(file != NULL);
1157
1158 gzputc(file, 'h');
1159 ASSERT_TRUE(gzputs(file, "ello") == FOUR);
1160 if (gzprintf(file, ", %s!", "hello") != EIGHT) {
1161 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
1162 ASSERT_TRUE(false);
1163 }
1164
1165 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1166 gzclose(file);
1167 file = gzopen(TESTFILE, "rb");
1168 ASSERT_TRUE(file != NULL);
1169
1170 Byte *uncompr;
1171 uLong uncomprLen = 100 * sizeof(int);
1172 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1173 ASSERT_TRUE(uncompr != Z_NULL);
1174
1175 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1176 ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
1177 gzclose(file);
1178 #endif
1179 }
1180
1181 /**
1182 * @tc.number : ActsZlibTest_3000
1183 * @tc.name : Test gzrewind
1184 * @tc.desc : [C- SOFTWARE -0200]
1185 */
1186 HWTEST_F(ActsZlibTest, ActsZlibTestGzRewind, Function | MediumTest | Level2)
1187 {
1188 #ifdef Z_SOLO
1189 fprintf(stderr, "*********ActsZlibTestGzRewind Z_SOLO**********\n");
1190 #else
1191 std::lock_guard<std::mutex> lock(file_mutex);
1192 int err = Z_OK;
1193 gzFile file;
1194 file = gzopen(TESTFILE, "wb");
1195 gzseek(file, 0L, SEEK_SET);
1196 err = gzrewind(file);
1197 ASSERT_TRUE(err == -1);
1198 gzclose(file);
1199 #endif
1200 }
1201
1202 /**
1203 * @tc.number : ActsZlibTest_3100
1204 * @tc.name : Test gzseek and gzseek64
1205 * @tc.desc : [C- SOFTWARE -0200]
1206 */
1207 HWTEST_F(ActsZlibTest, ActsZlibTestGzseek, Function | MediumTest | Level2)
1208 {
1209 std::lock_guard<std::mutex> lock(file_mutex);
1210 long err = 0L;
1211 gzFile file;
1212 file = gzopen(TESTFILE, "wb");
1213 ASSERT_TRUE(file != NULL);
1214 #ifdef Z_SOLO
1215 fprintf(stderr, "*********ActsZlibTestGzseek Z_SOLO**********\n");
1216 #else
1217 err = gzseek(file, 1L, SEEK_CUR);
1218 #endif
1219 #ifdef Z_LARGE64
1220 err = gzseek64(file, 1L, SEEK_CUR);
1221 #endif
1222 ASSERT_TRUE(err == 1L);
1223 gzclose(file);
1224 }
1225
1226 /**
1227 * @tc.number : ActsZlibTest_3200
1228 * @tc.name : Test gzsetparams
1229 * @tc.desc : [C- SOFTWARE -0200]
1230 */
1231 HWTEST_F(ActsZlibTest, ActsZlibTestGzSetParams, Function | MediumTest | Level2)
1232 {
1233 #ifdef Z_SOLO
1234 fprintf(stderr, "*********ActsZlibTestGzSetParams Z_SOLO**********\n");
1235 #else
1236 std::lock_guard<std::mutex> lock(file_mutex);
1237 int err = Z_OK;
1238 gzFile file;
1239 file = gzopen(TESTFILE, "wb");
1240 ASSERT_TRUE(file != NULL);
1241 err = gzsetparams(file, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY);
1242 ASSERT_TRUE(err == Z_OK);
1243 gzclose(file);
1244 #endif
1245 }
1246
1247 /**
1248 * @tc.number : ActsZlibTest_3300
1249 * @tc.name : Test gztell and gztell64
1250 * @tc.desc : [C- SOFTWARE -0200]
1251 */
1252 HWTEST_F(ActsZlibTest, ActsZlibTestGzTell, Function | MediumTest | Level2)
1253 {
1254 std::lock_guard<std::mutex> lock(file_mutex);
1255 # ifndef Z_LARGE64
1256 gzFile file;
1257 file = gzopen(TESTFILE, "wb");
1258 ASSERT_TRUE(file != NULL);
1259 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1260 gzclose(file);
1261 file = gzopen(TESTFILE, "rb");
1262 ASSERT_TRUE(file != NULL);
1263 z_off64_t pos;
1264 pos = gzseek(file, -8L, SEEK_CUR);
1265 ASSERT_FALSE(gztell(file) == pos); /* define gztell gztell in zlib.h */
1266 gzclose(file);
1267 #else
1268 gzFile file;
1269 file = gzopen(TESTFILE, "wb");
1270 ASSERT_TRUE(file != NULL);
1271 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
1272 gzclose(file);
1273 file = gzopen(TESTFILE, "rb");
1274 ASSERT_TRUE(file != NULL);
1275 z_off_t pos;
1276 pos = gzseek(file, -8L, SEEK_CUR);
1277 ASSERT_FALSE(pos != SIX || gztell64(file) != pos);
1278 gzclose(file);
1279 #endif
1280 }
1281
1282 /**
1283 * @tc.number : ActsZlibTest_3500
1284 * @tc.name : Test gzvprintf
1285 * @tc.desc : [C- SOFTWARE -0200]
1286 */
1287 HWTEST_F(ActsZlibTest, ActsZlibTestGzVprintf, Function | MediumTest | Level2)
1288 {
1289 #ifdef Z_SOLO
1290 fprintf(stderr, "*********ActsZlibTestGzVprintf Z_SOLO**********\n");
1291 #else
1292 std::lock_guard<std::mutex> lock(file_mutex);
1293 gzFile file;
1294 file = gzopen(TESTFILE, "wb");
1295 ASSERT_TRUE(file != NULL);
1296
1297 int err = TestGzPrintf(file, ", %s!", "hello");
1298 fprintf(stderr, "gzvprintf result: %d\n", err);
1299 gzclose(file);
1300 #endif
1301 }
1302
1303 /**
1304 * @tc.number : ActsZlibTest_3600
1305 * @tc.name : Test gzwrite
1306 * @tc.desc : [C- SOFTWARE -0200]
1307 */
1308 HWTEST_F(ActsZlibTest, ActsZlibTestGzwrite, Function | MediumTest | Level2)
1309 {
1310 #ifdef Z_SOLO
1311 fprintf(stderr, "*********ActsZlibTestGzWrite Z_SOLO**********\n");
1312 #else
1313 std::lock_guard<std::mutex> lock(file_mutex);
1314 int len = static_cast<int>(strlen(HELLO)) + 1;
1315 gzFile file;
1316 file = gzopen(TESTFILE, "wb");
1317 ASSERT_TRUE(file != NULL);
1318 int err = gzwrite(file, HELLO, len);
1319 ASSERT_EQ(err, len);
1320 gzclose(file);
1321 #endif
1322 }
1323
1324 /**
1325 * @tc.number : ActsZlibTest_3700
1326 * @tc.name : Test inflateBackInit, inflateBack, inflateBackEnd
1327 * @tc.desc : [C- SOFTWARE -0200]
1328 */
1329 HWTEST_F(ActsZlibTest, ActsZlibTestGzInflateBack, Function | MediumTest | Level2)
1330 {
1331 #ifdef Z_SOLO
1332 fprintf(stderr, "*********ActsZlibTestGzInflateBack Z_SOLO**********\n");
1333 #else
1334 int err = Z_OK;
1335 unsigned char *window;
1336 z_stream strm;
1337 unsigned char match[65280 + 2]; /* buffer for reversed match or gzip 32K sliding window */
1338 Byte *uncompr;
1339 uLong uncomprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1340 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1341 /* initialize inflateBack state for repeated use */
1342 window = match; /* reuse match buffer */
1343 strm.zalloc = nullptr;
1344 strm.zfree = nullptr;
1345 strm.opaque = nullptr;
1346 err = inflateBackInit_(
1347 &strm, 15, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
1348 ASSERT_EQ(err, Z_OK);
1349 if (err != Z_OK) {
1350 fprintf(stderr, "gun out of memory error--aborting\n");
1351 ASSERT_TRUE(false);
1352 }
1353 strm.next_in = uncompr;
1354 strm.avail_in = 1;
1355 err = inflateBack(&strm, pull, nullptr, push, &strm);
1356 ASSERT_TRUE(err != Z_OK);
1357 err = inflateBackEnd(&strm);
1358 ASSERT_EQ(err, Z_OK);
1359 #endif
1360 }
1361
1362 /**
1363 * @tc.number : ActsZlibTest_3800
1364 * @tc.name : Test inflateCodesUsed
1365 * @tc.desc : [C- SOFTWARE -0200]
1366 */
1367 HWTEST_F(ActsZlibTest, ActsZlibTestInflateCodesUsed, Function | MediumTest | Level2)
1368 {
1369 Byte *compr, *uncompr;
1370 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1371 uLong uncomprLen = comprLen;
1372 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1373 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1374 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1375 unsigned long err;
1376 z_stream d_stream; /* decompression stream */
1377 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1378 d_stream.zalloc = nullptr;
1379 d_stream.zfree = nullptr;
1380 d_stream.opaque = nullptr;
1381 d_stream.next_in = compr;
1382 d_stream.avail_in = 0;
1383 d_stream.next_out = uncompr;
1384 err = inflateCodesUsed(&d_stream);
1385 ASSERT_TRUE(err != Z_OK);
1386 free(compr);
1387 free(uncompr);
1388 }
1389
1390 /**
1391 * @tc.number : ActsZlibTest_3900
1392 * @tc.name : Test inflateCopy and inflateEnd
1393 * @tc.desc : [C- SOFTWARE -0200]
1394 */
1395 HWTEST_F(ActsZlibTest, ActsZlibTestInflateCopy_END, Function | MediumTest | Level2)
1396 {
1397 int err = Z_OK;
1398 err = inflate(nullptr, 0);
1399 ASSERT_TRUE(err == Z_STREAM_ERROR);
1400 err = inflateEnd(nullptr);
1401 ASSERT_TRUE(err == Z_STREAM_ERROR);
1402 err = inflateCopy(nullptr, nullptr);
1403 ASSERT_TRUE(err == Z_STREAM_ERROR);
1404 }
1405
1406 /**
1407 * @tc.number : ActsZlibTest_4000
1408 * @tc.name : Test inflateGetDictionary
1409 * @tc.desc : [C- SOFTWARE -0200]
1410 */
1411 HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetDictionary, Function | MediumTest | Level2)
1412 {
1413 Byte *compr, *uncompr;
1414 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1415 uLong uncomprLen = comprLen;
1416 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1417 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1418 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1419
1420 int err = Z_OK;
1421 z_stream d_stream; /* decompression stream */
1422 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1423 d_stream.zalloc = nullptr;
1424 d_stream.zfree = nullptr;
1425 d_stream.opaque = nullptr;
1426 d_stream.next_in = compr;
1427 d_stream.avail_in = static_cast<uInt>(comprLen);
1428 err = inflateInit(&d_stream);
1429 ASSERT_EQ(err, Z_OK);
1430 d_stream.next_out = uncompr;
1431 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1432 err = inflate(&d_stream, Z_NO_FLUSH);
1433 err = inflateGetDictionary(&d_stream, uncompr, nullptr);
1434 ASSERT_EQ(err, Z_OK);
1435 inflateMark(&d_stream);
1436 err = inflateEnd(&d_stream);
1437 ASSERT_EQ(err, Z_OK);
1438 free(compr);
1439 free(uncompr);
1440 }
1441
1442 /**
1443 * @tc.number : ActsZlibTest_4100
1444 * @tc.name : Test inflateGetHeader
1445 * @tc.desc : [C- SOFTWARE -0200]
1446 */
1447 HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetHeader, Function | MediumTest | Level2)
1448 {
1449 struct mem_zone *zone;
1450 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1451 ASSERT_TRUE(zone != NULL);
1452 zone->first = nullptr;
1453 zone->total = 0;
1454 zone->highwater = 0;
1455 zone->limit = 0;
1456 zone->notlifo = 0;
1457 zone->rogue = 0;
1458 int err = Z_OK;
1459 unsigned len = 1;
1460 unsigned char *out;
1461 z_stream strm;
1462 gz_header head;
1463 strm.opaque = zone;
1464 strm.zalloc = nullptr;
1465 strm.zfree = nullptr;
1466 strm.avail_in = 0;
1467 strm.next_in = nullptr;
1468 err = inflateInit2(&strm, 1);
1469 ASSERT_TRUE(err != Z_OK);
1470 out = (unsigned char *)malloc(len);
1471 ASSERT_TRUE(out != NULL);
1472 head.extra = out;
1473 head.extra_max = len;
1474 head.name = out;
1475 head.name_max = len;
1476 head.comment = out;
1477 head.comm_max = len;
1478 err = inflateGetHeader(&strm, &head);
1479 ASSERT_TRUE(err != Z_DATA_ERROR);
1480 free(out);
1481 free(zone);
1482 }
1483
1484 /**
1485 * @tc.number : ActsZlibTest_4200
1486 * @tc.name : Test inflateInit_ and inflateInit2_
1487 * @tc.desc : [C- SOFTWARE -0200]
1488 */
1489 HWTEST_F(ActsZlibTest, ActsZlibTestInflateInit_, Function | MediumTest | Level2)
1490 {
1491 int err = Z_OK;
1492 int windowBits = 8;
1493 z_stream strm;
1494 struct mem_zone *zone;
1495 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1496 ASSERT_TRUE(zone != NULL);
1497 zone->first = nullptr;
1498 zone->total = 0;
1499 zone->highwater = 0;
1500 zone->limit = 0;
1501 zone->notlifo = 0;
1502 zone->rogue = 0;
1503 strm.opaque = zone;
1504 strm.zalloc = nullptr;
1505 strm.zfree = nullptr;
1506 strm.avail_in = 0;
1507 strm.next_in = nullptr;
1508 err = inflateInit(&strm);
1509 ASSERT_TRUE(err == Z_OK);
1510 err = inflatePrime(&strm, 5, 31);
1511 ASSERT_TRUE(err == Z_OK);
1512 err = inflatePrime(&strm, -1, 0);
1513 ASSERT_TRUE(err == Z_OK);
1514 err = inflateSetDictionary(&strm, nullptr, 0);
1515 ASSERT_TRUE(err == Z_STREAM_ERROR);
1516 err = inflateEnd(&strm);
1517 ASSERT_TRUE(err == Z_OK);
1518 strm.avail_in = 0;
1519 strm.next_in = nullptr;
1520 err = inflateInit_(&strm, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
1521 ASSERT_TRUE(err == Z_VERSION_ERROR);
1522
1523 err = inflateInit2_(&strm, windowBits, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
1524 ASSERT_TRUE(err == Z_VERSION_ERROR);
1525 free(zone);
1526
1527 #ifdef Z_PREFIX
1528 deflate_state state;
1529 _tr_init(&state);
1530 _dist_code distCode = SIX;
1531 printf("_dist_code: %d\n", reinterpret_cast<int>(distCode));
1532 _length_code engthCode = FOUR;
1533 printf("_length_code: %d\n", reinterpret_cast<int>(engthCode));
1534 _tr_align trAlign;
1535 printf("_length_code: %d\n", sizeof(trAlign));
1536 _tr_flush_bits bits;
1537 printf("_length_code: %d\n", sizeof(bits));
1538 _tr_flush_block flushBlock;
1539 printf("_length_code: %d\n", sizeof(flushBlock));
1540 _tr_stored_block storedBlock;
1541 printf("_length_code: %d\n", sizeof(storedBlock));
1542 _tr_tally tally;
1543 printf("_length_code: %d\n", sizeof(tally));
1544 #endif
1545 }
1546
1547 /**
1548 * @tc.number : ActsZlibTest_4300
1549 * @tc.name : Test inflatePrime
1550 * @tc.desc : [C- SOFTWARE -0200]
1551 */
1552 HWTEST_F(ActsZlibTest, ActsZlibTestInflatePrime, Function | MediumTest | Level2)
1553 {
1554 int ret;
1555 z_stream strm;
1556 struct mem_zone *zone;
1557 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
1558 ASSERT_TRUE(zone != NULL);
1559 zone->first = nullptr;
1560 zone->total = 0;
1561 zone->highwater = 0;
1562 zone->limit = 0;
1563 zone->notlifo = 0;
1564 zone->rogue = 0;
1565 strm.opaque = zone;
1566 strm.zalloc = nullptr;
1567 strm.zfree = nullptr;
1568 strm.avail_in = 0;
1569 strm.next_in = nullptr;
1570 ret = inflateInit(&strm);
1571 ASSERT_TRUE(ret == Z_OK);
1572 ret = inflatePrime(&strm, 5, 31);
1573 ASSERT_TRUE(ret == Z_OK);
1574 free(zone);
1575 }
1576
1577 /**
1578 * @tc.number : ActsZlibTest_4400
1579 * @tc.name : Test inflateReset, inflateReset2, inflateResetKeep
1580 * @tc.desc : [C- SOFTWARE -0200]
1581 */
1582 HWTEST_F(ActsZlibTest, ActsZlibTestInflateReset, Function | MediumTest | Level2)
1583 {
1584 Byte *compr, *uncompr;
1585 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1586 uLong uncomprLen = comprLen;
1587 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1588 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1589 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1590 int err = Z_OK;
1591 int windowBits = 8;
1592 z_stream d_stream; /* decompression stream */
1593 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1594 d_stream.zalloc = nullptr;
1595 d_stream.zfree = nullptr;
1596 d_stream.opaque = nullptr;
1597 d_stream.next_in = compr;
1598 d_stream.avail_in = 2; /* just read the zlib header */
1599 err = inflateInit(&d_stream);
1600 ASSERT_EQ(err, Z_OK);
1601 d_stream.next_out = uncompr;
1602 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1603 inflate(&d_stream, Z_NO_FLUSH);
1604 err = inflateReset(&d_stream);
1605 ASSERT_TRUE(err == Z_OK);
1606
1607 err = inflateResetKeep(&d_stream);
1608 ASSERT_TRUE(err == Z_OK);
1609 err = inflateInit2(&d_stream, windowBits);
1610 inflate(&d_stream, Z_NO_FLUSH);
1611 err = inflateReset2(&d_stream, windowBits);
1612 ASSERT_TRUE(err == Z_OK);
1613 }
1614
1615 /**
1616 * @tc.number : ActsZlibTest_4500
1617 * @tc.name : Test inflateSetDictionary
1618 * @tc.desc : [C- SOFTWARE -0200]
1619 */
1620 HWTEST_F(ActsZlibTest, ActsZlibTestInflateSetDictionary, Function | MediumTest | Level2)
1621 {
1622 Byte *compr, *uncompr;
1623 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1624 uLong uncomprLen = comprLen;
1625 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1626 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1627 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1628
1629 int err = Z_OK;
1630 z_stream d_stream; /* decompression stream */
1631 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1632 d_stream.zalloc = nullptr;
1633 d_stream.zfree = nullptr;
1634 d_stream.opaque = nullptr;
1635 d_stream.next_in = compr;
1636 d_stream.avail_in = static_cast<uInt>(comprLen);
1637 err = inflateInit(&d_stream);
1638 ASSERT_EQ(err, Z_OK);
1639 d_stream.next_out = uncompr;
1640 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1641 inflate(&d_stream, Z_NO_FLUSH);
1642 inflateSetDictionary(&d_stream, reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
1643 err = inflateEnd(&d_stream);
1644 ASSERT_EQ(err, Z_OK);
1645 free(compr);
1646 free(uncompr);
1647 }
1648
1649 /**
1650 * @tc.number : ActsZlibTest_4600
1651 * @tc.name : Test inflateSyncPoint
1652 * @tc.desc : [C- SOFTWARE -0200]
1653 */
1654 HWTEST_F(ActsZlibTest, ActsZlibTestInflateSyncPoint, Function | MediumTest | Level2)
1655 {
1656 Byte *compr, *uncompr;
1657 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1658 uLong uncomprLen = comprLen;
1659 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1660 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1661 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1662
1663 int err = Z_OK;
1664 z_stream d_stream; /* decompression stream */
1665 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1666 d_stream.zalloc = nullptr;
1667 d_stream.zfree = nullptr;
1668 d_stream.opaque = nullptr;
1669 d_stream.next_in = compr;
1670 d_stream.avail_in = static_cast<uInt>(comprLen);
1671 err = inflateInit(&d_stream);
1672 ASSERT_EQ(err, Z_OK);
1673 d_stream.next_out = uncompr;
1674 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1675 err = inflateSyncPoint(&d_stream);
1676 ASSERT_EQ(err, Z_OK);
1677 free(compr);
1678 free(uncompr);
1679 }
1680
1681 /**
1682 * @tc.number : ActsZlibTest_4700
1683 * @tc.name : Test inflateUndermine
1684 * @tc.desc : [C- SOFTWARE -0200]
1685 */
1686 HWTEST_F(ActsZlibTest, ActsZlibTestInflateUndermine, Function | MediumTest | Level2)
1687 {
1688 Byte *compr, *uncompr;
1689 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1690 uLong uncomprLen = comprLen;
1691 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1692 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1693 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1694
1695 int err = Z_OK;
1696 z_stream d_stream; /* decompression stream */
1697 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1698 d_stream.zalloc = nullptr;
1699 d_stream.zfree = nullptr;
1700 d_stream.opaque = nullptr;
1701 d_stream.next_in = compr;
1702 d_stream.avail_in = static_cast<uInt>(comprLen);
1703 err = inflateInit(&d_stream);
1704 ASSERT_EQ(err, Z_OK);
1705 d_stream.next_out = uncompr;
1706 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1707 err = inflateUndermine(&d_stream, 1);
1708 ASSERT_EQ(err, Z_DATA_ERROR);
1709 free(compr);
1710 free(uncompr);
1711 }
1712
1713 /**
1714 * @tc.number : ActsZlibTest_4800
1715 * @tc.name : Test inflateValidate
1716 * @tc.desc : [C- SOFTWARE -0200]
1717 */
1718 HWTEST_F(ActsZlibTest, ActsZlibTestInflateValidate, Function | MediumTest | Level2)
1719 {
1720 Byte *compr, *uncompr;
1721 uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
1722 uLong uncomprLen = comprLen;
1723 compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1724 uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1725 ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
1726
1727 int err = Z_OK;
1728 z_stream d_stream; /* decompression stream */
1729 strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
1730 d_stream.zalloc = nullptr;
1731 d_stream.zfree = nullptr;
1732 d_stream.opaque = nullptr;
1733 d_stream.next_in = compr;
1734 d_stream.avail_in = static_cast<uInt>(comprLen);
1735 err = inflateInit(&d_stream);
1736 ASSERT_EQ(err, Z_OK);
1737 d_stream.next_out = uncompr;
1738 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1739 err = inflateValidate(&d_stream, 1);
1740 ASSERT_EQ(err, Z_OK);
1741 free(compr);
1742 free(uncompr);
1743 }
1744
1745 /**
1746 * @tc.number : ActsZlibTest_4900
1747 * @tc.name : Test zlibCompileFlags
1748 * @tc.desc : [C- SOFTWARE -0200]
1749 */
1750 HWTEST_F(ActsZlibTest, ActsZlibTestzlibCompileFlags, Function | MediumTest | Level2)
1751 {
1752 static const char* myVersion = ZLIB_VERSION;
1753
1754 if (zlibVersion()[0] != myVersion[0]) {
1755 fprintf(stderr, "incompatible zlib version\n");
1756 ASSERT_TRUE(false);
1757
1758 } else if (strcmp(zlibVersion(), ZLIB_VERSION)) {
1759 fprintf(stderr, "warning: different zlib version\n");
1760 }
1761
1762 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
1763 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
1764 }
1765
1766 /**
1767 * @tc.number : ActsZlibTest_5000
1768 * @tc.name : Test zError
1769 * @tc.desc : [C- SOFTWARE -0200]
1770 */
1771 HWTEST_F(ActsZlibTest, ActsZlibTestzError, Function | MediumTest | Level2)
1772 {
1773 const char* err;
1774 err = zError(Z_DATA_ERROR);
1775 ASSERT_EQ(err, "data error");
1776 }
1777
1778 /**
1779 * @tc.number : ActsZlibTest_5100
1780 * @tc.name : Test zlibVersion
1781 * @tc.desc : [C- SOFTWARE -0200]
1782 */
1783 HWTEST_F(ActsZlibTest, ActsZlibTestzlibVersion, Function | MediumTest | Level2)
1784 {
1785 static const char* myVersion = ZLIB_VERSION;
1786 static const char* err;
1787 err = zlibVersion();
1788 ASSERT_EQ(err, myVersion);
1789 }
1790
1791 /**
1792 * @tc.number : ActsZlibTest_5200
1793 * @tc.name : Test gzdopen
1794 * @tc.desc : [C- SOFTWARE -0200]
1795 */
1796 HWTEST_F(ActsZlibTest, ActsZlibTestGzdopen, Function | MediumTest | Level2)
1797 {
1798 #ifdef Z_SOLO
1799 fprintf(stderr, "*********ActsZlibTestGzdopen Z_SOLO**********\n");
1800 #else
1801 std::lock_guard<std::mutex> lock(file_mutex);
1802 FILE *fp = fopen(TESTFILE, "r");
1803 int fd = fileno(fp);
1804 gzFile file = gzdopen(fd, "r");
1805 ASSERT_TRUE(file != NULL);
1806
1807 int err = gzeof(file);
1808 fprintf(stderr, "gzeof result: %d\n", err);
1809 fclose(fp);
1810 gzclose(file);
1811 #endif
1812 }
1813 }
1814