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 */ 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 */ 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 */ 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; __anonb2b276460102(int32_t now) 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 */ 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 result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount); 128 if (result != nullptr) { 129 while (result->HasNext()) { 130 if (currentCount >= maxQueryCount) { 131 break; 132 } 133 auto info = result->Next(); 134 if (info == nullptr) { 135 FAIL(); 136 return; 137 } 138 info->GetStringFaultType(); 139 currentCount++; 140 } 141 } 142 printf("currentCount :%d \n", currentCount); 143 ASSERT_EQ(currentCount, maxQueryCount); 144 } 145 146 /** 147 * @tc.name: FaultLogInfoTest001 148 * @tc.desc: check FaultLogInfo class 149 * @tc.type: FUNC 150 */ 151 HWTEST_F(FaultloggerClientUnittest, FaultLogInfoTest001, testing::ext::TestSize.Level3) 152 { 153 FaultLogInfo info; 154 info.SetId(123); 155 ASSERT_EQ(info.GetId(), 123); 156 info.SetProcessId(1123); 157 ASSERT_EQ(info.GetProcessId(), 1123); 158 info.SetRawFileDescriptor(11); 159 ASSERT_EQ(info.GetRawFileDescriptor(), 11); 160 time_t time = std::time(nullptr); 161 info.SetTimeStamp(time); 162 ASSERT_EQ(info.GetTimeStamp(), time); 163 info.SetFaultReason("some reason"); 164 ASSERT_EQ(info.GetFaultReason(), "some reason"); 165 info.SetModuleName("some module"); 166 ASSERT_EQ(info.GetModuleName(), "some module"); 167 info.SetFaultSummary("some summary"); 168 ASSERT_EQ(info.GetFaultSummary(), "some summary"); 169 info.SetFaultType(2); 170 ASSERT_EQ(info.GetFaultType(), 2); 171 ASSERT_EQ(info.GetStringFaultType(), "CppCrash"); 172 info.SetFaultType(3); 173 ASSERT_EQ(info.GetFaultType(), 3); 174 ASSERT_EQ(info.GetStringFaultType(), "JsCrash"); 175 info.SetFaultType(4); 176 ASSERT_EQ(info.GetFaultType(), 4); 177 ASSERT_EQ(info.GetStringFaultType(), "AppFreeze"); 178 info.SetFaultType(5); 179 ASSERT_EQ(info.GetFaultType(), 5); 180 ASSERT_EQ(info.GetStringFaultType(), "SysFreeze"); 181 info.SetFaultType(6); 182 ASSERT_EQ(info.GetFaultType(), 6); 183 ASSERT_EQ(info.GetStringFaultType(), "UnknownFaultType"); 184 } 185 } // namespace HiviewDFX 186 } // namespace OHOS 187