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::atomic<int> counter{0}; __anond166dea80102(int32_t now, std::atomic<int>& counter) 88 auto task = [](int32_t now, std::atomic<int>& counter) { 89 printf("AddFaultLog %d\n", now); 90 auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::CPP_CRASH, "faultlogtest1"); 91 AddFaultLog(info); 92 sleep(5); // maybe 5 seconds is enough for process all AddLog request 93 if (CheckLogFileExist(now, getuid(), "cppcrash", "faultlogtest1")) { 94 counter++; 95 } 96 }; 97 printf("start AddFaultLog\n"); 98 sleep(1); 99 for (int32_t i = 0; i < loopCount; i++) { 100 now = now + 1; 101 task(now, std::ref(counter)); 102 } 103 104 ASSERT_GT(counter, 0); 105 printf("Add %d logs.\n", counter.load()); 106 } 107 108 /** 109 * @tc.name: QuerySelfFaultLogTest001 110 * @tc.desc: check the existence of the previous added faultlog 111 * @tc.type: FUNC 112 * @tc.require: AR000F83AK 113 */ 114 HWTEST_F(FaultloggerClientUnittest, QuerySelfFaultLogTest001, testing::ext::TestSize.Level3) 115 { 116 /** 117 * @tc.steps: step1. add multiple fault log by add fault log interface 118 * @tc.steps: step2. query the log by QuerySelfFaultLog interfaces 119 * @tc.steps: step3. check counts and contents of the log 120 * @tc.expected: the count is correct and the contents is complete 121 */ 122 const int maxQueryCount = 10; 123 int32_t currentCount = 0; 124 auto result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount); 125 if (result != nullptr) { 126 while (result->HasNext()) { 127 if (currentCount >= maxQueryCount) { 128 break; 129 } 130 auto info = result->Next(); 131 if (info == nullptr) { 132 FAIL(); 133 return; 134 } 135 info->GetStringFaultType(); 136 currentCount++; 137 } 138 } 139 printf("currentCount :%d \n", currentCount); 140 ASSERT_EQ(currentCount, maxQueryCount); 141 } 142 143 /** 144 * @tc.name: FaultLogInfoTest001 145 * @tc.desc: check FaultLogInfo class 146 * @tc.type: FUNC 147 */ 148 HWTEST_F(FaultloggerClientUnittest, FaultLogInfoTest001, testing::ext::TestSize.Level3) 149 { 150 FaultLogInfo info; 151 info.SetId(123); 152 ASSERT_EQ(info.GetId(), 123); 153 info.SetProcessId(1123); 154 ASSERT_EQ(info.GetProcessId(), 1123); 155 info.SetRawFileDescriptor(11); 156 ASSERT_EQ(info.GetRawFileDescriptor(), 11); 157 time_t time = std::time(nullptr); 158 info.SetTimeStamp(time); 159 ASSERT_EQ(info.GetTimeStamp(), time); 160 info.SetFaultReason("some reason"); 161 ASSERT_EQ(info.GetFaultReason(), "some reason"); 162 info.SetModuleName("some module"); 163 ASSERT_EQ(info.GetModuleName(), "some module"); 164 info.SetFaultSummary("some summary"); 165 ASSERT_EQ(info.GetFaultSummary(), "some summary"); 166 info.SetFaultType(2); 167 ASSERT_EQ(info.GetFaultType(), 2); 168 ASSERT_EQ(info.GetStringFaultType(), "CppCrash"); 169 info.SetFaultType(3); 170 ASSERT_EQ(info.GetFaultType(), 3); 171 ASSERT_EQ(info.GetStringFaultType(), "JsCrash"); 172 info.SetFaultType(4); 173 ASSERT_EQ(info.GetFaultType(), 4); 174 ASSERT_EQ(info.GetStringFaultType(), "AppFreeze"); 175 info.SetFaultType(5); 176 ASSERT_EQ(info.GetFaultType(), 5); 177 ASSERT_EQ(info.GetStringFaultType(), "SysFreeze"); 178 info.SetFaultType(6); 179 ASSERT_EQ(info.GetFaultType(), 6); 180 ASSERT_EQ(info.GetStringFaultType(), "UnknownFaultType"); 181 } 182 } // namespace HiviewDFX 183 } // namespace OHOS 184