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 OHOS::SaveStringToFile("/sys/fs/selinux/enforce", "0");
72 createEnc.createIfMissing = true;
73 createEnc.encrypt = true;
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 OHOS::SaveStringToFile("/sys/fs/selinux/enforce", "1");
90 RemoveAllStore(manager);
91 (void)remove((createEnc.baseDir + "/kvdb").c_str());
92 (void)remove(createEnc.baseDir.c_str());
93 }
94
SetUp(void)95 void DistributedKvDataManagerEncryptTest::SetUp(void)
96 {}
97
DistributedKvDataManagerEncryptTest(void)98 DistributedKvDataManagerEncryptTest::DistributedKvDataManagerEncryptTest(void)
99 {}
100
~DistributedKvDataManagerEncryptTest(void)101 DistributedKvDataManagerEncryptTest::~DistributedKvDataManagerEncryptTest(void)
102 {}
103
TearDown(void)104 void DistributedKvDataManagerEncryptTest::TearDown(void)
105 {}
106
107 /**
108 * @tc.name: kvstore_ddm_createEncryptedStore_001
109 * @tc.desc: Create an encrypted KvStore.
110 * @tc.type: FUNC
111 * @tc.require: SR000D08K4 AR000D08KQ
112 * @tc.author: liqiao
113 */
114 HWTEST_F(DistributedKvDataManagerEncryptTest, kvstore_ddm_createEncryptedStore_001, TestSize.Level1)
115 {
116 ZLOGI("kvstore_ddm_createEncryptedStore_001 begin.");
117 std::shared_ptr<SingleKvStore> kvStore;
118 Status status = manager.GetSingleKvStore(createEnc, appId, storeId, kvStore);
119 ASSERT_EQ(status, Status::SUCCESS);
120 ASSERT_NE(kvStore, nullptr);
121
122 Key key = "age";
123 Value value = "18";
124 status = kvStore->Put(key, value);
125 EXPECT_EQ(Status::SUCCESS, status) << "KvStore put data return wrong status";
126
127 // get value from kvstore.
128 Value valueRet;
129 Status statusRet = kvStore->Get(key, valueRet);
130 EXPECT_EQ(Status::SUCCESS, statusRet) << "get data return wrong status";
131
132 EXPECT_EQ(value, valueRet) << "value and valueRet are not equal";
133 }
134