1 /*
2 * Copyright (c) 2022 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 "distributed_data_storage_test.h"
17
18 #include <thread>
19 #include "distributed_sched_test_util.h"
20 #include "dtbschedmgr_device_info_storage.h"
21 #include "test_log.h"
22
23 namespace OHOS {
24 namespace DistributedSchedule {
25 using namespace std;
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::DistributedKv;
29 using namespace OHOS::DistributedHardware;
30 namespace {
31 constexpr int32_t TASK_ID_1 = 11;
32 constexpr int32_t TASK_ID_2 = 12;
33 constexpr size_t BYTESTREAM_LENGTH = 100;
34 constexpr uint8_t ONE_BYTE = '6';
35 }
36
SetUpTestCase()37 void DistributedDataStorageTest::SetUpTestCase()
38 {
39 DTEST_LOG << "DistributedDataStorageTest::SetUpTestCase" << std::endl;
40 if (!DistributedSchedUtil::LoadDistributedSchedService()) {
41 DTEST_LOG << "DMSMissionManagerTest::SetUpTestCase LoadDistributedSchedService failed" << std::endl;
42 }
43 const std::string pkgName = "DBinderBus_" + std::to_string(getprocpid());
44 std::shared_ptr<DmInitCallback> initCallback_ = std::make_shared<DeviceInitCallBack>();
45 DeviceManager::GetInstance().InitDeviceManager(pkgName, initCallback_);
46 }
47
TearDownTestCase()48 void DistributedDataStorageTest::TearDownTestCase()
49 {
50 DTEST_LOG << "DistributedDataStorageTest::TearDownTestCase" << std::endl;
51 }
52
SetUp()53 void DistributedDataStorageTest::SetUp()
54 {
55 DistributedSchedUtil::MockPermission();
56 distributedDataStorage_ = std::make_shared<DistributedDataStorage>();
57 DTEST_LOG << "DistributedDataStorageTest::SetUp" << std::endl;
58 }
59
TearDown()60 void DistributedDataStorageTest::TearDown()
61 {
62 DTEST_LOG << "DistributedDataStorageTest::TearDown" << std::endl;
63 }
64
OnRemoteDied()65 void DistributedDataStorageTest::DeviceInitCallBack::OnRemoteDied()
66 {
67 }
68
InitByteStream()69 uint8_t* DistributedDataStorageTest::InitByteStream()
70 {
71 uint8_t* byteStream = new uint8_t[BYTESTREAM_LENGTH];
72 for (size_t i = 0; i < BYTESTREAM_LENGTH; ++i) {
73 byteStream[i] = ONE_BYTE;
74 }
75 return byteStream;
76 }
77
GetLocalDeviceId() const78 std::string DistributedDataStorageTest::GetLocalDeviceId() const
79 {
80 std::string localDeviceId;
81 if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)) {
82 DTEST_LOG << "DistributedDataStorageTest::GetLocalDeviceId failed!" << std::endl;
83 }
84 return localDeviceId;
85 }
86
87 /**
88 * @tc.name: InitTest_001
89 * @tc.desc: test init DistributedDataStorage
90 * @tc.type: FUNC
91 */
92 HWTEST_F(DistributedDataStorageTest, InitTest_001, TestSize.Level0)
93 {
94 DTEST_LOG << "DistributedDataStorageTest InitTest_001 start" << std::endl;
95 ASSERT_NE(distributedDataStorage_, nullptr);
96 bool ret = distributedDataStorage_->Init();
97 EXPECT_EQ(true, ret);
98 this_thread::sleep_for(1s);
99 distributedDataStorage_->Stop();
100 DTEST_LOG << "DistributedDataStorageTest InitTest_001 end" << std::endl;
101 }
102
103 /**
104 * @tc.name: InsertTest_001
105 * @tc.desc: test insert DistributedDataStorage
106 * @tc.type: FUNC
107 */
108 HWTEST_F(DistributedDataStorageTest, InsertTest_001, TestSize.Level1)
109 {
110 DTEST_LOG << "DistributedDataStorageTest InsertTest_001 start" << std::endl;
111 ASSERT_NE(distributedDataStorage_, nullptr);
112 distributedDataStorage_->Init();
113 this_thread::sleep_for(1s);
114 std::string deviceId = GetLocalDeviceId();
115 uint8_t* byteStream = InitByteStream();
116 bool ret = distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
117 EXPECT_EQ(true, ret);
118 distributedDataStorage_->Stop();
119 DTEST_LOG << "DistributedDataStorageTest InsertTest_001 end" << std::endl;
120 }
121
122 /**
123 * @tc.name: InsertTest_002
124 * @tc.desc: test insert DistributedDataStorage
125 * @tc.type: FUNC
126 */
127 HWTEST_F(DistributedDataStorageTest, InsertTest_002, TestSize.Level1)
128 {
129 DTEST_LOG << "DistributedDataStorageTest InsertTest_002 start" << std::endl;
130 ASSERT_NE(distributedDataStorage_, nullptr);
131 distributedDataStorage_->Init();
132 this_thread::sleep_for(1s);
133 std::string deviceId;
134 uint8_t* byteStream = InitByteStream();
135 bool ret = distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
136 EXPECT_EQ(false, ret);
137 distributedDataStorage_->Stop();
138 DTEST_LOG << "DistributedDataStorageTest InsertTest_002 end" << std::endl;
139 }
140
141 /**
142 * @tc.name: InsertTest_003
143 * @tc.desc: test insert DistributedDataStorage
144 * @tc.type: FUNC
145 */
146 HWTEST_F(DistributedDataStorageTest, InsertTest_003, TestSize.Level1)
147 {
148 DTEST_LOG << "DistributedDataStorageTest InsertTest_003 start" << std::endl;
149 ASSERT_NE(distributedDataStorage_, nullptr);
150 distributedDataStorage_->Init();
151 this_thread::sleep_for(1s);
152 std::string deviceId = GetLocalDeviceId();
153 uint8_t* byteStream = InitByteStream();
154 bool ret = distributedDataStorage_->Insert(deviceId, -1, byteStream, BYTESTREAM_LENGTH);
155 EXPECT_EQ(false, ret);
156 distributedDataStorage_->Stop();
157 DTEST_LOG << "DistributedDataStorageTest InsertTest_003 end" << std::endl;
158 }
159
160 /**
161 * @tc.name: DeleteTest_001
162 * @tc.desc: test delete DistributedDataStorage
163 * @tc.type: FUNC
164 */
165 HWTEST_F(DistributedDataStorageTest, DeleteTest_001, TestSize.Level0)
166 {
167 DTEST_LOG << "DistributedDataStorageTest DeleteTest_001 start" << std::endl;
168 ASSERT_NE(distributedDataStorage_, nullptr);
169 distributedDataStorage_->Init();
170 this_thread::sleep_for(1s);
171 std::string deviceId = GetLocalDeviceId();
172 bool ret = distributedDataStorage_->Delete(deviceId, TASK_ID_1);
173 EXPECT_EQ(true, ret);
174 distributedDataStorage_->Stop();
175 DTEST_LOG << "DistributedDataStorageTest DeleteTest_001 end" << std::endl;
176 }
177
178 /**
179 * @tc.name: DeleteTest_002
180 * @tc.desc: test delete DistributedDataStorage
181 * @tc.type: FUNC
182 */
183 HWTEST_F(DistributedDataStorageTest, DeleteTest_002, TestSize.Level1)
184 {
185 DTEST_LOG << "DistributedDataStorageTest DeleteTest_002 start" << std::endl;
186 ASSERT_NE(distributedDataStorage_, nullptr);
187 distributedDataStorage_->Init();
188 this_thread::sleep_for(1s);
189 std::string deviceId = GetLocalDeviceId();
190 uint8_t* byteStream = InitByteStream();
191 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
192 bool ret = distributedDataStorage_->Delete(deviceId, TASK_ID_1);
193 EXPECT_EQ(true, ret);
194 distributedDataStorage_->Stop();
195 DTEST_LOG << "DistributedDataStorageTest DeleteTest_002 end" << std::endl;
196 }
197
198 /**
199 * @tc.name: DeleteTest_003
200 * @tc.desc: test delete DistributedDataStorage
201 * @tc.type: FUNC
202 */
203 HWTEST_F(DistributedDataStorageTest, DeleteTest_003, TestSize.Level1)
204 {
205 DTEST_LOG << "DistributedDataStorageTest DeleteTest_003 start" << std::endl;
206 ASSERT_NE(distributedDataStorage_, nullptr);
207 distributedDataStorage_->Init();
208 this_thread::sleep_for(1s);
209 std::string networkId;
210 int32_t missionId = 0;
211 bool ret = distributedDataStorage_->Delete(networkId, missionId);
212 EXPECT_EQ(false, ret);
213 distributedDataStorage_->Stop();
214 DTEST_LOG << "DistributedDataStorageTest DeleteTest_003 end" << std::endl;
215 }
216
217 /**
218 * @tc.name: DeleteTest_004
219 * @tc.desc: test delete DistributedDataStorage
220 * @tc.type: FUNC
221 */
222 HWTEST_F(DistributedDataStorageTest, DeleteTest_004, TestSize.Level1)
223 {
224 DTEST_LOG << "DistributedDataStorageTest DeleteTest_004 start" << std::endl;
225 ASSERT_NE(distributedDataStorage_, nullptr);
226 distributedDataStorage_->Init();
227 this_thread::sleep_for(1s);
228 std::string networkId = GetLocalDeviceId();
229 int32_t missionId = -1;
230 bool ret = distributedDataStorage_->Delete(networkId, missionId);
231 EXPECT_EQ(false, ret);
232 distributedDataStorage_->Stop();
233 DTEST_LOG << "DistributedDataStorageTest DeleteTest_004 end" << std::endl;
234 }
235
236 /**
237 * @tc.name: QueryTest_001
238 * @tc.desc: test query DistributedDataStorage
239 * @tc.type: FUNC
240 */
241 HWTEST_F(DistributedDataStorageTest, QueryTest_001, TestSize.Level0)
242 {
243 DTEST_LOG << "DistributedDataStorageTest QueryTest_001 start" << std::endl;
244 ASSERT_NE(distributedDataStorage_, nullptr);
245 distributedDataStorage_->Init();
246 this_thread::sleep_for(1s);
247 std::string deviceId = GetLocalDeviceId();
248 Value value;
249 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
250 EXPECT_EQ(false, ret);
251 distributedDataStorage_->Stop();
252 DTEST_LOG << "DistributedDataStorageTest QueryTest_001 end" << std::endl;
253 }
254
255 /**
256 * @tc.name: QueryTest_002
257 * @tc.desc: test query DistributedDataStorage
258 * @tc.type: FUNC
259 */
260 HWTEST_F(DistributedDataStorageTest, QueryTest_002, TestSize.Level1)
261 {
262 DTEST_LOG << "DistributedDataStorageTest QueryTest_002 start" << std::endl;
263 ASSERT_NE(distributedDataStorage_, nullptr);
264 distributedDataStorage_->Init();
265 this_thread::sleep_for(1s);
266 std::string deviceId = GetLocalDeviceId();
267 uint8_t* byteStream = InitByteStream();
268 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
269 Value value;
270 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
271 EXPECT_EQ(true, ret);
272 distributedDataStorage_->Stop();
273 DTEST_LOG << "DistributedDataStorageTest QueryTest_002 end" << std::endl;
274 }
275
276 /**
277 * @tc.name: QueryTest_003
278 * @tc.desc: test query DistributedDataStorage
279 * @tc.type: FUNC
280 */
281 HWTEST_F(DistributedDataStorageTest, QueryTest_003, TestSize.Level1)
282 {
283 DTEST_LOG << "DistributedDataStorageTest QueryTest_003 start" << std::endl;
284 ASSERT_NE(distributedDataStorage_, nullptr);
285 distributedDataStorage_->Init();
286 this_thread::sleep_for(1s);
287 std::string deviceId = GetLocalDeviceId();
288 uint8_t* byteStream = InitByteStream();
289 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
290 distributedDataStorage_->Delete(deviceId, TASK_ID_1);
291 Value value;
292 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
293 EXPECT_EQ(false, ret);
294 distributedDataStorage_->Stop();
295 DTEST_LOG << "DistributedDataStorageTest QueryTest_003 end" << std::endl;
296 }
297
298 /**
299 * @tc.name: QueryTest_004
300 * @tc.desc: test query DistributedDataStorage
301 * @tc.type: FUNC
302 */
303 HWTEST_F(DistributedDataStorageTest, QueryTest_004, TestSize.Level1)
304 {
305 DTEST_LOG << "DistributedDataStorageTest QueryTest_004 start" << std::endl;
306 ASSERT_NE(distributedDataStorage_, nullptr);
307 distributedDataStorage_->Init();
308 this_thread::sleep_for(1s);
309 std::string deviceId = GetLocalDeviceId();
310 uint8_t* byteStream = InitByteStream();
311 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
312 distributedDataStorage_->Insert(deviceId, TASK_ID_2, byteStream, BYTESTREAM_LENGTH);
313 distributedDataStorage_->Delete(deviceId, TASK_ID_1);
314 Value value;
315 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
316 EXPECT_EQ(false, ret);
317 ret = distributedDataStorage_->Query(deviceId, TASK_ID_2, value);
318 EXPECT_EQ(true, ret);
319 distributedDataStorage_->Stop();
320 DTEST_LOG << "DistributedDataStorageTest QueryTest_004 end" << std::endl;
321 }
322
323 /**
324 * @tc.name: QueryTest_005
325 * @tc.desc: test query DistributedDataStorage
326 * @tc.type: FUNC
327 */
328 HWTEST_F(DistributedDataStorageTest, QueryTest_005, TestSize.Level1)
329 {
330 DTEST_LOG << "DistributedDataStorageTest QueryTest_005 start" << std::endl;
331 ASSERT_NE(distributedDataStorage_, nullptr);
332 distributedDataStorage_->Init();
333 this_thread::sleep_for(1s);
334 std::string deviceId = GetLocalDeviceId();
335 uint8_t* byteStream = InitByteStream();
336 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
337 distributedDataStorage_->Insert(deviceId, TASK_ID_2, byteStream, BYTESTREAM_LENGTH);
338 distributedDataStorage_->FuzzyDelete(deviceId);
339 Value value;
340 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
341 EXPECT_EQ(true, ret);
342 ret = distributedDataStorage_->Query(deviceId, TASK_ID_2, value);
343 EXPECT_EQ(true, ret);
344 distributedDataStorage_->Stop();
345 DTEST_LOG << "DistributedDataStorageTest QueryTest_005 end" << std::endl;
346 }
347
348 /**
349 * @tc.name: QueryTest_006
350 * @tc.desc: test query DistributedDataStorage
351 * @tc.type: FUNC
352 */
353 HWTEST_F(DistributedDataStorageTest, QueryTest_006, TestSize.Level1)
354 {
355 DTEST_LOG << "DistributedDataStorageTest QueryTest_006 start" << std::endl;
356 ASSERT_NE(distributedDataStorage_, nullptr);
357 distributedDataStorage_->Init();
358 this_thread::sleep_for(1s);
359 std::string deviceId;
360 Value value;
361 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
362 EXPECT_EQ(false, ret);
363 distributedDataStorage_->Stop();
364 DTEST_LOG << "DistributedDataStorageTest QueryTest_006 end" << std::endl;
365 }
366
367 /**
368 * @tc.name: QueryTest_007
369 * @tc.desc: test query DistributedDataStorage
370 * @tc.type: FUNC
371 */
372 HWTEST_F(DistributedDataStorageTest, QueryTest_007, TestSize.Level1)
373 {
374 DTEST_LOG << "DistributedDataStorageTest QueryTest_007 start" << std::endl;
375 ASSERT_NE(distributedDataStorage_, nullptr);
376 distributedDataStorage_->Init();
377 this_thread::sleep_for(1s);
378 std::string deviceId = GetLocalDeviceId();
379 Value value;
380 bool ret = distributedDataStorage_->Query(deviceId, -1, value);
381 EXPECT_EQ(false, ret);
382 distributedDataStorage_->Stop();
383 DTEST_LOG << "DistributedDataStorageTest QueryTest_007 end" << std::endl;
384 }
385 } // namespace DistributedSchedule
386 } // namespace OHOS