1 /*
2 * Copyright (c) 2025 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 <unistd.h>
18
19 #include "av_shared_memory_base.h"
20 #include "avsession_errors.h"
21 #include "avsession_log.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AVSession {
27
28 static OHOS::Parcel g_parcel;
29
30 class AVSharedMemoryBaseTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase()38 void AVSharedMemoryBaseTest::SetUpTestCase() {}
39
TearDownTestCase()40 void AVSharedMemoryBaseTest::TearDownTestCase() {}
41
SetUp()42 void AVSharedMemoryBaseTest::SetUp() {}
43
TearDown()44 void AVSharedMemoryBaseTest::TearDown() {}
45
46 /**
47 * @tc.name: Unmarshalling001
48 * @tc.desc: Unmarshalling
49 * @tc.type: FUNC
50 */
51 static HWTEST(AVSharedMemoryBaseTest, Unmarshalling001, TestSize.Level0)
52 {
53 SLOGI("Unmarshalling001 Begin");
54 int32_t size = 10;
55 uint32_t flags = 1;
56 const std::string name = "test";
57 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
58 OHOS::Parcel& parcel = g_parcel;
59 auto unmarshallingPtr = memory->Unmarshalling(parcel);
60 EXPECT_EQ(unmarshallingPtr, nullptr);
61 }
62
63 /**
64 * @tc.name: Unmarshalling002
65 * @tc.desc: Unmarshalling
66 * @tc.type: FUNC
67 */
68 static HWTEST(AVSharedMemoryBaseTest, Unmarshalling002, TestSize.Level0)
69 {
70 SLOGI("Unmarshalling002 Begin");
71 OHOS::MessageParcel parcel;
72 parcel.WriteFileDescriptor(1);
73 auto unmarshallingPtr = AVSharedMemoryBase::Unmarshalling(parcel);
74 EXPECT_NE(unmarshallingPtr, nullptr);
75 }
76
77 /**
78 * @tc.name: Unmarshalling003
79 * @tc.desc: Unmarshalling
80 * @tc.type: FUNC
81 */
82 static HWTEST(AVSharedMemoryBaseTest, Unmarshalling003, TestSize.Level0)
83 {
84 SLOGI("Unmarshalling003 Begin");
85 OHOS::MessageParcel parcel;
86 parcel.WriteFileDescriptor(1);
87 parcel.WriteInt32(10);
88 parcel.WriteUint32(1);
89 parcel.WriteString("test");
90 auto unmarshallingPtr = AVSharedMemoryBase::Unmarshalling(parcel);
91 EXPECT_NE(unmarshallingPtr, nullptr);
92 }
93
94 /**
95 * @tc.name: Marshalling001
96 * @tc.desc: Marshalling
97 * @tc.type: FUNC
98 */
99 static HWTEST(AVSharedMemoryBaseTest, Marshalling001, TestSize.Level0)
100 {
101 SLOGI("Marshalling001 begin!");
102 int32_t size = 10;
103 uint32_t flags = 1;
104 const std::string name = "test";
105 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
106 OHOS::Parcel& parcel = g_parcel;
107 memory->fd_ = 1;
108 bool ret = memory->Marshalling(parcel);
109 EXPECT_EQ(ret, true);
110 }
111
112 /**
113 * @tc.name: WriteToParcel001
114 * @tc.desc: WriteToParcel
115 * @tc.type: FUNC
116 */
117 static HWTEST(AVSharedMemoryBaseTest, WriteToParcel001, TestSize.Level0)
118 {
119 SLOGI("WriteToParcel001 Begin");
120 int32_t size = 10;
121 uint32_t flags = 1;
122 const std::string name = "test";
123 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
124 OHOS::MessageParcel& m_parcel = static_cast<MessageParcel&>(g_parcel);
125 memory->fd_ = 1;
126 bool ret = memory->WriteToParcel(m_parcel);
127 EXPECT_NE(ret, true);
128 }
129
130 /**
131 * @tc.name: Write001
132 * @tc.desc: set the input array to nullptr
133 * @tc.type: FUNC
134 */
135 static HWTEST(AVSharedMemoryBaseTest, Write001, TestSize.Level0)
136 {
137 SLOGI("Write001 begin!");
138 int32_t size = 10;
139 uint32_t flags = 1;
140 const std::string name = "test";
141 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
142 uint8_t *in = nullptr;
143 int32_t writeSize = 0;
144 int32_t position = 0;
145 int32_t ret = memory->Write(in, writeSize, position);
146 EXPECT_EQ(ret, 0);
147 }
148
149 /**
150 * @tc.name: Write002
151 * @tc.desc: set writeSize to zero
152 * @tc.type: FUNC
153 */
154 static HWTEST(AVSharedMemoryBaseTest, Write002, TestSize.Level0)
155 {
156 SLOGI("Write002 begin!");
157 int32_t size = 10;
158 uint32_t flags = 1;
159 const std::string name = "test";
160 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
161 uint8_t *in = new uint8_t [2];
162 std::fill_n(in, 1, 2);
163 int32_t writeSize = 0;
164 int32_t position = 0;
165 int32_t ret = memory->Write(in, writeSize, position);
166 EXPECT_EQ(ret, 0);
167 delete[] in;
168 }
169
170 /**
171 * @tc.name: Write003
172 * @tc.desc: set writeSize equal to INVALID_POSITION
173 * @tc.type: FUNC
174 */
175 static HWTEST(AVSharedMemoryBaseTest, Write003, TestSize.Level0)
176 {
177 SLOGI("Write003 begin!");
178 int32_t size = 10;
179 uint32_t flags = 1;
180 const std::string name = "test";
181 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
182 uint8_t *in = new uint8_t [2];
183 std::fill_n(in, 1, 2);
184 int32_t writeSize = 1;
185 int32_t position = -1;
186 int32_t ret = memory->Write(in, writeSize, position);
187 EXPECT_EQ(ret, 0);
188 delete[] in;
189 }
190
191 /**
192 * @tc.name: Write004
193 * @tc.desc: set writeSize bigger than capacity
194 * @tc.type: FUNC
195 */
196 static HWTEST(AVSharedMemoryBaseTest, Write004, TestSize.Level0)
197 {
198 SLOGI("Write004 begin!");
199 int32_t size = 10;
200 uint32_t flags = 1;
201 const std::string name = "test";
202 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
203 uint8_t *in = new uint8_t [2];
204 std::fill_n(in, 1, 2);
205 int32_t writeSize = 100;
206 int32_t position = 0;
207 int32_t ret = memory->Write(in, writeSize, position);
208 EXPECT_EQ(ret, 0);
209 delete[] in;
210 }
211
212 /**
213 * @tc.name: Write005
214 * @tc.desc: the base_ of memory is nullptr
215 * @tc.type: FUNC
216 */
217 static HWTEST(AVSharedMemoryBaseTest, Write005, TestSize.Level0)
218 {
219 SLOGI("Write005 begin!");
220 int32_t size = 10;
221 uint32_t flags = 1;
222 const std::string name = "test";
223 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
224 uint8_t *in = new uint8_t [2];
225 std::fill_n(in, 1, 2);
226 int32_t writeSize = 2;
227 int32_t position = 0;
228 int32_t ret = memory->Write(in, writeSize, position);
229 EXPECT_EQ(ret, 0);
230 delete[] in;
231 }
232
233 /**
234 * @tc.name: Write006
235 * @tc.desc: success to write
236 * @tc.type: FUNC
237 */
238 static HWTEST(AVSharedMemoryBaseTest, Write006, TestSize.Level0)
239 {
240 SLOGI("Write006 begin!");
241 int32_t size = 10;
242 uint32_t flags = 1;
243 const std::string name = "test";
244 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
245 uint8_t *in = new uint8_t [2];
246 std::fill_n(in, 1, 2);
247 int32_t writeSize = 2;
248 int32_t position = 0;
249 memory->base_ = new uint8_t[size];
250 int32_t ret = memory->Write(in, writeSize, position);
251 EXPECT_EQ(ret, writeSize);
252 delete[] memory->base_;
253 delete[] in;
254 }
255
256 /**
257 * @tc.name: Read001
258 * @tc.desc: out array is nullptr
259 * @tc.type: FUNC
260 */
261 static HWTEST(AVSharedMemoryBaseTest, Read001, TestSize.Level0)
262 {
263 SLOGI("Read001 begin!");
264 int32_t size = 10;
265 uint32_t flags = 1;
266 const std::string name = "test";
267 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
268 uint8_t *out = nullptr;
269 int32_t readSize = 2;
270 int32_t position = 0;
271 int32_t ret = memory->Read(out, readSize, position);
272 EXPECT_EQ(ret, 0);
273 }
274
275 /**
276 * @tc.name: Read002
277 * @tc.desc: set readSize equal to zero
278 * @tc.type: FUNC
279 */
280 static HWTEST(AVSharedMemoryBaseTest, Read002, TestSize.Level0)
281 {
282 SLOGI("Read002 begin!");
283 int32_t size = 10;
284 uint32_t flags = 1;
285 const std::string name = "test";
286 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
287 uint8_t *out = new uint8_t [2];
288 std::fill_n(out, 0, 2);
289 int32_t readSize = 0;
290 int32_t position = 0;
291 int32_t ret = memory->Read(out, readSize, position);
292 EXPECT_EQ(ret, 0);
293 delete[] out;
294 }
295
296 /**
297 * @tc.name: Read003
298 * @tc.desc: set position equal to INVALID_POSITION
299 * @tc.type: FUNC
300 */
301 static HWTEST(AVSharedMemoryBaseTest, Read003, TestSize.Level0)
302 {
303 SLOGI("Read003 begin!");
304 int32_t size = 10;
305 uint32_t flags = 1;
306 const std::string name = "test";
307 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
308 uint8_t *out = new uint8_t [2];
309 std::fill_n(out, 0, 2);
310 int32_t readSize = 0;
311 int32_t position = -1;
312 int32_t ret = memory->Read(out, readSize, position);
313 EXPECT_EQ(ret, 0);
314 delete[] out;
315 }
316
317 /**
318 * @tc.name: Read004
319 * @tc.desc: set length bigger than capacity_
320 * @tc.type: FUNC
321 */
322 static HWTEST(AVSharedMemoryBaseTest, Read004, TestSize.Level0)
323 {
324 SLOGI("Read004 begin!");
325 int32_t size = 10;
326 uint32_t flags = 1;
327 const std::string name = "test";
328 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
329 uint8_t *out = new uint8_t [2];
330 std::fill_n(out, 0, 2);
331 int32_t readSize = 100;
332 int32_t position = 0;
333 memory->size_ = 100;
334 int32_t ret = memory->Read(out, readSize, position);
335 EXPECT_EQ(ret, 0);
336 delete[] out;
337 }
338
339 /**
340 * @tc.name: Read005
341 * @tc.desc: base_ array is nullptr
342 * @tc.type: FUNC
343 */
344 static HWTEST(AVSharedMemoryBaseTest, Read005, TestSize.Level0)
345 {
346 SLOGI("Read005 begin!");
347 int32_t size = 10;
348 uint32_t flags = 1;
349 const std::string name = "test";
350 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
351 uint8_t *out = new uint8_t [2];
352 std::fill_n(out, 0, 2);
353 int32_t readSize = 2;
354 int32_t position = 0;
355 int32_t ret = memory->Read(out, readSize, position);
356 EXPECT_EQ(ret, 0);
357 delete[] out;
358 }
359
360 /**
361 * @tc.name: Read006
362 * @tc.desc: success to read
363 * @tc.type: FUNC
364 */
365 static HWTEST(AVSharedMemoryBaseTest, Read006, TestSize.Level0)
366 {
367 SLOGI("Read006 begin!");
368 int32_t size = 10;
369 uint32_t flags = 1;
370 const std::string name = "test";
371 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
372 uint8_t *out = new uint8_t [3];
373 std::fill_n(out, 0, 3);
374 int32_t readSize = 2;
375 int32_t position = 0;
376 memory->size_ = 3;
377 memory->base_ = new uint8_t[3];
378 std::fill_n(memory->base_, 1, 3);
379 int32_t ret = memory->Read(out, readSize, position);
380 EXPECT_EQ(ret, 2);
381 delete[] memory->base_;
382 delete[] out;
383 }
384
385 } //AVSession
386 } //OHOS