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 #define LOG_TAG "DistributedKvDataManagerEncryptTest"
16 #include <gtest/gtest.h>
17
18 #include "distributed_kv_data_manager.h"
19 #include "file_ex.h"
20 #include "kvstore_death_recipient.h"
21 #include "log_print.h"
22 #include "types.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::DistributedKv;
26
27 class DistributedKvDataManagerEncryptTest : public testing::Test {
28 public:
29 static DistributedKvDataManager manager;
30 static Options createEnc;
31
32 static UserId userId;
33
34 static AppId appId;
35 static StoreId storeId;
36
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39
40 static void RemoveAllStore(DistributedKvDataManager &manager);
41
42 void SetUp();
43 void TearDown();
44 DistributedKvDataManagerEncryptTest();
45 virtual ~DistributedKvDataManagerEncryptTest();
46 };
47
48 class MyDeathRecipient : public KvStoreDeathRecipient {
49 public:
MyDeathRecipient()50 MyDeathRecipient() {}
~MyDeathRecipient()51 virtual ~MyDeathRecipient() {}
OnRemoteDied()52 void OnRemoteDied() override {}
53 };
54
55 DistributedKvDataManager DistributedKvDataManagerEncryptTest::manager;
56 Options DistributedKvDataManagerEncryptTest::createEnc;
57
58 UserId DistributedKvDataManagerEncryptTest::userId;
59
60 AppId DistributedKvDataManagerEncryptTest::appId;
61 StoreId DistributedKvDataManagerEncryptTest::storeId;
62
RemoveAllStore(DistributedKvDataManager & manager)63 void DistributedKvDataManagerEncryptTest::RemoveAllStore(DistributedKvDataManager &manager)
64 {
65 manager.CloseAllKvStore(appId);
66 manager.DeleteKvStore(appId, storeId, createEnc.baseDir);
67 manager.DeleteAllKvStore(appId, createEnc.baseDir);
68 }
SetUpTestCase(void)69 void DistributedKvDataManagerEncryptTest::SetUpTestCase(void)
70 {
71 createEnc.createIfMissing = true;
72 createEnc.encrypt = true;
73 createEnc.securityLevel = S1;
74 createEnc.autoSync = true;
75 createEnc.kvStoreType = SINGLE_VERSION;
76
77 userId.userId = "account0";
78 appId.appId = "com.ohos.nb.service";
79
80 storeId.storeId = "EncryptStoreId";
81
82 createEnc.area = EL1;
83 createEnc.baseDir = std::string("/data/service/el1/public/database/") + appId.appId;
84 mkdir(createEnc.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
85 }
86
TearDownTestCase(void)87 void DistributedKvDataManagerEncryptTest::TearDownTestCase(void)
88 {
89 RemoveAllStore(manager);
90 (void)remove((createEnc.baseDir + "/kvdb").c_str());
91 (void)remove(createEnc.baseDir.c_str());
92 }
93
SetUp(void)94 void DistributedKvDataManagerEncryptTest::SetUp(void)
95 {}
96
DistributedKvDataManagerEncryptTest(void)97 DistributedKvDataManagerEncryptTest::DistributedKvDataManagerEncryptTest(void)
98 {}
99
~DistributedKvDataManagerEncryptTest(void)100 DistributedKvDataManagerEncryptTest::~DistributedKvDataManagerEncryptTest(void)
101 {}
102
TearDown(void)103 void DistributedKvDataManagerEncryptTest::TearDown(void)
104 {}
105
106 /**
107 * @tc.name: kvstore_ddm_createEncryptedStore_001
108 * @tc.desc: Create an encrypted KvStore.
109 * @tc.type: FUNC
110 * @tc.require: SR000D08K4 AR000D08KQ
111 * @tc.author: liqiao
112 */
113 HWTEST_F(DistributedKvDataManagerEncryptTest, kvstore_ddm_createEncryptedStore_001, TestSize.Level1)
114 {
115 ZLOGI("kvstore_ddm_createEncryptedStore_001 begin.");
116 std::shared_ptr<SingleKvStore> kvStore;
117 Status status = manager.GetSingleKvStore(createEnc, appId, storeId, kvStore);
118 ASSERT_EQ(status, Status::SUCCESS);
119 ASSERT_NE(kvStore, nullptr);
120
121 Key key = "age";
122 Value value = "18";
123 status = kvStore->Put(key, value);
124 EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data return wrong status";
125
126 // get value from kvstore.
127 Value valueRet;
128 Status statusRet = kvStore->Get(key, valueRet);
129 EXPECT_EQ(Status::SUCCESS, statusRet) << "get data return wrong status";
130
131 EXPECT_EQ(value, valueRet) << "value and valueRet are not equal";
132 }