• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
18 #include <thread>
19 #include "datetime_ex.h"
20 #include "common_event_command.h"
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::EventFwk;
27 
28 namespace {
29 const std::string STRING_EVENT = "com.ces.event";
30 const std::string STRING_CODE = "1024";
31 const std::string STRING_CODE_TWO = "2048";
32 const std::string STRING_DATA = "data";
33 const std::string STRING_DATA_TWO = "data_two";
34 const std::string STRING_DEVICE_ID_001 = "device_001";
35 const std::string STRING_DEVICE_ID_002 = "device_002";
36 const std::string STRING_DEVICE_ID_003 = "device_003";
37 const int32_t TIME_DELAY_FOR_SERVICES = 2;
38 const time_t TIME_OUT_SECONDS_LIMIT = 5;
39 std::mutex mtx;
40 
ExecuteCommand(const std::string & command)41 std::string ExecuteCommand(const std::string &command)
42 {
43     std::string result = "";
44     FILE *file = popen(command.c_str(), "r");
45 
46     // wait for services
47     std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
48 
49     if (file != nullptr) {
50         char commandResult[1024] = {0};
51         while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
52             result.append(commandResult);
53         }
54         pclose(file);
55         file = nullptr;
56     }
57 
58     return result;
59 }
60 }  // namespace
61 
62 class CemCommandPublishSystemTest : public ::testing::Test {
63 public:
64     static void SetUpTestCase();
65     static void TearDownTestCase();
66     void SetUp() override;
67     void TearDown() override;
68 };
69 
SetUpTestCase()70 void CemCommandPublishSystemTest::SetUpTestCase()
71 {}
72 
TearDownTestCase()73 void CemCommandPublishSystemTest::TearDownTestCase()
74 {}
75 
SetUp()76 void CemCommandPublishSystemTest::SetUp()
77 {
78     // reset optind to 0
79     optind = 0;
80 }
81 
TearDown()82 void CemCommandPublishSystemTest::TearDown()
83 {}
84 
85 class CommonEventSubscriberTest : public CommonEventSubscriber {
86 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)87     explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
88         : CommonEventSubscriber(subscribeInfo)
89     {}
90 
~CommonEventSubscriberTest()91     ~CommonEventSubscriberTest()
92     {}
93 
OnReceiveEvent(const CommonEventData & commonEventData)94     void OnReceiveEvent(const CommonEventData &commonEventData)
95     {
96         GTEST_LOG_(INFO) << "OnReceiveEvent";
97 
98         std::string deviceId = GetSubscribeInfo().GetDeviceId();
99         GTEST_LOG_(INFO) << "deviceId = " << deviceId;
100 
101         int32_t code = commonEventData.GetCode();
102         GTEST_LOG_(INFO) << "code = " << code;
103 
104         std::string data = commonEventData.GetData();
105         GTEST_LOG_(INFO) << "data = " << data;
106 
107         if (deviceId == STRING_DEVICE_ID_001) {
108             if (atoi(STRING_CODE.c_str()) == code && STRING_DATA == data) {
109                 mtx.unlock();
110             }
111         } else if (deviceId == STRING_DEVICE_ID_002) {
112             if (atoi(STRING_CODE_TWO.c_str()) == code) {
113                 mtx.unlock();
114             }
115         } else if (deviceId == STRING_DEVICE_ID_003) {
116             if (STRING_DATA_TWO == data) {
117                 mtx.unlock();
118             }
119         } else {
120             mtx.unlock();
121         }
122     }
123 };
124 
125 /**
126  * @tc.number: Cem_Command_Publish_SystemTest_0100
127  * @tc.name: ExecCommand
128  * @tc.desc: Verify the "cem publish -e <name>" command.
129  */
130 HWTEST_F(CemCommandPublishSystemTest, Cem_Command_Publish_SystemTest_0100, Function | MediumTest | Level1)
131 {
132     /* Subscribe */
133 
134     // make matching skills
135     MatchingSkills matchingSkills;
136     matchingSkills.AddEvent(STRING_EVENT);
137 
138     // make subscribe info
139     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
140 
141     // make a subscriber object
142     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
143     // subscribe a common event
144     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
145 
146     // lock the mutex
147     mtx.lock();
148 
149     // publish a common event
150     std::string command = "cem publish -e " + STRING_EVENT;
151     std::string commandResult = ExecuteCommand(command);
152 
153     EXPECT_EQ(commandResult, STRING_PUBLISH_COMMON_EVENT_OK);
154 
155     // record start time of publishing
156     struct tm startTime = {0};
157     EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
158 
159     // record current time
160     struct tm doingTime = {0};
161     int64_t seconds = 0;
162 
163     while (!mtx.try_lock()) {
164         // get current time and compare it with the start time
165         EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
166         seconds = OHOS::GetSecondsBetween(startTime, doingTime);
167         if (seconds >= TIME_OUT_SECONDS_LIMIT) {
168             break;
169         }
170     }
171 
172     // unsubscribe the common event
173     CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
174 
175     // expect the subscriber could receive the event within 5 seconds.
176     EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
177 
178     // unlock the mutex
179     mtx.unlock();
180 }
181 
182 /**
183  * @tc.number: Cem_Command_Publish_SystemTest_0200
184  * @tc.name: ExecCommand
185  * @tc.desc: Verify the "cem publish -e <name> -c <code> -d <data>" command.
186  */
187 HWTEST_F(CemCommandPublishSystemTest, Cem_Command_Publish_SystemTest_0200, Function | MediumTest | Level1)
188 {
189     /* Subscribe */
190 
191     // make matching skills
192     MatchingSkills matchingSkills;
193     matchingSkills.AddEvent(STRING_EVENT);
194 
195     // make subscribe info
196     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
197 
198     // set device id
199     subscribeInfo.SetDeviceId(STRING_DEVICE_ID_001);
200 
201     // make a subscriber object
202     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
203     // subscribe a common event
204     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
205 
206     // lock the mutex
207     mtx.lock();
208 
209     // publish a common event with code and data
210     std::string command = "cem publish -e " + STRING_EVENT + " -c " + STRING_CODE + " -d " + STRING_DATA;
211     std::string commandResult = ExecuteCommand(command);
212 
213     EXPECT_EQ(commandResult, STRING_PUBLISH_COMMON_EVENT_OK);
214 
215     // record start time of publishing
216     struct tm startTime = {0};
217     EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
218 
219     // record current time
220     struct tm doingTime = {0};
221     int64_t seconds = 0;
222 
223     while (!mtx.try_lock()) {
224         // get current time and compare it with the start time
225         EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
226         seconds = OHOS::GetSecondsBetween(startTime, doingTime);
227         if (seconds >= TIME_OUT_SECONDS_LIMIT) {
228             break;
229         }
230     }
231 
232     // unsubscribe the common event
233     CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
234 
235     // expect the subscriber could receive the event within 5 seconds.
236     EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
237 
238     // unlock the mutex
239     mtx.unlock();
240 }
241 
242 /**
243  * @tc.number: Cem_Command_Publish_SystemTest_0300
244  * @tc.name: ExecCommand
245  * @tc.desc: Verify the "cem publish -e <name> -c <code>" command.
246  */
247 HWTEST_F(CemCommandPublishSystemTest, Cem_Command_Publish_SystemTest_0300, Function | MediumTest | Level1)
248 {
249     /* Subscribe */
250 
251     // make matching skills
252     MatchingSkills matchingSkills;
253     matchingSkills.AddEvent(STRING_EVENT);
254 
255     // make subscribe info
256     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
257 
258     // set device id
259     subscribeInfo.SetDeviceId(STRING_DEVICE_ID_002);
260 
261     // make a subscriber object
262     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
263     // subscribe a common event
264     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
265 
266     // lock the mutex
267     mtx.lock();
268 
269     // publish a common event with code and data
270     std::string command = "cem publish -e " + STRING_EVENT + " -c " + STRING_CODE_TWO;
271     std::string commandResult = ExecuteCommand(command);
272 
273     EXPECT_EQ(commandResult, STRING_PUBLISH_COMMON_EVENT_OK);
274 
275     // record start time of publishing
276     struct tm startTime = {0};
277     EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
278 
279     // record current time
280     struct tm doingTime = {0};
281     int64_t seconds = 0;
282 
283     while (!mtx.try_lock()) {
284         // get current time and compare it with the start time
285         EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
286         seconds = OHOS::GetSecondsBetween(startTime, doingTime);
287         if (seconds >= TIME_OUT_SECONDS_LIMIT) {
288             break;
289         }
290     }
291 
292     // unsubscribe the common event
293     CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
294 
295     // expect the subscriber could receive the event within 5 seconds.
296     EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
297 
298     // unlock the mutex
299     mtx.unlock();
300 }
301 
302 /**
303  * @tc.number: Cem_Command_Publish_SystemTest_0400
304  * @tc.name: ExecCommand
305  * @tc.desc: Verify the "cem publish -e <name> -d <data>" command.
306  */
307 HWTEST_F(CemCommandPublishSystemTest, Cem_Command_Publish_SystemTest_0400, Function | MediumTest | Level1)
308 {
309     /* Subscribe */
310 
311     // make matching skills
312     MatchingSkills matchingSkills;
313     matchingSkills.AddEvent(STRING_EVENT);
314 
315     // make subscribe info
316     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
317 
318     // set device id
319     subscribeInfo.SetDeviceId(STRING_DEVICE_ID_003);
320 
321     // make a subscriber object
322     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
323     // subscribe a common event
324     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
325 
326     // lock the mutex
327     mtx.lock();
328 
329     // publish a common event with code and data
330     std::string command = "cem publish -e " + STRING_EVENT + " -d " + STRING_DATA_TWO;
331     std::string commandResult = ExecuteCommand(command);
332 
333     EXPECT_EQ(commandResult, STRING_PUBLISH_COMMON_EVENT_OK);
334 
335     // record start time of publishing
336     struct tm startTime = {0};
337     EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
338 
339     // record current time
340     struct tm doingTime = {0};
341     int64_t seconds = 0;
342 
343     while (!mtx.try_lock()) {
344         // get current time and compare it with the start time
345         EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
346         seconds = OHOS::GetSecondsBetween(startTime, doingTime);
347         if (seconds >= TIME_OUT_SECONDS_LIMIT) {
348             break;
349         }
350     }
351 
352     // unsubscribe the common event
353     CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
354 
355     // expect the subscriber could receive the event within 5 seconds.
356     EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
357 
358     // unlock the mutex
359     mtx.unlock();
360 }
361