1 /* 2 * Copyright (c) 2024 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 "hilog_collector.h" 16 #include <iostream> 17 #include <unistd.h> 18 #include <gtest/gtest.h> 19 #include "hiview_logger.h" 20 21 using namespace testing::ext; 22 using namespace OHOS::HiviewDFX; 23 using namespace OHOS::HiviewDFX::UCollectUtil; 24 using namespace OHOS::HiviewDFX::UCollect; 25 26 class HilogCollectorTest : public testing::Test { 27 public: SetUp()28 void SetUp() {}; TearDown()29 void TearDown() {}; SetUpTestCase()30 static void SetUpTestCase() {}; TearDownTestCase()31 static void TearDownTestCase() {}; 32 }; 33 34 #ifdef UNIFIED_COLLECTOR_HILOG_ENABLE 35 namespace { 36 DEFINE_LOG_TAG("HilogCollectorTest"); 37 const std::string TEST_STR = "HilogCollectorTest"; 38 constexpr uint32_t HILOG_LINE_NUM = 100; 39 constexpr uint32_t SLEEP_TIME = 300 * 1000; // 300ms 40 constexpr uint32_t LOG_SLEEP_TIME = 1000; // 1ms 41 } 42 43 /** 44 * @tc.name: HilogCollectorTest001 45 * @tc.desc: write hilog, collect hilog success 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(HilogCollectorTest, HilogCollectorTest001, TestSize.Level1) 49 { 50 int childPid = fork(); 51 if (childPid < 0) { 52 HIVIEW_LOGE("fork fail"); 53 return; 54 } 55 56 if (childPid == 0) { 57 // clear hilog buffer at first 58 execl("/system/bin/hilog", "hilog", "-r", nullptr); 59 _exit(EXIT_SUCCESS); 60 } else { 61 usleep(SLEEP_TIME); 62 for (uint32_t idx = 0; idx < HILOG_LINE_NUM; idx++) { 63 HIVIEW_LOGE("%{public}s", TEST_STR.c_str()); 64 usleep(LOG_SLEEP_TIME); 65 } 66 67 // get current process log 68 usleep(SLEEP_TIME); 69 std::shared_ptr<HilogCollector> collector = HilogCollector::Create(); 70 pid_t pid = getpid(); 71 CollectResult<std::string> result = collector->CollectLastLog(pid, HILOG_LINE_NUM); 72 ASSERT_TRUE(result.retCode == UcError::SUCCESS); 73 ASSERT_TRUE(result.data.find(TEST_STR) != std::string::npos); 74 int lineNum = std::count(result.data.begin(), result.data.end(), '\n'); 75 std::cout << "line num :" << lineNum << std::endl; 76 ASSERT_TRUE(lineNum >= HILOG_LINE_NUM); 77 78 // get child process log 79 result = collector->CollectLastLog(childPid, HILOG_LINE_NUM); 80 ASSERT_TRUE(result.retCode == UcError::SUCCESS); 81 ASSERT_TRUE(result.data == ""); 82 } 83 } 84 #else 85 /** 86 * @tc.name: HilogCollectorTest001 87 * @tc.desc: empty test 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(HilogCollectorTest, HilogCollectorTest001, TestSize.Level1) 91 { 92 std::shared_ptr<HilogCollector> collector = HilogCollector::Create(); 93 CollectResult<std::string> result = collector->CollectLastLog(0, 0); 94 ASSERT_TRUE(result.retCode == UcError::FEATURE_CLOSED); 95 } 96 #endif 97