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 #include <atomic>
16 #include <ctime>
17 #include <list>
18 #include <thread>
19 
20 #include <gtest/gtest.h>
21 #include <unistd.h>
22 
23 #include "faultlog_info.h"
24 #include "faultlog_query_result.h"
25 #include "faultlogger_client.h"
26 #include "faultlogger_client_test.h"
27 #include "file_util.h"
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 namespace OHOS {
31 namespace HiviewDFX {
32 class FaultloggerClientUnittest : public testing::Test {
33 public:
SetUp()34     void SetUp()
35     {
36         chmod("/data/log/faultlog/", 0777); // 0777: add other user write permission
37         chmod("/data/log/faultlog/faultlogger/", 0777); // 0777: add other user write permission
38         sleep(1);
39     };
TearDown()40     void TearDown()
41     {
42         chmod("/data/log/faultlog/", 0770); // 0770: restore permission
43         chmod("/data/log/faultlog/faultlogger/", 0770); // 0770: restore permission
44     };
45 };
46 
47 /**
48  * @tc.name: ReportCppCrashEventTest001
49  * @tc.desc: Test calling ReportCppCrashEvent Func
50  * @tc.type: FUNC
51  */
HWTEST_F(FaultloggerClientUnittest, ReportCppCrashEventTest001, testing::ext::TestSize.Level3)52 HWTEST_F(FaultloggerClientUnittest, ReportCppCrashEventTest001, testing::ext::TestSize.Level3)
53 {
54     auto now = time(nullptr);
55     auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::CPP_CRASH, "faultlogtest0");
56     ReportCppCrashEvent(&info);
57     ASSERT_TRUE(true);
58 }
59 
60 /**
61  * @tc.name: CheckFaultloggerStatusTest001
62  * @tc.desc: Check status of the faultlogger systemcapabilty
63  * @tc.type: FUNC
64  */
HWTEST_F(FaultloggerClientUnittest, CheckFaultloggerStatusTest001, testing::ext::TestSize.Level3)65 HWTEST_F(FaultloggerClientUnittest, CheckFaultloggerStatusTest001, testing::ext::TestSize.Level3)
66 {
67     bool status = CheckFaultloggerStatus();
68     ASSERT_TRUE(status);
69 }
70 
71 /**
72  * @tc.name: AddFaultLogTest001
73  * @tc.desc: add multiple logs into faultlogger, check whether the logs have been created
74  * @tc.type: FUNC
75  * @tc.require: AR000F83AK
76  */
HWTEST_F(FaultloggerClientUnittest, AddFaultLogTest001, testing::ext::TestSize.Level3)77 HWTEST_F(FaultloggerClientUnittest, AddFaultLogTest001, testing::ext::TestSize.Level3)
78 {
79     /**
80      * @tc.steps: step1. add faultlog with simplified parameters
81      * @tc.steps: step2. check the return value of the interface
82      * @tc.steps: step3. check the existence of target log file
83      * @tc.expected: the calling is success and the file has been created
84      */
85     auto now = time(nullptr);
86     const int32_t loopCount = 10;
87     std::vector<int32_t> timeStamps;
88     int32_t counter = 0;
89     auto task = [](int32_t now) {
90         printf("AddFaultLog %d\n", now);
91         auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::CPP_CRASH, "faultlogtest1");
92         AddFaultLog(info);
93     };
94     printf("start AddFaultLog\n");
95     sleep(1);
96     for (int32_t i = 0; i < loopCount; i++) {
97         now = now + 1;
98         timeStamps.push_back(now);
99         task(now);
100         sleep(1);
101     }
102     for (auto timeStamp : timeStamps) {
103         if (CheckLogFileExist(timeStamp, getuid(), "cppcrash", "faultlogtest1")) {
104             counter++;
105         }
106     }
107     ASSERT_GT(counter, 0);
108     printf("Add %d logs.\n", counter);
109 }
110 
111 /**
112  * @tc.name: QuerySelfFaultLogTest001
113  * @tc.desc: check the existence of the previous added faultlog
114  * @tc.type: FUNC
115  * @tc.require: AR000F83AK
116  */
HWTEST_F(FaultloggerClientUnittest, QuerySelfFaultLogTest001, testing::ext::TestSize.Level3)117 HWTEST_F(FaultloggerClientUnittest, QuerySelfFaultLogTest001, testing::ext::TestSize.Level3)
118 {
119     /**
120      * @tc.steps: step1. add multiple fault log by add fault log interface
121      * @tc.steps: step2. query the log by QuerySelfFaultLog interfaces
122      * @tc.steps: step3. check counts and contents of the log
123      * @tc.expected: the count is correct and the contents is complete
124      */
125     const int maxQueryCount = 10;
126     int32_t currentCount = 0;
127     auto now = time(nullptr);
128     std::vector<int32_t> timeStamps;
129     auto task = [](int32_t now) {
130         printf("AddFaultLog %d\n", now);
131         auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::NO_SPECIFIC, "faultlogtest1");
132         AddFaultLog(info);
133     };
134     for (int32_t i = 0; i < maxQueryCount; i++) {
135         now = now + 1;
136         timeStamps.push_back(now);
137         task(now);
138         sleep(1);
139     }
140     auto result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount);
141     if (result != nullptr) {
142         while (result->HasNext()) {
143             if (currentCount >= maxQueryCount) {
144                 break;
145             }
146             auto info = result->Next();
147             ASSERT_TRUE(info != nullptr);
148             info->GetStringFaultType();
149             currentCount++;
150         }
151     }
152     printf("currentCount :%d \n", currentCount);
153     ASSERT_EQ(currentCount, maxQueryCount);
154 }
155 
156 /**
157  * @tc.name: FaultLogInfoTest001
158  * @tc.desc: check FaultLogInfo class
159  * @tc.type: FUNC
160  */
HWTEST_F(FaultloggerClientUnittest, FaultLogInfoTest001, testing::ext::TestSize.Level3)161 HWTEST_F(FaultloggerClientUnittest, FaultLogInfoTest001, testing::ext::TestSize.Level3)
162 {
163     FaultLogInfo info;
164     info.SetId(123);
165     ASSERT_EQ(info.GetId(), 123);
166     info.SetProcessId(1123);
167     ASSERT_EQ(info.GetProcessId(), 1123);
168     info.SetRawFileDescriptor(11);
169     ASSERT_EQ(info.GetRawFileDescriptor(), 11);
170     time_t time = std::time(nullptr);
171     info.SetTimeStamp(time);
172     ASSERT_EQ(info.GetTimeStamp(), time);
173     info.SetFaultReason("some reason");
174     ASSERT_EQ(info.GetFaultReason(), "some reason");
175     info.SetModuleName("some module");
176     ASSERT_EQ(info.GetModuleName(), "some module");
177     info.SetFaultSummary("some summary");
178     ASSERT_EQ(info.GetFaultSummary(), "some summary");
179     info.SetFaultType(2);
180     ASSERT_EQ(info.GetFaultType(), 2);
181     ASSERT_EQ(info.GetStringFaultType(), "CppCrash");
182     info.SetFaultType(3);
183     ASSERT_EQ(info.GetFaultType(), 3);
184     ASSERT_EQ(info.GetStringFaultType(), "JsCrash");
185     info.SetFaultType(4);
186     ASSERT_EQ(info.GetFaultType(), 4);
187     ASSERT_EQ(info.GetStringFaultType(), "AppFreeze");
188     info.SetFaultType(5);
189     ASSERT_EQ(info.GetFaultType(), 5);
190     ASSERT_EQ(info.GetStringFaultType(), "SysFreeze");
191     info.SetFaultType(6);
192     ASSERT_EQ(info.GetFaultType(), 6);
193     ASSERT_EQ(info.GetStringFaultType(), "SysWarning");
194 }
195 } // namespace HiviewDFX
196 } // namespace OHOS
197