1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <vector>
18
19 #include "distributeddb_tools_unit_test.h"
20 #include "data_compression.h"
21
22 using namespace testing::ext;
23 using namespace DistributedDB;
24 using namespace DistributedDBUnitTest;
25 using namespace std;
26
27 namespace {
28 #ifndef OMIT_ZLIB
29 // LENGTH IS 680.
30 unsigned char g_srcStr[] =
31 "I come from Alabama with my banjo on my knee,"
32 "I'm going to Louisiana, my true love for to see,"
33 "It rained all night the day I left,"
34 "The weather it was dry,"
35 "The sun so hot, I froze to death,"
36 "Susanna don't you cry,"
37 "Oh, Susanna,"
38 "Oh don't you cry for me,"
39 "For I come from Alabama,"
40 "With my banjo on my knee,"
41 "I had a dream the other night when everything was still,"
42 "I thought I saw Susanna a-coming down the hill,"
43 "The buckwheat cake was in her mouth,"
44 "The tear was in her eye,"
45 "Says I, I'm coming from the south,"
46 "Susanna, don't you cry,"
47 "Oh, Susanna,"
48 "Oh don't you cry for me,"
49 "I'm going to Louisiana,"
50 "With my banjo on my knee,"
51 "Oh, Susanna,"
52 "Oh don't you cry for me,"
53 "I'm going to Louisiana,"
54 "With my banjo on my knee.";
55 }
56 #endif
57 class DistributedDBDataCompressionTest : public testing::Test {
58 public:
59 static void SetUpTestCase(void);
60 static void TearDownTestCase(void);
61 void SetUp();
62 void TearDown();
63 };
64
SetUpTestCase(void)65 void DistributedDBDataCompressionTest::SetUpTestCase(void)
66 {}
67
TearDownTestCase(void)68 void DistributedDBDataCompressionTest::TearDownTestCase(void)
69 {}
70
SetUp(void)71 void DistributedDBDataCompressionTest::SetUp(void)
72 {
73 DistributedDBToolsUnitTest::PrintTestCaseInfo();
74 }
75
TearDown(void)76 void DistributedDBDataCompressionTest::TearDown(void)
77 {}
78
79 /**
80 * @tc.name: DataCompression1
81 * @tc.desc: To test the function compress and uncompress works well in normal situation.
82 * @tc.type: FUNC
83 * @tc.require: AR000G3QTT
84 * @tc.author: lidongwei
85 */
86 HWTEST_F(DistributedDBDataCompressionTest, DataCompression1, TestSize.Level1)
87 {
88 /**
89 * @tc.steps:step1. Prepare a source data. And compress it.
90 * @tc.expected: step1. Compress successfully. Compressed data length is less than srcLen.
91 */
92 #ifndef OMIT_ZLIB
93 const int origLen = static_cast<int>(sizeof(g_srcStr));
94 vector<uint8_t> srcData(g_srcStr, g_srcStr + sizeof(g_srcStr));
95
96 vector<uint8_t> compressedData;
97 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Compress(srcData, compressedData), E_OK);
98 EXPECT_LT(compressedData.size(), srcData.size());
99
100 /**
101 * @tc.steps:step2. Uncompress the compressed data.
102 * @tc.expected: step2. Uncompress successfully. Uncompressed data equals to source data.
103 */
104 vector<uint8_t> uncompressedData;
105 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Uncompress(
106 compressedData, uncompressedData, origLen), E_OK);
107 EXPECT_EQ(srcData, uncompressedData);
108 #endif // OMIT_ZLIB
109 }
110
111 /**
112 * @tc.name: DataCompression2
113 * @tc.desc: To test uncompress failed when compressed is destroyed.
114 * @tc.type: FUNC
115 * @tc.require: AR000G3QTT
116 * @tc.author: lidongwei
117 */
118 HWTEST_F(DistributedDBDataCompressionTest, DataCompression2, TestSize.Level1)
119 {
120 /**
121 * @tc.steps:step1. Prepare a source data. And compress it.
122 * @tc.expected: step1. Compress successfully. Compressed data length is less than srcLen.
123 */
124 #ifndef OMIT_ZLIB
125 const int origLen = static_cast<int>(sizeof(g_srcStr));
126 vector<uint8_t> srcData(g_srcStr, g_srcStr + sizeof(g_srcStr));
127
128 vector<uint8_t> compressedData;
129 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Compress(srcData, compressedData), E_OK);
130 EXPECT_LT(compressedData.size(), srcData.size());
131
132 /**
133 * @tc.steps:step2. Destroy the compressed data.
134 */
135 *(compressedData.begin()) = ~*(compressedData.begin());
136
137 /**
138 * @tc.steps:step3. Uncompress the compressed data.
139 * @tc.expected: step3. Uncompressed failed and return -E_SYSTEM_API_FAIL.
140 */
141 vector<uint8_t> uncompressedData;
142 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Uncompress(
143 compressedData, uncompressedData, origLen), -E_SYSTEM_API_FAIL);
144 #endif // OMIT_ZLIB
145 }
146
147 /**
148 * @tc.name: DataCompression3
149 * @tc.desc: To test uncompress works when bufferLen is larger but under 30M limit,
150 and uncompress failed when bufferLen is beyond the 30M limit.
151 * @tc.type: FUNC
152 * @tc.require: AR000G3QTT
153 * @tc.author: lidongwei
154 */
155 HWTEST_F(DistributedDBDataCompressionTest, DataCompression3, TestSize.Level1)
156 {
157 /**
158 * @tc.steps:step1. Prepare a source data. And compress it.
159 * @tc.expected: step1. Compress successfully. Compressed data length is less than srcLen.
160 */
161 #ifndef OMIT_ZLIB
162 vector<uint8_t> srcData(g_srcStr, g_srcStr + sizeof(g_srcStr));
163
164 vector<uint8_t> compressedData;
165 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Compress(srcData, compressedData), E_OK);
166 EXPECT_LT(compressedData.size(), srcData.size());
167
168 /**
169 * @tc.steps:step2. Set origLen a larger num under 30M limit. And uncompress.
170 * @tc.expected: step1. Uncompress successfully.
171 */
172 vector<uint8_t> uncompressedData;
173 int incorrectLen = 10000;
174 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Uncompress(
175 compressedData, uncompressedData, incorrectLen), E_OK);
176 EXPECT_EQ(srcData, uncompressedData);
177
178 /**
179 * @tc.steps:step2. Set origLen a larger num beyond 30M limit. And uncompress.
180 * @tc.expected: step1. Uncompress failed and return E_INVALID_ARGS.
181 */
182 uncompressedData.clear();
183 incorrectLen = 31457281; // 30M + 1
184 EXPECT_EQ(DataCompression::GetInstance(CompressAlgorithm::ZLIB)->Uncompress(
185 compressedData, uncompressedData, incorrectLen), -E_INVALID_ARGS);
186 #endif // OMIT_ZLIB
187 }
188