• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 
16 #include <cerrno>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include "dfx/log/ffrt_log_api.h"
22 #ifdef OHOS_STANDARD_SYSTEM
23 #include "faultloggerd_client.h"
24 #endif
25 #include "dfx/bbox/fault_logger_fd_manager.h"
26 #include "../common.h"
27 
28 static const char* FILE_NAME = "testLogToFaultlogger.txt";
29 
30 using namespace testing;
31 #ifdef HWTEST_TESTING_EXT_ENABLE
32 using namespace testing::ext;
33 #endif
34 
35 class FfrtLogTest : public testing::Test {
36 protected:
SetUpTestCase()37     static void SetUpTestCase()
38     {
39     }
40 
TearDownTestCase()41     static void TearDownTestCase()
42     {
43     }
44 
SetUp()45     void SetUp() override
46     {
47     }
48 
TearDown()49     void TearDown() override
50     {
51     }
52 };
53 #ifdef OHOS_STANDARD_SYSTEM
54 // FaultLogger获取句柄/写入日志/读取日志/关闭句柄
55 HWTEST_F(FfrtLogTest, faultLoggerFdManager, TestSize.Level0)
56 {
57     // 获取句柄
58     EXPECT_EQ(FaultLoggerFdManager::GetFaultLoggerFd(), -1);
59     EXPECT_TRUE(FaultLoggerFdManager::InitFaultLoggerFd() > 0);
60 
61     // 写入日志
62     FaultLoggerFdManager::WriteFaultLogger("test logToFaultlogger arg1[%s],arg2[%d]", "ARG1", 1);
63     FaultLoggerFdManager::WriteFaultLogger("test logToFaultlogger arg1[%s],arg2[%d]", "ARG1", 1);
64 
65     // 关闭句柄
66     FaultLoggerFdManager::CloseFd();
67     EXPECT_EQ(FaultLoggerFdManager::GetFaultLoggerFd(), -1);
68 
69     // 读取日志
70     EXPECT_TRUE(FaultLoggerFdManager::InitFaultLoggerFd() > 0);
71     int fd = FaultLoggerFdManager::GetFaultLoggerFd();
72     const int bufferLen = 2048;
73     std::array<char, bufferLen> readBuf {};
74     int readSize = read(fd, readBuf.data(), bufferLen);
75     std::string msg = "test logToFaultlogger arg1[ARG1],arg2[1]\ntest logToFaultlogger arg1[ARG1],arg2[1]\n";
76     EXPECT_EQ(readSize, msg.size());
77     EXPECT_EQ(readBuf.data(), msg);
78     close(fd);
79 
80     // 删除文件
81     remove(FILE_NAME);
82 }
83 #endif
84