ActsZlibTest.cpp 57.5 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
/**
 * 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>
I
inter515 已提交
21
#include <mutex>
D
dujingcheng 已提交
22
#include <securec.h>
D
dujingcheng 已提交
23
#include <sstream>
24
#include <mutex>
D
dujingcheng 已提交
25 26 27 28 29 30 31 32 33 34
#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";
I
inter515 已提交
35
static thread_local char HELLO[] = "hello, hello!";
D
dujingcheng 已提交
36
static unsigned int CALLOC_SIZE = 1;
D
dujingcheng 已提交
37
static int ONE = 1;
D
dujingcheng 已提交
38 39 40
static int FOUR = 4;
static int SIX = 6;
static int EIGHT = 8;
41
static int GARBAGE_LEN = strlen(GARBAGE) + 1;
D
dujingcheng 已提交
42
static unsigned BUFFER_SIZE = 8192;
I
inter515 已提交
43 44
std::mutex gzMutex_;
std::mutex puMutex_;
I
inter515 已提交
45
std::mutex file_mutex;
D
dujingcheng 已提交
46

D
dujingcheng 已提交
47 48
static unsigned pull(void *desc, unsigned char **buf)
{
I
inter515 已提交
49
    std::lock_guard<std::mutex> lock(puMutex_);
D
dujingcheng 已提交
50 51 52
    static unsigned int next = 0;
    static unsigned char dat[] = {0x63, 0, 2, 0};

R
ry 已提交
53
    if (!desc) {
D
dujingcheng 已提交
54 55 56 57 58 59 60 61
        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)
{
I
inter515 已提交
62
    std::lock_guard<std::mutex> lock(puMutex_);
D
dujingcheng 已提交
63 64 65 66 67 68
    buf += len;
    return desc != nullptr;      /* force error if desc not null */
}

static int TestGzPrintf(gzFile file, const char *format, ...)
{
I
inter515 已提交
69
    std::lock_guard<std::mutex> lock(gzMutex_);
D
dujingcheng 已提交
70 71 72 73 74 75 76 77 78
    va_list va;
    int ret;

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

D
dujingcheng 已提交
79
class ActsZlibTest : public testing::Test {
D
dujingcheng 已提交
80
protected:
D
dujingcheng 已提交
81 82
    ActsZlibTest();
    ~ActsZlibTest();
D
dujingcheng 已提交
83 84 85 86
    static void SetUpTestCase();
    static void TearDownTestCase();
};

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

D
dujingcheng 已提交
90
ActsZlibTest::~ActsZlibTest()
D
dujingcheng 已提交
91 92
{}

D
dujingcheng 已提交
93
void ActsZlibTest::SetUpTestCase()
D
dujingcheng 已提交
94 95
{}

D
dujingcheng 已提交
96
void ActsZlibTest::TearDownTestCase()
D
dujingcheng 已提交
97 98
{}

D
dujingcheng 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/* 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 已提交
114
/**
D
dujingcheng 已提交
115
 * @tc.number    : ActsZlibTest_0100
D
dujingcheng 已提交
116 117 118
 * @tc.name      : Test compress and uncompress test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
119
HWTEST_F(ActsZlibTest, ActsZlibTestCompress, Function | MediumTest | Level2)
D
dujingcheng 已提交
120 121
{
#ifdef Z_SOLO
D
dujingcheng 已提交
122
    fprintf(stderr, "*********ActsZlibTestCompress Z_SOLO**********\n");
D
dujingcheng 已提交
123 124
#else
    Byte *compr, *uncompr;
125
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
126 127 128 129 130
    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 已提交
131
    int err = Z_OK;
D
dujingcheng 已提交
132 133 134 135 136
    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);

D
dujingcheng 已提交
137
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
138 139 140 141 142 143 144 145 146 147
    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 已提交
148
 * @tc.number    : ActsZlibTest_0200
D
dujingcheng 已提交
149 150 151
 * @tc.name      : Test gzio
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
152
HWTEST_F(ActsZlibTest, ActsZlibTestGzio, Function | MediumTest | Level2)
D
dujingcheng 已提交
153 154
{
#ifdef Z_SOLO
D
dujingcheng 已提交
155
    fprintf(stderr, "*********ActsZlibTestGzio Z_SOLO**********\n");
D
dujingcheng 已提交
156
#else
I
inter515 已提交
157
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
158
    int err = Z_OK;
D
dujingcheng 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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;
178
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
179 180 181 182 183
    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 已提交
184
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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 已提交
203
 * @tc.number    : ActsZlibTest_0300
D
dujingcheng 已提交
204 205 206
 * @tc.name      : Test deflate
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
207
HWTEST_F(ActsZlibTest, ActsZlibTestDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
208 209
{
    Byte *compr;
210
    uLong comprLen = 100 * sizeof(int);
D
dujingcheng 已提交
211 212 213 214
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
215
    int err = Z_OK;
D
dujingcheng 已提交
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 241 242 243 244 245 246 247 248 249 250
    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 已提交
251
 * @tc.number    : ActsZlibTest_0400
D
dujingcheng 已提交
252 253 254
 * @tc.name      : Test inflate
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
255
HWTEST_F(ActsZlibTest, ActsZlibTestInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
256 257
{
    Byte *compr, *uncompr;
258
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
259 260 261 262 263
    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 已提交
264
    int err = Z_OK;
D
dujingcheng 已提交
265
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
266
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    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 已提交
293
 * @tc.number    : ActsZlibTest_0500
D
dujingcheng 已提交
294 295 296
 * @tc.name      : Test deflate with large buffers and dynamic change of compression level
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
297
HWTEST_F(ActsZlibTest, ActsZlibTestLargeDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
298 299
{
    Byte *compr, *uncompr;
300
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
301 302 303 304 305 306
    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 已提交
307
    int err = Z_OK;
D
dujingcheng 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    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);
R
ry 已提交
327
    ASSERT_TRUE(!c_stream.avail_in);
D
dujingcheng 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

    /* 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 已提交
354
 * @tc.number    : ActsZlibTest_0600
D
dujingcheng 已提交
355 356 357
 * @tc.name      : Test inflate with large buffers
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
358
HWTEST_F(ActsZlibTest, ActsZlibTestLargeInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
359 360
{
    Byte *compr, *uncompr;
361
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
362 363 364 365 366
    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 已提交
367
    int err = Z_OK;
D
dujingcheng 已提交
368
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
369
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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 已提交
395
 * @tc.number    : ActsZlibTest_0700
D
dujingcheng 已提交
396 397 398
 * @tc.name      : Test deflate with full flush
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
399
HWTEST_F(ActsZlibTest, ActsZlibTestFlush, Function | MediumTest | Level2)
D
dujingcheng 已提交
400 401
{
    Byte *compr;
402
    uLong comprLen = 100 * sizeof(int);
D
dujingcheng 已提交
403 404 405 406
    compr = static_cast<Byte*>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
    ASSERT_TRUE(compr != Z_NULL);

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
407
    int err = Z_OK;
D
dujingcheng 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    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 已提交
436
 * @tc.number    : ActsZlibTest_0800
D
dujingcheng 已提交
437 438 439
 * @tc.name      : Test inflateSync
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
440
HWTEST_F(ActsZlibTest, ActsZlibTestSync, Function | MediumTest | Level2)
D
dujingcheng 已提交
441 442
{
    Byte *compr, *uncompr;
443
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
444 445 446 447 448
    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 已提交
449
    int err = Z_OK;
D
dujingcheng 已提交
450
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
451
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    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);
I
inter515 已提交
468
    printf("after inflateSync: help%s\n", reinterpret_cast<char *>(uncompr));
D
dujingcheng 已提交
469 470 471 472 473
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
474
 * @tc.number    : ActsZlibTest_0900
D
dujingcheng 已提交
475 476 477
 * @tc.name      : Test deflate with preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
478
HWTEST_F(ActsZlibTest, ActsZlibTestDictDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
479 480
{
    Byte *compr, *uncompr;
481
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
482 483 484 485 486 487
    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 已提交
488
    int err = Z_OK;
D
dujingcheng 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    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 已提交
513
 * @tc.number    : ActsZlibTest_1000
D
dujingcheng 已提交
514 515 516
 * @tc.name      : Test inflate with a preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
517
HWTEST_F(ActsZlibTest, ActsZlibTestDictInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
518 519
{
    Byte *compr, *uncompr;
520
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
521 522 523 524 525
    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 已提交
526
    int err = Z_OK;
D
dujingcheng 已提交
527
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
528
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    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 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575
}

/**
 * @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;
576
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
577 578 579 580 581
    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 已提交
582
    int err = Z_OK;
D
dujingcheng 已提交
583 584 585 586 587 588 589
    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);

D
dujingcheng 已提交
590
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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 638 639
    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;
640
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
641 642 643 644 645 646 647
    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 已提交
648
    int err = Z_OK;
D
dujingcheng 已提交
649 650 651 652 653
    int windowBits = EIGHT;
    int memLevel = EIGHT;
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
D
dujingcheng@huawei.com 已提交
654 655
    err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits,
        memLevel, Z_FILTERED, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
D
dujingcheng 已提交
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 693 694 695 696 697 698 699 700 701 702
    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 已提交
703
#ifdef Z_SOLO
D
dujingcheng 已提交
704 705 706 707 708
    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 已提交
709
    int err = Z_OK;
D
dujingcheng 已提交
710 711 712 713 714 715 716 717

    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);
D
dujingcheng@huawei.com 已提交
718 719 720
    err = deflateInit_(&defstream, Z_DEFAULT_COMPRESSION,
        ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
    fprintf(stderr, "deflateInit_ result: %d\n", err);
D
dujingcheng 已提交
721 722 723 724 725 726 727 728 729 730 731
    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 已提交
732
#endif
D
dujingcheng 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
}

/**
 * @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
D
dujingcheng@huawei.com 已提交
754
#ifdef Z_LARGE64
D
dujingcheng 已提交
755 756 757
    err = crc32_combine64(crc1, crc2, 0);
    fprintf(stderr, "crc32_combine64 result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
D
dujingcheng@huawei.com 已提交
758 759 760 761
#else
    err = crc32_combine(crc1, crc2, 0);
    fprintf(stderr, "crc32_combine result: %lu\n", err);
    ASSERT_NE(err, Z_ERRNO);
D
dujingcheng 已提交
762 763 764
#endif
#else
    err = adler32_combine(crc1, crc2, 0);
D
dujingcheng@huawei.com 已提交
765
    fprintf(stderr, "adler32_combine result: %lu\n", err);
D
dujingcheng 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    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
I
inter515 已提交
791
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
792
    int err = Z_OK;
D
dujingcheng 已提交
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
    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;
819
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
820 821 822 823 824
    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 已提交
825
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    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 已提交
841 842 843 844 845 846 847 848 849 850 851 852
}

/**
 * @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
I
inter515 已提交
853
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
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
    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
I
inter515 已提交
881
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    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
I
inter515 已提交
904
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    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
I
inter515 已提交
926
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    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
I
inter515 已提交
947
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    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
I
inter515 已提交
968
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
969 970 971 972
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);
    Byte *uncompr;
973
    uLong uncomprLen = 100 * sizeof(int);
D
dujingcheng 已提交
974
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
D
dujingcheng 已提交
975
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    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
I
inter515 已提交
994
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
995 996 997 998 999 1000
    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);
D
zlib  
dujingcheng 已提交
1001
    ASSERT_TRUE(err != Z_OK);
D
dujingcheng 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    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)
{
I
inter515 已提交
1013
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
#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);
R
ry 已提交
1029
    gzclose(file);
D
dujingcheng 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
#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);
R
ry 已提交
1046
    gzclose(file);
D
dujingcheng 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
#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
I
inter515 已提交
1066
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    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
I
inter515 已提交
1085
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    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
I
inter515 已提交
1106
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    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
I
inter515 已提交
1125
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    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;
1145
    uLong uncomprLen = 100 * sizeof(int);
D
dujingcheng 已提交
1146 1147 1148
    uncompr = static_cast<Byte*>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
    ASSERT_TRUE(uncompr != Z_NULL);

D
dujingcheng 已提交
1149
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    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
I
inter515 已提交
1165
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    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)
{
I
inter515 已提交
1183
    std::lock_guard<std::mutex> lock(file_mutex);
R
r00559040 已提交
1184
    long err = 0L;
D
dujingcheng 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193
    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
R
ry 已提交
1194
    err = gzseek64(file, 1L, SEEK_CUR);
D
dujingcheng 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
#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
I
inter515 已提交
1210
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    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)
{
I
inter515 已提交
1228
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
#  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);
D
dujingcheng@huawei.com 已提交
1239
    ASSERT_FALSE(gztell(file) == pos); /* define gztell gztell in zlib.h */
D
dujingcheng 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    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);
D
dujingcheng@huawei.com 已提交
1251
    ASSERT_FALSE(pos != SIX || gztell64(file) != pos);
D
dujingcheng 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    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
I
inter515 已提交
1266
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1267 1268 1269 1270 1271 1272 1273 1274
    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) != ' ');
L
longwei 已提交
1275
    gzclose(file);
D
dujingcheng 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
#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
I
inter515 已提交
1289
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1290 1291 1292 1293 1294 1295
    gzFile file;
    file = gzopen(TESTFILE, "wb");
    ASSERT_TRUE(file != NULL);

    int err = TestGzPrintf(file, ", %s!", "hello");
    fprintf(stderr, "gzvprintf result: %d\n", err);
L
longwei 已提交
1296
    gzclose(file);
D
dujingcheng 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
#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
I
inter515 已提交
1310
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
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
    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;
1336
    uLong uncomprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1337 1338 1339 1340 1341 1342
    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;
D
dujingcheng@huawei.com 已提交
1343 1344
    err = inflateBackInit_(
        &strm, 15, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
D
dujingcheng 已提交
1345 1346
    ASSERT_EQ(err, Z_OK);
    if (err != Z_OK) {
D
zlib  
dujingcheng 已提交
1347 1348
        fprintf(stderr, "gun out of memory error--aborting\n");
        ASSERT_TRUE(false);
D
dujingcheng 已提交
1349 1350 1351 1352
    }
    strm.next_in = uncompr;
    strm.avail_in = 1;
    err = inflateBack(&strm, pull, nullptr, push, &strm);
D
zlib  
dujingcheng 已提交
1353
    ASSERT_TRUE(err != Z_OK);
D
dujingcheng 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
    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;
1367
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1368 1369 1370 1371
    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);
R
r00559040 已提交
1372
    unsigned long err;
D
dujingcheng 已提交
1373
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
1374
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1375 1376 1377 1378 1379 1380 1381
    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);
D
zlib  
dujingcheng 已提交
1382
    ASSERT_TRUE(err != Z_OK);
D
dujingcheng 已提交
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
    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;
1411
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1412 1413 1414 1415 1416 1417 1418
    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 */
D
dujingcheng 已提交
1419
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    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);
D
dujingcheng@huawei.com 已提交
1432
    inflateMark(&d_stream);
D
dujingcheng 已提交
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
    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;
1582
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1583 1584 1585 1586 1587 1588 1589
    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 */
D
dujingcheng 已提交
1590
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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
    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;
1620
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1621 1622 1623 1624 1625 1626 1627
    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 */
D
dujingcheng 已提交
1628
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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
    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;
1654
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1655 1656 1657 1658 1659 1660 1661
    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 */
D
dujingcheng 已提交
1662
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    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;
1686
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1687 1688 1689 1690 1691 1692 1693
    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 */
D
dujingcheng 已提交
1694
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
    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;
1718
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1719 1720 1721 1722 1723 1724 1725
    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 */
D
dujingcheng 已提交
1726
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
    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);

R
ry 已提交
1755
    } else if (strcmp(zlibVersion(), ZLIB_VERSION)) {
D
dujingcheng 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
        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)
{
R
ry 已提交
1770
    const char* err;
D
dujingcheng 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
    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);
R
ry 已提交
1786
}
D
dujingcheng@huawei.com 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

/**
 * @tc.number    : ActsZlibTest_5200
 * @tc.name      : Test gzdopen
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsZlibTest, ActsZlibTestGzdopen, Function | MediumTest | Level2)
{
#ifdef Z_SOLO
    fprintf(stderr, "*********ActsZlibTestGzdopen Z_SOLO**********\n");
#else
I
inter515 已提交
1798
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng@huawei.com 已提交
1799 1800 1801 1802 1803 1804 1805
    FILE *fp = fopen(TESTFILE, "r");
    int fd = fileno(fp);
    gzFile file = gzdopen(fd, "r");
    ASSERT_TRUE(file != NULL);

    int err = gzeof(file);
    fprintf(stderr, "gzeof result: %d\n", err);
W
wangdengjia 已提交
1806
    fclose(fp);
D
dujingcheng@huawei.com 已提交
1807 1808 1809
    gzclose(file);
#endif
}
D
dujingcheng@huawei.com 已提交
1810
}