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