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