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 "common_event_manager.h"
20 #include "common_event_subscriber.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::EventFwk;
25
26 namespace {
27 const std::string STRING_EVENT = "com.ces.event";
28 #ifdef CEM_BUILD_VARIANT_USER
29 const std::string STRING_NO_SUBSCRIBERS = "error: user version cannot use dump.\n";
30 #else
31 const std::string STRING_NO_SUBSCRIBERS =
32 "Subscribers:\tNo information\n"
33 "Sticky Events:\tNo information\n"
34 "Pending Events:\tNo information\n";
35 #endif // DEBUG
36
37 const int32_t TIME_DELAY_FOR_SERVICES = 2;
38
ExecuteCommand(const std::string & command)39 std::string ExecuteCommand(const std::string &command)
40 {
41 std::string result = "";
42 FILE *file = popen(command.c_str(), "r");
43
44 // wait for services
45 std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
46
47 if (file != nullptr) {
48 char commandResult[1024] = {0};
49 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
50 result.append(commandResult);
51 }
52 pclose(file);
53 file = nullptr;
54 }
55
56 return result;
57 }
58 } // namespace
59
60 class CemCommandDumpSystemTest : public ::testing::Test {
61 public:
62 static void SetUpTestCase();
63 static void TearDownTestCase();
64 void SetUp() override;
65 void TearDown() override;
66 };
67
SetUpTestCase()68 void CemCommandDumpSystemTest::SetUpTestCase()
69 {}
70
TearDownTestCase()71 void CemCommandDumpSystemTest::TearDownTestCase()
72 {}
73
SetUp()74 void CemCommandDumpSystemTest::SetUp()
75 {
76 // reset optind to 0
77 optind = 0;
78 }
79
TearDown()80 void CemCommandDumpSystemTest::TearDown()
81 {}
82
83 class CommonEventSubscriberTest : public CommonEventSubscriber {
84 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)85 explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
86 : CommonEventSubscriber(subscribeInfo)
87 {}
88
~CommonEventSubscriberTest()89 ~CommonEventSubscriberTest()
90 {}
91
OnReceiveEvent(const CommonEventData & data)92 void OnReceiveEvent(const CommonEventData &data)
93 {}
94 };
95
96 /**
97 * @tc.number: Cem_Command_Dump_SystemTest_0100
98 * @tc.name: ExecCommand
99 * @tc.desc: Verify the "cem dump -a" command with a subscriber.
100 */
101 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0100, Function | MediumTest | Level1)
102 {
103 /* Subscribe */
104
105 // make matching skills
106 MatchingSkills matchingSkills;
107 matchingSkills.AddEvent(STRING_EVENT);
108
109 // make subscriber info
110 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
111
112 // make a subscriber object
113 auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
114 // subscribe a common event
115 CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
116
117 // dump all subscribers
118 std::string command = "cem dump -a";
119 std::string commandResult = ExecuteCommand(command);
120
121 EXPECT_NE(commandResult, "");
122
123 // unsubscribe a common event
124 CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
125 }
126
127 /**
128 * @tc.number: Cem_Command_Dump_SystemTest_0200
129 * @tc.name: ExecCommand
130 * @tc.desc: Verify the "cem dump -e <name>" command with no subscriber.
131 */
132 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0200, Function | MediumTest | Level1)
133 {
134 // dump all subscribers for an event
135 std::string command = "cem dump -e " + STRING_EVENT + ".test";
136 std::string commandResult = ExecuteCommand(command);
137
138 EXPECT_EQ(commandResult, STRING_NO_SUBSCRIBERS);
139 }
140
141 /**
142 * @tc.number: Cem_Command_Dump_SystemTest_0300
143 * @tc.name: ExecCommand
144 * @tc.desc: Verify the "cem dump -e <name>" command with a subscriber.
145 */
146 HWTEST_F(CemCommandDumpSystemTest, Cem_Command_Dump_SystemTest_0300, Function | MediumTest | Level1)
147 {
148 /* Subscribe */
149
150 // make matching skills
151 MatchingSkills matchingSkills;
152 matchingSkills.AddEvent(STRING_EVENT);
153
154 // make subscriber info
155 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
156
157 // make a subscriber object
158 auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
159 // subscribe a common event
160 CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
161
162 // dump all subscribers for an event
163 std::string command = "cem dump -e " + STRING_EVENT;
164 std::string commandResult = ExecuteCommand(command);
165
166 EXPECT_NE(commandResult, "");
167
168 // unsubscribe a common event
169 CommonEventManager::UnSubscribeCommonEvent(subscriberTestPtr);
170 }
171