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: AddFaultLogTest001 49 * @tc.desc: add multiple logs into faultlogger, check whether the logs have been created 50 * @tc.type: FUNC 51 * @tc.require: AR000F83AK 52 */ 53 HWTEST_F(FaultloggerClientUnittest, AddFaultLogTest001, testing::ext::TestSize.Level3) 54 { 55 /** 56 * @tc.steps: step1. add faultlog with simplified parameters 57 * @tc.steps: step2. check the return value of the interface 58 * @tc.steps: step3. check the existance of target log file 59 * @tc.expected: the calling is success and the file has been created 60 */ 61 auto now = time(nullptr); 62 const int32_t loopCount = 10; 63 std::atomic<int> counter{0}; __anon8fec6f4c0102(int32_t now, std::atomic<int>& counter) 64 auto task = [](int32_t now, std::atomic<int>& counter) { 65 printf("AddFaultLog %d\n", now); 66 auto info = CreateFaultLogInfo(now, getuid(), FaultLogType::CPP_CRASH, "faultlogtest1"); 67 AddFaultLog(info); 68 sleep(5); // maybe 5 seconds is enough for process all AddLog request 69 if (CheckLogFileExist(now, getuid(), "cppcrash", "faultlogtest1")) { 70 counter++; 71 } 72 }; 73 printf("start AddFaultLog\n"); 74 for (int32_t i = 0; i < loopCount; i++) { 75 now = now + 1; 76 task(now, std::ref(counter)); 77 } 78 79 ASSERT_GT(counter, 0); 80 printf("Add %d logs.\n", counter.load()); 81 } 82 83 /** 84 * @tc.name: QuerySelfFaultLogTest001 85 * @tc.desc: check the existance of the previous added faultlog 86 * @tc.type: FUNC 87 * @tc.require: AR000F83AK 88 */ 89 HWTEST_F(FaultloggerClientUnittest, QuerySelfFaultLogTest001, testing::ext::TestSize.Level3) 90 { 91 /** 92 * @tc.steps: step1. add multiple fault log by add fault log interface 93 * @tc.steps: step2. query the log by QuerySelfFaultLog interfaces 94 * @tc.steps: step3. check counts and contents of the log 95 * @tc.expected: the count is correct and the contents is complete 96 */ 97 const int maxQueryCount = 10; 98 int32_t currentCount = 0; 99 auto result = QuerySelfFaultLog(FaultLogType::NO_SPECIFIC, maxQueryCount); 100 if (result != nullptr) { 101 while (result->HasNext()) { 102 if (currentCount >= maxQueryCount) { 103 break; 104 } 105 auto info = result->Next(); 106 if (info == nullptr) { 107 FAIL(); 108 return; 109 } 110 info->GetStringFaultType(); 111 currentCount++; 112 } 113 } 114 printf("currentCount :%d \n", currentCount); 115 ASSERT_EQ(currentCount, maxQueryCount); 116 } 117 } // namespace HiviewDFX 118 } // namespace OHOS 119