1 /*
2 * Copyright (c) 2024 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 <dlfcn.h>
17 #include <gtest/gtest.h>
18
19 #define private public
20 #include "rdb_dlopen_manager.h"
21 #undef private
22 #include "token_field_const.h"
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 namespace {
30 constexpr const int32_t WAIT_EVENTHANDLE_TIME = 2;
31 constexpr const int32_t WAIT_DELAY_DLCLOSE_TIME = 7;
32 }
33 class AccessTokenDatabaseDlopenTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36
37 static void TearDownTestCase();
38
39 void SetUp();
40
41 void TearDown();
42 };
43
SetUpTestCase()44 void AccessTokenDatabaseDlopenTest::SetUpTestCase() {}
45
TearDownTestCase()46 void AccessTokenDatabaseDlopenTest::TearDownTestCase() {}
47
SetUp()48 void AccessTokenDatabaseDlopenTest::SetUp() {}
49
TearDown()50 void AccessTokenDatabaseDlopenTest::TearDown()
51 {
52 RdbDlopenManager::DestroyInstance();
53 }
54
55 /*
56 * @tc.name: GetEventHandler001
57 * @tc.desc: RdbDlopenManager::GetEventHandler
58 * @tc.type: FUNC
59 * @tc.require:
60 */
61 HWTEST_F(AccessTokenDatabaseDlopenTest, GetEventHandler001, TestSize.Level4)
62 {
63 auto instance = RdbDlopenManager::GetInstance();
64 ASSERT_NE(nullptr, instance);
65 ASSERT_EQ(nullptr, instance->eventRunner_);
66 ASSERT_EQ(nullptr, instance->eventHandler_);
67
68 instance->GetEventHandler();
69 ASSERT_NE(nullptr, instance->eventRunner_);
70 ASSERT_NE(nullptr, instance->eventHandler_);
71 instance->GetEventHandler();
72 }
73
74 /*
75 * @tc.name: Create001
76 * @tc.desc: RdbDlopenManager::Create
77 * @tc.type: FUNC
78 * @tc.require:
79 */
80 HWTEST_F(AccessTokenDatabaseDlopenTest, Create001, TestSize.Level4)
81 {
82 auto instance = RdbDlopenManager::GetInstance();
83 ASSERT_NE(nullptr, instance);
84 ASSERT_EQ(nullptr, instance->handle_);
85 ASSERT_EQ(nullptr, instance->instance_);
86
87 instance->Init();
88 void* handle = nullptr;
89 instance->Create(handle);
90
91 handle = dlopen("libaccesstoken_sdk.z.so", RTLD_LAZY);
92 ASSERT_NE(nullptr, handle);
93 instance->Create(handle);
94 ASSERT_EQ(nullptr, instance->instance_);
95 dlclose(handle);
96 handle = nullptr;
97
98 handle = dlopen(RDB_ADAPTER_LIBPATH, RTLD_LAZY);
99 ASSERT_NE(nullptr, handle);
100 instance->Create(handle);
101 ASSERT_NE(nullptr, instance->instance_);
102 dlclose(handle);
103 handle = nullptr;
104 }
105
106 /*
107 * @tc.name: Destroy001
108 * @tc.desc: RdbDlopenManager::Destroy
109 * @tc.type: FUNC
110 * @tc.require:
111 */
112 HWTEST_F(AccessTokenDatabaseDlopenTest, Destroy001, TestSize.Level4)
113 {
114 auto instance = RdbDlopenManager::GetInstance();
115 ASSERT_NE(nullptr, instance);
116 ASSERT_EQ(nullptr, instance->handle_);
117 ASSERT_EQ(nullptr, instance->instance_);
118
119 void* handle = nullptr;
120 instance->Destroy(handle);
121
122 handle = dlopen("libaccesstoken_sdk.z.so", RTLD_LAZY);
123 ASSERT_NE(nullptr, handle);
124 instance->Destroy(handle);
125 ASSERT_EQ(nullptr, instance->instance_);
126 dlclose(handle);
127 handle = nullptr;
128
129 handle = dlopen(RDB_ADAPTER_LIBPATH, RTLD_LAZY);
130 ASSERT_NE(nullptr, handle);
131 instance->Destroy(handle);
132 instance->Create(handle);
133 ASSERT_NE(nullptr, instance->instance_);
134 instance->Destroy(handle);
135 ASSERT_EQ(nullptr, instance->instance_);
136
137 dlclose(handle);
138 handle = nullptr;
139 }
140
141 /*
142 * @tc.name: DelayDlcloseHandle001
143 * @tc.desc: RdbDlopenManager::DelayDlcloseHandle
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 HWTEST_F(AccessTokenDatabaseDlopenTest, DelayDlcloseHandle001, TestSize.Level4)
148 {
149 auto instance = RdbDlopenManager::GetInstance();
150 ASSERT_NE(nullptr, instance);
151 ASSERT_EQ(nullptr, instance->handle_);
152 ASSERT_EQ(nullptr, instance->instance_);
153
154 instance->DelayDlcloseHandle(0); // handle_ is nullptr
155
156 instance->handle_ = dlopen("libaccesstoken_sdk.z.so", RTLD_LAZY);
157 ASSERT_NE(nullptr, instance->handle_);
158 instance->DelayDlcloseHandle(0); // handle_ is not nullptr, instance_ is nullptr
159 sleep(WAIT_EVENTHANDLE_TIME);
160 ASSERT_EQ(nullptr, instance->handle_);
161
162 instance->handle_ = dlopen(RDB_ADAPTER_LIBPATH, RTLD_LAZY);
163 ASSERT_NE(nullptr, instance->handle_);
164 instance->Create(instance->handle_);
165 ASSERT_NE(nullptr, instance->instance_);
166 instance->DelayDlcloseHandle(0); // handle_ and instance_ both not nullptr
167 sleep(WAIT_EVENTHANDLE_TIME);
168 ASSERT_EQ(nullptr, instance->instance_);
169 ASSERT_EQ(nullptr, instance->handle_);
170 }
171
172 /*
173 * @tc.name: DelayDlcloseHandle002
174 * @tc.desc: RdbDlopenManager::DelayDlcloseHandle
175 * @tc.type: FUNC
176 * @tc.require:
177 */
178 HWTEST_F(AccessTokenDatabaseDlopenTest, DelayDlcloseHandle002, TestSize.Level4)
179 {
180 auto instance = RdbDlopenManager::GetInstance();
181 ASSERT_NE(nullptr, instance);
182 ASSERT_EQ(nullptr, instance->handle_);
183 ASSERT_EQ(nullptr, instance->instance_);
184 ASSERT_EQ(0, instance->taskNum_);
185 instance->DelayDlcloseHandle(0); // handle_ is nullptr
186 instance->handle_ = dlopen(RDB_ADAPTER_LIBPATH, RTLD_LAZY);
187 ASSERT_NE(nullptr, instance->handle_);
188 instance->Create(instance->handle_);
189 ASSERT_NE(nullptr, instance->instance_);
190 instance->taskNum_++;
191 instance->DelayDlcloseHandle(0); // handle_ and instance_ both not nullptr
192 sleep(WAIT_EVENTHANDLE_TIME);
193 ASSERT_NE(nullptr, instance->instance_);
194 ASSERT_NE(nullptr, instance->handle_);
195 instance->taskNum_--;
196 instance->DelayDlcloseHandle(0); // handle_ and instance_ both not nullptr
197 sleep(WAIT_EVENTHANDLE_TIME);
198 ASSERT_EQ(nullptr, instance->instance_);
199 ASSERT_EQ(nullptr, instance->handle_);
200 }
201
202 /*
203 * @tc.name: DynamicCallTest001
204 * @tc.desc: RdbDlopenManager::DynamicCallTest
205 * @tc.type: FUNC
206 * @tc.require:
207 */
208 HWTEST_F(AccessTokenDatabaseDlopenTest, DynamicCallTest001, TestSize.Level4)
209 {
210 auto instance = RdbDlopenManager::GetInstance();
211 ASSERT_NE(nullptr, instance);
212
213 AtmDataType type = AtmDataType::ACCESSTOKEN_SYSTEM_CONFIG;
214 GenericValues addValue;
215 addValue.Put(TokenFiledConst::FIELD_NAME, "test");
216 addValue.Put(TokenFiledConst::FIELD_VALUE, "test");
217 AddInfo addInfo;
218 addInfo.addType = type;
219 addInfo.addValues.emplace_back(addValue);
220
221 std::vector<DelInfo> delInfoVec;
222 std::vector<AddInfo> addInfoVec;
223 addInfoVec.emplace_back(addInfo);
224 ASSERT_EQ(0, instance->DeleteAndInsertValues(delInfoVec, addInfoVec)); // add a test value to system_config_table
225
226 GenericValues conditionValue;
227 conditionValue.Put(TokenFiledConst::FIELD_NAME, "test");
228 std::vector<GenericValues> results1;
229 EXPECT_EQ(0, instance->Find(type, conditionValue, results1));
230 EXPECT_EQ(1, results1.size());
231 EXPECT_EQ("test", results1[0].GetString(TokenFiledConst::FIELD_VALUE));
232
233 GenericValues modifyValue;
234 modifyValue.Put(TokenFiledConst::FIELD_NAME, "test");
235 modifyValue.Put(TokenFiledConst::FIELD_VALUE, "test123"); // modify "test": "test" to "test": "test123"
236 EXPECT_EQ(0, instance->Modify(type, modifyValue, conditionValue));
237
238 std::vector<GenericValues> results2;
239 EXPECT_EQ(0, instance->Find(type, conditionValue, results2));
240 EXPECT_EQ(1, results2.size());
241 EXPECT_EQ("test123", results2[0].GetString(TokenFiledConst::FIELD_VALUE));
242
243 DelInfo delInfo;
244 delInfo.delType = type;
245 delInfo.delValue = conditionValue;
246
247 std::vector<DelInfo> delInfoVec2;
248 delInfoVec2.emplace_back(delInfo);
249 std::vector<AddInfo> addInfoVec2;
250 // remove test value from system_config_table
251 ASSERT_EQ(0, instance->DeleteAndInsertValues(delInfoVec2, addInfoVec2));
252
253 std::vector<GenericValues> results3;
254 ASSERT_EQ(0, instance->Find(type, conditionValue, results3));
255 ASSERT_EQ(true, results3.empty());
256 }
257 } // namespace AccessToken
258 } // namespace Security
259 } // namespace OHOS
260