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 <cmath>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 #include <dirent.h>
21 #include <iostream>
22 #include <securec.h>
23 #include <sstream> // 使用stringstream
24 #include <string>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <vector>
29
30 #include "cstring"
31 #include "directory_ex.h"
32 #include "distributed_kv_data_manager.h"
33 #include "types.h"
34 #include "process_msg.h"
35
36 using namespace OHOS::DistributedKv;
37 using namespace OHOS;
38 using namespace std;
39 const char LOGSTR[20] = "LOG_PROCESS::----";
40 const double DEFDELTA = 0.00001;
41 const int MAX_DATA_LENGTH = 1024;
42 const int NUMTHREE = 3;
43 const int STR_VALUE = 23;
44 const int RESULR_TWO = 2;
45 const int RESULT_OK = 0;
46 const int RESULT_ERR = 1;
47 namespace DisKvTest {
48 DistributedKvDataManager manager;
49 std::shared_ptr<SingleKvStore> KvStorePtr;
50 Status statusGetKvStore;
51 Status statusCloseKvStore;
52 Status statusDeleteKvStore;
53 Options create;
54 UserId userId;
55 AppId appId;
56 StoreId storeId;
57 }; // namespace DisKvTest
58
initKvstoreId(void)59 void initKvstoreId(void)
60 {
61 DisKvTest::KvStorePtr = nullptr; // declare kvstore instance.
62 DisKvTest::statusGetKvStore = Status::ERROR;
63 DisKvTest::statusCloseKvStore = Status::ERROR;
64 DisKvTest::statusDeleteKvStore = Status::ERROR;
65
66 DisKvTest::userId.userId = "account0";
67 DisKvTest::appId.appId = "com.ohos.kvdatamanager3.test";
68 DisKvTest::storeId.storeId = "test3";
69 DisKvTest::create.createIfMissing = true;
70 DisKvTest::create.encrypt = false;
71 DisKvTest::create.autoSync = false;
72 DisKvTest::create.kvStoreType = KvStoreType::SINGLE_VERSION;
73 DisKvTest::create.area = EL1;
74 DisKvTest::create.baseDir = std::string("/data/service/el1/public/database/") + DisKvTest::appId.appId;
75
76 if (mkdir(DisKvTest::create.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0 && errno != EEXIST) {
77 LOG("mkdir errno:%d, path:%s", errno, DisKvTest::create.baseDir.c_str());
78 } else {
79 LOG("%s SUCCESS: mkdir", LOGSTR);
80 }
81
82 DisKvTest::statusGetKvStore = DisKvTest::manager.GetSingleKvStore(
83 DisKvTest::create, DisKvTest::appId, DisKvTest::storeId, DisKvTest::KvStorePtr);
84 LOG("%s GetSingleKvStore , status=%d ", LOGSTR, DisKvTest::statusGetKvStore);
85
86 if (DisKvTest::KvStorePtr == nullptr) {
87 std::cout << "ERR:DisKvTest::KvStorePtr == nullptr " << std::endl;
88 } else {
89 std::cout << "SUCC:DisKvTest::KvStorePtr " << std::endl;
90 }
91 OHOS::ChangeModeDirectory(DisKvTest::create.baseDir.c_str(), 0777);
92 }
93
getRealData(char * str,char * delims)94 char* getRealData(char* str, char* delims)
95 {
96 if (str == nullptr || delims == nullptr) {
97 return nullptr;
98 }
99 char* result = strtok(str, delims);
100 char* second = nullptr;
101 while (result != nullptr) {
102 second = result;
103 result = strtok(nullptr, delims);
104 }
105 return second;
106 }
107
getParam(char * putData,char ret[][1024])108 void getParam(char* putData, char ret[][1024])
109 {
110 char str[MAX_DATA_LENGTH] = { 0 };
111 if (putData == nullptr) {
112 return;
113 }
114 strcpy_s(str, strlen(putData) + 1, putData);
115 char delims[2] = { ":" };
116 char* result = strtok(str, delims);
117 int i = 0;
118 while (result != nullptr) {
119 printf("result is \"%s\"\n", result);
120 strcpy_s(ret[i], strlen(result) + 1, result);
121 result = strtok(nullptr, delims);
122 i++;
123 if (i == NUMTHREE) {
124 return;
125 }
126 }
127 return;
128 }
129
ProcessSoftBus(int code,char * recvData)130 int ProcessSoftBus(int code, char* recvData)
131 {
132 return RESULT_OK;
133 }
134
ProcessDataMgr(int code,char * recvData)135 int ProcessDataMgr(int code, char* recvData)
136 {
137 LOG("ProcessDataMgr, begin");
138
139 initKvstoreId();
140
141 LOG("%s GetSingleKvStore, status=%d", LOGSTR, DisKvTest::statusGetKvStore);
142
143 if (DisKvTest::KvStorePtr == nullptr) {
144 std::cout << "ERR:DisKvTest::KvStorePtr == nullptr" << std::endl;
145 return RESULT_ERR;
146 }
147 if (Status::SUCCESS != DisKvTest::statusGetKvStore) {
148 std::cout << "ERR:statusGetKvStore" << std::endl;
149 return RESULT_ERR;
150 }
151
152 switch (code) {
153 case CTRL_CODE_DATAMGR_GET_DATA:
154 return processGetData(recvData);
155 case CTRL_CODE_DATAMGR_GET_DATA_REPLY:
156 return RESULT_OK;
157 case CTRL_CODE_DATAMGR_CREATE_KV:
158 return processCreateKv(recvData);
159 case CTRL_CODE_DATAMGR_DELETE_KV:
160 return processDeleteKv(recvData);
161 case CTRL_CODE_DATAMGR_PUT_DATA:
162 return processPutData(recvData);
163 case CTRL_CODE_DATAMGR_DELETE_DATA:
164 return processDeletetData(recvData);
165 default:
166 break;
167 }
168 return -1;
169 }
170
processDeletetData(char * putData)171 int processDeletetData(char* putData)
172 {
173 LOG("LOGdisDataTest---processDeletetData, begin");
174 // 解析远端发来的数据 result[0]=code result[1]=key result[RESULR_TWO]=value
175 char result[NUMTHREE][MAX_DATA_LENGTH] = { { 0 }, { 0 }, { 0 } };
176 memset_s(result, NUMTHREE * MAX_DATA_LENGTH, 0, NUMTHREE * MAX_DATA_LENGTH);
177 getParam(putData, result);
178 for (int i = 0; i < NUMTHREE; i++) {
179 LOG("LOGdisDataTest---processGetData %s", result[i]);
180 }
181
182 // put data删除数据
183 Value valueInt;
184 Key keyInt = result[1];
185 Status status = DisKvTest::KvStorePtr->Delete(keyInt);
186 if (Status::SUCCESS == status) {
187 LOG("LOGdisDataTest---Delete:RESULT_OK");
188 return RESULT_OK;
189 }
190 LOG("LOGdisDataTest---Delete:RESULT_ERR");
191 return RESULT_ERR;
192 }
193
processPutData(char * putData)194 int processPutData(char* putData)
195 {
196 LOG("LOGdisDataTest-processPutData, begin");
197 // 解析远端发来的数据 result[0]=code result[1]=key result[RESULR_TWO]=value
198 char result[NUMTHREE][MAX_DATA_LENGTH] = { { 0 }, { 0 }, { 0 } };
199 memset_s(result, NUMTHREE * MAX_DATA_LENGTH, 0, NUMTHREE * MAX_DATA_LENGTH);
200 getParam(putData, result);
201 for (int i = 0; i < NUMTHREE; i++) {
202 LOG("LOGdisDataTest-processPutData %s", result[i]);
203 }
204
205 // put data修改数据
206 Value valueInt;
207 Key keyInt = result[1];
208 if (strcmp(result[1], "math_score_int") == 0) {
209 int numInt = atoi(result[RESULR_TWO]);
210 valueInt = Value(TransferTypeToByteArray<int>(numInt));
211 } else if (strcmp(result[1], "math_score_float") == 0) {
212 float f2 = atof(result[RESULR_TWO]);
213 valueInt = Value(TransferTypeToByteArray<float>(f2));
214 } else if (strcmp(result[1], "math_score_double") == 0) {
215 double f2 = atof(result[RESULR_TWO]);
216 valueInt = Value(TransferTypeToByteArray<double>(f2));
217 } else if (strcmp(result[1], "math_score_int64_t") == 0) {
218 int64_t numInt = atoi(result[RESULR_TWO]);
219 valueInt = Value(TransferTypeToByteArray<int64_t>(numInt));
220 } else if (strcmp(result[1], "math_score_size_t") == 0) {
221 size_t numSize = atoi(result[RESULR_TWO]);
222 valueInt = Value(TransferTypeToByteArray<size_t>(numSize));
223 }
224
225 else if (strcmp(result[1], "math_score_string") == 0) {
226 // result[RESULR_TWO]解析的不完整
227 string ss;
228 string ss2;
229 ss = putData;
230 ss2 = ss.substr(STR_VALUE);
231 cout << "LOGdisDataTest2--ss = " << ss << endl;
232 cout << "LOGdisDataTest2--ss2 = " << ss2 << endl;
233 LOG("LOGdisDataTest2--ss2.c_str() = %s", ss2.c_str());
234 valueInt = Value(ss2);
235
236 } else if (strcmp(result[1], "math_score_vector") == 0) {
237 std::string str = result[RESULR_TWO];
238 std::vector<uint8_t> vect;
239 vect.assign(str.begin(), str.end());
240 valueInt = Value(vect);
241 }
242 Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt);
243 if (Status::SUCCESS == status) {
244 LOG("LOGdisDataTest2--putData:RESULT_OK");
245 return RESULT_OK;
246 }
247 LOG("LOGdisDataTest2--putData:RESULT_ERR");
248 return RESULT_ERR;
249 }
250
processGetData(char * putData)251 int processGetData(char* putData)
252 {
253 LOG("LOGdisDataTest-processGetData, begin");
254
255 // 解析远端发来的数据 result[0]=code result[1]=key result[RESULR_TWO]=value
256 char result[NUMTHREE][MAX_DATA_LENGTH] = { { 0 }, { 0 }, { 0 } };
257 memset_s(result, NUMTHREE * MAX_DATA_LENGTH, 0, NUMTHREE * MAX_DATA_LENGTH);
258 getParam(putData, result);
259 for (int i = 0; i < NUMTHREE; i++) {
260 LOG("for result[i] %s", result[i]);
261 }
262
263 // 获取到本端数据
264 Value valueRetInt;
265 Key keyInt = result[1];
266 Status status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt);
267 if (Status::SUCCESS != status) {
268 LOG("LOGdisDataTest--ERROR: Get(keyInt, valueRetInt)");
269 return RESULT_ERR;
270 }
271
272 std::string ret = valueRetInt.ToString();
273 LOG("LOGdisDataTest--result[RESULR_TWO]= %s", result[RESULR_TWO]);
274 LOG("LOGdisDataTest--ret.c_str() = %s", ret.c_str());
275
276 if (strcmp(result[1], "math_score_int") == 0) {
277 LOG("LOGdisDataTest--math_score_int ");
278 int iaaa = TransferByteArrayToType<int>(valueRetInt.Data());
279 int i2 = atoi(result[RESULR_TWO]);
280 LOG("LOGdisDataTest--iaaa= %d", iaaa);
281 LOG("LOGdisDataTest--i2 = %d", i2);
282 if (iaaa == i2) {
283 return RESULT_OK;
284 }
285
286 } else if (strcmp(result[1], "math_score_float") == 0) {
287 LOG("LOGdisDataTest--math_score_float ");
288 float faaa = TransferByteArrayToType<float>(valueRetInt.Data());
289 float f2 = atof(result[RESULR_TWO]);
290 float fdelta = f2 - faaa;
291 LOG("LOGdisDataTest--faaa= %f", faaa);
292 LOG("LOGdisDataTest--f2 = %f", f2);
293 LOG("LOGdisDataTest--fdelta = %f", fdelta);
294 if (std::fabs(fdelta) <= DEFDELTA) {
295 return RESULT_OK;
296 }
297 } else if (strcmp(result[1], "math_score_double") == 0) {
298 LOG("LOGdisDataTest--math_score_double ");
299 double daaa = TransferByteArrayToType<double>(valueRetInt.Data());
300 double d2 = atof(result[RESULR_TWO]);
301 double delta = d2 - daaa;
302 LOG("LOGdisDataTest--daaa= %f", daaa);
303 LOG("LOGdisDataTest--d2 = %f", d2);
304 LOG("LOGdisDataTest--delta = %f", delta);
305 if (std::fabs(delta) <= DEFDELTA) {
306 return RESULT_OK;
307 }
308 } else if (strcmp(result[1], "math_score_int64_t") == 0) {
309 LOG("LOGdisDataTest--math_score_int64_t ");
310 int64_t iaaa_t = TransferByteArrayToType<int64_t>(valueRetInt.Data());
311 int64_t i2_t = atoi(result[RESULR_TWO]);
312 if (iaaa_t == i2_t) {
313 return RESULT_OK;
314 }
315 } else if (strcmp(result[1], "math_score_size_t") == 0) {
316 LOG("LOGdisDataTest--math_score_size_t ");
317 size_t sizeaaa = TransferByteArrayToType<size_t>(valueRetInt.Data());
318 size_t size2 = atoi(result[RESULR_TWO]);
319 if (sizeaaa == size2) {
320 return RESULT_OK;
321 }
322 } else if (strcmp(result[1], "math_score_string") == 0) {
323 // result[RESULR_TWO]解析的不完整
324 string ss;
325 string ss2;
326 ss = putData;
327 ss2 = ss.substr(STR_VALUE);
328 cout << "LOGdisDataTest--ss = " << ss << endl;
329 cout << "LOGdisDataTest--ss2 = " << ss2 << endl;
330 LOG("LOGdisDataTest--ss2.c_str() = %s", ss2.c_str());
331 LOG("LOGdisDataTest--ret.c_str() = %s", ret.c_str());
332 if (strcmp(ss2.c_str(), ret.c_str()) == 0) {
333 return RESULT_OK;
334 }
335 } else if (strcmp(result[1], "math_score_vector") == 0) {
336 LOG("LOGdisDataTest--result[RESULR_TWO]= %s", result[RESULR_TWO]);
337 LOG("LOGdisDataTest--ret.c_str() = %s", ret.c_str());
338 if (strcmp(result[RESULR_TWO], ret.c_str()) == 0) {
339 return RESULT_OK;
340 }
341 }
342 return RESULT_ERR;
343 }
344
processCreateKv(char * putData)345 int processCreateKv(char* putData)
346 {
347 LOG("processCreateKv, begin");
348 std::cout << "create status=" << static_cast<int>(DisKvTest::statusGetKvStore) << std::endl;
349 if (Status::SUCCESS == DisKvTest::statusGetKvStore) {
350 std::cout << "SUCCESS:statusGetKvStore" << std::endl;
351 return RESULT_OK;
352 } else {
353 std::cout << "ERR:statusGetKvStore" << std::endl;
354 return RESULT_ERR;
355 }
356 }
357
processDeleteKv(char * putData)358 int processDeleteKv(char* putData)
359 {
360 LOG("processDeleteKv, begin");
361 DisKvTest::statusCloseKvStore = DisKvTest::manager.CloseAllKvStore(DisKvTest::appId);
362 DisKvTest::statusDeleteKvStore = DisKvTest::manager.DeleteAllKvStore(DisKvTest::appId, DisKvTest::create.baseDir);
363 if (Status::SUCCESS != DisKvTest::statusDeleteKvStore) {
364 std::cout << "ERR:statusDeleteKvStore" << std::endl;
365 return RESULT_ERR;
366 }
367
368 (void)remove((DisKvTest::create.baseDir + "/kvdb").c_str());
369 (void)remove(DisKvTest::create.baseDir.c_str());
370
371 std::cout << "SUCCESS:statusDeleteKvStore" << std::endl;
372 return RESULT_OK;
373 }
374
ProcessDP(int code,char * recvData)375 int ProcessDP(int code, char* recvData)
376 {
377 return RESULT_OK;
378 }
379
ProcessDM(int code,char * recvData)380 int ProcessDM(int code, char* recvData)
381 {
382 return RESULT_OK;
383 }
384
ProcessFileMgr(int code,char * recvData)385 int ProcessFileMgr(int code, char* recvData)
386 {
387 return RESULT_OK;
388 }
389
ProcessSecMgr(int code,char * recvData)390 int ProcessSecMgr(int code, char* recvData)
391 {
392 return RESULT_OK;
393 }
394
ProcessMediaMgr(int code,char * recvData)395 int ProcessMediaMgr(int code, char* recvData)
396 {
397 return RESULT_OK;
398 }
399