• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstring>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 
20 #include "plugin_service_types.pb.h"
21 #include "share_memory_block.h"
22 
23 using namespace testing::ext;
24 
25 namespace {
26 constexpr size_t ARRAYSIZE = 1024;
27 
28 class SharedMemoryBlockTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
32 
SetUp()33     void SetUp() {}
TearDown()34     void TearDown() {}
35 };
36 
37 /**
38  * @tc.name: share memory
39  * @tc.desc: read lock.
40  * @tc.type: FUNC
41  */
42 HWTEST_F(SharedMemoryBlockTest, ReadLock, TestSize.Level1)
43 {
44     ShareMemoryBlock shareMemoryBlock("testname", 4096);
45     ASSERT_TRUE(shareMemoryBlock.Valid());
46 
47     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
48 }
49 
50 /**
51  * @tc.name: share memory
52  * @tc.desc: get name.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(SharedMemoryBlockTest, GetName, TestSize.Level1)
56 {
57     ShareMemoryBlock shareMemoryBlock("testname", 4096);
58     ASSERT_TRUE(shareMemoryBlock.Valid());
59 
60     shareMemoryBlock.GetName();
61 
62     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
63 }
64 
65 /**
66  * @tc.name: share memory
67  * @tc.desc: get size.
68  * @tc.type: FUNC
69  */
70 HWTEST_F(SharedMemoryBlockTest, GetSize, TestSize.Level1)
71 {
72     ShareMemoryBlock shareMemoryBlock("testname", 4096);
73     ASSERT_TRUE(shareMemoryBlock.Valid());
74 
75     shareMemoryBlock.GetSize();
76 
77     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
78 }
79 
80 /**
81  * @tc.name: share memory
82  * @tc.desc: get file descriptor.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(SharedMemoryBlockTest, GetfileDescriptor, TestSize.Level1)
86 {
87     ShareMemoryBlock shareMemoryBlock("testname", 4096);
88     ASSERT_TRUE(shareMemoryBlock.Valid());
89 
90     shareMemoryBlock.GetfileDescriptor();
91 
92     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
93 }
94 
95 /**
96  * @tc.name: share memory
97  * @tc.desc: Shared memory type test.
98  * @tc.type: FUNC
99  */
100 HWTEST_F(SharedMemoryBlockTest, DROP_NONE, TestSize.Level1)
101 {
102     ShareMemoryBlock shareMemoryBlock("testname", 4096);
103     ASSERT_TRUE(shareMemoryBlock.Valid());
104 
105     shareMemoryBlock.SetReusePolicy(ShareMemoryBlock::ReusePolicy::DROP_NONE);
106 
107     int8_t data[ARRAYSIZE];
108     for (int i = 0; i < 5; i++) {
109         *((uint32_t*)data) = i;
110         shareMemoryBlock.PutRaw(data, ARRAYSIZE);
111     }
112     int8_t* p = shareMemoryBlock.GetFreeMemory(ARRAYSIZE);
113     ASSERT_TRUE(p == nullptr);
114 
115     do {
116         p = const_cast<int8_t*>(shareMemoryBlock.GetDataPoint());
117     } while (shareMemoryBlock.Next() && shareMemoryBlock.GetDataSize() > 0);
118 
119     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
120 }
121 
122 /**
123  * @tc.name: share memory
124  * @tc.desc: Shared memory type test.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(SharedMemoryBlockTest, DROP_OLD, TestSize.Level1)
128 {
129     ShareMemoryBlock shareMemoryBlock("testname", 4096);
130     ASSERT_TRUE(shareMemoryBlock.Valid());
131 
132     shareMemoryBlock.SetReusePolicy(ShareMemoryBlock::ReusePolicy::DROP_OLD);
133 
134     int8_t data[ARRAYSIZE];
135     for (int i = 0; i < 5; i++) {
136         *((uint32_t*)data) = i;
137         shareMemoryBlock.PutRaw(data, ARRAYSIZE);
138     }
139     int8_t* p = shareMemoryBlock.GetFreeMemory(ARRAYSIZE);
140     ASSERT_TRUE(p != nullptr);
141 
142     do {
143         p = const_cast<int8_t*>(shareMemoryBlock.GetDataPoint());
144     } while (shareMemoryBlock.Next() && shareMemoryBlock.GetDataSize() > 0);
145 
146     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
147 }
148 
149 /**
150  * @tc.name: share memory
151  * @tc.desc: put protobuf.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(SharedMemoryBlockTest, PutMessage, TestSize.Level1)
155 {
156     ShareMemoryBlock shareMemoryBlock("testname", 4096);
157     ASSERT_TRUE(shareMemoryBlock.Valid());
158     ASSERT_TRUE(shareMemoryBlock.GetDataSize() == 0);
159 
160     NotifyResultResponse response;
161     response.set_status(ResponseStatus::OK);
162     ASSERT_TRUE(shareMemoryBlock.PutMessage(response, "test"));
163     EXPECT_EQ(shareMemoryBlock.GetDataSize(), response.ByteSizeLong());
164     response.ParseFromArray(shareMemoryBlock.GetDataPoint(), shareMemoryBlock.GetDataSize());
165     ASSERT_TRUE(response.status() == ResponseStatus::OK);
166 
167     // 调用next移动指针,取值正常
168     shareMemoryBlock.Next();
169     NotifyResultResponse response2;
170     response2.set_status(ResponseStatus::OK);
171     ASSERT_TRUE(shareMemoryBlock.PutMessage(response2, "test"));
172     EXPECT_EQ(shareMemoryBlock.GetDataSize(), response2.ByteSizeLong());
173     response2.ParseFromArray(shareMemoryBlock.GetDataPoint(), shareMemoryBlock.GetDataSize());
174     EXPECT_TRUE(response2.status() == ResponseStatus::OK);
175 
176     // 调用next,设置空message
177     shareMemoryBlock.Next();
178     NotifyResultRequest request;
179     ASSERT_TRUE(shareMemoryBlock.PutMessage(request, "test"));
180     EXPECT_EQ(shareMemoryBlock.GetDataSize(), request.ByteSizeLong());
181 
182     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
183 }
184 
185 /**
186  * @tc.name: share memory
187  * @tc.desc: Shared memory PutMessage abnormal test.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(SharedMemoryBlockTest, PutMessageAbnormal, TestSize.Level1)
191 {
192     ShareMemoryBlock shareMemoryBlock("testname", 4096);
193     ASSERT_TRUE(shareMemoryBlock.Valid());
194     ASSERT_TRUE(shareMemoryBlock.GetDataSize() == 0);
195 
196     NotifyResultResponse response;
197     response.set_status(ResponseStatus::OK);
198     ASSERT_TRUE(shareMemoryBlock.PutMessage(response, "test"));
199     EXPECT_EQ(shareMemoryBlock.GetDataSize(), response.ByteSizeLong());
200     response.ParseFromArray(shareMemoryBlock.GetDataPoint(), shareMemoryBlock.GetDataSize());
201     ASSERT_TRUE(response.status() == ResponseStatus::OK);
202 
203     // 不调用next无法移动指针,取值出错
204     NotifyResultResponse response2;
205     response2.set_status(ResponseStatus::ERR);
206     ASSERT_TRUE(shareMemoryBlock.PutMessage(response2, "test"));
207     EXPECT_EQ(shareMemoryBlock.GetDataSize(), response2.ByteSizeLong());
208     EXPECT_EQ(shareMemoryBlock.GetDataSize(), response.ByteSizeLong());
209     response2.ParseFromArray(shareMemoryBlock.GetDataPoint(), shareMemoryBlock.GetDataSize());
210     EXPECT_FALSE(response2.status() == ResponseStatus::ERR);
211     EXPECT_TRUE(response2.status() == ResponseStatus::OK);
212 
213     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
214 }
215 
216 /**
217  * @tc.name: share memory
218  * @tc.desc: Shared memory PutRaw abnormal test.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(SharedMemoryBlockTest, PutRawAbnormal, TestSize.Level1)
222 {
223     ShareMemoryBlock shareMemoryBlock("testname", 4096);
224     ASSERT_TRUE(shareMemoryBlock.Valid());
225 
226     ASSERT_FALSE(shareMemoryBlock.PutRaw(nullptr, ARRAYSIZE));
227     ASSERT_NE(shareMemoryBlock.GetFreeMemory(ARRAYSIZE), nullptr);
228 
229     int8_t data[ARRAYSIZE];
230     ASSERT_FALSE(shareMemoryBlock.PutRaw(data, 0));
231     ASSERT_NE(shareMemoryBlock.GetFreeMemory(0), nullptr);
232 
233     ASSERT_FALSE(shareMemoryBlock.PutRaw(data, 4096+1));
234     ASSERT_EQ(shareMemoryBlock.GetFreeMemory(4096+1), nullptr);
235 
236     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
237 }
238 
function(const int8_t data[],uint32_t size)239 bool function(const int8_t data[], uint32_t size)
240 {
241     auto pluginData = std::make_shared<ProfilerPluginData>();
242     return pluginData->ParseFromArray(reinterpret_cast<const char*>(data), 6);
243 }
244 
functionErr(const int8_t data[],uint32_t size)245 bool functionErr(const int8_t data[], uint32_t size)
246 {
247     auto pluginData = std::make_shared<ProfilerPluginData>();
248     return pluginData->ParseFromArray(reinterpret_cast<const char*>(data), 4096);
249 }
250 
251 /**
252  * @tc.name: share memory
253  * @tc.desc: Shared memory TakeData test.
254  * @tc.type: FUNC
255  */
256 HWTEST_F(SharedMemoryBlockTest, TakeData, TestSize.Level1)
257 {
258     ShareMemoryBlock shareMemoryBlock("testname", 4096);
259     ASSERT_TRUE(shareMemoryBlock.Valid());
260 
261     // 不匹配的空message
262     NotifyResultRequest request;
263     ASSERT_TRUE(shareMemoryBlock.PutMessage(request, "test"));
264     ASSERT_TRUE(shareMemoryBlock.GetDataSize() == 0);
265     EXPECT_FALSE(shareMemoryBlock.TakeData(function));
266 
267     // 不匹配的非空message
268     shareMemoryBlock.Next();
269     NotifyResultResponse response;
270     response.set_status(ResponseStatus::OK);
271     ASSERT_TRUE(shareMemoryBlock.PutMessage(response, "test"));
272     EXPECT_FALSE(shareMemoryBlock.GetDataSize() == 0);
273     EXPECT_FALSE(shareMemoryBlock.TakeData(function));
274 
275     // 匹配的空message
276     shareMemoryBlock.Next();
277     ProfilerPluginData data;
278     ASSERT_TRUE(shareMemoryBlock.PutMessage(data, "test"));
279     ASSERT_TRUE(shareMemoryBlock.GetDataSize() == 0);
280     EXPECT_FALSE(shareMemoryBlock.TakeData(function));
281 
282     // 匹配的非空message, 但DataSize设置为大值
283     shareMemoryBlock.Next();
284     data.set_name("test");
285     ASSERT_TRUE(shareMemoryBlock.PutMessage(data, "test"));
286     EXPECT_FALSE(shareMemoryBlock.GetDataSize() == 0);
287     EXPECT_FALSE(shareMemoryBlock.TakeData(functionErr));
288 
289     // 匹配的非空message,正确的DataSize
290     shareMemoryBlock.Next();
291     data.set_name("test");
292     ASSERT_TRUE(shareMemoryBlock.PutMessage(data, "test"));
293     EXPECT_FALSE(shareMemoryBlock.GetDataSize() == 0);
294     EXPECT_TRUE(shareMemoryBlock.TakeData(function));
295 
296     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
297 }
298 
299 /**
300  * @tc.name: share memory PutRawTimeout
301  * @tc.desc: Shared memory type test.
302  * @tc.type: FUNC
303  */
304 HWTEST_F(SharedMemoryBlockTest, PutRawTimeout, TestSize.Level1)
305 {
306     ShareMemoryBlock shareMemoryBlock("testname", 4096);
307     ASSERT_TRUE(shareMemoryBlock.Valid());
308     shareMemoryBlock.SetReusePolicy(ShareMemoryBlock::ReusePolicy::DROP_OLD);
309     int8_t data[ARRAYSIZE];
310     for (int i = 0; i < 5; i++) {
311         *((uint32_t*)data) = i;
312         shareMemoryBlock.PutRawTimeout(data, ARRAYSIZE);
313     }
314     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
315 }
316 /**
317  * @tc.name: share memory PutWithPayloadTimeout
318  * @tc.desc: Shared memory type test.
319  * @tc.type: FUNC
320  */
321 HWTEST_F(SharedMemoryBlockTest, PutWithPayloadTimeout, TestSize.Level1)
322 {
323     ShareMemoryBlock shareMemoryBlock("testname", 4096);
324     ASSERT_TRUE(shareMemoryBlock.Valid());
325     shareMemoryBlock.SetReusePolicy(ShareMemoryBlock::ReusePolicy::DROP_OLD);
326     int8_t data[ARRAYSIZE];
327     int8_t header[ARRAYSIZE];
328     for (int i = 0; i < 5; i++) {
329         *((uint32_t*)data) = i;
330         *((uint32_t*)header) = i + 1;
331         shareMemoryBlock.PutWithPayloadTimeout(header, ARRAYSIZE, data, ARRAYSIZE);
332     }
333     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
334 }
335 
336 /**
337  * @tc.name: share memory PutWithPayloadSync
338  * @tc.desc: Shared memory type test.
339  * @tc.type: FUNC
340  */
341 HWTEST_F(SharedMemoryBlockTest, PutWithPayloadSync, TestSize.Level1)
342 {
343     ShareMemoryBlock shareMemoryBlock("testname", 8192);
344     ASSERT_TRUE(shareMemoryBlock.Valid());
345     shareMemoryBlock.SetReusePolicy(ShareMemoryBlock::ReusePolicy::DROP_OLD);
346     int8_t data[ARRAYSIZE];
347     int8_t header[ARRAYSIZE];
348     for (int i = 0; i < 2; i++) {
349         *((uint32_t*)data) = i;
350         *((uint32_t*)header) = i + 1;
351         shareMemoryBlock.PutWithPayloadSync(header, ARRAYSIZE, data, ARRAYSIZE);
352     }
353     ASSERT_TRUE(shareMemoryBlock.ReleaseBlock());
354 }
355 } // namespace
356