1 /*
2 * Copyright (c) 2020-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 <securec.h>
17 #include <ohos_init.h>
18 #include "gtest/gtest.h"
19 #include "broadcast_interface.h"
20 #include "utils/SamgrTestBase.h"
21
22 using namespace testing::ext;
23
24 static const int16 TEST_LEN = 10;
25 static const int16 WAIT_LEN = 5;
26 static const char *BROADCASR_TEST_SERVICE = "S_broadcast2";
27
C1Callback(Consumer * consumer,const Topic * topic,const Request * request)28 static void C1Callback(Consumer *consumer, const Topic *topic, const Request *request)
29 {
30 (void)consumer;
31 (void)topic;
32 }
C2Callback(Consumer * consumer,const Topic * topic,const Request * request)33 static void C2Callback(Consumer *consumer, const Topic *topic, const Request *request)
34 {
35 (void)consumer;
36 (void)topic;
37 }
38
Equal(const Consumer * current,const Consumer * other)39 static BOOL Equal(const Consumer *current, const Consumer *other)
40 {
41 if (current->Notify == other->Notify) {
42 return TRUE;
43 } else {
44 return FALSE;
45 }
46 }
47
GetName(Service * service)48 static const char *GetName(Service *service)
49 {
50 (void)service;
51 return BROADCASR_TEST_SERVICE;
52 };
53
54 static Identity g_identity = { -1, -1, nullptr};
55
Initialize(Service * service,Identity identity)56 static BOOL Initialize(Service *service, Identity identity)
57 {
58 g_identity = identity;
59 (void)service;
60 return TRUE;
61 };
62
MessageHandle(Service * service,Request * msg)63 static BOOL MessageHandle(Service *service, Request *msg)
64 {
65 (void)service;
66 return FALSE;
67 };
68
GetTaskConfig(Service * service)69 static TaskConfig GetTaskConfig(Service *service)
70 {
71 TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 0x800, 20, SHARED_TASK}; // share with broadcast
72 (void)service;
73 return config;
74 };
75
76 static Service g_service = {GetName, Initialize, MessageHandle, GetTaskConfig};
GInit(void)77 static void GInit(void)
78 {
79 SAMGR_GetInstance()->RegisterService(&g_service);
80 }
81 SYSEX_SERVICE_INIT(GInit);
82
CASE_GetIUnknown(void)83 static PubSubInterface *CASE_GetIUnknown(void)
84 {
85 PubSubInterface *fapi = nullptr;
86 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(BROADCAST_SERVICE, PUB_SUB_FEATURE);
87 if (iUnknown == nullptr) {
88 printf("[hctest]CASE_GetIUnknown error ");
89 return nullptr;
90 }
91
92 int result = iUnknown->QueryInterface(iUnknown, 0x20, (void **)&fapi);
93 if (result != 0 || fapi == nullptr) {
94 printf("[hctest]CASE_GetIUnknown error ");
95 }
96 return fapi;
97 }
98
CASE_ReleaseIUnknown(PubSubInterface * fapi)99 static void CASE_ReleaseIUnknown(PubSubInterface *fapi)
100 {
101 fapi->Release((IUnknown *)fapi);
102 }
103
104 class BroadcastPublishTest : public testing::Test {
105 protected:
106 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)107 static void SetUpTestCase(void)
108 {
109 printf("[hcpptest]SetUpTestCase ! \n");
110 SystemInitProxy();
111 sleep(WAIT_LEN);
112 }
113 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)114 static void TearDownTestCase(void)
115 {
116 }
117 // Testcase setup
SetUp()118 virtual void SetUp()
119 {
120 }
121 // Testcase teardown
TearDown()122 virtual void TearDown()
123 {
124 }
125 };
126
127 /**
128 * @tc.number : DMSLite_SAMGR_Publish_0010
129 * @tc.name : Provider can publish a broadcast message
130 * @tc.desc : [C- SOFTWARE -0200]
131 */
132 HWTEST_F(BroadcastPublishTest, testPublish0010, Function | MediumTest | Level1)
133 {
134 PubSubInterface *fapi = CASE_GetIUnknown();
135 if (fapi == nullptr) {
136 ADD_FAILURE();
137 }
138 Subscriber *subscriber = &fapi->subscriber;
139 if (subscriber == nullptr) {
140 ADD_FAILURE();
141 }
142
143 Provider *provider = &fapi->provider;
144 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
145 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
146
147 Topic topic0 = 0;
148 subscriber->AddTopic((IUnknown *)fapi, &topic0);
149 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
150 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
151
152 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN);
153 ASSERT_EQ(result, TRUE);
154
155 CASE_ReleaseIUnknown(fapi);
156 }
157
158 /**
159 * @tc.number : DMSLite_SAMGR_Publish_0020
160 * @tc.name : Provider failed to publish broadcast for the specified topic not exist
161 * @tc.desc : [C- SOFTWARE -0200]
162 */
163 HWTEST_F(BroadcastPublishTest, testPublish0020, Function | MediumTest | Level2)
164 {
165 PubSubInterface *fapi = CASE_GetIUnknown();
166 if (fapi == nullptr) {
167 ADD_FAILURE();
168 }
169 Subscriber *subscriber = &fapi->subscriber;
170 if (subscriber == nullptr) {
171 ADD_FAILURE();
172 }
173
174 Provider *provider = &fapi->provider;
175 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
176 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
177
178 Topic topic0 = 0;
179 subscriber->AddTopic((IUnknown *)fapi, &topic0);
180 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
181 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
182
183 Topic noExisttopic = 99;
184 BOOL result = provider->Publish((IUnknown *)fapi, &noExisttopic, (uint8_t *) "==>111<==", TEST_LEN);
185 ASSERT_EQ(result, 0);
186
187 CASE_ReleaseIUnknown(fapi);
188 }
189
190 /**
191 * @tc.number : DMSLite_SAMGR_Publish_0030
192 * @tc.name : Provider failed to publish broadcast for len and data not match
193 * @tc.desc : [C- SOFTWARE -0200]
194 */
195 HWTEST_F(BroadcastPublishTest, testPublish0030, Function | MediumTest | Level2)
196 {
197 PubSubInterface *fapi = CASE_GetIUnknown();
198 if (fapi == nullptr) {
199 ADD_FAILURE();
200 }
201 Subscriber *subscriber = &fapi->subscriber;
202 if (subscriber == nullptr) {
203 ADD_FAILURE();
204 }
205
206 Provider *provider = &fapi->provider;
207 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
208 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
209
210 Topic topic0 = 0;
211 subscriber->AddTopic((IUnknown *)fapi, &topic0);
212 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
213 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
214
215 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN - 1);
216 ASSERT_EQ(result, TRUE);
217
218 result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN + 1);
219 ASSERT_EQ(result, TRUE);
220
221 CASE_ReleaseIUnknown(fapi);
222 }
223
224 /**
225 * @tc.number : DMSLite_SAMGR_Publish_0040
226 * @tc.name : Provider publish broadcast successfully for 0 and max length message
227 * @tc.desc : [C- SOFTWARE -0200]
228 */
229 HWTEST_F(BroadcastPublishTest, testPublish0040, Function | MediumTest | Level2)
230 {
231 PubSubInterface *fapi = CASE_GetIUnknown();
232 if (fapi == nullptr) {
233 ADD_FAILURE();
234 }
235 Subscriber *subscriber = &fapi->subscriber;
236 if (subscriber == nullptr) {
237 ADD_FAILURE();
238 }
239
240 Provider *provider = &fapi->provider;
241 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
242 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
243
244 Topic topic0 = 0;
245 subscriber->AddTopic((IUnknown *)fapi, &topic0);
246 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
247 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
248
249 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "", 0);
250 ASSERT_EQ(result, 0);
251
252 CASE_ReleaseIUnknown(fapi);
253 }
254
255 /**
256 * @tc.number : DMSLite_SAMGR_Publish_0050
257 * @tc.name : Provider publish broadcast successfully for no consumer
258 * @tc.desc : [C- SOFTWARE -0200]
259 */
260 HWTEST_F(BroadcastPublishTest, testPublish0050, Function | MediumTest | Level2)
261 {
262 PubSubInterface *fapi = CASE_GetIUnknown();
263 if (fapi == nullptr) {
264 ADD_FAILURE();
265 }
266 Subscriber *subscriber = &fapi->subscriber;
267 if (subscriber == nullptr) {
268 ADD_FAILURE();
269 }
270
271 Provider *provider = &fapi->provider;
272
273 Topic topic0 = 0;
274 subscriber->AddTopic((IUnknown *)fapi, &topic0);
275
276 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN);
277 ASSERT_EQ(result, 1);
278
279 CASE_ReleaseIUnknown(fapi);
280 }
281
282 /**
283 * @tc.number : DMSLite_SAMGR_Publish_0060
284 * @tc.name : Provider publish broadcast successfully for multi consumers
285 * @tc.desc : [C- SOFTWARE -0200]
286 */
287 HWTEST_F(BroadcastPublishTest, testPublish0060, Function | MediumTest | Level2)
288 {
289 PubSubInterface *fapi = CASE_GetIUnknown();
290 if (fapi == nullptr) {
291 ADD_FAILURE();
292 }
293 Subscriber *subscriber = &fapi->subscriber;
294 if (subscriber == nullptr) {
295 ADD_FAILURE();
296 }
297
298 Provider *provider = &fapi->provider;
299 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
300 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
301
302 Topic topic0 = 0;
303 subscriber->AddTopic((IUnknown *)fapi, &topic0);
304 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
305 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
306
307 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN);
308 ASSERT_EQ(result, TRUE);
309
310 CASE_ReleaseIUnknown(fapi);
311 }
312
313 /**
314 * @tc.number : DMSLite_SAMGR_Publish_0070
315 * @tc.name : Provider send broadcast repeatedly, no memory leak
316 * @tc.desc : [C- SOFTWARE -0200]
317 */
318 HWTEST_F(BroadcastPublishTest, testPublish0070, Function | MediumTest | Level2)
319 {
320 PubSubInterface *fapi = CASE_GetIUnknown();
321 if (fapi == nullptr) {
322 ADD_FAILURE();
323 }
324 Subscriber *subscriber = &fapi->subscriber;
325 if (subscriber == nullptr) {
326 ADD_FAILURE();
327 }
328
329 Provider *provider = &fapi->provider;
330 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
331 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
332
333 Topic topic0 = 70;
334 subscriber->AddTopic((IUnknown *)fapi, &topic0);
335 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
336 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
337
338 for (int i = 0; i < PRESSURE_BASE; i++) {
339 // after this pressure, some operation will fail.
340 provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN);
341 }
342
343 usleep(OPER_INTERVAL * MS2US);
344 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8_t *) "==>111<==", TEST_LEN);
345 ASSERT_EQ(result, TRUE);
346
347 CASE_ReleaseIUnknown(fapi);
348 }