1 /*
2  * Copyright (c) 2021-2023 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 #include <ctime>
16 #include <list>
17 
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 #include "faultlog_info.h"
22 #include "faultlog_query_result.h"
23 #include "faultlogger_client.h"
24 #include "faultlogger_client_test.h"
25 #include "file_util.h"
26 #include "hisysevent.h"
27 using namespace testing::ext;
28 using namespace OHOS::HiviewDFX;
29 namespace OHOS {
30 namespace HiviewDFX {
31 class FaultloggerNativeInterfaceTest : public testing::Test {
32 public:
SetUp()33     void SetUp()
34     {
35         sleep(1);
36     };
TearDown()37     void TearDown(){};
38 };
39 
40 /**
41  * @tc.name: AddFaultLogTest001
42  * @tc.desc: check the action of the AddFaultLog interface
43  * @tc.type: FUNC
44  * @tc.require: SR000F7ULN AR000F83AK
45  */
46 HWTEST_F(FaultloggerNativeInterfaceTest, AddFaultLogTest001, testing::ext::TestSize.Level3)
47 {
48     /**
49      * @tc.steps: step1. add faultlog with simplified parameters
50      * @tc.steps: step2. check the return value of the interface
51      * @tc.steps: step3. check the existence of target log file
52      * @tc.expected: the calling is success and the file has been created
53      */
54     auto now = time(nullptr);
55     auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::CPP_CRASH, "faultloggertest");
56     AddFaultLog(info);
57     sleep(1);
58     ASSERT_TRUE(CheckLogFileExist(now, getuid(), "cppcrash", "faultloggertest"));
59 }
60 
61 /**
62  * @tc.name: AddFaultLogTest002
63  * @tc.desc: check the action of the AddFaultLog interface
64  * @tc.type: FUNC
65  * @tc.require: SR000F7ULN AR000F83AK
66  */
67 HWTEST_F(FaultloggerNativeInterfaceTest, AddFaultLogTest002, testing::ext::TestSize.Level3)
68 {
69     /**
70      * @tc.steps: step1. add faultlog with complete parameters
71      * @tc.steps: step2. check the return value of the interface
72      * @tc.steps: step3. check the existence of target log file
73      * @tc.expected: the calling is success and the file has been created
74      */
75     auto now = time(nullptr);
76     auto module = "faultlogtest1";
77     AddFaultLog(now, FaultLogType::CPP_CRASH, module, "faultloggertestsummary2");
78     sleep(1);
79     ASSERT_TRUE(CheckLogFileExist(now, getuid(), "cppcrash", module));
80 }
81 
82 /**
83  * @tc.name: AddFaultLogTest003
84  * @tc.desc: add more than MAX_KEEP_LOGS into faultlogger, check the exist files
85  * @tc.type: FUNC
86  * @tc.require: SR000F7ULN AR000F83AL
87  */
88 HWTEST_F(FaultloggerNativeInterfaceTest, AddFaultLogTest003, testing::ext::TestSize.Level3)
89 {
90     /**
91      * @tc.steps: step1. add faultlog with complete parameters
92      * @tc.steps: step2. check the return value of the interface
93      * @tc.steps: step3. check the existence of target log file
94      * @tc.expected: the calling is success and the file has been created
95      */
96     auto now = time(nullptr);
97     auto module = "faultlogtest5";
98     const int loopTimes = 20;
99     const int maxKeepFiles = 10;
100     for (int i = 0; i < loopTimes; i++) {
101         AddFaultLog(now + i, FaultLogType::CPP_CRASH, module, "faultloggertestsummary2");
102         sleep(1);
103     }
104 
105     sleep(1);
106     for (int i = loopTimes - 1; i > maxKeepFiles; i--) {
107         ASSERT_TRUE(CheckLogFileExist(now + i, getuid(), "cppcrash", module));
108     }
109 
110     for (int i = 0; i < maxKeepFiles; i++) {
111         ASSERT_FALSE(CheckLogFileExist(now + i, getuid(), "cppcrash", module));
112     }
113 }
114 
115 /**
116  * @tc.name: QuerySelfFaultLogTest001
117  * @tc.desc: check the existence of the previous added faultlog
118  * @tc.type: FUNC
119  * @tc.require: SR000F7ULN AR000F83AK
120  */
121 HWTEST_F(FaultloggerNativeInterfaceTest, QuerySelfFaultLogTest001, testing::ext::TestSize.Level3)
122 {
123     /**
124      * @tc.steps: step1. add multiple fault log by add fault log interface
125      */
126     auto now = time(nullptr);
127     auto module = "faultlogtest6";
128     const int loopTimes = 10;
129     for (int i = 0; i < loopTimes; i++) {
130         AddFaultLog(now + i, FaultLogType::CPP_CRASH, module, "faultloggertestsummary6");
131         sleep(1); // 1 s
132     }
133     sleep(1);
134 
135     /**
136      * @tc.steps: step2. query the log by QuerySelfFaultLog interfaces
137      * @tc.steps: step3. check counts and contents of the log
138      * @tc.expected: the count is correct and the contents is complete
139      */
140     const int maxQueryCount = 100;
141     int32_t currentCount = 0;
142     auto result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount);
143     if (result != nullptr) {
144         while (result->HasNext()) {
145             if (currentCount >= maxQueryCount) {
146                 break;
147             }
148             auto info = result->Next();
149             if (info == nullptr) {
150                 FAIL();
151             }
152             currentCount++;
153             printf("Current LogNo:%d module:%s time:%d \n", currentCount, info->GetModuleName().c_str(),
154                    static_cast<int32_t>(info->GetTimeStamp()));
155             if (info->GetModuleName() == "faultloggertest") {
156                 ASSERT_EQ("TestReason", info->GetFaultReason());
157             }
158 
159             int32_t fd = info->GetRawFileDescriptor();
160             if (fd < 0) {
161                 printf("Fail to get fd:%d\n", fd);
162             } else {
163                 close(fd);
164             }
165         }
166     }
167     ASSERT_GE(currentCount, 0);
168     printf("QuerySelfFaultLog count:%d", currentCount);
169 }
170 
171 /**
172  * @tc.name: QuerySelfFaultLogTest002
173  * @tc.desc: One application should always has one query
174  * @tc.type: FUNC
175  * @tc.require: SR000F7ULN AR000F83AK
176  */
177 HWTEST_F(FaultloggerNativeInterfaceTest, QuerySelfFaultLogTest002, testing::ext::TestSize.Level3)
178 {
179     /**
180      * @tc.steps: step1. call QuerySelfFaultLog interface twice
181      * @tc.steps: step2. check the query result
182      * @tc.expected: the first query success and the second fail
183      */
184     const int maxQueryCount = 10;
185     auto result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount);
186     ASSERT_NE(nullptr, result);
187     auto result2 = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount);
188     ASSERT_EQ(nullptr, result2);
189 }
190 
191 /**
192  * @tc.name: QuerySelfFaultLogTest004
193  * @tc.desc: query cpp crash log
194  * @tc.type: FUNC
195  * @tc.require: SR000F7ULN AR000F84UR
196  */
197 HWTEST_F(FaultloggerNativeInterfaceTest, QuerySelfFaultLogTest004, testing::ext::TestSize.Level3)
198 {
199     /**
200      * @tc.steps: step1. call QuerySelfFaultLog interface twice
201      * @tc.steps: step2. check the query result
202      * @tc.expected: the first query success and the second fail
203      */
204     const int maxQueryCount = 10;
205     auto result = QuerySelfFaultLog(FaultLogType::CPP_CRASH, maxQueryCount);
206     ASSERT_NE(nullptr, result);
207     auto result2 = QuerySelfFaultLog(FaultLogType::CPP_CRASH, maxQueryCount);
208     ASSERT_EQ(nullptr, result2);
209 }
210 
211 
212 /**
213  * @tc.name: QuerySelfFaultLogTest005
214  * @tc.desc: query interface pass invalid parameter
215  * @tc.type: FUNC
216  * @tc.require: SR000F7UQ8 AR000F83AJ
217  */
218 HWTEST_F(FaultloggerNativeInterfaceTest, QuerySelfFaultLogTest005, testing::ext::TestSize.Level3)
219 {
220     /**
221      * @tc.steps: step1. call QuerySelfFaultLog interface twice
222      * @tc.steps: step2. check the query result
223      * @tc.expected: the first query success and the second fail
224      */
225     auto result = QuerySelfFaultLog(static_cast<OHOS::HiviewDFX::FaultLogType>(-1), -1); // -1 : invalid value
226     ASSERT_EQ(nullptr, result);
227     auto result2 = QuerySelfFaultLog(FaultLogType::CPP_CRASH, 100000); // 100000 : max count
228     ASSERT_NE(nullptr, result2);
229 }
230 
SendJsErrorEvent()231 static void SendJsErrorEvent()
232 {
233     HiSysEventWrite(HiSysEvent::Domain::ACE,
234         "JS_ERROR",
235         HiSysEvent::EventType::FAULT,
236         "PID", 478, // 478 : test pid
237         "UID", getuid(),
238         "PACKAGE_NAME", "com.ohos.faultlogger.test",
239         "PROCESS_NAME", "com.ohos.faultlogger.test",
240         "MSG", "faultlogger testcase test.",
241         "REASON", "faultlogger testcase test.");
242 }
243 
244 /**
245  * @tc.name: QuerySelfFaultLogTest006
246  * @tc.desc: query js crash log
247  * @tc.type: FUNC
248  */
249 HWTEST_F(FaultloggerNativeInterfaceTest, QuerySelfFaultLogTest006, testing::ext::TestSize.Level3)
250 {
251     const int maxQueryCount = 1;
252     SendJsErrorEvent();
253     sleep(3); // 3 : wait for event saving
254     auto results = QuerySelfFaultLog(FaultLogType::JS_CRASH, maxQueryCount);
255     ASSERT_NE(nullptr, results);
256     while (results->HasNext()) {
257         auto result = results->Next();
258         if (result != nullptr) {
259             ASSERT_EQ(result->GetId(), getuid());
260             ASSERT_EQ(result->GetProcessId(), 478); // 478 : test pid
261             ASSERT_EQ(result->GetFaultReason(), "faultlogger testcase test.");
262             GTEST_LOG_(INFO) << "finish check js error log";
263             break;
264         }
265     }
266 }
267 
268 /**
269  * @tc.name: FaultlogInfoTest001
270  * @tc.desc: create faultloginfo object and check the getter&setter func
271  * @tc.type: FUNC
272  */
273 HWTEST_F(FaultloggerNativeInterfaceTest, FaultlogInfoTest001, testing::ext::TestSize.Level3)
274 {
275     auto info = std::make_unique<FaultLogInfo>();
276     int64_t ts = time(nullptr);
277     std::string reason = "TestReason";
278     std::string module = "com.example.myapplication";
279     std::string summary = "TestSummary";
280     info->SetId(getuid());
281     info->SetProcessId(getpid());
282     info->SetFaultType(FaultLogType::CPP_CRASH);
283     info->SetRawFileDescriptor(-1);
284     info->SetTimeStamp(ts);
285     info->SetFaultReason(reason);
286     info->SetModuleName(module);
287     info->SetFaultSummary(summary);
288     bool ret = info->GetId() == static_cast<uint32_t>(getuid());
289     ret &= info->GetProcessId() == getpid();
290     ret &= info->GetFaultType() == FaultLogType::CPP_CRASH;
291     ret &= info->GetRawFileDescriptor() == -1;
292     ret &= info->GetTimeStamp() == ts;
293     ret &= info->GetFaultReason() == reason;
294     ret &= info->GetModuleName() == module;
295     ret &= info->GetFaultSummary() == summary;
296     ASSERT_TRUE(ret);
297 }
298 } // namespace HiviewDFX
299 } // namespace OHOS
300