• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "data_buf.h"
18 
19 using namespace testing::ext;
20 using namespace testing;
21 using namespace OHOS::Media;
22 
23 namespace OHOS {
24 namespace Media {
25 static const size_t MOCKOFFSET = 4;
26 static const size_t MOCKSIZE = 4;
27 class DataBufTest : public testing::Test {
28 public:
DataBufTest()29     DataBufTest() {}
~DataBufTest()30     ~DataBufTest() override {}
31 };
32 
33 /**
34  * @tc.name: DataBufTest_Write001
35  * @tc.desc: 验证DataBuf的write_uint8函数
36  * @tc.type: FUNC
37  */
38 HWTEST_F(DataBufTest, DataBufTest_Write001, TestSize.Level3)
39 {
40     DataBuf dataBuf(10);
41     dataBuf.WriteUInt8(0, 123);
42     EXPECT_EQ(dataBuf.ReadUInt8(0), 123);
43 }
44 
45 /**
46  * @tc.name: DataBufTest_GetUShort001
47  * @tc.desc: Validate the GetUShort function of DataBuf
48  * @tc.type: FUNC
49  */
50 HWTEST_F(DataBufTest, DataBufTest_GetUShort001, TestSize.Level3)
51 {
52     // Define test data
53     byte buf[2] = {0x01, 0x02};
54 
55     // Test the littleEndian case
56     uint16_t result = GetUShort(buf, littleEndian);
57     ASSERT_EQ(result, 0x0201);
58 
59     // Test the bigEndian case
60     result = GetUShort(buf, bigEndian);
61     ASSERT_EQ(result, 0x0102);
62 }
63 
64 /**
65  * @tc.name: DataBufTest_US2Data001
66  * @tc.desc: Validate the US2Data function of DataBuf
67  * @tc.type: FUNC
68  */
69 HWTEST_F(DataBufTest, DataBufTest_US2Data001, TestSize.Level3)
70 {
71     // Define test data
72     byte buf[2];
73     uint16_t value = 0x0201;
74 
75     // Test the littleEndian case
76     US2Data(buf, value, littleEndian);
77     ASSERT_EQ(buf[0], 0x01);
78     ASSERT_EQ(buf[1], 0x02);
79 
80     // Test the bigEndian case
81     US2Data(buf, value, bigEndian);
82     ASSERT_EQ(buf[0], 0x02);
83     ASSERT_EQ(buf[1], 0x01);
84 }
85 
86 /**
87  * @tc.name: DataBufTest_UL2Data001
88  * @tc.desc: Validate the UL2Data function of DataBuf
89  * @tc.type: FUNC
90  */
91 HWTEST_F(DataBufTest, DataBufTest_UL2Data001, TestSize.Level3)
92 {
93     // Define test data
94     byte buf[4];
95     uint32_t value = 0x04030201;
96 
97     // Test the littleEndian case
98     UL2Data(buf, value, littleEndian);
99     ASSERT_EQ(buf[0], 0x01);
100     ASSERT_EQ(buf[1], 0x02);
101     ASSERT_EQ(buf[2], 0x03);
102     ASSERT_EQ(buf[3], 0x04);
103 
104     // Test the bigEndian case
105     UL2Data(buf, value, bigEndian);
106     ASSERT_EQ(buf[0], 0x04);
107     ASSERT_EQ(buf[1], 0x03);
108     ASSERT_EQ(buf[2], 0x02);
109     ASSERT_EQ(buf[3], 0x01);
110 }
111 
112 /**
113  * @tc.name: DataBufTest_GetULong001
114  * @tc.desc: Validate the GetULong function of DataBuf
115  * @tc.type: FUNC
116  */
117 HWTEST_F(DataBufTest, DataBufTest_GetULong001, TestSize.Level3)
118 {
119     // Define test data
120     byte buf[4] = {0x01, 0x02, 0x03, 0x04};
121 
122     // Test the littleEndian case
123     uint32_t result = GetULong(buf, littleEndian);
124     ASSERT_EQ(result, 0x04030201);
125 
126     // Test the bigEndian case
127     result = GetULong(buf, bigEndian);
128     ASSERT_EQ(result, 0x01020304);
129 }
130 
131 /**
132  * @tc.name: ReadUInt8Test002
133  * @tc.desc: Verify that DataBuf call ReadUInt8 when offset larger than pData_.size().
134  * @tc.type: FUNC
135  */
136 HWTEST_F(DataBufTest, ReadUInt8Test002, TestSize.Level3)
137 {
138     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test002 start";
139     DataBuf dataBuf;
140     auto ret = dataBuf.ReadUInt8(MOCKOFFSET);
141     ASSERT_EQ(ret, 0);
142     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test002 end";
143 }
144 
145 /**
146  * @tc.name: ReadUInt32Test002
147  * @tc.desc: Verify that DataBuf call ReadUInt32 when pData_.size() less than UINT32_SIZE.
148  * @tc.type: FUNC
149  */
150 HWTEST_F(DataBufTest, ReadUInt32Test002, TestSize.Level3)
151 {
152     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test002 start";
153     DataBuf dataBuf;
154     auto ret = dataBuf.ReadUInt32(MOCKOFFSET, ByteOrder::invalidByteOrder);
155     ASSERT_EQ(ret, 0);
156     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test002 end";
157 }
158 
159 /**
160  * @tc.name: CmpBytesTest002
161  * @tc.desc: Verify that DataBuf call CmpBytes when offset larger than pData_.size().
162  * @tc.type: FUNC
163  */
164 HWTEST_F(DataBufTest, CmpBytesTest002, TestSize.Level3)
165 {
166     GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest002 start";
167     DataBuf dataBuf;
168     auto ret = dataBuf.CmpBytes(MOCKOFFSET, nullptr, MOCKSIZE);
169     ASSERT_EQ(ret, -1);
170     GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest002 end";
171 }
172 
173 /**
174  * @tc.name: ReadUInt8Test001
175  * @tc.desc: test the ReadUInt8 function of DataBuf
176  * @tc.type: FUNC
177  */
178 HWTEST_F(DataBufTest, ReadUInt8Test001, TestSize.Level3)
179 {
180     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test001 start";
181     size_t size = 2;
182     size_t offset = 4;
183     DataBuf dataBuf(size);
184     uint8_t ret = dataBuf.ReadUInt8(offset);
185     ASSERT_EQ(ret, 0);
186     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt8Test001 end";
187 }
188 
189 /**
190  * @tc.name: ReadUInt32Test001
191  * @tc.desc: test the ReadUInt32 function of DataBuf
192  * @tc.type: FUNC
193  */
194 HWTEST_F(DataBufTest, ReadUInt32Test001, TestSize.Level3)
195 {
196     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test001 start";
197     size_t size = 2;
198     size_t offset = 4;
199     ByteOrder byteOrder = littleEndian;
200     DataBuf dataBuf(size);
201     uint32_t ret = dataBuf.ReadUInt32(offset, byteOrder);
202     ASSERT_EQ(ret, 0);
203     GTEST_LOG_(INFO) << "DataBufTest: ReadUInt32Test001 end";
204 }
205 
206 /**
207  * @tc.name: CmpBytesTest001
208  * @tc.desc: test the CmpBytes function of DataBuf
209  * @tc.type: FUNC
210  */
211 HWTEST_F(DataBufTest, CmpBytesTest001, TestSize.Level3)
212 {
213     GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest001 start";
214     size_t size = 2;
215     size_t offset = 4;
216     const void *buf = nullptr;
217     size_t bufsize = 8;
218     DataBuf dataBuf(size);
219     int ret = dataBuf.CmpBytes(offset, buf, bufsize);
220     ASSERT_EQ(ret, -1);
221     GTEST_LOG_(INFO) << "DataBufTest: CmpBytesTest001 end";
222 }
223 
224 /**
225  * @tc.name: CDataTest001
226  * @tc.desc: test the CData function of DataBuf
227  * @tc.type: FUNC
228  */
229 HWTEST_F(DataBufTest, CDataTest001, TestSize.Level3)
230 {
231     GTEST_LOG_(INFO) << "DataBufTest: CDataTest001 start";
232     size_t size = 2;
233     size_t offset = 4;
234     DataBuf dataBuf(size);
235     const byte *ret = dataBuf.CData(offset);
236     ASSERT_EQ(ret, nullptr);
237     GTEST_LOG_(INFO) << "DataBufTest: CDataTest001 end";
238 }
239 } // namespace Media
240 } // namespace OHOS