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