1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <variant>
23
24 #include "gdb_errors.h"
25 #include "db_store_impl.h"
26 #include "db_store_manager.h"
27 #include "edge.h"
28 #include "grd_adapter.h"
29 #include "grd_adapter_manager.h"
30 #include "gdb_helper.h"
31 #include "gdb_store.h"
32 #include "gdb_utils.h"
33 #include "graph_statement.h"
34 #include "graph_connection.h"
35 #include "path.h"
36 #include "result.h"
37 #include "vertex.h"
38
39 using namespace testing::ext;
40 using namespace OHOS::DistributedDataAip;
41 class GdbEncryptTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 void VerifyPersonInfo(const GraphValue &person, const std::string &name, const int32_t &age);
48 static const std::string createGraphGql;
49 static const std::string databasePath;
50 };
51 const std::string GdbEncryptTest::databasePath = "/data";
52 const std::string GdbEncryptTest::createGraphGql = "CREATE GRAPH test { "
53 "(person:Person {name STRING, age INT, sex BOOL DEFAULT false}),"
54 "(dog:Dog {name STRING, age INT}), "
55 "(person) -[:Friend]-> (person) "
56 "};";
57
SetUpTestCase()58 void GdbEncryptTest::SetUpTestCase()
59 {
60 if (!IsSupportArkDataDb()) {
61 GTEST_SKIP() << "Current testcase is not compatible from current gdb";
62 return;
63 }
64 }
65
TearDownTestCase()66 void GdbEncryptTest::TearDownTestCase()
67 {
68 }
69
SetUp()70 void GdbEncryptTest::SetUp()
71 {
72 if (!IsSupportArkDataDb()) {
73 GTEST_SKIP() << "Current testcase is not compatible from current gdb";
74 return;
75 }
76 }
77
TearDown()78 void GdbEncryptTest::TearDown()
79 {
80 }
81
82 HWTEST_F(GdbEncryptTest, GdbEncrypt_DeaultIsUnencrypt, TestSize.Level1)
83 {
84 int errCode = E_OK;
85 std::string dbName = "DeaultIsUnencrypt";
86 std::string dbPath = databasePath;
87 // DeaultIsUnencrypt
88 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH);
89 auto store = GDBHelper::GetDBStore(config, errCode);
90 EXPECT_NE(store, nullptr);
91 EXPECT_EQ(errCode, E_OK);
92 auto result = store->ExecuteGql(createGraphGql);
93 EXPECT_NE(result.second, nullptr);
94 EXPECT_EQ(result.first, E_OK);
95 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
96 EXPECT_EQ(result.first, E_OK);
97 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
98 ASSERT_EQ(result.first, E_OK);
99 ASSERT_NE(result.second, nullptr);
100 EXPECT_EQ(result.second->GetAllData().size(), 1);
101 GraphValue person = result.second->GetAllData()[0]["person"];
102 VerifyPersonInfo(person, "name_1", 11);
103 store->Close();
104 GDBHelper::DeleteDBStore(config);
105 }
106
107 HWTEST_F(GdbEncryptTest, GdbEncrypt_Unencrypt, TestSize.Level1)
108 {
109 int errCode = E_OK;
110 std::string dbName = "Unencrypt";
111 std::string dbPath = databasePath;
112 // Unencryptdb
113 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, false);
114 auto store = GDBHelper::GetDBStore(config, errCode);
115 EXPECT_NE(store, nullptr);
116 EXPECT_EQ(errCode, E_OK);
117 auto result = store->ExecuteGql(createGraphGql);
118 EXPECT_NE(result.second, nullptr);
119 EXPECT_EQ(result.first, E_OK);
120 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
121 EXPECT_EQ(result.first, E_OK);
122 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
123 ASSERT_EQ(result.first, E_OK);
124 ASSERT_NE(result.second, nullptr);
125 EXPECT_EQ(result.second->GetAllData().size(), 1);
126 GraphValue person = result.second->GetAllData()[0]["person"];
127 VerifyPersonInfo(person, "name_1", 11);
128 store->Close();
129 GDBHelper::DeleteDBStore(config);
130 }
131
132 HWTEST_F(GdbEncryptTest, GdbEncrypt_Encrypt, TestSize.Level1)
133 {
134 int errCode = E_OK;
135 std::string dbName = "Encrypt";
136 std::string dbPath = databasePath;
137 // Unencryptdb
138 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, true);
139 auto store = GDBHelper::GetDBStore(config, errCode);
140 EXPECT_NE(store, nullptr);
141 EXPECT_EQ(errCode, E_OK);
142 auto result = store->ExecuteGql(createGraphGql);
143 EXPECT_NE(result.second, nullptr);
144 EXPECT_EQ(result.first, E_OK);
145 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
146 EXPECT_EQ(result.first, E_OK);
147 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
148 ASSERT_EQ(result.first, E_OK);
149 ASSERT_NE(result.second, nullptr);
150 EXPECT_EQ(result.second->GetAllData().size(), 1);
151 GraphValue person = result.second->GetAllData()[0]["person"];
152 VerifyPersonInfo(person, "name_1", 11);
153 store->Close();
154 GDBHelper::DeleteDBStore(config);
155 }
156
157 HWTEST_F(GdbEncryptTest, GdbEncrypt_UnencryptToEncrypt, TestSize.Level1)
158 {
159 int errCode = E_OK;
160 std::string dbName = "UnencryptToEncrypt";
161 std::string dbPath = databasePath;
162 // Unencryptdb
163 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, false);
164 auto store = GDBHelper::GetDBStore(config, errCode);
165 EXPECT_NE(store, nullptr);
166 EXPECT_EQ(errCode, E_OK);
167 auto result = store->ExecuteGql(createGraphGql);
168 EXPECT_NE(result.second, nullptr);
169 EXPECT_EQ(result.first, E_OK);
170 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
171 EXPECT_EQ(result.first, E_OK);
172 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
173 ASSERT_EQ(result.first, E_OK);
174 ASSERT_NE(result.second, nullptr);
175 EXPECT_EQ(result.second->GetAllData().size(), 1);
176 GraphValue person = result.second->GetAllData()[0]["person"];
177 VerifyPersonInfo(person, "name_1", 11);
178 result.second = nullptr;
179 store->Close();
180 StoreManager::GetInstance().Clear();
181 // Unencryptdb to encryptdb
182 config.SetEncryptStatus(true);
183 store = GDBHelper::GetDBStore(config, errCode);
184 EXPECT_NE(store, nullptr);
185 EXPECT_EQ(errCode, E_OK);
186 if (store != nullptr) {
187 result = store->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
188 EXPECT_EQ(result.first, E_OK);
189 result = store->QueryGql("MATCH (person:Person {age: 22}) RETURN person;");
190 ASSERT_EQ(result.first, E_OK);
191 ASSERT_NE(result.second, nullptr);
192 EXPECT_EQ(result.second->GetAllData().size(), 1);
193 GraphValue person = result.second->GetAllData()[0]["person"];
194 VerifyPersonInfo(person, "name_2", 22);
195 store->Close();
196 }
197 GDBHelper::DeleteDBStore(config);
198 }
199
200 HWTEST_F(GdbEncryptTest, GdbEncrypt_EncryptToUnencrypt, TestSize.Level1)
201 {
202 int errCode = E_OK;
203 std::string dbName = "EncryptToUnencrypt";
204 std::string dbPath = databasePath;
205 // encryptdb
206 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, true);
207 auto store = GDBHelper::GetDBStore(config, errCode);
208 EXPECT_NE(store, nullptr);
209 EXPECT_EQ(errCode, E_OK);
210 auto result = store->ExecuteGql(createGraphGql);
211 EXPECT_NE(result.second, nullptr);
212 EXPECT_EQ(result.first, E_OK);
213 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
214 EXPECT_EQ(result.first, E_OK);
215 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
216 ASSERT_EQ(result.first, E_OK);
217 ASSERT_NE(result.second, nullptr);
218 EXPECT_EQ(result.second->GetAllData().size(), 1);
219 GraphValue person = result.second->GetAllData()[0]["person"];
220 VerifyPersonInfo(person, "name_1", 11);
221 store->Close();
222 StoreManager::GetInstance().Clear();
223 // encryptdb to Unencryptdb
224 config.SetEncryptStatus(false);
225 store = GDBHelper::GetDBStore(config, errCode);
226 EXPECT_EQ(store, nullptr);
227 EXPECT_EQ(errCode, E_CONFIG_INVALID_CHANGE);
228 GDBHelper::DeleteDBStore(config);
229 }
230
231 HWTEST_F(GdbEncryptTest, GdbEncrypt_DefaultToEncrypt, TestSize.Level1)
232 {
233 int errCode = E_OK;
234 std::string dbName = "DefaultToEncrypt";
235 std::string dbPath = databasePath;
236 // Unencryptdb
237 auto config = StoreConfig(dbName, dbPath);
238 auto store = GDBHelper::GetDBStore(config, errCode);
239 EXPECT_NE(store, nullptr);
240 EXPECT_EQ(errCode, E_OK);
241 auto result = store->ExecuteGql(createGraphGql);
242 EXPECT_NE(result.second, nullptr);
243 EXPECT_EQ(result.first, E_OK);
244 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
245 EXPECT_EQ(result.first, E_OK);
246 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
247 ASSERT_EQ(result.first, E_OK);
248 ASSERT_NE(result.second, nullptr);
249 EXPECT_EQ(result.second->GetAllData().size(), 1);
250 GraphValue person = result.second->GetAllData()[0]["person"];
251 VerifyPersonInfo(person, "name_1", 11);
252 result.second = nullptr;
253 store->Close();
254 StoreManager::GetInstance().Clear();
255 // Unencryptdb to encryptdb
256 config.SetEncryptStatus(true);
257 store = GDBHelper::GetDBStore(config, errCode);
258 EXPECT_NE(store, nullptr);
259 EXPECT_EQ(errCode, E_OK);
260 if (store != nullptr) {
261 result = store->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
262 EXPECT_EQ(result.first, E_OK);
263 result = store->QueryGql("MATCH (person:Person {age: 22}) RETURN person;");
264 ASSERT_EQ(result.first, E_OK);
265 ASSERT_NE(result.second, nullptr);
266 EXPECT_EQ(result.second->GetAllData().size(), 1);
267 GraphValue person = result.second->GetAllData()[0]["person"];
268 VerifyPersonInfo(person, "name_2", 22);
269 store->Close();
270 }
271 GDBHelper::DeleteDBStore(config);
272 }
273
274 HWTEST_F(GdbEncryptTest, GdbEncrypt_EncryptToEncrypt, TestSize.Level1)
275 {
276 int errCode = E_OK;
277 std::string dbName = "EncryptToUnencrypt";
278 std::string dbPath = databasePath;
279 // encryptdb
280 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, true);
281 auto store = GDBHelper::GetDBStore(config, errCode);
282 EXPECT_NE(store, nullptr);
283 EXPECT_EQ(errCode, E_OK);
284 auto result = store->ExecuteGql(createGraphGql);
285 EXPECT_NE(result.second, nullptr);
286 EXPECT_EQ(result.first, E_OK);
287 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
288 EXPECT_EQ(result.first, E_OK);
289 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
290 ASSERT_EQ(result.first, E_OK);
291 ASSERT_NE(result.second, nullptr);
292 EXPECT_EQ(result.second->GetAllData().size(), 1);
293 GraphValue person = result.second->GetAllData()[0]["person"];
294 VerifyPersonInfo(person, "name_1", 11);
295 store->Close();
296 StoreManager::GetInstance().Clear();
297 // encryptdb to encryptdb
298 config.SetEncryptStatus(true);
299 store = GDBHelper::GetDBStore(config, errCode);
300 EXPECT_NE(store, nullptr);
301 EXPECT_EQ(errCode, E_OK);
302 result = store->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
303 EXPECT_EQ(result.first, E_OK);
304 result = store->QueryGql("MATCH (person:Person {age: 22}) RETURN person;");
305 ASSERT_EQ(result.first, E_OK);
306 ASSERT_NE(result.second, nullptr);
307 EXPECT_EQ(result.second->GetAllData().size(), 1);
308 person = result.second->GetAllData()[0]["person"];
309 VerifyPersonInfo(person, "name_2", 22);
310 store->Close();
311 GDBHelper::DeleteDBStore(config);
312 }
313
314 HWTEST_F(GdbEncryptTest, GdbEncrypt_UnencryptToUnencrypt, TestSize.Level1)
315 {
316 int errCode = E_OK;
317 std::string dbName = "UnencryptToEncrypt";
318 std::string dbPath = databasePath;
319 // Unencryptdb
320 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, false);
321 auto store = GDBHelper::GetDBStore(config, errCode);
322 EXPECT_NE(store, nullptr);
323 EXPECT_EQ(errCode, E_OK);
324 auto result = store->ExecuteGql(createGraphGql);
325 EXPECT_NE(result.second, nullptr);
326 EXPECT_EQ(result.first, E_OK);
327 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
328 EXPECT_EQ(result.first, E_OK);
329 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
330 ASSERT_EQ(result.first, E_OK);
331 ASSERT_NE(result.second, nullptr);
332 EXPECT_EQ(result.second->GetAllData().size(), 1);
333 GraphValue person = result.second->GetAllData()[0]["person"];
334 VerifyPersonInfo(person, "name_1", 11);
335 store->Close();
336 StoreManager::GetInstance().Clear();
337 // Unencryptdb to Unencryptdb
338 config.SetEncryptStatus(false);
339 store = GDBHelper::GetDBStore(config, errCode);
340 EXPECT_NE(store, nullptr);
341 EXPECT_EQ(errCode, E_OK);
342 result = store->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
343 EXPECT_EQ(result.first, E_OK);
344 result = store->QueryGql("MATCH (person:Person {age: 22}) RETURN person;");
345 ASSERT_EQ(result.first, E_OK);
346 ASSERT_NE(result.second, nullptr);
347 EXPECT_EQ(result.second->GetAllData().size(), 1);
348 person = result.second->GetAllData()[0]["person"];
349 VerifyPersonInfo(person, "name_2", 22);
350 store->Close();
351 GDBHelper::DeleteDBStore(config);
352 }
353
354 HWTEST_F(GdbEncryptTest, GdbEncrypt_DefaultToUnencrypt, TestSize.Level1)
355 {
356 int errCode = E_OK;
357 std::string dbName = "UnencryptToEncrypt";
358 std::string dbPath = databasePath;
359 // Unencryptdb
360 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH);
361 auto store = GDBHelper::GetDBStore(config, errCode);
362 EXPECT_NE(store, nullptr);
363 EXPECT_EQ(errCode, E_OK);
364 auto result = store->ExecuteGql(createGraphGql);
365 EXPECT_NE(result.second, nullptr);
366 EXPECT_EQ(result.first, E_OK);
367 result = store->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
368 EXPECT_EQ(result.first, E_OK);
369 result = store->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
370 ASSERT_EQ(result.first, E_OK);
371 ASSERT_NE(result.second, nullptr);
372 EXPECT_EQ(result.second->GetAllData().size(), 1);
373 GraphValue person = result.second->GetAllData()[0]["person"];
374 VerifyPersonInfo(person, "name_1", 11);
375 store->Close();
376 StoreManager::GetInstance().Clear();
377 // Unencryptdb to Unencryptdb
378 config.SetEncryptStatus(false);
379 store = GDBHelper::GetDBStore(config, errCode);
380 EXPECT_NE(store, nullptr);
381 EXPECT_EQ(errCode, E_OK);
382 result = store->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
383 EXPECT_EQ(result.first, E_OK);
384 result = store->QueryGql("MATCH (person:Person {age: 22}) RETURN person;");
385 ASSERT_EQ(result.first, E_OK);
386 ASSERT_NE(result.second, nullptr);
387 EXPECT_EQ(result.second->GetAllData().size(), 1);
388 person = result.second->GetAllData()[0]["person"];
389 VerifyPersonInfo(person, "name_2", 22);
390 store->Close();
391 GDBHelper::DeleteDBStore(config);
392 }
393
VerifyPersonInfo(const GraphValue & person,const std::string & name,const int32_t & age)394 void GdbEncryptTest::VerifyPersonInfo(const GraphValue &person, const std::string &name, const int32_t &age)
395 {
396 auto expectSize = 3;
397 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
398 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
399 EXPECT_EQ(personVertex->GetLabel(), "Person");
400 ASSERT_EQ(personVertex->GetProperties().size(), expectSize);
401
402 auto nameDb = personVertex->GetProperties().find("name");
403 ASSERT_NE(nameDb, personVertex->GetProperties().end());
404 ASSERT_TRUE(std::holds_alternative<std::string>(nameDb->second));
405 EXPECT_EQ(std::get<std::string>(nameDb->second), name);
406
407 auto ageDb = personVertex->GetProperties().find("age");
408 ASSERT_NE(ageDb, personVertex->GetProperties().end());
409 ASSERT_TRUE(std::holds_alternative<int64_t>(ageDb->second));
410 EXPECT_EQ(std::get<int64_t>(ageDb->second), age);
411
412 auto sex = personVertex->GetProperties().find("sex");
413 ASSERT_NE(sex, personVertex->GetProperties().end());
414 ASSERT_TRUE(std::holds_alternative<int64_t>(sex->second));
415 EXPECT_EQ(std::get<int64_t>(sex->second), 0);
416 }
417
418 HWTEST_F(GdbEncryptTest, GdbEncrypt_Config, TestSize.Level1)
419 {
420 std::string dbName = "UnencryptToEncrypt";
421 std::string dbPath = databasePath;
422 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH);
423 EXPECT_EQ(config.IsEncrypt(), false);
424 config.GenerateEncryptedKey();
425 EXPECT_EQ(config.GetEncryptKey().size(), 0);
426 EXPECT_EQ(config.GetNewEncryptKey().size(), 0);
427
428 config.SetBundleName("");
429 EXPECT_EQ(config.GetBundleName(), "");
430 config.SetEncryptStatus(true);
431 EXPECT_EQ(config.IsEncrypt(), true);
432 config.SetBundleName("test_name");
433 EXPECT_EQ(config.GetBundleName(), "test_name");
434 // empty return E_ERROR
435 EXPECT_EQ(config.SetBundleName(""), E_ERROR);
436 EXPECT_EQ(config.GetBundleName(), "test_name");
437
438 config.GenerateEncryptedKey();
439 // not exists bundleName, failed
440 EXPECT_EQ(config.GetEncryptKey().size(), 0);
441 EXPECT_EQ(config.GetNewEncryptKey().size(), 0);
442 config.ChangeEncryptKey();
443 config.GenerateEncryptedKey();
444 config.ChangeEncryptKey();
445 }