ActsZlibTest.cpp 55.0 KB
Newer Older
D
dujingcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <gtest/gtest.h>
#include <sstream>
#include <string>

#include "zlib.h"

using namespace std;
using namespace testing::ext;
namespace {
static const char DICTIONARY[] = "hello";
static const char GARBAGE[] = "garbage";
static const char TESTFILE[] = "foo.gz";
static char HELLO[] = "hello, hello!";
static unsigned int CALLOC_SIZE = 1;
D
dujingcheng 已提交
34
static int ONE = 1;
D
dujingcheng 已提交
35 36 37
static int FOUR = 4;
static int SIX = 6;
static int EIGHT = 8;
D
dujingcheng 已提交
38
static unsigned BUFFER_SIZE = 8192;
D
dujingcheng 已提交
39 40
}

D
dujingcheng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static unsigned pull(void *desc, unsigned char **buf)
{
    static unsigned int next = 0;
    static unsigned char dat[] = {0x63, 0, 2, 0};

    if (desc == nullptr) {
        next = 0;
        return 0;   /* no input (already provided at next_in) */
    }
    return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
}

static int push(void *desc, unsigned char *buf, unsigned len)
{
    buf += len;
    return desc != nullptr;      /* force error if desc not null */
}

static int TestGzPrintf(gzFile file, const char *format, ...)
{
    va_list va;
    int ret;

    va_start(va, format);
    ret = gzvprintf(file, format, va);
    va_end(va);
    return ret;
}

D
dujingcheng 已提交
70
class ActsZlibTest : public testing::Test {
D
dujingcheng 已提交
71
protected:
D
dujingcheng 已提交
72 73
    ActsZlibTest();
    ~ActsZlibTest();
D
dujingcheng 已提交
74 75 76 77
    static void SetUpTestCase();
    static void TearDownTestCase();
};

D
dujingcheng 已提交
78
ActsZlibTest::ActsZlibTest()
D
dujingcheng 已提交
79 80
{}

D
dujingcheng 已提交
81
ActsZlibTest::~ActsZlibTest()
D
dujingcheng 已提交
82 83
{}

D
dujingcheng 已提交
84
void ActsZlibTest::SetUpTestCase()
D
dujingcheng 已提交
85 86
{}

D
dujingcheng 已提交
87
void ActsZlibTest::TearDownTestCase()
D
dujingcheng 已提交
88 89
{}

D
dujingcheng 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* these items are strung together in a linked list, one for each allocation */
struct mem_item {
    void *ptr;                  /* pointer to allocated memory */
    size_t size;                /* requested size of allocation */
    struct mem_item *next;      /* pointer to next item in list, or NULL */
};

/* this structure is at the root of the linked list, and tracks statistics */
struct mem_zone {
    struct mem_item *first;     /* pointer to first item in list, or NULL */
    size_t total, highwater;    /* total allocations, and largest total */
    size_t limit;               /* memory allocation limit, or 0 if no limit */
    int notlifo, rogue;         /* counts of non-LIFO frees and rogue frees */
};

D
dujingcheng 已提交
105
/**
D
dujingcheng 已提交
106
 * @tc.number    : ActsZlibTest_0100
D
dujingcheng 已提交
107 108 109
 * @tc.name      : Test compress and uncompress test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
110
HWTEST_F(ActsZlibTest, ActsZlibTestCompress, Function | MediumTest | Level2)
D
dujingcheng 已提交
111 112
{
#ifdef Z_SOLO
D
dujingcheng 已提交
113
    fprintf(stderr, "*********ActsZlibTestCompress Z_SOLO**********\n");
D
dujingcheng 已提交
114 115 116 117 118 119 120 121
#else
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
122
    int err = Z_OK;
D
dujingcheng 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
    err = compress(compr, &comprLen, reinterpret_cast<const Bytef*>(HELLO), len);
    fprintf(stderr, "compress error: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    fprintf(stderr, "uncompress error: %d\n", err);
    ASSERT_EQ(err, Z_OK);
    fprintf(stderr, "uncompress: %s\n", reinterpret_cast<char *>(uncompr));
    free(compr);
    free(uncompr);
#endif
}

/**
D
dujingcheng 已提交
139
 * @tc.number    : ActsZlibTest_0200
D
dujingcheng 已提交
140 141 142
 * @tc.name      : Test gzio
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
143
HWTEST_F(ActsZlibTest, ActsZlibTestGzio, Function | MediumTest | Level2)
D
dujingcheng 已提交
144 145
{
#ifdef Z_SOLO
D
dujingcheng 已提交
146
    fprintf(stderr, "*********ActsZlibTestGzio Z_SOLO**********\n");
D
dujingcheng 已提交
147
#else
D
dujingcheng 已提交
148
    int err = Z_OK;
D
dujingcheng 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    z_off_t pos;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }

    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);

    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
    ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO));

    pos = gzseek(file, -8L, SEEK_CUR);
    ASSERT_FALSE(pos != SIX || gztell(file) != pos);
    ASSERT_FALSE(gzgetc(file) != ' ');
    ASSERT_FALSE(gzungetc(' ', file) != ' ');

    fprintf(stderr, "gzgets\n");
    gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
    ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
    gzclose(file);
    free(compr);
    free(uncompr);
#endif
}

/**
D
dujingcheng 已提交
193
 * @tc.number    : ActsZlibTest_0300
D
dujingcheng 已提交
194 195 196
 * @tc.name      : Test deflate
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
197
HWTEST_F(ActsZlibTest, ActsZlibTestDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
198 199 200 201 202 203 204
{
    Byte *compr;
    uLong comprLen = 10000 * sizeof(int);
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
205
    int err = Z_OK;
D
dujingcheng 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    fprintf(stderr, "deflateInit result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    c_stream.next_in  = reinterpret_cast<z_const unsigned char *>(HELLO);
    c_stream.next_out = compr;
    fprintf(stderr, "deflate\n");
    while (c_stream.total_in != len && c_stream.total_out < comprLen) {
        c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
        err = deflate(&c_stream, Z_NO_FLUSH);
        fprintf(stderr, "deflate result: %d\n", err);
        ASSERT_EQ(err, Z_OK);
    }

    for (;;) {
        c_stream.avail_out = 1;
        err = deflate(&c_stream, Z_FINISH);
        fprintf(stderr, "deflate result: %d\n", err);
        if (err == Z_STREAM_END) {
            break;
        }
        ASSERT_EQ(err, Z_OK);
    }

    err = deflateEnd(&c_stream);
    fprintf(stderr, "deflateEnd result: %d\n", err);
    ASSERT_EQ(err, Z_OK);
    free(compr);
}

/**
D
dujingcheng 已提交
241
 * @tc.number    : ActsZlibTest_0400
D
dujingcheng 已提交
242 243 244
 * @tc.name      : Test inflate
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
245
HWTEST_F(ActsZlibTest, ActsZlibTestInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
246 247 248 249 250 251 252 253
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
254
    int err = Z_OK;
D
dujingcheng 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = 0;
    d_stream.next_out = uncompr;
    err = inflateInit(&d_stream);
    fprintf(stderr, "inflateInit result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
        d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END || err == Z_DATA_ERROR) {
            break;
        }
        fprintf(stderr, "inflate result: %d\n", err);
        ASSERT_EQ(err, Z_OK);
    }

    fprintf(stderr, "inflateEnd result: %d\n", inflateEnd(&d_stream));
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
283
 * @tc.number    : ActsZlibTest_0500
D
dujingcheng 已提交
284 285 286
 * @tc.name      : Test deflate with large buffers and dynamic change of compression level
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
287
HWTEST_F(ActsZlibTest, ActsZlibTestLargeDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
288 289 290 291 292 293 294 295 296
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
297
    int err = Z_OK;
D
dujingcheng 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;

    err = deflateInit(&c_stream, Z_BEST_SPEED);
    fprintf(stderr, "deflateInit result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    c_stream.next_out = compr;
    c_stream.avail_out = static_cast<uInt>(comprLen);

    /* At this point, uncompr is still mostly zeroes, so it should compress
     * very well:
     */
    c_stream.next_in = uncompr;
    c_stream.avail_in = static_cast<uInt>(uncomprLen);
    err = deflate(&c_stream, Z_NO_FLUSH);
    fprintf(stderr, "deflate result: %d\n", err);
    ASSERT_EQ(err, Z_OK);
    ASSERT_TRUE(c_stream.avail_in == 0);

    /* Feed in already compressed data and switch to no compression: */
    deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
    c_stream.next_in = compr;
    c_stream.avail_in = static_cast<uInt>(comprLen) / 2;
    err = deflate(&c_stream, Z_NO_FLUSH);
    fprintf(stderr, "deflate result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    /* Switch back to compressing mode: */
    deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
    c_stream.next_in = uncompr;
    c_stream.avail_in = static_cast<uInt>(uncomprLen);
    err = deflate(&c_stream, Z_NO_FLUSH);
    ASSERT_EQ(err, Z_OK);

    err = deflate(&c_stream, Z_FINISH);
    ASSERT_EQ(err, Z_STREAM_END);

    err = deflateEnd(&c_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
344
 * @tc.number    : ActsZlibTest_0600
D
dujingcheng 已提交
345 346 347
 * @tc.name      : Test inflate with large buffers
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
348
HWTEST_F(ActsZlibTest, ActsZlibTestLargeInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
349 350 351 352 353 354 355 356
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
357
    int err = Z_OK;
D
dujingcheng 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);

    for (;;) {
        d_stream.next_out = uncompr;            /* discard the output */
        d_stream.avail_out = static_cast<uInt>(uncomprLen);
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END || err == Z_DATA_ERROR) {
            break;
        }
        ASSERT_EQ(err, Z_OK);
    }

    err = inflateEnd(&d_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
385
 * @tc.number    : ActsZlibTest_0700
D
dujingcheng 已提交
386 387 388
 * @tc.name      : Test deflate with full flush
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
389
HWTEST_F(ActsZlibTest, ActsZlibTestFlush, Function | MediumTest | Level2)
D
dujingcheng 已提交
390 391 392 393 394 395 396
{
    Byte *compr;
    uLong comprLen = 10000 * sizeof(int);
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
397
    int err = Z_OK;
D
dujingcheng 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    uInt len = static_cast<uInt>(strlen(HELLO)) + 1;
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    ASSERT_EQ(err, Z_OK);

    c_stream.next_in  = reinterpret_cast<z_const unsigned char *>(HELLO);
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
    c_stream.avail_out = static_cast<uInt>(comprLen);
    err = deflate(&c_stream, Z_FULL_FLUSH);
    ASSERT_EQ(err, Z_OK);

    compr[3]++; /* force an error in first compressed block */
    c_stream.avail_in = len - 3;
    err = deflate(&c_stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        ASSERT_EQ(err, Z_OK);
    }

    err = deflateEnd(&c_stream);
    ASSERT_EQ(err, Z_OK);
    comprLen = c_stream.total_out;
    free(compr);
}

/**
D
dujingcheng 已提交
426
 * @tc.number    : ActsZlibTest_0800
D
dujingcheng 已提交
427 428 429
 * @tc.name      : Test inflateSync
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
430
HWTEST_F(ActsZlibTest, ActsZlibTestSync, Function | MediumTest | Level2)
D
dujingcheng 已提交
431 432 433 434 435 436 437 438
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
439
    int err = Z_OK;
D
dujingcheng 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = 2; /* just read the zlib header */
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);

    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);

    inflate(&d_stream, Z_NO_FLUSH);
    d_stream.avail_in = static_cast<uInt>(comprLen) - 2;   /* read all compressed data */
    inflateSync(&d_stream);
    inflate(&d_stream, Z_FINISH);
    inflateEnd(&d_stream);
    printf("after inflateSync: hel%s\n", reinterpret_cast<char *>(uncompr));
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
464
 * @tc.number    : ActsZlibTest_0900
D
dujingcheng 已提交
465 466 467
 * @tc.name      : Test deflate with preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
468
HWTEST_F(ActsZlibTest, ActsZlibTestDictDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
469 470 471 472 473 474 475 476 477
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
478
    int err = Z_OK;
D
dujingcheng 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
    ASSERT_EQ(err, Z_OK);

    err = deflateSetDictionary(&c_stream,
                reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
    ASSERT_EQ(err, Z_OK);

    c_stream.next_out = compr;
    c_stream.avail_out = static_cast<uInt>(comprLen);
    c_stream.next_in  = reinterpret_cast<z_const unsigned char *>(HELLO);
    c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + 1;
    err = deflate(&c_stream, Z_FINISH);
    ASSERT_EQ(err, Z_STREAM_END);

    err = deflateEnd(&c_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
503
 * @tc.number    : ActsZlibTest_1000
D
dujingcheng 已提交
504 505 506
 * @tc.name      : Test inflate with a preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
507
HWTEST_F(ActsZlibTest, ActsZlibTestDictInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
508 509 510 511 512 513 514 515
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
516
    int err = Z_OK;
D
dujingcheng 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    for (;;) {
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END) {
            break;
        }
        if (err == Z_NEED_DICT) {
            err = inflateSetDictionary(
                &d_stream, reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
        }
        if (err == Z_DATA_ERROR) {
            break;
        }
        ASSERT_EQ(err, Z_OK);
    }

    err = inflateEnd(&d_stream);
    ASSERT_EQ(err, Z_OK);
    if (strcmp(reinterpret_cast<char *>(uncompr), HELLO)) {
        fprintf(stderr, "bad inflate with dict\n");
    } else {
        printf("inflate with dictionary: %s\n", reinterpret_cast<char *>(uncompr));
    }

    free(compr);
    free(uncompr);
D
dujingcheng 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
}

/**
 * @tc.number    : ActsZlibTest_1100
 * @tc.name      : Test compress2 with Z_BEST_COMPRESSION level
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestCompress2, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestCompress2 Z_BEST_COMPRESSION Z_SOLO**********\n");
#else
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

D
dujingcheng 已提交
572
    int err = Z_OK;
D
dujingcheng 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    uLong len = static_cast<uLong>(strlen(HELLO)) + 1;
    uLong outLen = compressBound(len);
    fprintf(stderr, "compressBound result: %lu\n", outLen);
    err = compress2(compr, &comprLen, reinterpret_cast<const Bytef*>(HELLO), outLen, Z_BEST_COMPRESSION);
    fprintf(stderr, "compress2 Z_BEST_COMPRESSION result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    err = uncompress2(uncompr, &uncomprLen, compr, &comprLen);
    fprintf(stderr, "uncompress2 Z_BEST_COMPRESSION result: %d\n", err);
    ASSERT_EQ(err, Z_OK);
    fprintf(stderr, "uncompress2: %s\n", reinterpret_cast<char *>(uncompr));
    free(compr);
    free(uncompr);
#endif
}

/**
 * @tc.number    : ActsZlibTest_1200
 * @tc.name      : Test adler32
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestAdler, Function | MediumTest | Level2)
{
    uLong err = Z_ERRNO;
    uLong adler1 = 0L;
    uLong adler2 = 0L;
    const Bytef *buf = reinterpret_cast<const Bytef*>(DICTIONARY);
    err = adler32(0L, buf, 0);
    fprintf(stderr, "adler32 result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);

    err = adler32_z(0L, buf, 0);
    fprintf(stderr, "adler32_z result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#ifdef Z_SOLO
#ifndef Z_LARGE64
    err = adler32_combine64(adler1, adler2, 0);
    fprintf(stderr, "adler32_combine64 result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#endif
#else
    err = adler32_combine(adler1, adler2, 0);
    fprintf(stderr, "adler32_combine result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#endif
}

/**
 * @tc.number    : ActsZlibTest_1300
 * @tc.name      : Test deflate state
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestDeflateState, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    int *bits = nullptr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    gz_headerp headerp = nullptr;
    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
638
    int err = Z_OK;
D
dujingcheng 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    int windowBits = EIGHT;
    int memLevel = EIGHT;
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
    err = deflateInit2(
         &c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED);
    ASSERT_EQ(err, Z_OK);
    deflateSetHeader(&c_stream, headerp);
    deflateTune(&c_stream, ONE, FOUR, EIGHT, ONE);
    memLevel = ONE;
    err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
    fprintf(stderr, "deflateParams result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    err = deflatePending(&c_stream, nullptr, bits);
    fprintf(stderr, "deflatePending result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    err = deflateSetDictionary(&c_stream,
                reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
    fprintf(stderr, "deflateGetDictionary result: %d\n", err);
    ASSERT_EQ(err, Z_OK);

    err = deflateGetDictionary(&c_stream, uncompr, nullptr);
    fprintf(stderr, "deflateGetDictionary result: %d\n", err);
    err = deflatePrime(&c_stream, EIGHT, ONE);
    fprintf(stderr, "deflatePrime result: %d\n", err);
    c_stream.next_out = compr;
    c_stream.avail_out = static_cast<uInt>(comprLen);
    c_stream.next_in  = reinterpret_cast<z_const unsigned char *>(HELLO);
    c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + 1;
    err = deflate(&c_stream, Z_FINISH);
    ASSERT_EQ(err, Z_STREAM_END);
    err = deflateEnd(&c_stream);
    ASSERT_EQ(err, Z_OK);
#ifdef Z_SOLO
    err = deflateResetKeep(&c_stream);
    fprintf(stderr, "deflateReset result: %d\n", err);
#else
    err = deflateReset(&c_stream);
    fprintf(stderr, "deflateReset result: %d\n", err);
#endif
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_1400
 * @tc.name      : Test deflateBound
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestDeflateBound, Function | MediumTest | Level2)
{
D
dujingcheng 已提交
693
#ifdef Z_SOLO
D
dujingcheng 已提交
694 695 696 697 698
    z_stream defstream;
    char *inBuf = reinterpret_cast<char *>(HELLO);
    uint32_t inLen = strlen(inBuf) + 1;
    uint8_t *outBuf = nullptr;
    uint32_t outLen = 0;
D
dujingcheng 已提交
699
    int err = Z_OK;
D
dujingcheng 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

    defstream.zalloc = nullptr;
    defstream.zfree = nullptr;
    defstream.opaque = nullptr;
    defstream.avail_in = static_cast<uInt>(inLen);
    defstream.next_in = reinterpret_cast<Bytef *>(inBuf);
    defstream.avail_out = static_cast<uInt>(outLen);
    defstream.next_out = reinterpret_cast<Bytef *>(outBuf);
    err = deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
    fprintf(stderr, "deflateInit result: %d\n", err);
    ASSERT_EQ(err, Z_OK);
    uint32_t  estimateLen = deflateBound(&defstream, inLen);
    outBuf = reinterpret_cast<uint8_t *>(malloc(estimateLen));
    defstream.avail_out = static_cast<uInt>(estimateLen);
    deflate(&defstream, Z_FINISH);
    deflateEnd(&defstream);
    z_stream outStream;
    err = deflateCopy(&defstream, &outStream);
    fprintf(stderr, "deflateCopy result: %d\n", err);
    free(inBuf);
    free(outBuf);
D
dujingcheng 已提交
721
#endif
D
dujingcheng 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
}

/**
 * @tc.number    : ActsZlibTest_1500
 * @tc.name      : Test adler32
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestCRC, Function | MediumTest | Level2)
{
    uLong err = Z_ERRNO;
    uLong crc1 = 0L;
    uLong crc2 = 0L;
    const Bytef *buf = reinterpret_cast<const Bytef*>(DICTIONARY);
    err = crc32(0L, buf, 0);
    fprintf(stderr, "crc32 result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);

    err = crc32_z(0L, buf, 0);
    fprintf(stderr, "crc32_z result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#ifdef Z_SOLO
#ifndef Z_LARGE64
    err = crc32_combine64(crc1, crc2, 0);
    fprintf(stderr, "crc32_combine64 result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#endif
#else
    err = adler32_combine(crc1, crc2, 0);
    fprintf(stderr, "crc32_combine result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
#endif
}

/**
 * @tc.number    : ActsZlibTest_1600
 * @tc.name      : Test get_crc_table
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGetCrcTable, Function | MediumTest | Level2)
{
    auto table = get_crc_table();
    ASSERT_TRUE(table != nullptr);
}

/**
 * @tc.number    : ActsZlibTest_1700
 * @tc.name      : Test gzBuffer
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzBuffer, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzBuffer Z_SOLO**********\n");
#else
D
dujingcheng 已提交
776
    int err = Z_OK;
D
dujingcheng 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    z_off_t pos;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    err = gzbuffer(file, BUFFER_SIZE);
    ASSERT_EQ(err, Z_OK);

    gzclearerr(file);
    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }

    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclearerr(file);
    gzclose_w(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);

    int res = gzdirect(file);
    fprintf(stderr, "gzdirect result: %d\n", res);
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
    ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO));

    pos = gzseek(file, -8L, SEEK_CUR);
    ASSERT_FALSE(pos != SIX || gztell(file) != pos);
    ASSERT_FALSE(gzgetc(file) != ' ');
    ASSERT_FALSE(gzungetc(' ', file) != ' ');

    fprintf(stderr, "gzgets\n");
    gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
    ASSERT_FALSE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
    gzclose_r(file);
    free(compr);
    free(uncompr);
#endif
D
dujingcheng 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
}

/**
 * @tc.number    : ActsZlibTest_1800
 * @tc.name      : Test gzflush
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzFlush, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzFlush Z_SOLO**********\n");
#else
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzflush(file, Z_FINISH); /* add one zero byte */
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_1900
 * @tc.name      : Test gzfread
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzFread, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzFread Z_SOLO**********\n");
#else
    int err = Z_OK;
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    err = gzfread(HELLO, len, len, file);
    ASSERT_EQ(err, 1);
    gzclose(file);

#endif
}

/**
 * @tc.number    : ActsZlibTest_2000
 * @tc.name      : Test gzfwrite
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzWrite, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzWrite Z_SOLO**********\n");
#else
    int err = Z_OK;
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    err = gzfwrite(HELLO, len, len, file);
    ASSERT_EQ(err, len);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2100
 * @tc.name      : Test gzgetc
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzGetc, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzGetc Z_SOLO**********\n");
#else
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    err = gzgetc(file);
    ASSERT_TRUE(err == 'h');
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2200
 * @tc.name      : Test gzgetc_
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzGetc_, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzGetc_ Z_SOLO**********\n");
#else
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    err = gzgetc_(file);
    ASSERT_TRUE(err == 'h');
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2300
 * @tc.name      : Test gzgets
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzGets, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzGets Z_SOLO**********\n");
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    Byte *uncompr;
    uLong uncomprLen = 10000 * sizeof(int);
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    fprintf(stderr, "gzgets\n");
    gzgets(file, reinterpret_cast<char *>(uncompr), static_cast<int>(uncomprLen));
    ASSERT_TRUE(strcmp(reinterpret_cast<char *>(uncompr), HELLO + SIX));
    gzclose(file);
    free(uncompr);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2400
 * @tc.name      : Test gzoffset64
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzOffset64, Function | MediumTest | Level2)
{
#ifndef Z_LARGE64
    fprintf(stderr, "*********ActsZlibTestGzOffset64 Z_LARGE64**********\n");
#else
    int err = Z_OK;
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    err = gzoffset64(file);
    ASSERT_TRUE(err != NULL);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2500
 * @tc.name      : Test gzopen and gzopen64 and gzopen_w
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzOpen, Function | MediumTest | Level2)
{
#ifndef Z_SOLO
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    gzclose(file); 
#endif
#ifdef Z_LARGE64
    int err = Z_OK;
    gzFile file;
    file = gzopen64(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen64(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    gzclose(file); 
#endif
#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO)
    gzFile file;
    file = gzopen_w(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2600
 * @tc.name      : Test gzprintf
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzPrintf, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzPrintf Z_SOLO**********\n");
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    ASSERT_TRUE(gzprintf(file, ", %s!", "hello") == EIGHT);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2700
 * @tc.name      : Test gzputc
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzPutc, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzPutc Z_SOLO**********\n");
#else
    char err;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    err = gzputc(file, 'h');
    ASSERT_TRUE(err == 'h');
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2800
 * @tc.name      : Test gzputs
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzPuts, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzPuts Z_SOLO**********\n");
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_2900
 * @tc.name      : Test gzread
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzRead, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzRead Z_SOLO**********\n");
#else
    int err = Z_OK;
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    gzputc(file, 'h');
    ASSERT_TRUE(gzputs(file, "ello") == FOUR);
    if (gzprintf(file, ", %s!", "hello") != EIGHT) {
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
        ASSERT_TRUE(false);
    }

    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);

    Byte *uncompr;
    uLong uncomprLen = 10000 * sizeof(int);
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(uncompr != Z_NULL);

    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    ASSERT_TRUE(gzread(file, uncompr, static_cast<unsigned>(uncomprLen)) == len);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3000
 * @tc.name      : Test gzrewind
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzRewind, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzRewind Z_SOLO**********\n");
#else
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    gzseek(file, 0L, SEEK_SET);
    err = gzrewind(file);
    ASSERT_TRUE(err == -1);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3100
 * @tc.name      : Test gzseek and gzseek64
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzseek, Function | MediumTest | Level2)
{
    long err = 0L;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzseek Z_SOLO**********\n");
#else
    err = gzseek(file, 1L, SEEK_CUR);
#endif
#ifdef Z_LARGE64
    err = gzseek64(file, 1L, SEEK_CUR); 
#endif
    ASSERT_TRUE(err == 1L);
    gzclose(file);
}

/**
 * @tc.number    : ActsZlibTest_3200
 * @tc.name      : Test gzsetparams
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzSetParams, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzSetParams Z_SOLO**********\n");
#else
    int err = Z_OK;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    err = gzsetparams(file, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY);
    ASSERT_TRUE(err == Z_OK);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3300
 * @tc.name      : Test gztell and gztell64
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzTell, Function | MediumTest | Level2)
{
#  ifndef Z_LARGE64
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    z_off64_t pos;
    pos = gzseek(file, -8L, SEEK_CUR);
    ASSERT_FALSE(gztell(file) == pos); /* define gztell gztell64 in zlib.h */
    gzclose(file);
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    z_off_t pos;
    pos = gzseek(file, -8L, SEEK_CUR);
    ASSERT_FALSE(pos != SIX || gztell(file) != pos);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3400
 * @tc.name      : Test gzungetc
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzUnGetc, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzUnGetc Z_SOLO**********\n");
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
    gzclose(file);
    file = gzopen(TESTFILE, "rb");
    ASSERT_TRUE(file != NULL);
    ASSERT_FALSE(gzungetc(' ', file) != ' ');
#endif
}

/**
 * @tc.number    : ActsZlibTest_3500
 * @tc.name      : Test gzvprintf
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzVprintf, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzVprintf Z_SOLO**********\n");
#else
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    int err = TestGzPrintf(file, ", %s!", "hello");
    fprintf(stderr, "gzvprintf result: %d\n", err);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3600
 * @tc.name      : Test gzwrite
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzwrite, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzWrite Z_SOLO**********\n");
#else
    int len = static_cast<int>(strlen(HELLO)) + 1;
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    int err = gzwrite(file, HELLO, len);
    ASSERT_EQ(err, len);
    gzclose(file);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3700
 * @tc.name      : Test inflateBackInit, inflateBack, inflateBackEnd
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzInflateBack, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzInflateBack Z_SOLO**********\n");
#else
    int err = Z_OK;
    unsigned char *window;
    z_stream strm;
    unsigned char match[65280 + 2]; /* buffer for reversed match or gzip 32K sliding window */
    Byte *uncompr;
    uLong uncomprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    /* initialize inflateBack state for repeated use */
    window = match; /* reuse match buffer */
    strm.zalloc = nullptr;
    strm.zfree = nullptr;
    strm.opaque = nullptr;
    err = inflateBackInit(&strm, 15, window);
    ASSERT_EQ(err, Z_OK);
    if (err != Z_OK) {
    fprintf(stderr, "gun out of memory error--aborting\n");
    ASSERT_TRUE(false);
    }
    strm.next_in = uncompr;
    strm.avail_in = 1;
    err = inflateBack(&strm, pull, nullptr, push, &strm);
    ASSERT_TRUE(err != NULL);
    err = inflateBackEnd(&strm);
    ASSERT_EQ(err, Z_OK);
#endif
}

/**
 * @tc.number    : ActsZlibTest_3800
 * @tc.name      : Test inflateCodesUsed
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateCodesUsed, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
    unsigned long err;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = 0;
    d_stream.next_out = uncompr;
    err = inflateCodesUsed(&d_stream);
    ASSERT_TRUE(err != Z_NULL);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_3900
 * @tc.name      : Test inflateCopy and inflateEnd
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateCopy_END, Function | MediumTest | Level2)
{
    int err = Z_OK;
    err = inflate(nullptr, 0);
    ASSERT_TRUE(err == Z_STREAM_ERROR);
    err = inflateEnd(nullptr);
    ASSERT_TRUE(err == Z_STREAM_ERROR);
    err = inflateCopy(nullptr, nullptr);
    ASSERT_TRUE(err == Z_STREAM_ERROR);
}

/**
 * @tc.number    : ActsZlibTest_4000
 * @tc.name      : Test inflateGetDictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetDictionary, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    int err = Z_OK;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    err = inflate(&d_stream, Z_NO_FLUSH);
    err = inflateGetDictionary(&d_stream, uncompr, nullptr);
    ASSERT_EQ(err, Z_OK);
    err = inflateEnd(&d_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_4100
 * @tc.name      : Test inflateGetHeader
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateGetHeader, Function | MediumTest | Level2)
{
    struct mem_zone *zone;
    zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
    ASSERT_TRUE(zone != NULL);
    zone->first = nullptr;
    zone->total = 0;
    zone->highwater = 0;
    zone->limit = 0;
    zone->notlifo = 0;
    zone->rogue = 0;
    int err = Z_OK;
    unsigned len = 1;
    unsigned char *out;
    z_stream strm;
    gz_header head;
    strm.opaque = zone;
    strm.zalloc = nullptr;
    strm.zfree = nullptr;
    strm.avail_in = 0;
    strm.next_in = nullptr;
    err = inflateInit2(&strm, 1);
    ASSERT_TRUE(err != Z_OK);
    out = (unsigned char *)malloc(len);
    ASSERT_TRUE(out != NULL);
    head.extra = out;
    head.extra_max = len;
    head.name = out;
    head.name_max = len;
    head.comment = out;
    head.comm_max = len;
    err = inflateGetHeader(&strm, &head);
    ASSERT_TRUE(err != Z_DATA_ERROR);
    free(out);
    free(zone);
}

/**
 * @tc.number    : ActsZlibTest_4200
 * @tc.name      : Test inflateInit_ and inflateInit2_
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateInit_, Function | MediumTest | Level2)
{
    int err = Z_OK;
    int windowBits = 8;
    z_stream strm;
    struct mem_zone *zone;
    zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
    ASSERT_TRUE(zone != NULL);
    zone->first = nullptr;
    zone->total = 0;
    zone->highwater = 0;
    zone->limit = 0;
    zone->notlifo = 0;
    zone->rogue = 0;
    strm.opaque = zone;
    strm.zalloc = nullptr;
    strm.zfree = nullptr;
    strm.avail_in = 0;
    strm.next_in = nullptr;
    err = inflateInit(&strm);
    ASSERT_TRUE(err == Z_OK);
    err = inflatePrime(&strm, 5, 31);
    ASSERT_TRUE(err == Z_OK);
    err = inflatePrime(&strm, -1, 0);
    ASSERT_TRUE(err == Z_OK);
    err = inflateSetDictionary(&strm, nullptr, 0);
    ASSERT_TRUE(err == Z_STREAM_ERROR);
    err = inflateEnd(&strm);
    ASSERT_TRUE(err == Z_OK);
    strm.avail_in = 0;
    strm.next_in = nullptr;
    err = inflateInit_(&strm, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
    ASSERT_TRUE(err == Z_VERSION_ERROR);

    err = inflateInit2_(&strm, windowBits, ZLIB_VERSION - 1, static_cast<int>(sizeof(z_stream)));
    ASSERT_TRUE(err == Z_VERSION_ERROR);
    free(zone);

#ifdef Z_PREFIX
    deflate_state state;
    _tr_init(&state);
    _dist_code distCode = SIX;
    printf("_dist_code: %d\n", reinterpret_cast<int>(distCode));
    _length_code engthCode = FOUR;
    printf("_length_code: %d\n", reinterpret_cast<int>(engthCode));
    _tr_align trAlign;
    printf("_length_code: %d\n", sizeof(trAlign));
    _tr_flush_bits bits;
    printf("_length_code: %d\n", sizeof(bits));
    _tr_flush_block flushBlock;
    printf("_length_code: %d\n", sizeof(flushBlock));
    _tr_stored_block storedBlock;
    printf("_length_code: %d\n", sizeof(storedBlock));
    _tr_tally tally;
    printf("_length_code: %d\n", sizeof(tally));
#endif
}

/**
 * @tc.number    : ActsZlibTest_4300
 * @tc.name      : Test inflatePrime
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflatePrime, Function | MediumTest | Level2)
{
    int ret;
    z_stream strm;
    struct mem_zone *zone;
    zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
    ASSERT_TRUE(zone != NULL);
    zone->first = nullptr;
    zone->total = 0;
    zone->highwater = 0;
    zone->limit = 0;
    zone->notlifo = 0;
    zone->rogue = 0;
    strm.opaque = zone;
    strm.zalloc = nullptr;
    strm.zfree = nullptr;
    strm.avail_in = 0;
    strm.next_in = nullptr;
    ret = inflateInit(&strm);
    ASSERT_TRUE(ret == Z_OK);
    ret = inflatePrime(&strm, 5, 31);
    ASSERT_TRUE(ret == Z_OK);
    free(zone);
}

/**
 * @tc.number    : ActsZlibTest_4400
 * @tc.name      : Test inflateReset, inflateReset2, inflateResetKeep
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateReset, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);
    int err = Z_OK;
    int windowBits = 8;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = 2; /* just read the zlib header */
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    inflate(&d_stream, Z_NO_FLUSH);
    err = inflateReset(&d_stream);
    ASSERT_TRUE(err == Z_OK);

    err = inflateResetKeep(&d_stream);
    ASSERT_TRUE(err == Z_OK);
    err = inflateInit2(&d_stream, windowBits);
    inflate(&d_stream, Z_NO_FLUSH);
    err = inflateReset2(&d_stream, windowBits);
    ASSERT_TRUE(err == Z_OK);
}

/**
 * @tc.number    : ActsZlibTest_4500
 * @tc.name      : Test inflateSetDictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateSetDictionary, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    int err = Z_OK;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    inflate(&d_stream, Z_NO_FLUSH);
    inflateSetDictionary(&d_stream, reinterpret_cast<const Bytef*>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
    err = inflateEnd(&d_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_4600
 * @tc.name      : Test inflateSyncPoint
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateSyncPoint, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    int err = Z_OK;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    err = inflateSyncPoint(&d_stream);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_4700
 * @tc.name      : Test inflateUndermine
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateUndermine, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    int err = Z_OK;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    err = inflateUndermine(&d_stream, 1);
    ASSERT_EQ(err, Z_DATA_ERROR);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_4800
 * @tc.name      : Test inflateValidate
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestInflateValidate, Function | MediumTest | Level2)
{
    Byte *compr, *uncompr;
    uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */
    uLong uncomprLen = comprLen;
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL);

    int err = Z_OK;
    z_stream d_stream; /* decompression stream */
    strcpy(reinterpret_cast<char *>(uncompr), GARBAGE);
    d_stream.zalloc = nullptr;
    d_stream.zfree = nullptr;
    d_stream.opaque = nullptr;
    d_stream.next_in  = compr;
    d_stream.avail_in = static_cast<uInt>(comprLen);
    err = inflateInit(&d_stream);
    ASSERT_EQ(err, Z_OK);
    d_stream.next_out = uncompr;
    d_stream.avail_out = static_cast<uInt>(uncomprLen);
    err = inflateValidate(&d_stream, 1);
    ASSERT_EQ(err, Z_OK);
    free(compr);
    free(uncompr);
}

/**
 * @tc.number    : ActsZlibTest_4900
 * @tc.name      : Test zlibCompileFlags
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestzlibCompileFlags, Function | MediumTest | Level2)
{
    static const char* myVersion = ZLIB_VERSION;

    if (zlibVersion()[0] != myVersion[0]) {
        fprintf(stderr, "incompatible zlib version\n");
        ASSERT_TRUE(false);

    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
        fprintf(stderr, "warning: different zlib version\n");
    }

    printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
            ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
}

/**
 * @tc.number    : ActsZlibTest_5000
 * @tc.name      : Test zError
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestzError, Function | MediumTest | Level2)
{
    const char* err;   
    err = zError(Z_DATA_ERROR);
    ASSERT_EQ(err, "data error");
}

/**
 * @tc.number    : ActsZlibTest_5100
 * @tc.name      : Test zlibVersion
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestzlibVersion, Function | MediumTest | Level2)
{
    static const char* myVersion = ZLIB_VERSION;
    static const char* err;
    err = zlibVersion();
    ASSERT_EQ(err, myVersion);
D
dujingcheng 已提交
1747
}