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_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(getpid());
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 bool ret = distributedDataStorage_->Init();
96 EXPECT_EQ(true, ret);
97 this_thread::sleep_for(1s);
98 distributedDataStorage_->Stop();
99 DTEST_LOG << "DistributedDataStorageTest InitTest_001 end" << std::endl;
100 }
101
102 /**
103 * @tc.name: InsertTest_001
104 * @tc.desc: test insert DistributedDataStorage
105 * @tc.type: FUNC
106 */
107 HWTEST_F(DistributedDataStorageTest, InsertTest_001, TestSize.Level1)
108 {
109 DTEST_LOG << "DistributedDataStorageTest InsertTest_001 start" << std::endl;
110 distributedDataStorage_->Init();
111 this_thread::sleep_for(1s);
112 std::string deviceId = GetLocalDeviceId();
113 uint8_t* byteStream = InitByteStream();
114 bool ret = distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
115 EXPECT_EQ(true, ret);
116 distributedDataStorage_->Stop();
117 DTEST_LOG << "DistributedDataStorageTest InsertTest_001 end" << std::endl;
118 }
119
120 /**
121 * @tc.name: DeleteTest_001
122 * @tc.desc: test delete DistributedDataStorage
123 * @tc.type: FUNC
124 */
125 HWTEST_F(DistributedDataStorageTest, DeleteTest_001, TestSize.Level0)
126 {
127 DTEST_LOG << "DistributedDataStorageTest DeleteTest_001 start" << std::endl;
128 distributedDataStorage_->Init();
129 this_thread::sleep_for(1s);
130 std::string deviceId = GetLocalDeviceId();
131 bool ret = distributedDataStorage_->Delete(deviceId, TASK_ID_1);
132 EXPECT_EQ(true, ret);
133 distributedDataStorage_->Stop();
134 DTEST_LOG << "DistributedDataStorageTest DeleteTest_001 end" << std::endl;
135 }
136
137 /**
138 * @tc.name: DeleteTest_002
139 * @tc.desc: test delete DistributedDataStorage
140 * @tc.type: FUNC
141 */
142 HWTEST_F(DistributedDataStorageTest, DeleteTest_002, TestSize.Level1)
143 {
144 DTEST_LOG << "DistributedDataStorageTest DeleteTest_002 start" << std::endl;
145 distributedDataStorage_->Init();
146 this_thread::sleep_for(1s);
147 std::string deviceId = GetLocalDeviceId();
148 uint8_t* byteStream = InitByteStream();
149 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
150 bool ret = distributedDataStorage_->Delete(deviceId, TASK_ID_1);
151 EXPECT_EQ(true, ret);
152 distributedDataStorage_->Stop();
153 DTEST_LOG << "DistributedDataStorageTest DeleteTest_002 end" << std::endl;
154 }
155
156 /**
157 * @tc.name: QueryTest_001
158 * @tc.desc: test query DistributedDataStorage
159 * @tc.type: FUNC
160 */
161 HWTEST_F(DistributedDataStorageTest, QueryTest_001, TestSize.Level0)
162 {
163 DTEST_LOG << "DistributedDataStorageTest QueryTest_001 start" << std::endl;
164 distributedDataStorage_->Init();
165 this_thread::sleep_for(1s);
166 std::string deviceId = GetLocalDeviceId();
167 Value value;
168 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
169 EXPECT_EQ(false, ret);
170 distributedDataStorage_->Stop();
171 DTEST_LOG << "DistributedDataStorageTest QueryTest_001 end" << std::endl;
172 }
173
174 /**
175 * @tc.name: QueryTest_002
176 * @tc.desc: test query DistributedDataStorage
177 * @tc.type: FUNC
178 */
179 HWTEST_F(DistributedDataStorageTest, QueryTest_002, TestSize.Level1)
180 {
181 DTEST_LOG << "DistributedDataStorageTest QueryTest_002 start" << std::endl;
182 distributedDataStorage_->Init();
183 this_thread::sleep_for(1s);
184 std::string deviceId = GetLocalDeviceId();
185 uint8_t* byteStream = InitByteStream();
186 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
187 Value value;
188 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
189 EXPECT_EQ(true, ret);
190 distributedDataStorage_->Stop();
191 DTEST_LOG << "DistributedDataStorageTest QueryTest_002 end" << std::endl;
192 }
193
194 /**
195 * @tc.name: QueryTest_003
196 * @tc.desc: test query DistributedDataStorage
197 * @tc.type: FUNC
198 */
199 HWTEST_F(DistributedDataStorageTest, QueryTest_003, TestSize.Level1)
200 {
201 DTEST_LOG << "DistributedDataStorageTest QueryTest_003 start" << std::endl;
202 distributedDataStorage_->Init();
203 this_thread::sleep_for(1s);
204 std::string deviceId = GetLocalDeviceId();
205 uint8_t* byteStream = InitByteStream();
206 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
207 distributedDataStorage_->Delete(deviceId, TASK_ID_1);
208 Value value;
209 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
210 EXPECT_EQ(false, ret);
211 distributedDataStorage_->Stop();
212 DTEST_LOG << "DistributedDataStorageTest QueryTest_003 end" << std::endl;
213 }
214
215 /**
216 * @tc.name: QueryTest_004
217 * @tc.desc: test query DistributedDataStorage
218 * @tc.type: FUNC
219 */
220 HWTEST_F(DistributedDataStorageTest, QueryTest_004, TestSize.Level1)
221 {
222 DTEST_LOG << "DistributedDataStorageTest QueryTest_004 start" << std::endl;
223 distributedDataStorage_->Init();
224 this_thread::sleep_for(1s);
225 std::string deviceId = GetLocalDeviceId();
226 uint8_t* byteStream = InitByteStream();
227 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
228 distributedDataStorage_->Insert(deviceId, TASK_ID_2, byteStream, BYTESTREAM_LENGTH);
229 distributedDataStorage_->Delete(deviceId, TASK_ID_1);
230 Value value;
231 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
232 EXPECT_EQ(false, ret);
233 ret = distributedDataStorage_->Query(deviceId, TASK_ID_2, value);
234 EXPECT_EQ(true, ret);
235 distributedDataStorage_->Stop();
236 DTEST_LOG << "DistributedDataStorageTest QueryTest_004 end" << std::endl;
237 }
238
239 /**
240 * @tc.name: QueryTest_005
241 * @tc.desc: test query DistributedDataStorage
242 * @tc.type: FUNC
243 */
244 HWTEST_F(DistributedDataStorageTest, QueryTest_005, TestSize.Level1)
245 {
246 DTEST_LOG << "DistributedDataStorageTest QueryTest_005 start" << std::endl;
247 distributedDataStorage_->Init();
248 this_thread::sleep_for(1s);
249 std::string deviceId = GetLocalDeviceId();
250 uint8_t* byteStream = InitByteStream();
251 distributedDataStorage_->Insert(deviceId, TASK_ID_1, byteStream, BYTESTREAM_LENGTH);
252 distributedDataStorage_->Insert(deviceId, TASK_ID_2, byteStream, BYTESTREAM_LENGTH);
253 distributedDataStorage_->FuzzyDelete(deviceId);
254 Value value;
255 bool ret = distributedDataStorage_->Query(deviceId, TASK_ID_1, value);
256 EXPECT_EQ(true, ret);
257 ret = distributedDataStorage_->Query(deviceId, TASK_ID_2, value);
258 EXPECT_EQ(true, ret);
259 distributedDataStorage_->Stop();
260 DTEST_LOG << "DistributedDataStorageTest QueryTest_005 end" << std::endl;
261 }
262 } // namespace DistributedSchedule
263 } // namespace OHOS