106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include <cstdio> 1706f6ba60Sopenharmony_ci#include <string> 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include <gtest/gtest.h> 2006f6ba60Sopenharmony_ci 2106f6ba60Sopenharmony_ci#include "hhlog.h" 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_ciusing namespace testing::ext; 2406f6ba60Sopenharmony_ciusing namespace std; 2506f6ba60Sopenharmony_cinamespace OHOS { 2606f6ba60Sopenharmony_cinamespace Developtools { 2706f6ba60Sopenharmony_cinamespace Hiebpf { 2806f6ba60Sopenharmony_ciclass HHLoggerTest : public ::testing::Test { 2906f6ba60Sopenharmony_cipublic: 3006f6ba60Sopenharmony_ci static void SetUpTestCase() {}; 3106f6ba60Sopenharmony_ci static void TearDownTestCase() {}; 3206f6ba60Sopenharmony_ci 3306f6ba60Sopenharmony_ci void SetUp() {} 3406f6ba60Sopenharmony_ci void TearDown() {} 3506f6ba60Sopenharmony_ci}; 3606f6ba60Sopenharmony_ci 3706f6ba60Sopenharmony_ci/** 3806f6ba60Sopenharmony_ci * @tc.name: GetLogFileName 3906f6ba60Sopenharmony_ci * @tc.desc: Test HHLogger GetLogFileName 4006f6ba60Sopenharmony_ci * @tc.type: FUNC 4106f6ba60Sopenharmony_ci */ 4206f6ba60Sopenharmony_ciHWTEST_F(HHLoggerTest, GetLogFileName, TestSize.Level1) 4306f6ba60Sopenharmony_ci{ 4406f6ba60Sopenharmony_ci std::string fileName = HHLogger::GetInstance().GetLogFileName(); 4506f6ba60Sopenharmony_ci EXPECT_STRNE(fileName.c_str(), ""); 4606f6ba60Sopenharmony_ci} 4706f6ba60Sopenharmony_ci 4806f6ba60Sopenharmony_ci/** 4906f6ba60Sopenharmony_ci * @tc.name: GetFormatTime 5006f6ba60Sopenharmony_ci * @tc.desc: Test HHLogger GetFormatTime 5106f6ba60Sopenharmony_ci * @tc.type: FUNC 5206f6ba60Sopenharmony_ci */ 5306f6ba60Sopenharmony_ciHWTEST_F(HHLoggerTest, GetFormatTime, TestSize.Level1) 5406f6ba60Sopenharmony_ci{ 5506f6ba60Sopenharmony_ci std::string formatTime = HHLogger::GetInstance().GetFormatTime(); 5606f6ba60Sopenharmony_ci EXPECT_STRNE(formatTime.c_str(), ""); 5706f6ba60Sopenharmony_ci} 5806f6ba60Sopenharmony_ci 5906f6ba60Sopenharmony_ci/** 6006f6ba60Sopenharmony_ci * @tc.name: UpdateTimer 6106f6ba60Sopenharmony_ci * @tc.desc: Test HHLogger UpdateTimer 6206f6ba60Sopenharmony_ci * @tc.type: FUNC 6306f6ba60Sopenharmony_ci */ 6406f6ba60Sopenharmony_ciHWTEST_F(HHLoggerTest, UpdateTimer, TestSize.Level1) 6506f6ba60Sopenharmony_ci{ 6606f6ba60Sopenharmony_ci int ret = HHLogger::GetInstance().UpdateTimer(); 6706f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 6806f6ba60Sopenharmony_ci} 6906f6ba60Sopenharmony_ci 7006f6ba60Sopenharmony_ci/** 7106f6ba60Sopenharmony_ci * @tc.name: InitLogger 7206f6ba60Sopenharmony_ci * @tc.desc: Test HHLogger InitLogger 7306f6ba60Sopenharmony_ci * @tc.type: FUNC 7406f6ba60Sopenharmony_ci */ 7506f6ba60Sopenharmony_ciHWTEST_F(HHLoggerTest, InitLogger, TestSize.Level1) 7606f6ba60Sopenharmony_ci{ 7706f6ba60Sopenharmony_ci auto ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, ""); 7806f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 7906f6ba60Sopenharmony_ci 8006f6ba60Sopenharmony_ci ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, "stdout"); 8106f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 8206f6ba60Sopenharmony_ci 8306f6ba60Sopenharmony_ci std::string fileName = "/data/local/tmp/libebpf.log"; 8406f6ba60Sopenharmony_ci ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, fileName); 8506f6ba60Sopenharmony_ci ASSERT_EQ(ret, 0); 8606f6ba60Sopenharmony_ci std::string cmd = "rm " + fileName; 8706f6ba60Sopenharmony_ci system(cmd.c_str()); 8806f6ba60Sopenharmony_ci} 8906f6ba60Sopenharmony_ci 9006f6ba60Sopenharmony_ci/** 9106f6ba60Sopenharmony_ci * @tc.name: Start 9206f6ba60Sopenharmony_ci * @tc.desc: Test HHLogger Start 9306f6ba60Sopenharmony_ci * @tc.type: FUNC 9406f6ba60Sopenharmony_ci */ 9506f6ba60Sopenharmony_ciHWTEST_F(HHLoggerTest, Start, TestSize.Level1) 9606f6ba60Sopenharmony_ci{ 9706f6ba60Sopenharmony_ci HHLogger logger; 9806f6ba60Sopenharmony_ci auto ret = logger.Start(HHLOG_NONE); 9906f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 10006f6ba60Sopenharmony_ci ret = logger.GetLogLevel(); 10106f6ba60Sopenharmony_ci EXPECT_EQ(ret, HHLOG_NONE); 10206f6ba60Sopenharmony_ci 10306f6ba60Sopenharmony_ci ret = logger.Start(HHLOG_DEBUG, ""); 10406f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 10506f6ba60Sopenharmony_ci 10606f6ba60Sopenharmony_ci ret = logger.Start(HHLOG_DEBUG, "stdout"); 10706f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 10806f6ba60Sopenharmony_ci HHLOGD(true, HHLOG_DEBUG, "this is test"); 10906f6ba60Sopenharmony_ci sleep(1); 11006f6ba60Sopenharmony_ci 11106f6ba60Sopenharmony_ci ret = logger.GetLogLevel(); 11206f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 11306f6ba60Sopenharmony_ci auto isStop = logger.IsStopped(); 11406f6ba60Sopenharmony_ci EXPECT_FALSE(isStop); 11506f6ba60Sopenharmony_ci} 11606f6ba60Sopenharmony_ci} // namespace Hiebpf 11706f6ba60Sopenharmony_ci} // namespace Developtools 11806f6ba60Sopenharmony_ci} // namespace OHOS 119