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 <gtest/gtest.h>
17 #include <unistd.h>
18 #include <cstddef>
19 #include <cstdint>
20 #include <vector>
21 #include "dev_manager.h"
22 #include "distributed_kv_data_manager.h"
23 #include "file_ex.h"
24 #include "types.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 namespace OHOS::Test {
29 class EndPointTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 static std::string GetKey(const std::string &key);
36 static std::shared_ptr<SingleKvStore> kvStore_; // declare kvstore instance.
37 static Status status_;
38 static std::string deviceId_;
39 static Options options_;
40 };
41
42 const std::string VALID_SCHEMA = "{\"SCHEMA_VERSION\":\"1.0\","
43 "\"SCHEMA_MODE\":\"STRICT\","
44 "\"SCHEMA_SKIPSIZE\":0,"
45 "\"SCHEMA_DEFINE\":{"
46 "\"age\":\"INTEGER, NOT NULL\""
47 "},"
48 "\"SCHEMA_INDEXES\":[\"$.age\"]}";
49
50 std::shared_ptr<SingleKvStore> EndPointTest::kvStore_ = nullptr;
51 Status EndPointTest::status_ = Status::ERROR;
52 std::string EndPointTest::deviceId_;
53 Options EndPointTest::options_;
54
SetUpTestCase(void)55 void EndPointTest::SetUpTestCase(void)
56 {
57 DistributedKvDataManager manager;
58 options_.area = EL1;
59 options_.baseDir = std::string("/data/service/el1/public/database/odmf");
60 options_.securityLevel = S1;
61 mkdir(options_.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
62 AppId appId = { "odmf" };
63 StoreId storeId = { "student_device" }; // define kvstore(database) name.
64 // [create and] open and initialize kvstore instance.
65 status_ = manager.GetSingleKvStore(options_, appId, storeId, kvStore_);
66 auto deviceInfo = DevManager::GetInstance().GetLocalDevice();
67 deviceId_ = deviceInfo.networkId;
68 }
69
TearDownTestCase(void)70 void EndPointTest::TearDownTestCase(void)
71 {
72 DistributedKvDataManager manager;
73 AppId appId = { "odmf" };
74 manager.DeleteAllKvStore(appId, options_.baseDir);
75 (void)remove("/data/service/el1/public/database/odmf/key");
76 (void)remove("/data/service/el1/public/database/odmf/kvdb");
77 (void)remove("/data/service/el1/public/database/odmf");
78 }
79
SetUp(void)80 void EndPointTest::SetUp(void)
81 {}
82
TearDown(void)83 void EndPointTest::TearDown(void)
84 {}
85
GetKey(const std::string & key)86 std::string EndPointTest::GetKey(const std::string& key)
87 {
88 std::ostringstream oss;
89 oss << std::setfill('0') << std::setw(sizeof(uint32_t)) << deviceId_.length();
90 oss << deviceId_ << std::string(key.begin(), key.end());
91 return oss.str();
92 }
93
94 class EndpointMock : public Endpoint {
95 public:
EndpointMock()96 EndpointMock() {}
~EndpointMock()97 virtual ~EndpointMock() {}
98
Start()99 Status Start() override
100 {
101 return Status::SUCCESS;
102 }
103
Stop()104 Status Stop() override
105 {
106 return Status::SUCCESS;
107 }
108
RegOnDataReceive(const RecvHandler & callback)109 Status RegOnDataReceive(const RecvHandler &callback) override
110 {
111 return Status::SUCCESS;
112 }
113
SendData(const std::string & dtsIdentifier,const uint8_t * data,uint32_t length)114 Status SendData(const std::string &dtsIdentifier, const uint8_t *data, uint32_t length) override
115 {
116 return Status::SUCCESS;
117 }
118
GetMtuSize(const std::string & identifier)119 uint32_t GetMtuSize(const std::string &identifier) override
120 {
121 return 1 * 1024 * 1024; // 1 * 1024 * 1024 Byte.
122 }
123
GetLocalDeviceInfos()124 std::string GetLocalDeviceInfos() override
125 {
126 return "Mock Device";
127 }
128
IsSaferThanDevice(int securityLevel,const std::string & devId)129 bool IsSaferThanDevice(int securityLevel, const std::string &devId) override
130 {
131 return true;
132 }
133
HasDataSyncPermission(const StoreBriefInfo & param,uint8_t flag)134 bool HasDataSyncPermission(const StoreBriefInfo ¶m, uint8_t flag) override
135 {
136 return true;
137 }
138 };
139
140 /**
141 * @tc.name: SetEndpoint001
142 * @tc.desc: test the SetEndpoint(std::shared_ptr<Endpoint> endpoint)
143 * @tc.type: FUNC
144 * @tc.require:
145 * @tc.author: SQL
146 */
147 HWTEST_F(EndPointTest, SetEndpoint001, TestSize.Level1)
148 {
149 DistributedKvDataManager manager;
150 std::shared_ptr<EndpointMock> endpoint = nullptr;
151 Status status = manager.SetEndpoint(endpoint);
152 ASSERT_EQ(status, Status::INVALID_ARGUMENT);
153 }
154
155 /**
156 * @tc.name: SetEndpoint002
157 * @tc.desc: test the SetEndpoint(std::shared_ptr<Endpoint> endpoint)
158 * @tc.type: FUNC
159 * @tc.require:
160 * @tc.author: SQL
161 */
162 HWTEST_F(EndPointTest, SetEndpoint002, TestSize.Level1)
163 {
164 DistributedKvDataManager manager;
165 std::shared_ptr<EndpointMock> endpoint = std::make_shared<EndpointMock>();
166 Status status = manager.SetEndpoint(endpoint);
167 EXPECT_EQ(status, Status::SUCCESS);
168 status = manager.SetEndpoint(endpoint);
169 EXPECT_EQ(status, Status::SUCCESS);
170 }
171
172 /**
173 * @tc.name: SetIdentifier001
174 * desc: test the Status set identifier function.
175 * type: FUNC
176 * require:
177 * author:SQL
178 */
179 HWTEST_F(EndPointTest, SetIdentifier001, TestSize.Level1)
180 {
181 DistributedKvDataManager manager;
182 EXPECT_NE(kvStore_, nullptr) << "kvStorePtr is null.";
183 AppId appId = { "odmf" };
184 StoreId storeId = { "test_storeid" };
185 std::vector<std::string> targetDev = {"devicid1", "devicid2"};
186 std::string accountId = "testAccount";
187 std::shared_ptr<EndpointMock> endpoint = std::make_shared<EndpointMock>();
188 Status status = manager.SetEndpoint(endpoint);
189 EXPECT_EQ(status, Status::SUCCESS);
190 auto testStatus = kvStore_->SetIdentifier(accountId, appId, storeId, targetDev);
191 EXPECT_EQ(testStatus, Status::SUCCESS);
192 }
193
194 /**
195 * @tc.name: SetIdentifier002
196 * desc: test the Status set identifier function.
197 * type: FUNC
198 * require:
199 * author:SQL
200 */
201 HWTEST_F(EndPointTest, SetIdentifier002, TestSize.Level1)
202 {
203 DistributedKvDataManager manager;
204 EXPECT_NE(kvStore_, nullptr) << "kvStorePtr is null.";
205 AppId appId = { "" };
206 StoreId storeId = { "" };
207 std::vector<std::string> targetDev = {"devicid1", "devicid2"};
208 std::string accountId = "testAccount";
209 std::shared_ptr<EndpointMock> endpoint = std::make_shared<EndpointMock>();
210 Status status = manager.SetEndpoint(endpoint);
211 EXPECT_EQ(status, Status::SUCCESS);
212 auto testStatus = kvStore_->SetIdentifier(accountId, appId, storeId, targetDev);
213 EXPECT_NE(testStatus, Status::SUCCESS);
214 }
215 } // namespace OHOS::Test