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 <gtest/gtest.h>
17
18#include <string>
19#include "dfx_util.h"
20#include "fault_logger_pipe.h"
21
22using namespace OHOS::HiviewDFX;
23using namespace testing::ext;
24using namespace std;
25
26namespace OHOS {
27namespace HiviewDFX {
28class FaultLoggerPipeTest : public testing::Test {
29public:
30    static void SetUpTestCase(void) {}
31    static void TearDownTestCase(void) {}
32    void SetUp() {}
33    void TearDown() {}
34};
35} // namespace HiviewDFX
36} // namespace OHOS
37
38namespace {
39/**
40 * @tc.name: FaultLoggerPipeTest001
41 * @tc.desc: test FaultLoggerPipeMap Check Set Get func
42 * @tc.type: FUNC
43 */
44HWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest001, TestSize.Level2)
45{
46    GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: start.";
47    std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
48    uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
49    int pid = 100;
50    bool check = ptr->Check(pid, time);
51    EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
52
53    ptr->Set(pid, time);
54    auto ret = ptr->Get(pid);
55    EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest001 Get failed";
56
57    time = time + 11000; // 11000 : add 11 seconds
58    check = ptr->Check(pid, time);
59    EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
60
61    GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: end.";
62}
63
64/**
65 * @tc.name: FaultLoggerPipeTest002
66 * @tc.desc: test FaultLoggerPipeMap Del func
67 * @tc.type: FUNC
68 */
69HWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest002, TestSize.Level2)
70{
71    GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: start.";
72    std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
73    uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
74    int pid = 100;
75    ptr->Set(pid, time);
76    auto ret = ptr->Get(pid);
77    EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest002 Get failed";
78    ptr->Del(pid);
79    ret = ptr->Get(pid);
80    EXPECT_EQ(true, ret == nullptr) << "FaultLoggerPipeTest002 Del failed";
81    GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: end.";
82}
83
84/**
85 * @tc.name: FaultLoggerPipeTest003
86 * @tc.desc: test FaultLoggerPipeMap Del func
87 * @tc.type: FUNC
88 */
89HWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest003, TestSize.Level2)
90{
91    GTEST_LOG_(INFO) << "FaultLoggerPipeTest003: start.";
92    FaultLoggerPipe faultLoggerPipe;
93    faultLoggerPipe.init_ = false;
94    bool ret = faultLoggerPipe.SetSize(1);
95    EXPECT_FALSE(ret);
96    ret = faultLoggerPipe.Init();
97    EXPECT_TRUE(ret);
98    ret = faultLoggerPipe.SetSize(1);
99    EXPECT_TRUE(ret);
100    int result = faultLoggerPipe.GetWriteFd();
101    EXPECT_NE(-1, result);
102    faultLoggerPipe.write_ = true;
103    result = faultLoggerPipe.GetWriteFd();
104    EXPECT_EQ(-1, result);
105
106    faultLoggerPipe.init_ = false;
107    faultLoggerPipe.Destroy();
108    EXPECT_FALSE(faultLoggerPipe.init_);
109    faultLoggerPipe.init_ = true;
110    faultLoggerPipe.Destroy();
111    EXPECT_FALSE(faultLoggerPipe.init_);
112
113    faultLoggerPipe.Close(-1);
114    faultLoggerPipe.Close(1);
115
116    FaultLoggerPipe2 faultLoggerPipe2(time(nullptr), true);
117
118    FaultLoggerPipeMap faultLoggerPipeMap;
119    ret = faultLoggerPipeMap.Check(1, time(nullptr));
120    EXPECT_FALSE(ret);
121
122    GTEST_LOG_(INFO) << "FaultLoggerPipeTest003: end.";
123}
124}