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 <memory>
17 #include "kvstore_client_death_observer.h"
18 #include "bootstrap.h"
19 #include "gtest/gtest.h"
20 #include "kvstore_data_service.h"
21
22 using namespace testing::ext;
23 using namespace OHOS::DistributedKv;
24 using namespace OHOS::DistributedData;
25 using namespace OHOS;
26
27 class KvStoreDataServiceTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 static constexpr int32_t TEST_USERID = 100;
34 static constexpr int32_t TEST_APP_INDEX = 0;
35 static constexpr int32_t TEST_TOKENID = 100;
36 std::string TEST_INDENTATION = " ";
37 };
38
SetUpTestCase(void)39 void KvStoreDataServiceTest::SetUpTestCase(void)
40 {}
41
TearDownTestCase(void)42 void KvStoreDataServiceTest::TearDownTestCase(void)
43 {}
44
SetUp(void)45 void KvStoreDataServiceTest::SetUp(void)
46 {}
47
TearDown(void)48 void KvStoreDataServiceTest::TearDown(void)
49 {}
50
51 /**
52 * @tc.name: RegisterClientDeathObserver001
53 * @tc.desc: register client death observer
54 * @tc.type: FUNC
55 * @tc.require: AR000CQDU2
56 * @tc.author: liuyuhui
57 */
58 HWTEST_F(KvStoreDataServiceTest, RegisterClientDeathObserver001, TestSize.Level1)
59 {
60 AppId appId;
61 appId.appId = "app0";
62 KvStoreDataService kvDataService;
63 Bootstrap::GetInstance().LoadComponents();
64 Bootstrap::GetInstance().LoadCheckers();
65 KvStoreMetaManager::GetInstance().BindExecutor(std::make_shared<ExecutorPool>(12, 5));
66 KvStoreMetaManager::GetInstance().InitMetaParameter();
67 Status status = kvDataService.RegisterClientDeathObserver(appId, new KvStoreClientDeathObserver());
68 EXPECT_EQ(status, Status::SUCCESS) << "RegisterClientDeathObserver failed";
69 }
70
71 /**
72 * @tc.name: GetIndentation001
73 * @tc.desc:
74 * @tc.type: FUNC
75 * @tc.require:
76 * @tc.author: wangbin
77 */
78 HWTEST_F(KvStoreDataServiceTest, GetIndentation001, TestSize.Level1)
79 {
80 KvStoreDataService kvStoreDataServiceTest;
81 std::string indentation = kvStoreDataServiceTest.GetIndentation(1);
82 EXPECT_EQ(indentation, TEST_INDENTATION);
83 }
84
85 /**
86 * @tc.name: GetFeatureInterface001
87 * @tc.desc:
88 * @tc.type: FUNC
89 * @tc.require:
90 * @tc.author: wangbin
91 */
92 HWTEST_F(KvStoreDataServiceTest, GetFeatureInterface001, TestSize.Level1)
93 {
94 KvStoreDataService kvStoreDataServiceTest;
95 std::string name = "kvstoredatatest";
96 sptr<IRemoteObject> object = kvStoreDataServiceTest.GetFeatureInterface(name);
97 EXPECT_EQ(object, nullptr);
98 }
99
100 /**
101 * @tc.name: Dump001
102 * @tc.desc:
103 * @tc.type: FUNC
104 * @tc.require:
105 * @tc.author: wangbin
106 */
107 HWTEST_F(KvStoreDataServiceTest, Dump001, TestSize.Level1)
108 {
109 KvStoreDataService kvStoreDataServiceTest;
110 std::vector<std::u16string> args;
111 const std::u16string argstest1 = u"OHOS.DistributedKv.IKvStoreDataService1";
112 const std::u16string argstest2 = u"OHOS.DistributedKv.IKvStoreDataService2";
113 args.emplace_back(argstest1);
114 args.emplace_back(argstest2);
115 int32_t status = kvStoreDataServiceTest.Dump(1, args);
116 EXPECT_EQ(status, SUCCESS);
117 }
118
119 /**
120 * @tc.name: OnUninstall001
121 * @tc.desc:
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author: wangbin
125 */
126 HWTEST_F(KvStoreDataServiceTest, OnUninstall001, TestSize.Level1)
127 {
128 KvStoreDataService kvStoreDataServiceTest;
129 std::string bundleNametest = "ohos.kvstoredatatest.demo";
130 int32_t status = kvStoreDataServiceTest.OnUninstall(bundleNametest, TEST_USERID, TEST_APP_INDEX);
131 EXPECT_EQ(status, SUCCESS);
132 }
133
134 /**
135 * @tc.name: OnUpdate001
136 * @tc.desc:
137 * @tc.type: FUNC
138 * @tc.require:
139 * @tc.author: wangbin
140 */
141 HWTEST_F(KvStoreDataServiceTest, OnUpdate001, TestSize.Level1)
142 {
143 KvStoreDataService kvStoreDataServiceTest;
144 std::string bundleNametest = "ohos.kvstoredatatest.demo";
145 int32_t status = kvStoreDataServiceTest.OnUpdate(bundleNametest, TEST_USERID, TEST_APP_INDEX);
146 EXPECT_EQ(status, SUCCESS);
147 }
148
149 /**
150 * @tc.name: OnInstall001
151 * @tc.desc:
152 * @tc.type: FUNC
153 * @tc.require:
154 * @tc.author: wangbin
155 */
156 HWTEST_F(KvStoreDataServiceTest, OnInstall001, TestSize.Level1)
157 {
158 KvStoreDataService kvStoreDataServiceTest;
159 std::string bundleNametest = "ohos.kvstoredatatest.demo";
160 int32_t status = kvStoreDataServiceTest.OnInstall(bundleNametest, TEST_USERID, TEST_APP_INDEX);
161 EXPECT_EQ(status, SUCCESS);
162 }
163
164 /**
165 * @tc.name: AppExit001
166 * @tc.desc:
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author: wangbin
170 */
171 HWTEST_F(KvStoreDataServiceTest, AppExit001, TestSize.Level1)
172 {
173 KvStoreDataService kvStoreDataServiceTest;
174 pid_t uid = 1;
175 pid_t pid = 2;
176 uint32_t token = 3;
177 OHOS::DistributedKv::AppId appId = { "ohos.test.kvstoredataservice" };
178 Status status = kvStoreDataServiceTest.AppExit(uid, pid, token, appId);
179 EXPECT_EQ(status, SUCCESS);
180 }
181
182 /**
183 * @tc.name: ResolveAutoLaunchParamByIdentifier001
184 * @tc.desc:
185 * @tc.type: FUNC
186 * @tc.require:
187 * @tc.author: wangbin
188 */
189 HWTEST_F(KvStoreDataServiceTest, ResolveAutoLaunchParamByIdentifier001, TestSize.Level1)
190 {
191 KvStoreDataService kvStoreDataServiceTest;
192 std::string identifier = "kvstoredataservicetest";
193 DistributedDB::AutoLaunchParam param;
194 auto status = kvStoreDataServiceTest.ResolveAutoLaunchParamByIdentifier(identifier, param);
195 EXPECT_EQ(status, SUCCESS);
196 }
197
198 /**
199 * @tc.name: ConvertSecurity001
200 * @tc.desc:
201 * @tc.type: FUNC
202 * @tc.require:
203 * @tc.author: wangbin
204 */
205 HWTEST_F(KvStoreDataServiceTest, ConvertSecurity001, TestSize.Level1)
206 {
207 KvStoreDataService kvStoreDataServiceTest;
208 auto object = kvStoreDataServiceTest.ConvertSecurity(0);
209 ASSERT_NE(&object, nullptr);
210 }
211
212 /**
213 * @tc.name: InitNbDbOption001
214 * @tc.desc:
215 * @tc.type: FUNC
216 * @tc.require:
217 * @tc.author: wangbin
218 */
219 HWTEST_F(KvStoreDataServiceTest, InitNbDbOption001, TestSize.Level1)
220 {
221 KvStoreDataService kvStoreDataServiceTest;
222 DistributedDB::KvStoreNbDelegate::Option dbOption;
223 Options options = {
224 .createIfMissing = false,
225 .encrypt = true,
226 .autoSync = false,
227 .securityLevel = 1,
228 };
229 std::vector<uint8_t> decryptKey;
230 DistributedDB::KvStoreNbDelegate::Option dbOptions;
231 auto status = kvStoreDataServiceTest.InitNbDbOption(options, decryptKey, dbOptions);
232 EXPECT_EQ(status, SUCCESS);
233 }