1 /*
2 * Copyright (c) 2024-2024 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
18 #include "../hapVerify/test_const.h"
19 #include "byte_buffer.h"
20
21 namespace OHOS {
22 namespace SignatureTools {
23 class ByteBufferTest : public testing::Test {
24 public:
SetUpTestCase(void)25 static void SetUpTestCase(void)
26 {
27 };
TearDownTestCase(void)28 static void TearDownTestCase(void)
29 {
30 };
SetUp()31 void SetUp()
32 {
33 };
TearDown()34 void TearDown()
35 {
36 };
37 };
38
39 /**
40 * @tc.name: Test ByteBuffer Constructor and overload function.
41 * @tc.desc: The static function will return an object of ByteBuffer;
42 * @tc.type: FUNC
43 */
HWTEST_F(ByteBufferTest, ByteBufferConstructorTest001, testing::ext::TestSize.Level1)44 HWTEST_F(ByteBufferTest, ByteBufferConstructorTest001, testing::ext::TestSize.Level1)
45 {
46 ByteBuffer buffer1;
47 ByteBuffer buffer2(buffer1);
48 bool judge1 = (buffer2.GetCapacity() == buffer1.GetCapacity()) &&
49 (buffer2.GetPosition() == buffer1.GetPosition()) &&
50 (buffer2.GetLimit() == buffer1.GetLimit()) &&
51 (buffer2.GetBufferPtr() == nullptr);
52 ASSERT_TRUE(judge1);
53
54 ByteBuffer buffer3(1);
55 ByteBuffer buffer4(buffer3);
56 bool judge2 = (buffer4.GetCapacity() == buffer3.GetCapacity()) &&
57 (buffer4.GetPosition() == buffer3.GetPosition()) &&
58 (buffer4.GetLimit() == buffer3.GetLimit()) &&
59 (buffer4.GetBufferPtr() != nullptr);
60 ASSERT_TRUE(judge2);
61 }
62
63 /**
64 * @tc.name: Test ByteBuffer IsEqual.
65 * @tc.desc: Test function of ByteBuffer::IsEqual() interface.
66 * @tc.type: FUNC
67 */
HWTEST_F(ByteBufferTest, ByteBufferIsEqualTest001, testing::ext::TestSize.Level1)68 HWTEST_F(ByteBufferTest, ByteBufferIsEqualTest001, testing::ext::TestSize.Level1)
69 {
70 ByteBuffer buffer1(2);
71 buffer1.PutInt32(0, 10);
72 ByteBuffer buffer2(1);
73 buffer2.PutInt32(0, 1);
74 buffer1 = buffer2;
75 ASSERT_TRUE(buffer1.IsEqual(buffer2));
76
77 ByteBuffer buffer3;
78 ByteBuffer buffer4;
79 buffer4 = buffer3;
80 ASSERT_FALSE(buffer4.IsEqual(buffer3));
81
82 ASSERT_FALSE(buffer1.IsEqual(buffer3));
83
84 ASSERT_FALSE(buffer3.IsEqual(buffer1));
85
86 ByteBuffer buffer5(1);
87 ByteBuffer buffer6(3);
88 ASSERT_FALSE(buffer5.IsEqual(buffer6));
89
90 std::string str{ "abc" };
91 ASSERT_FALSE(buffer5.IsEqual(str));
92
93 buffer6.PutByte('a');
94 buffer6.PutByte('b');
95 buffer6.PutByte('c');
96
97 ASSERT_TRUE(buffer6.IsEqual(str));
98 }
99
100 /**
101 * @tc.name: Test ByteBuffer IsEqual.
102 * @tc.desc: Test function of ByteBuffer's PutGetData interface.
103 * @tc.type: FUNC
104 */
HWTEST_F(ByteBufferTest, PutGetDataTest001, testing::ext::TestSize.Level1)105 HWTEST_F(ByteBufferTest, PutGetDataTest001, testing::ext::TestSize.Level1)
106 {
107 ByteBuffer buffer(1);
108 uint32_t v1;
109 ASSERT_FALSE(buffer.GetUInt32(v1));
110
111 uint16_t v2;
112 ASSERT_FALSE(buffer.GetUInt16(v2));
113
114 int16_t v3;
115 ASSERT_FALSE(buffer.GetInt16(v3));
116
117 uint8_t v4 = 1;
118 buffer.PutUInt8(v4);
119 uint8_t v5 = 0;
120 buffer.PutUInt8(v5);
121 ASSERT_FALSE(buffer.GetUInt8(v5));
122
123 int8_t v6;
124 ASSERT_FALSE(buffer.GetInt8(v6));
125
126
127 int16_t v7 = 1;
128 int32_t v8 = 1;
129 int64_t v9 = 1;
130 uint8_t v10 = 1;
131 buffer.SetPosition(1);
132 buffer.PutInt16(v7);
133 buffer.PutInt32(v8);
134 buffer.PutInt64(v9);
135 buffer.PutUInt8(v10);
136 buffer.PutData("a", 2);
137
138 buffer.SetPosition(0);
139 const char* dummy = nullptr;
140 buffer.PutData(dummy, 1);
141 buffer.PutData("a", -1);
142 int8_t* data = nullptr;
143 buffer.PutData(data, 1);
144 buffer.PutInt16(1);
145 uint8_t v11;
146 buffer.GetUInt8(v11);
147 buffer.GetUInt8(0, v11);
148 buffer.ClearData();
149
150 ByteBuffer empty;
151 empty.PutByte(1);
152 empty.PutInt16(1);
153 empty.PutInt32(1);
154 empty.PutInt64(1);
155 empty.PutUInt8(1);
156 empty.PutData("a", 2);
157 empty.PutData(data, 1);
158 empty.ClearData();
159 }
160
161 /**
162 * @tc.name: Test a ByteBuffer object's operation of GetInt and Put
163 * @tc.desc: The static function will return data from ByteBuffer's buffer
164 * @tc.type: FUNC
165 */
HWTEST_F(ByteBufferTest, GetIntAndPutOperation001, testing::ext::TestSize.Level1)166 HWTEST_F(ByteBufferTest, GetIntAndPutOperation001, testing::ext::TestSize.Level1)
167 {
168 /*
169 * @tc.steps: step1. Create an empty buffer and get data from it.
170 * @tc.expected: step1. The return result is false.
171 */
172 ByteBuffer emptyBuffer;
173 int32_t dataInt32;
174 ASSERT_FALSE(emptyBuffer.GetInt32(dataInt32));
175 int64_t dataInt64;
176 ASSERT_FALSE(emptyBuffer.GetInt64(dataInt64));
177 unsigned short dataUInt16;
178 ASSERT_FALSE(emptyBuffer.GetUInt16(0, dataUInt16));
179 /*
180 * @tc.steps: step2. Create a ByteBuffer with one byte's buffer and get data from second byte.
181 * @tc.expected: step2. The return result is false.
182 */
183 ByteBuffer testBuffer(1);
184 char testChar = TEST_HAPBYTEBUFFER_CHAR_DATA;
185 testBuffer.PutData(0, &testChar, sizeof(testChar));
186 uint32_t dataUInt32;
187 ASSERT_FALSE(testBuffer.GetUInt32(1, dataUInt32));
188 ASSERT_FALSE(testBuffer.GetInt32(1, dataInt32));
189 ASSERT_FALSE(testBuffer.GetInt64(1, dataInt64));
190 ASSERT_FALSE(testBuffer.GetUInt16(1, dataUInt16));
191 /*
192 * @tc.steps: step3. Get data from negative position.
193 * @tc.expected: step3. The return result is false.
194 */
195 ASSERT_FALSE(testBuffer.GetInt64(TEST_HAPBYTEBUFFER_INVALID_INDEX, dataInt64));
196 /*
197 * @tc.steps: step4. Put data to buffer and get data from it.
198 * @tc.expected: step4. The return data is same as which we put.
199 */
200 ByteBuffer testBuffer2(TEST_HAPBYTEBUFFER_LENGTH);
201 testBuffer2.PutByte(0, testChar);
202 /*
203 * @tc.steps: step5. Put data to buffer with type and get data from it.
204 * @tc.expected: step5. The return data is same as which we put.
205 */
206 ByteBuffer testBuffer3(TEST_HAPBYTEBUFFER_LENGTH);
207 testBuffer3.PutData(0, &testChar, sizeof(testChar), 1);
208 testBuffer3.PutData(1, nullptr, 0, 1);
209
210 ByteBuffer testBuffer4;
211 testBuffer4.PutData(-1, &testChar, sizeof(testChar), 1);
212 }
213
214 /**
215 * @tc.name: Test ByteBuffer function of slice
216 * @tc.desc: The static function will get an object after slice and detect it is right;
217 * @tc.type: FUNC
218 */
HWTEST_F(ByteBufferTest, Slice001, testing::ext::TestSize.Level1)219 HWTEST_F(ByteBufferTest, Slice001, testing::ext::TestSize.Level1)
220 {
221 /*
222 * @tc.steps: step1. Set a fixed length buffer.
223 * @tc.expected: step1. The return is same as value is set.
224 */
225 ByteBuffer buffer1(TEST_HAPBYTEBUFFER_LENGTH_2);
226 buffer1.SetCapacity(TEST_HAPBYTEBUFFER_LENGTH);
227 ASSERT_TRUE(buffer1.GetCapacity() == TEST_HAPBYTEBUFFER_LENGTH);
228 /*
229 * @tc.steps: step2. Slice buffer.
230 * @tc.expected: step2. The return is the target length after slice.
231 */
232 buffer1.PutInt32(0, TEST_HAPBYTEBUFFER_INT32_DATA);
233 buffer1.PutInt32(sizeof(int), TEST_HAPBYTEBUFFER_INT32_DATA_2);
234 buffer1.SetPosition(sizeof(int));
235 buffer1.SetLimit(sizeof(int) + sizeof(int));
236 buffer1.Slice();
237 ASSERT_TRUE(buffer1.Remaining() == sizeof(int));
238 /*
239 * @tc.steps: step3. Get int32 from buffer1.
240 * @tc.expected: step3. The return result is equal to TEST_HAPBYTEBUFFER_INT32_DATA_2.
241 */
242 int32_t testDataInt32;
243 ASSERT_TRUE(buffer1.GetInt32(testDataInt32));
244 ASSERT_EQ(testDataInt32, TEST_HAPBYTEBUFFER_INT32_DATA_2);
245 /*
246 * @tc.steps: step4. Slice continue, reset position and limit, and calculate if buffer has remain.
247 * @tc.expected: step4. The return result is true.
248 */
249 buffer1.Slice();
250 buffer1.Clear();
251 ASSERT_TRUE(buffer1.HasRemaining());
252 }
253
254 /**
255 * @tc.name: Test ByteBuffer function of IsEqual001
256 * @tc.desc: The static function will return two object whether is equal.
257 * @tc.type: FUNC
258 */
HWTEST_F(ByteBufferTest, IsEqual001, testing::ext::TestSize.Level1)259 HWTEST_F(ByteBufferTest, IsEqual001, testing::ext::TestSize.Level1)
260 {
261 /*
262 * @tc.steps: step1. Create a buffer, and compare it with itself.
263 * @tc.expected: step1. The return result is true.
264 */
265 char testChar[TEST_HAPBYTEBUFFER_LENGTH] = "Hello, world!!";
266 ByteBuffer buffer1(TEST_HAPBYTEBUFFER_LENGTH);
267 buffer1.PutData(0, testChar, TEST_HAPBYTEBUFFER_LENGTH);
268 ASSERT_TRUE(buffer1.IsEqual(buffer1));
269 /*
270 * @tc.steps: step2. Create another buffer and compare it with buffer1.
271 * @tc.expected: step2. The return result is false.
272 */
273 ByteBuffer buffer2;
274 ASSERT_FALSE(buffer1.IsEqual(buffer2));
275 /*
276 * @tc.steps: step3. Set length of buffer2 same as buffer1.
277 * @tc.expected: step3. The return result is false.
278 */
279 buffer2.SetCapacity(TEST_HAPBYTEBUFFER_LENGTH);
280 ASSERT_FALSE(buffer1.IsEqual(buffer2));
281 /*
282 * @tc.steps: step4. Use copy constructor to create an buffer3, and compare it with buffer1.
283 * @tc.expected: step4. The return result is true.
284 */
285 ByteBuffer buffer3(buffer1);
286 ASSERT_TRUE(buffer1.IsEqual(buffer3));
287 }
288
289 /**
290 * @tc.name: Test ByteBuffer function of IsEqual002
291 * @tc.desc: The static function will return whether the value in buffer is equal to a string.
292 * @tc.type: FUNC
293 */
HWTEST_F(ByteBufferTest, IsEqual002, testing::ext::TestSize.Level1)294 HWTEST_F(ByteBufferTest, IsEqual002, testing::ext::TestSize.Level1)
295 {
296 /*
297 * @tc.steps: step1. Create a buffer and string, and compare.
298 * @tc.expected: step1. The return is false.
299 */
300 std::string testStr = "Hello, world!!!";
301 ByteBuffer buffer1;
302 const ByteBuffer& buffer2 = buffer1;
303 buffer1 = buffer2;
304 ASSERT_FALSE(buffer1.IsEqual(testStr));
305 /*
306 * @tc.steps: step2. Set length of buffer1 same as string.
307 * @tc.expected: step2. The return is false.
308 */
309 buffer1.SetCapacity(static_cast<int>(testStr.size()));
310 ASSERT_FALSE(buffer1.IsEqual(testStr));
311 /*
312 * @tc.steps: step3. Put string to buffer1 and compare.
313 * @tc.expected: step3. The return is true.
314 */
315 for (int32_t i = 0; i < static_cast<int>(testStr.size()); i++) {
316 buffer1.PutByte(i, testStr[i]);
317 }
318 ASSERT_TRUE(buffer1.IsEqual(testStr));
319 }
320
321 } // namespace SignatureTools
322 } // namespace OHOS
323