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_helper.h"
20 #include "av_shared_memory_base.h"
21 #include "avsession_errors.h"
22 #include "avsession_log.h"
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace AVSession {
28
29 class AVSharedMemoryBaseTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase()37 void AVSharedMemoryBaseTest::SetUpTestCase() {}
38
TearDownTestCase()39 void AVSharedMemoryBaseTest::TearDownTestCase() {}
40
SetUp()41 void AVSharedMemoryBaseTest::SetUp() {}
42
TearDown()43 void AVSharedMemoryBaseTest::TearDown() {}
44
45 /**
46 * @tc.name: Write001
47 * @tc.desc: set the input array to nullptr
48 * @tc.type: FUNC
49 */
50 static HWTEST(AVSharedMemoryBaseTest, Write001, TestSize.Level1)
51 {
52 SLOGI("Write001 begin!");
53 int32_t size = 10;
54 uint32_t flags = 1;
55 const std::string name = "test";
56 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
57 uint8_t *in = nullptr;
58 int32_t writeSize = 0;
59 int32_t position = 0;
60 int32_t ret = memory->Write(in, writeSize, position);
61 EXPECT_EQ(ret, 0);
62 }
63
64 /**
65 * @tc.name: Write002
66 * @tc.desc: set writeSize to zero
67 * @tc.type: FUNC
68 */
69 static HWTEST(AVSharedMemoryBaseTest, Write002, TestSize.Level1)
70 {
71 SLOGI("Write002 begin!");
72 int32_t size = 10;
73 uint32_t flags = 1;
74 const std::string name = "test";
75 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
76 uint8_t *in = new uint8_t [2];
77 std::fill_n(in, 1, 2);
78 int32_t writeSize = 0;
79 int32_t position = 0;
80 int32_t ret = memory->Write(in, writeSize, position);
81 EXPECT_EQ(ret, 0);
82 delete[] in;
83 }
84
85 /**
86 * @tc.name: Write003
87 * @tc.desc: set writeSize equal to INVALID_POSITION
88 * @tc.type: FUNC
89 */
90 static HWTEST(AVSharedMemoryBaseTest, Write003, TestSize.Level1)
91 {
92 SLOGI("Write003 begin!");
93 int32_t size = 10;
94 uint32_t flags = 1;
95 const std::string name = "test";
96 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
97 uint8_t *in = new uint8_t [2];
98 std::fill_n(in, 1, 2);
99 int32_t writeSize = 1;
100 int32_t position = -1;
101 int32_t ret = memory->Write(in, writeSize, position);
102 EXPECT_EQ(ret, 0);
103 delete[] in;
104 }
105
106 /**
107 * @tc.name: Write004
108 * @tc.desc: set writeSize bigger than capacity
109 * @tc.type: FUNC
110 */
111 static HWTEST(AVSharedMemoryBaseTest, Write004, TestSize.Level1)
112 {
113 SLOGI("Write004 begin!");
114 int32_t size = 10;
115 uint32_t flags = 1;
116 const std::string name = "test";
117 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
118 uint8_t *in = new uint8_t [2];
119 std::fill_n(in, 1, 2);
120 int32_t writeSize = 100;
121 int32_t position = 0;
122 int32_t ret = memory->Write(in, writeSize, position);
123 EXPECT_EQ(ret, 0);
124 delete[] in;
125 }
126
127 /**
128 * @tc.name: Write005
129 * @tc.desc: the base_ of memory is nullptr
130 * @tc.type: FUNC
131 */
132 static HWTEST(AVSharedMemoryBaseTest, Write005, TestSize.Level1)
133 {
134 SLOGI("Write005 begin!");
135 int32_t size = 10;
136 uint32_t flags = 1;
137 const std::string name = "test";
138 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
139 uint8_t *in = new uint8_t [2];
140 std::fill_n(in, 1, 2);
141 int32_t writeSize = 2;
142 int32_t position = 0;
143 int32_t ret = memory->Write(in, writeSize, position);
144 EXPECT_EQ(ret, 0);
145 delete[] in;
146 }
147
148 /**
149 * @tc.name: Write006
150 * @tc.desc: success to write
151 * @tc.type: FUNC
152 */
153 static HWTEST(AVSharedMemoryBaseTest, Write006, TestSize.Level1)
154 {
155 SLOGI("Write006 begin!");
156 int32_t size = 10;
157 uint32_t flags = 1;
158 const std::string name = "test";
159 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
160 uint8_t *in = new uint8_t [2];
161 std::fill_n(in, 1, 2);
162 int32_t writeSize = 2;
163 int32_t position = 0;
164 memory->base_ = new uint8_t[size];
165 int32_t ret = memory->Write(in, writeSize, position);
166 EXPECT_EQ(ret, writeSize);
167 delete[] memory->base_;
168 delete[] in;
169 }
170
171 /**
172 * @tc.name: Read001
173 * @tc.desc: out array is nullptr
174 * @tc.type: FUNC
175 */
176 static HWTEST(AVSharedMemoryBaseTest, Read001, TestSize.Level1)
177 {
178 SLOGI("Read001 begin!");
179 int32_t size = 10;
180 uint32_t flags = 1;
181 const std::string name = "test";
182 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
183 uint8_t *out = nullptr;
184 int32_t readSize = 2;
185 int32_t position = 0;
186 int32_t ret = memory->Read(out, readSize, position);
187 EXPECT_EQ(ret, 0);
188 }
189
190 /**
191 * @tc.name: Read002
192 * @tc.desc: set readSize equal to zero
193 * @tc.type: FUNC
194 */
195 static HWTEST(AVSharedMemoryBaseTest, Read002, TestSize.Level1)
196 {
197 SLOGI("Read002 begin!");
198 int32_t size = 10;
199 uint32_t flags = 1;
200 const std::string name = "test";
201 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
202 uint8_t *out = new uint8_t [2];
203 std::fill_n(out, 0, 2);
204 int32_t readSize = 0;
205 int32_t position = 0;
206 int32_t ret = memory->Read(out, readSize, position);
207 EXPECT_EQ(ret, 0);
208 delete[] out;
209 }
210
211 /**
212 * @tc.name: Read003
213 * @tc.desc: set position equal to INVALID_POSITION
214 * @tc.type: FUNC
215 */
216 static HWTEST(AVSharedMemoryBaseTest, Read003, TestSize.Level1)
217 {
218 SLOGI("Read003 begin!");
219 int32_t size = 10;
220 uint32_t flags = 1;
221 const std::string name = "test";
222 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
223 uint8_t *out = new uint8_t [2];
224 std::fill_n(out, 0, 2);
225 int32_t readSize = 0;
226 int32_t position = -1;
227 int32_t ret = memory->Read(out, readSize, position);
228 EXPECT_EQ(ret, 0);
229 delete[] out;
230 }
231
232 /**
233 * @tc.name: Read004
234 * @tc.desc: set length bigger than capacity_
235 * @tc.type: FUNC
236 */
237 static HWTEST(AVSharedMemoryBaseTest, Read004, TestSize.Level1)
238 {
239 SLOGI("Read004 begin!");
240 int32_t size = 10;
241 uint32_t flags = 1;
242 const std::string name = "test";
243 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
244 uint8_t *out = new uint8_t [2];
245 std::fill_n(out, 0, 2);
246 int32_t readSize = 100;
247 int32_t position = 0;
248 memory->size_ = 100;
249 int32_t ret = memory->Read(out, readSize, position);
250 EXPECT_EQ(ret, 0);
251 delete[] out;
252 }
253
254 /**
255 * @tc.name: Read005
256 * @tc.desc: base_ array is nullptr
257 * @tc.type: FUNC
258 */
259 static HWTEST(AVSharedMemoryBaseTest, Read005, TestSize.Level1)
260 {
261 SLOGI("Read005 begin!");
262 int32_t size = 10;
263 uint32_t flags = 1;
264 const std::string name = "test";
265 auto memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
266 uint8_t *out = new uint8_t [2];
267 std::fill_n(out, 0, 2);
268 int32_t readSize = 2;
269 int32_t position = 0;
270 int32_t ret = memory->Read(out, readSize, position);
271 EXPECT_EQ(ret, 0);
272 delete[] out;
273 }
274
275 /**
276 * @tc.name: Read006
277 * @tc.desc: success to read
278 * @tc.type: FUNC
279 */
280 static HWTEST(AVSharedMemoryBaseTest, Read006, TestSize.Level1)
281 {
282 SLOGI("Read006 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 [3];
288 std::fill_n(out, 0, 3);
289 int32_t readSize = 2;
290 int32_t position = 0;
291 memory->size_ = 3;
292 memory->base_ = new uint8_t[3];
293 std::fill_n(memory->base_, 1, 3);
294 int32_t ret = memory->Read(out, readSize, position);
295 EXPECT_EQ(ret, 2);
296 delete[] memory->base_;
297 delete[] out;
298 }
299
300 } //AVSession
301 } //OHOS