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
17 #include <gtest/gtest.h>
18
19 #include <devmgr_hdi.h>
20 #include <osal_time.h>
21 #include <servmgr_hdi.h>
22 #include <fcntl.h>
23 #include "hdf_dump.h"
24 #include "hdf_dump_reg.h"
25 #include "hcs_tree_if.h"
26 #include "hcs_dm_parser.h"
27
28 #define HDF_LOG_TAG driver_manager
29
30 namespace OHOS {
31 using namespace testing::ext;
32
33 static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service";
34
35 class DevMgrTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 static const uint32_t waitTime = 30;
42 static const uint32_t timeout = 200;
43 static struct HDIServiceManager *servmgr;
44 static struct HDIDeviceManager *devmgr;
45 };
46
47 struct HDIServiceManager *DevMgrTest::servmgr = nullptr;
48 struct HDIDeviceManager *DevMgrTest::devmgr = nullptr;
49
SetUpTestCase()50 void DevMgrTest::SetUpTestCase()
51 {
52 servmgr = HDIServiceManagerGet();
53 devmgr = HDIDeviceManagerGet();
54 }
55
TearDownTestCase()56 void DevMgrTest::TearDownTestCase()
57 {
58 }
59
SetUp()60 void DevMgrTest::SetUp()
61 {
62 }
63
TearDown()64 void DevMgrTest::TearDown()
65 {
66 }
67
68 #ifdef SAMPLE_DRIVER
69 /*
70 * @tc.name: DriverLoaderTest
71 * @tc.desc: driver load test
72 * @tc.type: FUNC
73 * @tc.require: AR000DT1TK
74 */
75 HWTEST_F(DevMgrTest, DriverLoaderTest, TestSize.Level1)
76 {
77 ASSERT_TRUE(servmgr != nullptr);
78 ASSERT_TRUE(devmgr != nullptr);
79
80 int ret = devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME);
81 ASSERT_EQ(ret, HDF_SUCCESS);
82
83 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
84 uint32_t cnt = 0;
85 while (sampleService == nullptr && cnt < timeout) {
86 OsalMSleep(waitTime);
87 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
88 cnt++;
89 }
90
91 ASSERT_TRUE(sampleService != nullptr);
92 }
93 #endif
94
95 /*
96 * @tc.name: DriverUnLoaderTest
97 * @tc.desc: driver unload test
98 * @tc.type: FUNC
99 * @tc.require: AR000DT1TK
100 */
101 HWTEST_F(DevMgrTest, DriverUnLoaderTest, TestSize.Level1)
102 {
103 ASSERT_TRUE(servmgr != nullptr);
104 ASSERT_TRUE(devmgr != nullptr);
105
106 int ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
107 ASSERT_TRUE(ret != HDF_SUCCESS);
108
109 uint32_t cnt = 0;
110 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
111 while (sampleService != nullptr && cnt < timeout) {
112 OsalMSleep(waitTime);
113 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
114 cnt++;
115 }
116
117 ASSERT_TRUE(sampleService == nullptr);
118 }
119
120 #ifdef SAMPLE_DRIVER
121 HWTEST_F(DevMgrTest, DriverTest, TestSize.Level1)
122 {
123 ASSERT_TRUE(servmgr != nullptr);
124 ASSERT_TRUE(devmgr != nullptr);
125 int ret;
126 constexpr int loop = 100;
127
128 for (int i = 0; i < loop; i++) {
129 ret = devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME);
130 ASSERT_EQ(ret, HDF_SUCCESS);
131 uint32_t cnt = 0;
132 struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
133 while (sampleService == nullptr && cnt < timeout) {
134 OsalMSleep(waitTime);
135 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
136 cnt++;
137 }
138 ASSERT_TRUE(sampleService != nullptr);
139
140 ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
141 ASSERT_EQ(ret, HDF_SUCCESS);
142 cnt = 0;
143 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
144 while (sampleService != nullptr && cnt < timeout) {
145 OsalMSleep(waitTime);
146 sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
147 cnt++;
148 }
149 ASSERT_TRUE(sampleService == nullptr);
150 }
151 }
152 #endif
153
TestDump(struct HdfSBuf *,struct HdfSBuf *)154 static int TestDump(struct HdfSBuf *, struct HdfSBuf *)
155 {
156 return HDF_SUCCESS;
157 }
158
159 HWTEST_F(DevMgrTest, DevMgrDumpErrorTest, TestSize.Level1)
160 {
161 ASSERT_TRUE(servmgr != nullptr);
162 ASSERT_TRUE(devmgr != nullptr);
163 HdfRegisterDumpFunc(nullptr);
164 int32_t fd = open("/dev/null", O_WRONLY);
165 HdfRegisterDumpFunc(TestDump);
166 const std::vector<std::u16string> vcr = {u"123", u"456"};
167 const std::vector<std::u16string> vcr1 = {
168 u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"10",
169 u"11", u"12", u"13", u"14", u"15", u"16", u"17", u"18", u"19", u"20",
170 u"21", u"22", u"23", u"24"
171 };
172 int ret = HdfDump(fd, vcr);
173 ASSERT_TRUE(ret == HDF_SUCCESS);
174 fd = -1;
175 ret = HdfDump(fd, vcr1);
176 ASSERT_TRUE(ret != HDF_SUCCESS);
177
178 int32_t illegalFd = -1;
179 std::vector<std::u16string> illegalArgs = {};
180 ret = HdfDump(illegalFd, illegalArgs);
181 ASSERT_TRUE(ret != HDF_SUCCESS);
182 }
183
184 HWTEST_F(DevMgrTest, DevMgrDumpTest, TestSize.Level1)
185 {
186 int32_t ret;
187 constexpr int loop = 100;
188 int32_t fd = open("/dev/null", O_WRONLY);
189 for (int i = 0; i < loop; i++) {
190 const std::vector<std::u16string> vcr = {u"--ipc", u"all", u"--start-stat"};
191 ret = HdfDump(fd, vcr);
192 ASSERT_TRUE(ret == HDF_SUCCESS);
193
194 const std::vector<std::u16string> vcr1 = {u"--ipc", u"all", u"--stop-stat"};
195 ret = HdfDump(fd, vcr1);
196 ASSERT_TRUE(ret == HDF_SUCCESS);
197
198 const std::vector<std::u16string> vcr2 = {u"--ipc", u"all", u"--stat"};
199 ret = HdfDump(fd, vcr2);
200 ASSERT_TRUE(ret == HDF_SUCCESS);
201 }
202 std::string cmd = "123456";
203 HdfDumpIpcStat(0, nullptr);
204 HdfDumpIpcStat(1, cmd.c_str());
205 }
206
207 HWTEST_F(DevMgrTest, HdfUtilsTest, TestSize.Level1)
208 {
209 std::string CONFIGPATH = "/system/etc/hdfconfig/default";
210 std::string CONFIGPATH1 = "/system/etc/hdfconfig/default.hcb";
211 SetHcsBlobPath(CONFIGPATH.c_str());
212 const struct DeviceResourceNode *node = HcsGetRootNode();
213 ASSERT_TRUE(node == nullptr);
214 SetHcsBlobPath(CONFIGPATH1.c_str());
215 const struct DeviceResourceNode *node1 = HcsGetRootNode();
216 ASSERT_TRUE(node1 == nullptr);
217 }
218 } // namespace OHOS
219