1 /* 2 * Copyright (c) 2024-2025 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 ASSERT_GE(childPid, 0); 52 53 if (childPid == 0) { 54 // clear hilog buffer at first 55 execl("/system/bin/hilog", "hilog", "-r", nullptr); 56 _exit(EXIT_SUCCESS); 57 } else { 58 usleep(SLEEP_TIME); 59 for (uint32_t idx = 0; idx < HILOG_LINE_NUM; idx++) { 60 HIVIEW_LOGE("%{public}s", TEST_STR.c_str()); 61 usleep(LOG_SLEEP_TIME); 62 } 63 64 // get current process log 65 usleep(SLEEP_TIME); 66 std::shared_ptr<HilogCollector> collector = HilogCollector::Create(); 67 pid_t pid = getpid(); 68 CollectResult<std::string> result = collector->CollectLastLog(pid, HILOG_LINE_NUM); 69 ASSERT_TRUE(result.retCode == UcError::SUCCESS); 70 ASSERT_TRUE(result.data.find(TEST_STR) != std::string::npos); 71 int lineNum = std::count(result.data.begin(), result.data.end(), '\n'); 72 std::cout << "line num :" << lineNum << std::endl; 73 ASSERT_TRUE(lineNum >= HILOG_LINE_NUM); 74 75 // get child process log 76 result = collector->CollectLastLog(childPid, HILOG_LINE_NUM); 77 ASSERT_TRUE(result.retCode == UcError::SUCCESS); 78 ASSERT_TRUE(result.data == ""); 79 } 80 } 81 #else 82 /** 83 * @tc.name: HilogCollectorTest001 84 * @tc.desc: empty test 85 * @tc.type: FUNC 86 */ 87 HWTEST_F(HilogCollectorTest, HilogCollectorTest001, TestSize.Level1) 88 { 89 std::shared_ptr<HilogCollector> collector = HilogCollector::Create(); 90 CollectResult<std::string> result = collector->CollectLastLog(0, 0); 91 ASSERT_TRUE(result.retCode == UcError::FEATURE_CLOSED); 92 } 93 #endif 94