1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include <gtest/gtest.h>
10
11 #include <devmgr_hdi.h>
12 #include <osal_time.h>
13 #include <servmgr_hdi.h>
14
15 using namespace testing::ext;
16 static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service";
17 static constexpr const char *TEST_DEV_NODE = "/sys/devices/virtual/hdf/hdf_uevent_ut/uevent";
18 static constexpr const char *ADD_EVENT_CMD = "echo \"add\" > /sys/devices/virtual/hdf/hdf_uevent_ut/uevent";
19 static constexpr const char *REMOVE_EVENT_CMD = "echo \"remove\" > /sys/devices/virtual/hdf/hdf_uevent_ut/uevent";
20
21 class DevmgrUeventTest : public testing::Test {
22 public:
23 static void SetUpTestCase();
24 static void TearDownTestCase();
25 void SetUp();
26 void TearDown();
27
28 static const uint32_t waitTime = 30;
29 static const uint32_t timeout = 200;
30 static const uint32_t stressTime = 10;
31 static struct HDIServiceManager *servmgr;
32 static struct HDIDeviceManager *devmgr;
33 static bool haveDevNode;
34 };
35
36 struct HDIServiceManager *DevmgrUeventTest::servmgr = nullptr;
37 struct HDIDeviceManager *DevmgrUeventTest::devmgr = nullptr;
38 bool DevmgrUeventTest::haveDevNode = false;
39
SetUpTestCase()40 void DevmgrUeventTest::SetUpTestCase()
41 {
42 // Confirm whether there is a kernel node of HDF, testing is supported only when there is a kernel node
43 if (access(TEST_DEV_NODE, F_OK) == 0) {
44 haveDevNode = true;
45 servmgr = HDIServiceManagerGet();
46 devmgr = HDIDeviceManagerGet();
47 }
48 }
49
TearDownTestCase()50 void DevmgrUeventTest::TearDownTestCase() {}
51
SetUp()52 void DevmgrUeventTest::SetUp() {}
53
TearDown()54 void DevmgrUeventTest::TearDown() {}
55
56 /**
57 * @tc.name: DevmgrUeventTestAdd
58 * @tc.desc: trigger add uevent
59 * @tc.type: FUNC
60 * @tc.require: SR000H0E0E
61 */
62 HWTEST_F(DevmgrUeventTest, DevmgrUeventTestAdd, TestSize.Level3)
63 {
64 if (!haveDevNode) {
65 ASSERT_TRUE(true);
66 return;
67 }
68
69 ASSERT_TRUE(servmgr != nullptr);
70 ASSERT_TRUE(devmgr != nullptr);
71
72 // prepare:ensure that the service is not loaded
73 int ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
74 ASSERT_EQ(ret, HDF_SUCCESS);
75
76 // prepare:waiting for service offline
77 uint32_t cnt = 0;
78 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
79 while (sampleService != nullptr && cnt < timeout) {
80 OsalMSleep(waitTime);
81 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
82 cnt++;
83 }
84
85 // prepare:confirm that the service is unavailable
86 ASSERT_TRUE(sampleService == nullptr);
87
88 // trigger add uevent
89 system(ADD_EVENT_CMD);
90 cnt = 0;
91 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
92 while (sampleService == nullptr && cnt < timeout) {
93 OsalMSleep(waitTime);
94 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
95 cnt++;
96 }
97
98 // expect:confirm that the service is available
99 ASSERT_TRUE(sampleService != nullptr);
100 }
101
102 /**
103 * @tc.name: DevmgrUeventTestRemove
104 * @tc.desc: trigger remove uevent
105 * @tc.type: FUNC
106 * @tc.require: SR000H0E0E
107 */
108 HWTEST_F(DevmgrUeventTest, DevmgrUeventTestRemove, TestSize.Level3)
109 {
110 if (!haveDevNode) {
111 ASSERT_TRUE(true);
112 return;
113 }
114
115 ASSERT_TRUE(servmgr != nullptr);
116 ASSERT_TRUE(devmgr != nullptr);
117
118 // prepare:ensure that the service is loaded
119 int ret = devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME);
120 ASSERT_EQ(ret, HDF_SUCCESS);
121
122 // prepare:waiting for service online
123 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
124 uint32_t cnt = 0;
125 while (sampleService == nullptr && cnt < timeout) {
126 OsalMSleep(waitTime);
127 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
128 cnt++;
129 }
130
131 // prepare:confirm that the service is available
132 ASSERT_TRUE(sampleService != nullptr);
133
134 // trigger remove uevent
135 system(REMOVE_EVENT_CMD);
136 cnt = 0;
137 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
138 while (sampleService != nullptr && cnt < timeout) {
139 OsalMSleep(waitTime);
140 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
141 cnt++;
142 }
143
144 // expect:confirm that the service is unavailable
145 ASSERT_TRUE(sampleService == nullptr);
146 }
147
148 /**
149 * @tc.name: DevmgrUeventStressTest
150 * @tc.desc: Stress Test
151 * @tc.type: FUNC
152 * @tc.require: SR000H0E0E
153 */
154 HWTEST_F(DevmgrUeventTest, DevmgrUeventStressTest, TestSize.Level3)
155 {
156 if (!haveDevNode) {
157 ASSERT_TRUE(true);
158 return;
159 }
160
161 ASSERT_TRUE(servmgr != nullptr);
162 ASSERT_TRUE(devmgr != nullptr);
163
164 // prepare:ensure that the service is not loaded
165 int ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
166 ASSERT_EQ(ret, HDF_SUCCESS);
167
168 // prepare:waiting for service offline
169 uint32_t cnt = 0;
170 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
171 while (sampleService != nullptr && cnt < timeout) {
172 OsalMSleep(waitTime);
173 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
174 cnt++;
175 }
176
177 // prepare:confirm that the service is unavailable
178 ASSERT_TRUE(sampleService == nullptr);
179
180 for (uint32_t i = 0; i < stressTime; i++) {
181 // trigger add uevent
182 system(ADD_EVENT_CMD);
183 cnt = 0;
184 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
185 while (sampleService == nullptr && cnt < timeout) {
186 OsalMSleep(waitTime);
187 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
188 cnt++;
189 }
190
191 // expect:confirm that the service is available
192 ASSERT_TRUE(sampleService != nullptr);
193
194 // trigger remove uevent
195 system(REMOVE_EVENT_CMD);
196 cnt = 0;
197 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
198 while (sampleService != nullptr && cnt < timeout) {
199 OsalMSleep(waitTime);
200 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
201 cnt++;
202 }
203
204 // expect:confirm that the service is unavailable
205 ASSERT_TRUE(sampleService == nullptr);
206 }
207 }
208