1 /*
2 * Copyright (c) 2023 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 #include "hdf_io_service.h"
11 #include "hdf_load_vdi.h"
12 #include "securec.h"
13 #include "vdi_sample1_driver.h"
14 #include "vdi_sample1_symbol.h"
15 #include "vdi_sample2_driver.h"
16
17 namespace OHOS {
18 using namespace testing::ext;
19 using OHOS::VDI::Sample::V1_0::VdiWrapperB;
20 using OHOS::VDI::Sample::V1_0::VdiSample;
21
22 class HdfVdiTest : public testing::Test {
23 public:
24 static void SetUpTestCase();
25 static void TearDownTestCase();
26 void SetUp();
27 void TearDown();
28 };
29
SetUpTestCase()30 void HdfVdiTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void HdfVdiTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void HdfVdiTest::SetUp()
39 {
40 }
41
TearDown()42 void HdfVdiTest::TearDown()
43 {
44 }
45
46 HWTEST_F(HdfVdiTest, HdfVdiTestSampleABase, TestSize.Level3)
47 {
48 struct HdfVdiObject *vdi = nullptr;
49 vdi = HdfLoadVdi("libvdi_sample1_driver.z.so");
50 ASSERT_TRUE(vdi != nullptr);
51 ASSERT_TRUE(vdi->vdiBase != nullptr);
52
53 uint32_t version = HdfGetVdiVersion(vdi);
54 ASSERT_TRUE(version == 1);
55
56 struct VdiWrapperA *vdiWrapper = reinterpret_cast<struct VdiWrapperA *>(vdi->vdiBase);
57 ASSERT_TRUE(vdiWrapper->module != nullptr);
58 struct ModuleA *modA = reinterpret_cast<struct ModuleA *>(vdiWrapper->module);
59 int ret = modA->ServiceA();
60 ASSERT_TRUE(ret == HDF_SUCCESS);
61 ret = modA->ServiceB(modA);
62 ASSERT_TRUE(ret == HDF_SUCCESS);
63
64 HdfCloseVdi(vdi);
65 }
66
67 HWTEST_F(HdfVdiTest, HdfVdiTestSampleAErrorSo, TestSize.Level3)
68 {
69 struct HdfVdiObject *vdi = nullptr;
70 vdi = HdfLoadVdi("libvdi_sample1_driver_error.z.so");
71 ASSERT_TRUE(vdi == nullptr);
72 HdfCloseVdi(vdi);
73 }
74
75 HWTEST_F(HdfVdiTest, HdfVdiTestSampleBBase, TestSize.Level3)
76 {
77 struct HdfVdiObject *vdi = nullptr;
78 vdi = HdfLoadVdi("libvdi_sample2_driver.z.so");
79 ASSERT_TRUE(vdi != nullptr);
80 ASSERT_TRUE(vdi->vdiBase != nullptr);
81
82 uint32_t version = HdfGetVdiVersion(vdi);
83 ASSERT_TRUE(version == 1);
84
85 struct VdiWrapperB *vdiWrapper = reinterpret_cast<struct VdiWrapperB *>(vdi->vdiBase);
86 ASSERT_TRUE(vdiWrapper->module != nullptr);
87 VdiSample *vdiSample = reinterpret_cast<VdiSample *>(vdiWrapper->module);
88 int ret = vdiSample->ServiceA();
89 ASSERT_TRUE(ret == HDF_SUCCESS);
90 ret = vdiSample->ServiceB(vdiSample);
91 ASSERT_TRUE(ret == HDF_SUCCESS);
92
93 HdfCloseVdi(vdi);
94 }
95
96 HWTEST_F(HdfVdiTest, HdfVdiTestSampleBErrorSo, TestSize.Level3)
97 {
98 struct HdfVdiObject *vdi = nullptr;
99 vdi = HdfLoadVdi("libvdi_sample2_driver_error.z.so");
100 ASSERT_TRUE(vdi == nullptr);
101 HdfCloseVdi(vdi);
102 }
103
104 HWTEST_F(HdfVdiTest, HdfVdiTestLoadInvalidLibName, TestSize.Level3)
105 {
106 struct HdfVdiObject *vdi = nullptr;
107 vdi = HdfLoadVdi(nullptr);
108 ASSERT_TRUE(vdi == nullptr);
109 HdfCloseVdi(vdi);
110
111 char libName[PATH_MAX + 1];
112 (void)memset_s(libName, PATH_MAX, 'a', PATH_MAX);
113 libName[PATH_MAX] = 0;
114 vdi = HdfLoadVdi(libName);
115 ASSERT_TRUE(vdi == nullptr);
116
117 HdfCloseVdi(vdi);
118 }
119
120 HWTEST_F(HdfVdiTest, HdfVdiTestLoadInvalidSymbol, TestSize.Level3)
121 {
122 struct HdfVdiObject *vdi = nullptr;
123 vdi = HdfLoadVdi("libvdi_sample1_symbol.z.so");
124 ASSERT_TRUE(vdi == nullptr);
125 }
126
127 HWTEST_F(HdfVdiTest, HdfVdiTestNulVdiGetVersion, TestSize.Level3)
128 {
129 struct HdfVdiObject *vdi = nullptr;
130 uint32_t version = HdfGetVdiVersion(vdi);
131 HdfCloseVdi(vdi);
132 ASSERT_TRUE(version == HDF_INVALID_VERSION);
133 }
134
135 HWTEST_F(HdfVdiTest, HdfVdiTestAbnormal, TestSize.Level3)
136 {
137 struct HdfVdiObject obj;
138 struct HdfVdiBase base;
139 struct HdfVdiObject *vdi = &obj;
140
141 obj.vdiBase = &base;
142 obj.dlHandler = 0;
143 HdfCloseVdi(vdi);
144
145 obj.vdiBase = nullptr;
146 obj.dlHandler = 1;
147 HdfCloseVdi(vdi);
148
149 uint32_t version = HdfGetVdiVersion(vdi);
150 ASSERT_TRUE(version == HDF_INVALID_VERSION);
151 }
152 } // namespace OHOS
153