From a756d8c0459cfa7a722e48fb980c54fbf4af5e50 Mon Sep 17 00:00:00 2001 From: dujingcheng Date: Mon, 7 Feb 2022 12:32:14 +0000 Subject: [PATCH] zlib xts Signed-off-by: dujingcheng Change-Id: If9578e058a1a131b3768fbf6d412bf6fd33b90c0 --- appexecfwk/BUILD.gn | 2 +- .../actszlibtest/ActsZlibTest.cpp} | 331 ++++++++++++++++-- .../actszlibtest}/BUILD.gn | 6 +- .../actszlibtest}/Test.json | 4 +- 4 files changed, 308 insertions(+), 35 deletions(-) rename appexecfwk/{zlibtest/zlibcpptest/ZlibCppTest.cpp => zlib/actszlibtest/ActsZlibTest.cpp} (56%) rename appexecfwk/{zlibtest/zlibcpptest => zlib/actszlibtest}/BUILD.gn (87%) rename appexecfwk/{zlibtest/zlibcpptest => zlib/actszlibtest}/Test.json (80%) diff --git a/appexecfwk/BUILD.gn b/appexecfwk/BUILD.gn index 7d7f59410..0d49f0179 100644 --- a/appexecfwk/BUILD.gn +++ b/appexecfwk/BUILD.gn @@ -18,7 +18,7 @@ group("appexecfwk") { if (is_standard_system) { deps = [ "bundle_standard:bundle_standard", - "zlibtest/zlibcpptest:ZlibCppTest", + "zlib/actszlibtest:ActsZlibTest", ] } } diff --git a/appexecfwk/zlibtest/zlibcpptest/ZlibCppTest.cpp b/appexecfwk/zlib/actszlibtest/ActsZlibTest.cpp similarity index 56% rename from appexecfwk/zlibtest/zlibcpptest/ZlibCppTest.cpp rename to appexecfwk/zlib/actszlibtest/ActsZlibTest.cpp index e905b7927..aef70243d 100644 --- a/appexecfwk/zlibtest/zlibcpptest/ZlibCppTest.cpp +++ b/appexecfwk/zlib/actszlibtest/ActsZlibTest.cpp @@ -31,40 +31,42 @@ static const char GARBAGE[] = "garbage"; static const char TESTFILE[] = "foo.gz"; static char HELLO[] = "hello, hello!"; static unsigned int CALLOC_SIZE = 1; +static int ONE = 1; static int FOUR = 4; static int SIX = 6; static int EIGHT = 8; +static unsigned BUFFER_SIZE = 8192; } -class ZlibCppTest : public testing::Test { +class ActsZlibTest : public testing::Test { protected: - ZlibCppTest(); - ~ZlibCppTest(); + ActsZlibTest(); + ~ActsZlibTest(); static void SetUpTestCase(); static void TearDownTestCase(); }; -ZlibCppTest::ZlibCppTest() +ActsZlibTest::ActsZlibTest() {} -ZlibCppTest::~ZlibCppTest() +ActsZlibTest::~ActsZlibTest() {} -void ZlibCppTest::SetUpTestCase() +void ActsZlibTest::SetUpTestCase() {} -void ZlibCppTest::TearDownTestCase() +void ActsZlibTest::TearDownTestCase() {} /** - * @tc.number : ZlibCppTest_0100 + * @tc.number : ActsZlibTest_0100 * @tc.name : Test compress and uncompress test * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestCompress, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestCompress, Function | MediumTest | Level2) { #ifdef Z_SOLO - fprintf(stderr, "*********ZlibCppTestCompress Z_SOLO**********\n"); + fprintf(stderr, "*********ActsZlibTestCompress Z_SOLO**********\n"); #else Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -90,14 +92,14 @@ HWTEST_F(ZlibCppTest, ZlibCppTestCompress, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0200 + * @tc.number : ActsZlibTest_0200 * @tc.name : Test gzio * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestGzio, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestGzio, Function | MediumTest | Level2) { #ifdef Z_SOLO - fprintf(stderr, "*********ZlibCppTestGzio Z_SOLO**********\n"); + fprintf(stderr, "*********ActsZlibTestGzio Z_SOLO**********\n"); #else int err; int len = static_cast(strlen(HELLO)) + 1; @@ -144,11 +146,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestGzio, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0300 + * @tc.number : ActsZlibTest_0300 * @tc.name : Test deflate * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestDeflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestDeflate, Function | MediumTest | Level2) { Byte *compr; uLong comprLen = 10000 * sizeof(int); @@ -192,11 +194,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestDeflate, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0400 + * @tc.number : ActsZlibTest_0400 * @tc.name : Test inflate * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestInflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestInflate, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -234,11 +236,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestInflate, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0500 + * @tc.number : ActsZlibTest_0500 * @tc.name : Test deflate with large buffers and dynamic change of compression level * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestLargeDeflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestLargeDeflate, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -295,11 +297,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestLargeDeflate, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0600 + * @tc.number : ActsZlibTest_0600 * @tc.name : Test inflate with large buffers * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestLargeInflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestLargeInflate, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -336,11 +338,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestLargeInflate, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0700 + * @tc.number : ActsZlibTest_0700 * @tc.name : Test deflate with full flush * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestFlush, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestFlush, Function | MediumTest | Level2) { Byte *compr; uLong comprLen = 10000 * sizeof(int); @@ -377,11 +379,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestFlush, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0800 + * @tc.number : ActsZlibTest_0800 * @tc.name : Test inflateSync * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestSync, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestSync, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -415,11 +417,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestSync, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_0900 + * @tc.number : ActsZlibTest_0900 * @tc.name : Test deflate with preset dictionary * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestDictDeflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestDictDeflate, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -454,11 +456,11 @@ HWTEST_F(ZlibCppTest, ZlibCppTestDictDeflate, Function | MediumTest | Level2) } /** - * @tc.number : ZlibCppTest_1000 + * @tc.number : ActsZlibTest_1000 * @tc.name : Test inflate with a preset dictionary * @tc.desc : [C- SOFTWARE -0200] */ -HWTEST_F(ZlibCppTest, ZlibCppTestDictInflate, Function | MediumTest | Level2) +HWTEST_F(ActsZlibTest, ActsZlibTestDictInflate, Function | MediumTest | Level2) { Byte *compr, *uncompr; uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ @@ -504,4 +506,275 @@ HWTEST_F(ZlibCppTest, ZlibCppTestDictInflate, Function | MediumTest | Level2) free(compr); free(uncompr); +} + +/** + * @tc.number : ActsZlibTest_1100 + * @tc.name : Test compress2 with Z_BEST_COMPRESSION level + * @tc.desc : [C- SOFTWARE -0200] + */ +HWTEST_F(ActsZlibTest, ActsZlibTestCompress2, Function | MediumTest | Level2) +{ +#ifdef Z_SOLO + fprintf(stderr, "*********ActsZlibTestCompress2 Z_BEST_COMPRESSION Z_SOLO**********\n"); +#else + Byte *compr, *uncompr; + uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ + uLong uncomprLen = comprLen; + compr = static_cast(calloc(static_cast(comprLen), CALLOC_SIZE)); + uncompr = static_cast(calloc(static_cast(uncomprLen), CALLOC_SIZE)); + ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL); + + int err; + uLong len = static_cast(strlen(HELLO)) + 1; + uLong outLen = compressBound(len); + fprintf(stderr, "compressBound result: %lu\n", outLen); + err = compress2(compr, &comprLen, reinterpret_cast(HELLO), outLen, Z_BEST_COMPRESSION); + fprintf(stderr, "compress2 Z_BEST_COMPRESSION result: %d\n", err); + ASSERT_EQ(err, Z_OK); + + strcpy(reinterpret_cast(uncompr), GARBAGE); + err = uncompress2(uncompr, &uncomprLen, compr, &comprLen); + fprintf(stderr, "uncompress2 Z_BEST_COMPRESSION result: %d\n", err); + ASSERT_EQ(err, Z_OK); + fprintf(stderr, "uncompress2: %s\n", reinterpret_cast(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(DICTIONARY); + err = adler32(0L, buf, 0); + fprintf(stderr, "adler32 result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); + + err = adler32_z(0L, buf, 0); + fprintf(stderr, "adler32_z result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#ifdef Z_SOLO +#ifndef Z_LARGE64 + err = adler32_combine64(adler1, adler2, 0); + fprintf(stderr, "adler32_combine64 result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#endif +#else + err = adler32_combine(adler1, adler2, 0); + fprintf(stderr, "adler32_combine result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#endif +} + +/** + * @tc.number : ActsZlibTest_1300 + * @tc.name : Test deflate state + * @tc.desc : [C- SOFTWARE -0200] + */ +HWTEST_F(ActsZlibTest, ActsZlibTestDeflateState, Function | MediumTest | Level2) +{ + Byte *compr, *uncompr; + int *bits = nullptr; + uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ + uLong uncomprLen = comprLen; + compr = static_cast(calloc(static_cast(comprLen), CALLOC_SIZE)); + uncompr = static_cast(calloc(static_cast(uncomprLen), CALLOC_SIZE)); + ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL); + + gz_headerp headerp = nullptr; + z_stream c_stream; /* compression stream */ + int err; + int windowBits = EIGHT; + int memLevel = EIGHT; + c_stream.zalloc = nullptr; + c_stream.zfree = nullptr; + c_stream.opaque = nullptr; + err = deflateInit2( + &c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED); + ASSERT_EQ(err, Z_OK); + deflateSetHeader(&c_stream, headerp); + deflateTune(&c_stream, ONE, FOUR, EIGHT, ONE); + memLevel = ONE; + err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY); + fprintf(stderr, "deflateParams result: %d\n", err); + ASSERT_EQ(err, Z_OK); + + err = deflatePending(&c_stream, nullptr, bits); + fprintf(stderr, "deflatePending result: %d\n", err); + ASSERT_EQ(err, Z_OK); + + err = deflateSetDictionary(&c_stream, + reinterpret_cast(DICTIONARY), static_cast(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(comprLen); + c_stream.next_in = reinterpret_cast(HELLO); + c_stream.avail_in = static_cast(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) +{ + z_stream defstream; + char *inBuf = reinterpret_cast(HELLO); + uint32_t inLen = strlen(inBuf) + 1; + uint8_t *outBuf = nullptr; + uint32_t outLen = 0; + int err; + + defstream.zalloc = nullptr; + defstream.zfree = nullptr; + defstream.opaque = nullptr; + defstream.avail_in = static_cast(inLen); + defstream.next_in = reinterpret_cast(inBuf); + defstream.avail_out = static_cast(outLen); + defstream.next_out = reinterpret_cast(outBuf); + err = deflateInit(&defstream, Z_DEFAULT_COMPRESSION); + fprintf(stderr, "deflateInit result: %d\n", err); + ASSERT_EQ(err, Z_OK); + + uint32_t estimateLen = deflateBound(&defstream, inLen); + outBuf = reinterpret_cast(malloc(estimateLen)); + defstream.avail_out = static_cast(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); +} + +/** + * @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(DICTIONARY); + err = crc32(0L, buf, 0); + fprintf(stderr, "crc32 result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); + + err = crc32_z(0L, buf, 0); + fprintf(stderr, "crc32_z result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#ifdef Z_SOLO +#ifndef Z_LARGE64 + err = crc32_combine64(crc1, crc2, 0); + fprintf(stderr, "crc32_combine64 result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#endif +#else + err = adler32_combine(crc1, crc2, 0); + fprintf(stderr, "crc32_combine result: %lu\n", err); + ASSERT_NE(err, Z_ERRNO); +#endif +} + +/** + * @tc.number : ActsZlibTest_1600 + * @tc.name : Test get_crc_table + * @tc.desc : [C- SOFTWARE -0200] + */ +HWTEST_F(ActsZlibTest, ActsZlibTestGetCrcTable, Function | MediumTest | Level2) +{ + auto table = get_crc_table(); + ASSERT_TRUE(table != nullptr); +} + +/** + * @tc.number : ActsZlibTest_1700 + * @tc.name : Test gzBuffer + * @tc.desc : [C- SOFTWARE -0200] + */ +HWTEST_F(ActsZlibTest, ActsZlibTestGzBuffer, Function | MediumTest | Level2) +{ +#ifdef Z_SOLO + fprintf(stderr, "*********ActsZlibTestGzBuffer Z_SOLO**********\n"); +#else + int err; + int len = static_cast(strlen(HELLO)) + 1; + gzFile file; + z_off_t pos; + file = gzopen(TESTFILE, "wb"); + ASSERT_TRUE(file != NULL); + + err = gzbuffer(file, BUFFER_SIZE); + ASSERT_EQ(err, Z_OK); + + gzclearerr(file); + gzputc(file, 'h'); + ASSERT_TRUE(gzputs(file, "ello") == FOUR); + if (gzprintf(file, ", %s!", "hello") != EIGHT) { + fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); + ASSERT_TRUE(false); + } + + gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ + gzclearerr(file); + gzclose_w(file); + file = gzopen(TESTFILE, "rb"); + ASSERT_TRUE(file != NULL); + + int res = gzdirect(file); + fprintf(stderr, "gzdirect result: %d\n", res); + Byte *compr, *uncompr; + uLong comprLen = 10000 * sizeof(int); /* don't overflow on MSDOS */ + uLong uncomprLen = comprLen; + compr = static_cast(calloc(static_cast(comprLen), CALLOC_SIZE)); + uncompr = static_cast(calloc(static_cast(uncomprLen), CALLOC_SIZE)); + ASSERT_TRUE(compr != Z_NULL && uncompr != Z_NULL); + + strcpy(reinterpret_cast(uncompr), GARBAGE); + ASSERT_TRUE(gzread(file, uncompr, static_cast(uncomprLen)) == len); + ASSERT_FALSE(strcmp(reinterpret_cast(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(uncompr), static_cast(uncomprLen)); + ASSERT_FALSE(strcmp(reinterpret_cast(uncompr), HELLO + SIX)); + gzclose_r(file); + free(compr); + free(uncompr); +#endif } \ No newline at end of file diff --git a/appexecfwk/zlibtest/zlibcpptest/BUILD.gn b/appexecfwk/zlib/actszlibtest/BUILD.gn similarity index 87% rename from appexecfwk/zlibtest/zlibcpptest/BUILD.gn rename to appexecfwk/zlib/actszlibtest/BUILD.gn index 5f47d0dd9..330536956 100644 --- a/appexecfwk/zlibtest/zlibcpptest/BUILD.gn +++ b/appexecfwk/zlib/actszlibtest/BUILD.gn @@ -12,11 +12,11 @@ # limitations under the License. import("//test/xts/tools/build/suite.gni") -module_output_path = "hits/ZlibCppTest" +module_output_path = "hits/ActsZlibTest" -ohos_moduletest_suite("ZlibCppTest") { +ohos_moduletest_suite("ActsZlibTest") { module_out_path = module_output_path - sources = [ "ZlibCppTest.cpp" ] + sources = [ "ActsZlibTest.cpp" ] configs = [ "//third_party/zlib:zlib_config" ] public_configs = [ "//third_party/zlib:zlib_public_config" ] deps = [ "//third_party/zlib:libz" ] diff --git a/appexecfwk/zlibtest/zlibcpptest/Test.json b/appexecfwk/zlib/actszlibtest/Test.json similarity index 80% rename from appexecfwk/zlibtest/zlibcpptest/Test.json rename to appexecfwk/zlib/actszlibtest/Test.json index 84e01bfa3..b62bc1844 100644 --- a/appexecfwk/zlibtest/zlibcpptest/Test.json +++ b/appexecfwk/zlib/actszlibtest/Test.json @@ -1,7 +1,7 @@ { "description": "Config for zlib test cases", "driver": { - "module-name": "ZlibCppTest", + "module-name": "ActsZlibTest", "native-test-timeout": "120000", "native-test-device-path": "/data/local/tmp", "runtime-hint": "1s", @@ -13,7 +13,7 @@ "chmod -R 777 /data/local/tmp/*" ], "push": [ - "ZlibCppTest->/data/local/tmp/ZlibCppTest" + "ActsZlibTest->/data/local/tmp/ActsZlibTest" ], "type": "PushKit" } -- GitLab