ActsZlibTest.cpp 57.6 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()
W
wangdengjia 已提交
88 89 90
{
    chdir("/data/local/tmp");
}
D
dujingcheng 已提交
91

D
dujingcheng 已提交
92
ActsZlibTest::~ActsZlibTest()
D
dujingcheng 已提交
93 94
{}

D
dujingcheng 已提交
95
void ActsZlibTest::SetUpTestCase()
D
dujingcheng 已提交
96 97
{}

D
dujingcheng 已提交
98
void ActsZlibTest::TearDownTestCase()
D
dujingcheng 已提交
99 100
{}

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

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

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

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

    z_stream c_stream; /* compression stream */
D
dujingcheng 已提交
409
    int err = Z_OK;
D
dujingcheng 已提交
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 436 437
    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 已提交
438
 * @tc.number    : ActsZlibTest_0800
D
dujingcheng 已提交
439 440 441
 * @tc.name      : Test inflateSync
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
442
HWTEST_F(ActsZlibTest, ActsZlibTestSync, Function | MediumTest | Level2)
D
dujingcheng 已提交
443 444
{
    Byte *compr, *uncompr;
445
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
446 447 448 449 450
    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 已提交
451
    int err = Z_OK;
D
dujingcheng 已提交
452
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
453
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    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 已提交
470
    printf("after inflateSync: help%s\n", reinterpret_cast<char *>(uncompr));
D
dujingcheng 已提交
471 472 473 474 475
    free(compr);
    free(uncompr);
}

/**
D
dujingcheng 已提交
476
 * @tc.number    : ActsZlibTest_0900
D
dujingcheng 已提交
477 478 479
 * @tc.name      : Test deflate with preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
480
HWTEST_F(ActsZlibTest, ActsZlibTestDictDeflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
481 482
{
    Byte *compr, *uncompr;
483
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
484 485 486 487 488 489
    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 已提交
490
    int err = Z_OK;
D
dujingcheng 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    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 已提交
515
 * @tc.number    : ActsZlibTest_1000
D
dujingcheng 已提交
516 517 518
 * @tc.name      : Test inflate with a preset dictionary
 * @tc.desc      : [C- SOFTWARE -0200]
 */
D
dujingcheng 已提交
519
HWTEST_F(ActsZlibTest, ActsZlibTestDictInflate, Function | MediumTest | Level2)
D
dujingcheng 已提交
520 521
{
    Byte *compr, *uncompr;
522
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
523 524 525 526 527
    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 已提交
528
    int err = Z_OK;
D
dujingcheng 已提交
529
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
530
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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 563 564
    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 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577
}

/**
 * @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;
578
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
579 580 581 582 583
    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 已提交
584
    int err = Z_OK;
D
dujingcheng 已提交
585 586 587 588 589 590 591
    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 已提交
592
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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 640 641
    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;
642
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
643 644 645 646 647 648 649
    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 已提交
650
    int err = Z_OK;
D
dujingcheng 已提交
651 652 653 654 655
    int windowBits = EIGHT;
    int memLevel = EIGHT;
    c_stream.zalloc = nullptr;
    c_stream.zfree = nullptr;
    c_stream.opaque = nullptr;
D
dujingcheng@huawei.com 已提交
656 657
    err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits,
        memLevel, Z_FILTERED, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
D
dujingcheng 已提交
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 703 704
    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 已提交
705
#ifdef Z_SOLO
D
dujingcheng 已提交
706 707 708 709 710
    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 已提交
711
    int err = Z_OK;
D
dujingcheng 已提交
712 713 714 715 716 717 718 719

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

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

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

D
dujingcheng 已提交
1151
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    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 已提交
1167
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    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 已提交
1185
    std::lock_guard<std::mutex> lock(file_mutex);
R
r00559040 已提交
1186
    long err = 0L;
D
dujingcheng 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195
    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 已提交
1196
    err = gzseek64(file, 1L, SEEK_CUR);
D
dujingcheng 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
#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 已提交
1212
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    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 已提交
1230
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
#  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 已提交
1241
    ASSERT_FALSE(gztell(file) == pos); /* define gztell gztell in zlib.h */
D
dujingcheng 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    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 已提交
1253
    ASSERT_FALSE(pos != SIX || gztell64(file) != pos);
D
dujingcheng 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    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 已提交
1268
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1269 1270 1271 1272 1273 1274 1275 1276
    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 已提交
1277
    gzclose(file);
D
dujingcheng 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
#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 已提交
1291
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
1292 1293 1294 1295 1296 1297
    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 已提交
1298
    gzclose(file);
D
dujingcheng 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
#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 已提交
1312
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng 已提交
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
    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;
1338
    uLong uncomprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1339 1340 1341 1342 1343 1344
    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 已提交
1345 1346
    err = inflateBackInit_(
        &strm, 15, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
D
dujingcheng 已提交
1347 1348
    ASSERT_EQ(err, Z_OK);
    if (err != Z_OK) {
D
zlib  
dujingcheng 已提交
1349 1350
        fprintf(stderr, "gun out of memory error--aborting\n");
        ASSERT_TRUE(false);
D
dujingcheng 已提交
1351 1352 1353 1354
    }
    strm.next_in = uncompr;
    strm.avail_in = 1;
    err = inflateBack(&strm, pull, nullptr, push, &strm);
D
zlib  
dujingcheng 已提交
1355
    ASSERT_TRUE(err != Z_OK);
D
dujingcheng 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
    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;
1369
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1370 1371 1372 1373
    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 已提交
1374
    unsigned long err;
D
dujingcheng 已提交
1375
    z_stream d_stream; /* decompression stream */
D
dujingcheng 已提交
1376
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1377 1378 1379 1380 1381 1382 1383
    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 已提交
1384
    ASSERT_TRUE(err != Z_OK);
D
dujingcheng 已提交
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
    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;
1413
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1414 1415 1416 1417 1418 1419 1420
    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 已提交
1421
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    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 已提交
1434
    inflateMark(&d_stream);
D
dujingcheng 已提交
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
    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;
1584
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1585 1586 1587 1588 1589 1590 1591
    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 已提交
1592
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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
    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;
1622
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1623 1624 1625 1626 1627 1628 1629
    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 已提交
1630
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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
    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;
1656
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1657 1658 1659 1660 1661 1662 1663
    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 已提交
1664
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    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;
1688
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1689 1690 1691 1692 1693 1694 1695
    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 已提交
1696
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
    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;
1720
    uLong comprLen = 100 * sizeof(int); /* don't overflow on MSDOS */
D
dujingcheng 已提交
1721 1722 1723 1724 1725 1726 1727
    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 已提交
1728
    strcpy_s(reinterpret_cast<char *>(uncompr), GARBAGE_LEN, GARBAGE);
D
dujingcheng 已提交
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 1755 1756
    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 已提交
1757
    } else if (strcmp(zlibVersion(), ZLIB_VERSION)) {
D
dujingcheng 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
        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 已提交
1772
    const char* err;
D
dujingcheng 已提交
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
    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 已提交
1788
}
D
dujingcheng@huawei.com 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799

/**
 * @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 已提交
1800
    std::lock_guard<std::mutex> lock(file_mutex);
D
dujingcheng@huawei.com 已提交
1801 1802 1803 1804 1805 1806 1807
    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 已提交
1808
    fclose(fp);
D
dujingcheng@huawei.com 已提交
1809 1810 1811
    gzclose(file);
#endif
}
D
dujingcheng@huawei.com 已提交
1812
}