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
TearDown()48 void DistributedataDfxUTTest::TearDown() {}
49
50 /**
51 * @tc.name: Dfx001
52 * @tc.desc: send data to 1 device, then check reporter message.
53 * @tc.type: send data
54 * @tc.require: AR000CQE1L SR000CQE1J
55 * @tc.author: hongbo
56 */
57 HWTEST_F(DistributedataDfxUTTest, Dfx001, TestSize.Level0)
58 {
59 /**
60 * @tc.steps: step1. getcommunicationFault instance
61 * @tc.expected: step1. Expect instance is not null.
62 */
63 auto comFault = Reporter::GetInstance()->CommunicationFault();
64 EXPECT_NE(nullptr, comFault);
65 struct CommFaultMsg msg{.userId = "user001", .appId = "myApp", .storeId = "storeTest"};
66 msg.deviceId.push_back("device001");
67 msg.errorCode.push_back(001);
68 msg.deviceId.push_back("device002");
69 msg.errorCode.push_back(002);
70
71 auto repStatus = comFault->Report(msg);
72 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
73 /**
74 * @tc.steps:step2. check dfx reporter.
75 * @tc.expected: step2. Expect report message success.
76 */
77 std::string val = FakeHivew::GetString("ANONYMOUS_UID");
78 if (!val.empty()) {
79 EXPECT_STREQ(val.c_str(), string("user001").c_str());
80 }
81 val = FakeHivew::GetString("APP_ID");
82 if (!val.empty()) {
83 EXPECT_STREQ(val.c_str(), string("myApp").c_str());
84 }
85 val = FakeHivew::GetString("STORE_ID");
86 if (!val.empty()) {
87 EXPECT_STREQ(val.c_str(), string("storeTest").c_str());
88 }
89 FakeHivew::Clear();
90 }
91
92 /**
93 * @tc.name: Dfx002
94 * @tc.desc: check database dfx report
95 * @tc.type: get kvStore
96 * @tc.require: AR000CQE1L AR000CQE1K SR000CQE1J
97 * @tc.author: hongbo
98 */
99 HWTEST_F(DistributedataDfxUTTest, Dfx002, TestSize.Level0)
100 {
101 FakeHivew::Clear();
102 /**
103 * @tc.steps: step1. get database fault report instance
104 * @tc.expected: step1. Expect get instance success.
105 */
106 auto dbFault = Reporter::GetInstance()->DatabaseFault();
107 EXPECT_NE(nullptr, dbFault);
108 struct DBFaultMsg msg {.appId = "MyApp", .storeId = "MyDatabase",
109 .moduleName = "database", .errorType = Fault::DF_DB_DAMAGE};
110
111 auto repStatus = dbFault->Report(msg);
112 /**
113 * @tc.steps: step2. check fault reporter.
114 * @tc.expected: step2. Expect report has bad report message.
115 */
116 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
117 auto val = FakeHivew::GetString("MODULE_NAME");
118 if (!val.empty()) {
119 EXPECT_STREQ(string("database").c_str(), val.c_str());
120 }
121 auto typeVal = FakeHivew::GetInt("ERROR_TYPE");
122 if (typeVal > 0) {
123 EXPECT_EQ(static_cast<int>(Fault::DF_DB_DAMAGE), typeVal);
124 }
125 FakeHivew::Clear();
126 }
127
128 /**
129 * @tc.name: Dfx003
130 * @tc.desc: Database file size statistic.
131 * @tc.type: check database file size.
132 * @tc.require: AR000CQE1O SR000CQE1J
133 * @tc.author: hongbo
134 */
135 HWTEST_F(DistributedataDfxUTTest, Dfx003, TestSize.Level0)
136 {
137 /**
138 * @tc.steps: step1. get database reporter instance.
139 * @tc.expected: step1. Expect get success.
140 */
141 auto dbs = Reporter::GetInstance()->DatabaseStatistic();
142 EXPECT_NE(nullptr, dbs);
143 DbStat ds = {"uid", "appid", "storeId001", 100};
144 auto dbsRet = dbs->Report(ds);
145 /**
146 * @tc.steps:step2. check reporter.
147 * @tc.expected: step2. Expect statistic database size is 100.
148 */
149 EXPECT_TRUE(dbsRet == ReportStatus::SUCCESS);
150 FakeHivew::Clear();
151 }
152
153 /**
154 * @tc.name: Dfx004
155 * @tc.desc: Set invalid information, call getKvStore, expect return INVALID_ARGS.
156 * @tc.type: CreateKvStore test
157 * @tc.require: AR000CQE1L SR000CQE1J
158 * @tc.author: hongbo
159 */
160 HWTEST_F(DistributedataDfxUTTest, Dfx004, TestSize.Level0)
161 {
162 /**
163 * @tc.steps: step1. Get runtime fault instance.
164 * @tc.expected: step1. Expect get runtime fault instance success.
165 */
166 auto rtFault = Reporter::GetInstance()->RuntimeFault();
167 auto rtFault2 = Reporter::GetInstance()->RuntimeFault();
168 EXPECT_NE(nullptr, rtFault);
169 EXPECT_EQ(rtFault, rtFault2);
170
171 struct FaultMsg rfMsg{FaultType::SERVICE_FAULT, "database", "closeKvStore",
172 Fault::RF_CLOSE_DB};
173 auto rfReportRet = rtFault->Report(rfMsg);
174 /**
175 * @tc.steps:step2. check report message.
176 * @tc.expected: step2. Expected reported message.
177 */
178 EXPECT_TRUE(rfReportRet == ReportStatus::SUCCESS);
179 auto val = FakeHivew::GetString("INTERFACE_NAME");
180 if (!val.empty()) {
181 EXPECT_STREQ(string("closeKvStore").c_str(), val.c_str());
182 }
183 auto typeVal = FakeHivew::GetInt("ERROR_TYPE");
184 if (typeVal > 0) {
185 EXPECT_EQ(static_cast<int>(Fault::RF_CLOSE_DB), typeVal);
186 }
187 FakeHivew::Clear();
188 }
189
190 /**
191 * @tc.name: Dfx005
192 * @tc.desc: send data to 1 device, then check send size.
193 * @tc.type: send data
194 * @tc.require: AR000CQE1P SR000CQE1J
195 * @tc.author: hongbo
196 */
197 HWTEST_F(DistributedataDfxUTTest, Dfx005, TestSize.Level0)
198 {
199 /**
200 * @tc.steps:step1. send data to 1 device
201 * @tc.expected: step1. Expect put success.
202 */
203 auto ts = Reporter::GetInstance()->TrafficStatistic();
204 EXPECT_NE(nullptr, ts);
205 struct TrafficStat tss = {"appId001", "deviceId001", 100, 200};
206 auto tsRet = ts->Report(tss);
207 /**
208 * @tc.steps:step2. check dfx reporter.
209 * @tc.expected: step2. Expect report has same size.
210 */
211 EXPECT_TRUE(tsRet == ReportStatus::SUCCESS);
212
213 FakeHivew::Clear();
214 }
215
216 /**
217 * @tc.name: Dfx006
218 * @tc.desc: call interface statistic.
219 * @tc.type: statistic
220 * @tc.require: AR000CQE1N SR000CQE1J
221 * @tc.author: hongbo
222 */
223 HWTEST_F(DistributedataDfxUTTest, Dfx006, TestSize.Level0)
224 {
225 /**
226 * @tc.steps:step1. create call interface statistic instance
227 * @tc.expected: step1. Expect get instance success.
228 */
229 auto vs = Reporter::GetInstance()->VisitStatistic();
230 EXPECT_NE(nullptr, vs);
231 struct VisitStat vss = {"appid001", "Put"};
232 auto vsRet = vs->Report(vss);
233 /**
234 * @tc.steps:step2. check dfx reporter.
235 * @tc.expected: step2. Expect report has same information.
236 */
237 EXPECT_TRUE(vsRet == ReportStatus::SUCCESS);
238
239 FakeHivew::Clear();
240 std::string myuid = "203230afadj020003";
241 std::string result;
242 ValueHash vh;
243 vh.CalcValueHash(myuid, result);
244 }
245
246 /**
247 * @tc.name: Dfx007
248 * @tc.desc: call api performance statistic.
249 * @tc.type: statistic
250 * @tc.require: AR000DPVGP SR000DPVGH
251 * @tc.author: liwei
252 */
253 HWTEST_F(DistributedataDfxUTTest, Dfx007, TestSize.Level0)
254 {
255 /**
256 * @tc.steps:step1. create call api perforamnce statistic instance
257 * @tc.expected: step1. Expect get instance success.
258 */
259 auto ap = Reporter::GetInstance()->ApiPerformanceStatistic();
260 EXPECT_NE(nullptr, ap);
261 struct ApiPerformanceStat aps = { "interface", 10000, 5000, 20000 };
262 auto apRet = ap->Report(aps);
263 /**
264 * @tc.steps:step2. check dfx reporter return value.
265 * @tc.expected: step2. Expect report has same information.
266 */
267 EXPECT_TRUE(apRet == ReportStatus::SUCCESS);
268
269 FakeHivew::Clear();
270 }
271
272