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
16 #include <gtest/gtest.h>
17 #include <string>
18 #include "reporter.h"
19 #include "fake_hiview.h"
20 #include "value_hash.h"
21
22 using namespace testing::ext;
23 using namespace OHOS::DistributedDataDfx;
24
25 class DistributedataDfxUTTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28
29 static void TearDownTestCase(void);
30
31 void SetUp();
32
33 void TearDown();
34 };
35
SetUpTestCase()36 void DistributedataDfxUTTest::SetUpTestCase()
37 {
38 FakeHivew::Clear();
39 }
40
TearDownTestCase()41 void DistributedataDfxUTTest::TearDownTestCase()
42 {
43 FakeHivew::Clear();
44 }
45
SetUp()46 void DistributedataDfxUTTest::SetUp()
47 {
48 size_t max = 12;
49 size_t min = 5;
50 Reporter::GetInstance()->SetThreadPool(std::make_shared<OHOS::ExecutorPool>(max, min));
51 }
52
TearDown()53 void DistributedataDfxUTTest::TearDown()
54 {
55 Reporter::GetInstance()->SetThreadPool(nullptr);
56 }
57
58 /**
59 * @tc.name: Dfx001
60 * @tc.desc: send data to 1 device, then check reporter message.
61 * @tc.type: send data
62 * @tc.require: AR000CQE1L SR000CQE1J
63 * @tc.author: hongbo
64 */
65 HWTEST_F(DistributedataDfxUTTest, Dfx001, TestSize.Level0)
66 {
67 /**
68 * @tc.steps: step1. getcommunicationFault instance
69 * @tc.expected: step1. Expect instance is not null.
70 */
71 auto comFault = Reporter::GetInstance()->CommunicationFault();
72 EXPECT_NE(nullptr, comFault);
73 struct CommFaultMsg msg{.userId = "user001", .appId = "myApp", .storeId = "storeTest"};
74 msg.deviceId.push_back("device001");
75 msg.errorCode.push_back(001);
76 msg.deviceId.push_back("device002");
77 msg.errorCode.push_back(002);
78
79 auto repStatus = comFault->Report(msg);
80 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
81 /**
82 * @tc.steps:step2. check dfx reporter.
83 * @tc.expected: step2. Expect report message success.
84 */
85 std::string val = FakeHivew::GetString("ANONYMOUS_UID");
86 if (!val.empty()) {
87 EXPECT_STREQ(val.c_str(), string("user001").c_str());
88 }
89 val = FakeHivew::GetString("APP_ID");
90 if (!val.empty()) {
91 EXPECT_STREQ(val.c_str(), string("myApp").c_str());
92 }
93 val = FakeHivew::GetString("STORE_ID");
94 if (!val.empty()) {
95 EXPECT_STREQ(val.c_str(), string("storeTest").c_str());
96 }
97 FakeHivew::Clear();
98 }
99
100 /**
101 * @tc.name: Dfx002
102 * @tc.desc: check database dfx report
103 * @tc.type: get kvStore
104 * @tc.require: AR000CQE1L AR000CQE1K SR000CQE1J
105 * @tc.author: hongbo
106 */
107 HWTEST_F(DistributedataDfxUTTest, Dfx002, TestSize.Level0)
108 {
109 FakeHivew::Clear();
110 /**
111 * @tc.steps: step1. get database fault report instance
112 * @tc.expected: step1. Expect get instance success.
113 */
114 auto dbFault = Reporter::GetInstance()->DatabaseFault();
115 EXPECT_NE(nullptr, dbFault);
116 struct DBFaultMsg msg {.appId = "MyApp", .storeId = "MyDatabase",
117 .moduleName = "database", .errorType = Fault::DF_DB_DAMAGE};
118
119 auto repStatus = dbFault->Report(msg);
120 /**
121 * @tc.steps: step2. check fault reporter.
122 * @tc.expected: step2. Expect report has bad report message.
123 */
124 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
125 auto val = FakeHivew::GetString("MODULE_NAME");
126 if (!val.empty()) {
127 EXPECT_STREQ(string("database").c_str(), val.c_str());
128 }
129 auto typeVal = FakeHivew::GetInt("ERROR_TYPE");
130 if (typeVal > 0) {
131 EXPECT_EQ(static_cast<int>(Fault::DF_DB_DAMAGE), typeVal);
132 }
133 FakeHivew::Clear();
134 }
135
136 /**
137 * @tc.name: Dfx003
138 * @tc.desc: Database file size statistic.
139 * @tc.type: check database file size.
140 * @tc.require: AR000CQE1O SR000CQE1J
141 * @tc.author: hongbo
142 */
143 HWTEST_F(DistributedataDfxUTTest, Dfx003, TestSize.Level0)
144 {
145 /**
146 * @tc.steps: step1. get database reporter instance.
147 * @tc.expected: step1. Expect get success.
148 */
149 auto dbs = Reporter::GetInstance()->DatabaseStatistic();
150 EXPECT_NE(nullptr, dbs);
151 DbStat ds = {"uid", "appid", "storeId001", 100};
152 auto dbsRet = dbs->Report(ds);
153 /**
154 * @tc.steps:step2. check reporter.
155 * @tc.expected: step2. Expect statistic database size is 100.
156 */
157 EXPECT_TRUE(dbsRet == ReportStatus::SUCCESS);
158 FakeHivew::Clear();
159 }
160
161 /**
162 * @tc.name: Dfx004
163 * @tc.desc: Set invalid information, call getKvStore, expect return INVALID_ARGS.
164 * @tc.type: CreateKvStore test
165 * @tc.require: AR000CQE1L SR000CQE1J
166 * @tc.author: hongbo
167 */
168 HWTEST_F(DistributedataDfxUTTest, Dfx004, TestSize.Level0)
169 {
170 /**
171 * @tc.steps: step1. Get runtime fault instance.
172 * @tc.expected: step1. Expect get runtime fault instance success.
173 */
174 auto rtFault = Reporter::GetInstance()->RuntimeFault();
175 auto rtFault2 = Reporter::GetInstance()->RuntimeFault();
176 EXPECT_NE(nullptr, rtFault);
177 EXPECT_EQ(rtFault, rtFault2);
178
179 struct FaultMsg rfMsg{FaultType::SERVICE_FAULT, "database", "closeKvStore",
180 Fault::RF_CLOSE_DB};
181 auto rfReportRet = rtFault->Report(rfMsg);
182 /**
183 * @tc.steps:step2. check report message.
184 * @tc.expected: step2. Expected reported message.
185 */
186 EXPECT_TRUE(rfReportRet == ReportStatus::SUCCESS);
187 auto val = FakeHivew::GetString("INTERFACE_NAME");
188 if (!val.empty()) {
189 EXPECT_STREQ(string("closeKvStore").c_str(), val.c_str());
190 }
191 auto typeVal = FakeHivew::GetInt("ERROR_TYPE");
192 if (typeVal > 0) {
193 EXPECT_EQ(static_cast<int>(Fault::RF_CLOSE_DB), typeVal);
194 }
195 FakeHivew::Clear();
196 }
197
198 /**
199 * @tc.name: Dfx005
200 * @tc.desc: send data to 1 device, then check send size.
201 * @tc.type: send data
202 * @tc.require: AR000CQE1P SR000CQE1J
203 * @tc.author: hongbo
204 */
205 HWTEST_F(DistributedataDfxUTTest, Dfx005, TestSize.Level0)
206 {
207 /**
208 * @tc.steps:step1. send data to 1 device
209 * @tc.expected: step1. Expect put success.
210 */
211 auto ts = Reporter::GetInstance()->TrafficStatistic();
212 EXPECT_NE(nullptr, ts);
213 struct TrafficStat tss = {"appId001", "deviceId001", 100, 200};
214 auto tsRet = ts->Report(tss);
215 /**
216 * @tc.steps:step2. check dfx reporter.
217 * @tc.expected: step2. Expect report has same size.
218 */
219 EXPECT_TRUE(tsRet == ReportStatus::SUCCESS);
220
221 FakeHivew::Clear();
222 }
223
224 /**
225 * @tc.name: Dfx006
226 * @tc.desc: call interface statistic.
227 * @tc.type: statistic
228 * @tc.require: AR000CQE1N SR000CQE1J
229 * @tc.author: hongbo
230 */
231 HWTEST_F(DistributedataDfxUTTest, Dfx006, TestSize.Level0)
232 {
233 /**
234 * @tc.steps:step1. create call interface statistic instance
235 * @tc.expected: step1. Expect get instance success.
236 */
237 auto vs = Reporter::GetInstance()->VisitStatistic();
238 EXPECT_NE(nullptr, vs);
239 struct VisitStat vss = {"appid001", "Put"};
240 auto vsRet = vs->Report(vss);
241 /**
242 * @tc.steps:step2. check dfx reporter.
243 * @tc.expected: step2. Expect report has same information.
244 */
245 EXPECT_TRUE(vsRet == ReportStatus::SUCCESS);
246
247 FakeHivew::Clear();
248 std::string myuid = "203230afadj020003";
249 std::string result;
250 ValueHash vh;
251 vh.CalcValueHash(myuid, result);
252 }
253
254 /**
255 * @tc.name: Dfx007
256 * @tc.desc: call api performance statistic.
257 * @tc.type: statistic
258 * @tc.require: AR000DPVGP SR000DPVGH
259 * @tc.author: liwei
260 */
261 HWTEST_F(DistributedataDfxUTTest, Dfx007, TestSize.Level0)
262 {
263 /**
264 * @tc.steps:step1. create call api perforamnce statistic instance
265 * @tc.expected: step1. Expect get instance success.
266 */
267 auto ap = Reporter::GetInstance()->ApiPerformanceStatistic();
268 EXPECT_NE(nullptr, ap);
269 struct ApiPerformanceStat aps = { "interface", 10000, 5000, 20000 };
270 auto apRet = ap->Report(aps);
271 /**
272 * @tc.steps:step2. check dfx reporter return value.
273 * @tc.expected: step2. Expect report has same information.
274 */
275 EXPECT_TRUE(apRet == ReportStatus::SUCCESS);
276
277 FakeHivew::Clear();
278 }
279
280 /**
281 * @tc.name: Dfx008
282 * @tc.desc: send data to 1 device, then check reporter message.
283 * @tc.type: send data
284 * @tc.author: nhj
285 */
286 HWTEST_F(DistributedataDfxUTTest, Dfx008, TestSize.Level0)
287 {
288 /**
289 * @tc.steps: step1. get database fault report instance
290 * @tc.expected: step1. Expect get instance success.
291 */
292 auto behavior = Reporter::GetInstance()->BehaviourReporter();
293 EXPECT_NE(nullptr, behavior);
294 struct BehaviourMsg msg{.userId = "user008", .appId = "myApp08", .storeId = "storeTest08",
295 .behaviourType = BehaviourType::DATABASE_BACKUP, .extensionInfo="test111"};
296
297 auto repStatus = behavior->Report(msg);
298 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
299 /**
300 * @tc.steps:step2. check dfx reporter.
301 * @tc.expected: step2. Expect report message success.
302 */
303 std::string val = FakeHivew::GetString("ANONYMOUS_UID");
304 if (!val.empty()) {
305 EXPECT_STREQ(val.c_str(), string("user008").c_str());
306 }
307 val = FakeHivew::GetString("APP_ID");
308 if (!val.empty()) {
309 EXPECT_STREQ(val.c_str(), string("myApp08").c_str());
310 }
311 val = FakeHivew::GetString("STORE_ID");
312 if (!val.empty()) {
313 EXPECT_STREQ(val.c_str(), string("storeTest08").c_str());
314 }
315 FakeHivew::Clear();
316 }
317
318 /**
319 * @tc.name: Dfx009
320 * @tc.desc: Set invalid information, call getKvStore, expect return INVALID_ARGS.
321 * @tc.type: CreateKvStore test
322 * @tc.author: nhj
323 */
324 HWTEST_F(DistributedataDfxUTTest, Dfx009, TestSize.Level0)
325 {
326 /**
327 * @tc.steps: step1. Get runtime fault instance.
328 * @tc.expected: step1. Expect get runtime fault instance success.
329 */
330 auto svFault = Reporter::GetInstance()->ServiceFault();
331 auto svFault2 = Reporter::GetInstance()->ServiceFault();
332 EXPECT_NE(nullptr, svFault);
333 EXPECT_EQ(svFault, svFault2);
334
335 struct FaultMsg svMsg{FaultType::SERVICE_FAULT, "myData", "createKvStore",
336 Fault::SF_CREATE_DIR};
337 auto rfReportRet = svFault->Report(svMsg);
338 /**
339 * @tc.steps:step2. check report message.
340 * @tc.expected: step2. Expected reported message.
341 */
342 EXPECT_TRUE(rfReportRet == ReportStatus::SUCCESS);
343 auto val = FakeHivew::GetString("INTERFACE_NAME");
344 if (!val.empty()) {
345 EXPECT_STREQ(string("createKvStore").c_str(), val.c_str());
346 }
347 auto typeVal = FakeHivew::GetInt("ERROR_TYPE");
348 if (typeVal > 0) {
349 EXPECT_EQ(static_cast<int>(Fault::SF_CREATE_DIR), typeVal);
350 }
351 FakeHivew::Clear();
352 }
353
354 /**
355 * @tc.name: Dfx010
356 * @tc.desc: Database file size statistic.
357 * @tc.type: check database file size.
358 * @tc.author: nhj
359 */
360 HWTEST_F(DistributedataDfxUTTest, Dfx010, TestSize.Level0)
361 {
362 /**
363 * @tc.steps: step1. get database reporter instance.
364 * @tc.expected: step1. Expect get success.
365 */
366 auto dbs = Reporter::GetInstance()->DatabaseStatistic();
367 EXPECT_NE(nullptr, dbs);
368 DbStat ds = {"uid", "appid", "storeId002", 100};
369 auto dbsRet = dbs->Report(ds);
370 /**
371 * @tc.steps:step2. check reporter.
372 * @tc.expected: step2. Expect statistic database size is 100.
373 */
374 EXPECT_TRUE(dbsRet == ReportStatus::SUCCESS);
375 auto val = FakeHivew::GetString("STORE_ID");
376 if (!val.empty()) {
377 EXPECT_STREQ(string("storeId002").c_str(), val.c_str());
378 }
379 auto typeVal = FakeHivew::GetInt("DB_SIZE");
380 if (typeVal > 0) {
381 EXPECT_EQ(100, typeVal);
382 }
383 FakeHivew::Clear();
384 }
385
386 /**
387 * @tc.name: Dfx011
388 * @tc.desc: Send UdmfBehaviourMsg
389 * @tc.type: Send data
390 * @tc.author: nhj
391 */
392 HWTEST_F(DistributedataDfxUTTest, Dfx011, TestSize.Level0)
393 {
394 /**
395 * @tc.steps: step1. get database fault report instance
396 * @tc.expected: step1. Expect get instance success.
397 */
398 auto UdmfBehavior = Reporter::GetInstance()->BehaviourReporter();
399 EXPECT_NE(nullptr, UdmfBehavior);
400 struct UdmfBehaviourMsg UdMsg{"myApp", "channel", 200, "dataType", "operation", "result"};
401 auto repStatus = UdmfBehavior->UDMFReport(UdMsg);
402 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
403 /**
404 * @tc.steps:step2. check dfx reporter.
405 * @tc.expected: step2. Expect report message success.
406 */
407 auto val = FakeHivew::GetString("APP_ID");
408 if (!val.empty()) {
409 EXPECT_STREQ(val.c_str(), string("myApp").c_str());
410 }
411 auto val01 = FakeHivew::GetString("CHANNEL");
412 if (!val01.empty()) {
413 EXPECT_STREQ(val01.c_str(), string("channel").c_str());
414 }
415 auto val02 = FakeHivew::GetString("OPERATION");
416 if (!val02.empty()) {
417 EXPECT_STREQ(val02.c_str(), string("operation").c_str());
418 }
419 auto val03 = FakeHivew::GetString("RESULT");
420 if (!val03.empty()) {
421 EXPECT_STREQ(val03.c_str(), string("result").c_str());
422 }
423 auto val04 = FakeHivew::GetInt("DB_SIZE");
424 if (val04 > 0) {
425 EXPECT_EQ(200, val04);
426 }
427 FakeHivew::Clear();
428 }
429
430 /**
431 * @tc.name: Dfx012
432 * @tc.desc: send data to 1 device, then check send size.
433 * @tc.type: send data
434 * @tc.author: nhj
435 */
436 HWTEST_F(DistributedataDfxUTTest, Dfx012, TestSize.Level0)
437 {
438 /**
439 * @tc.steps:step1. send data to 1 device
440 * @tc.expected: step1. Expect put success.
441 */
442 auto ts = Reporter::GetInstance()->TrafficStatistic();
443 EXPECT_NE(nullptr, ts);
444 struct TrafficStat tss = {"appId001", "deviceId001", 100, 200};
445 auto tsRet = ts->Report(tss);
446 /**
447 * @tc.steps:step2. check dfx reporter.
448 * @tc.expected: step2. Expect report has same size.
449 */
450 EXPECT_TRUE(tsRet == ReportStatus::SUCCESS);
451 std::string myuid = "cjsdblvdfbfss11";
452 std::string result;
453 ValueHash vh;
454 vh.CalcValueHash(myuid, result);
455
456 FakeHivew::Clear();
457 }
458
459 /**
460 * @tc.name: Dfx013
461 * @tc.desc: call api performance statistic.
462 * @tc.type:
463 * @tc.author: nhj
464 */
465 HWTEST_F(DistributedataDfxUTTest, Dfx013, TestSize.Level0)
466 {
467 /**
468 * @tc.steps:step1. create call api perforamnce statistic instance
469 * @tc.expected: step1. Expect get instance success.
470 */
471 auto ap = Reporter::GetInstance()->ApiPerformanceStatistic();
472 EXPECT_NE(nullptr, ap);
473 struct ApiPerformanceStat aps = { "interface", 2000, 500, 1000 };
474 auto apRet = ap->Report(aps);
475 /**
476 * @tc.steps:step2. check dfx reporter return value.
477 * @tc.expected: step2. Expect report has same information.
478 */
479 EXPECT_TRUE(apRet == ReportStatus::SUCCESS);
480 auto val = FakeHivew::GetString("INTERFACE_NAME");
481 if (!val.empty()) {
482 EXPECT_STREQ(string("interface").c_str(), val.c_str());
483 }
484 FakeHivew::Clear();
485 }