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 #include <cstdint>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <fcntl.h>
20 #include <gtest/gtest.h>
21 #include <string>
22 #include <unistd.h>
23
24 #include "hdf_io_service.h"
25 #include "hdf_uhdf_test.h"
26 #include "osal_time.h"
27 #include "sample_driver_test.h"
28
29 using namespace testing::ext;
30
31 class HdfManagerTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase()39 void HdfManagerTest::SetUpTestCase()
40 {
41 HdfTestOpenService();
42 }
43
TearDownTestCase()44 void HdfManagerTest::TearDownTestCase()
45 {
46 HdfTestCloseService();
47 }
48
SetUp()49 void HdfManagerTest::SetUp() {}
50
TearDown()51 void HdfManagerTest::TearDown() {}
52
53 /**
54 * @tc.name: HdfIoServiceBind001
55 * @tc.desc: service bind test
56 * @tc.type: FUNC
57 * @tc.require: AR000F8698 AR000F8699 AR000F869A AR000F869B AR000F869C
58 */
59 HWTEST_F(HdfManagerTest, HdfIoServiceBind001, TestSize.Level0)
60 {
61 const char *svcName = "khdf_ut";
62 struct HdfIoService *hdfSvc = HdfIoServiceBind(svcName);
63 EXPECT_TRUE(hdfSvc != nullptr);
64 HdfIoServiceRecycle(hdfSvc);
65 }
66
67 /**
68 * @tc.name: HdfIoServiceBind002
69 * @tc.desc: service bind test
70 * @tc.type: FUNC
71 * @tc.require: AR000F8698 AR000F8699 AR000F869A AR000F869B AR000F869C
72 */
73 HWTEST_F(HdfManagerTest, HdfIoServiceBind002, TestSize.Level0)
74 {
75 struct HdfIoService *hdfSvc = HdfIoServiceBind(DEV_MGR_NODE);
76 EXPECT_TRUE(hdfSvc != nullptr);
77 HdfIoServiceRecycle(hdfSvc);
78 }
79
80 /**
81 * @tc.name: HdfRegisterDevice001
82 * @tc.desc: register device
83 * @tc.type: FUNC
84 * @tc.require: SR000F8697
85 */
86 HWTEST_F(HdfManagerTest, HdfRegisterDevice001, TestSize.Level0)
87 {
88 int32_t ret = HDF_FAILURE;
89 struct HdfSBuf *data = nullptr;
90 struct HdfIoService *ioService = HdfIoServiceBind(SAMPLE_SERVICE);
91 ASSERT_TRUE(ioService != nullptr);
92 data = HdfSbufObtainDefaultSize();
93 EXPECT_TRUE(data != nullptr);
94 EXPECT_TRUE(HdfSbufWriteString(data, "sample_driver"));
95 EXPECT_TRUE(HdfSbufWriteString(data, "sample_service1"));
96 uint64_t timeBefore = OsalGetSysTimeMs();
97 ret = ioService->dispatcher->Dispatch(&ioService->object, SAMPLE_DRIVER_REGISTER_DEVICE, data, nullptr);
98 EXPECT_TRUE(ret == HDF_SUCCESS);
99 uint64_t timeAfter = OsalGetSysTimeMs();
100 EXPECT_TRUE((timeAfter - timeBefore) < 100);
101
102 struct HdfIoService *ioService1 = HdfIoServiceBind("sample_service1");
103 ASSERT_TRUE(ioService1 != nullptr);
104 HdfIoServiceRecycle(ioService1);
105
106 ret = ioService->dispatcher->Dispatch(&ioService->object, SAMPLE_DRIVER_UNREGISTER_DEVICE, data, nullptr);
107 EXPECT_TRUE(ret == HDF_SUCCESS);
108
109 ioService1 = HdfIoServiceBind("sample_service1");
110 EXPECT_TRUE(ioService1 == nullptr);
111 HdfIoServiceRecycle(ioService);
112 HdfIoServiceRecycle(ioService1);
113 HdfSbufRecycle(data);
114 }
115
116 /**
117 * @tc.name: HdfGetServiceNameByDeviceClass001
118 * @tc.desc: get service test
119 * @tc.type: FUNC
120 * @tc.require: AR000F8698 AR000F8699 AR000F869A AR000F869B AR000F869C
121 */
122 HWTEST_F(HdfManagerTest, HdfGetServiceNameByDeviceClass001, TestSize.Level0)
123 {
124 struct HdfSBuf *data = HdfSbufObtain(2000);
125 ASSERT_TRUE(data != nullptr);
126
127 bool flag = false;
128 for (size_t i = DEVICE_CLASS_DEFAULT; i < DEVICE_CLASS_MAX; i++) {
129 int32_t ret = HdfGetServiceNameByDeviceClass((DeviceClass)i, data);
130 std::cout << "clasee " << i << " device list:" << std::endl;
131 EXPECT_TRUE(ret == HDF_SUCCESS);
132 const char *svcName = nullptr;
133 while (true) {
134 svcName = HdfSbufReadString(data);
135 if (svcName == nullptr) {
136 break;
137 }
138 std::cout << svcName << std::endl;
139 if (strcmp(svcName, "sample_service") == 0) {
140 flag = true;
141 }
142 }
143 HdfSbufFlush(data);
144 }
145 HdfSbufRecycle(data);
146 EXPECT_TRUE(flag);
147 }
148