1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <string>
19800b99b8Sopenharmony_ci#include "dfx_util.h"
20800b99b8Sopenharmony_ci#include "fault_logger_pipe.h"
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX;
23800b99b8Sopenharmony_ciusing namespace testing::ext;
24800b99b8Sopenharmony_ciusing namespace std;
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_cinamespace OHOS {
27800b99b8Sopenharmony_cinamespace HiviewDFX {
28800b99b8Sopenharmony_ciclass FaultLoggerPipeTest : public testing::Test {
29800b99b8Sopenharmony_cipublic:
30800b99b8Sopenharmony_ci    static void SetUpTestCase(void) {}
31800b99b8Sopenharmony_ci    static void TearDownTestCase(void) {}
32800b99b8Sopenharmony_ci    void SetUp() {}
33800b99b8Sopenharmony_ci    void TearDown() {}
34800b99b8Sopenharmony_ci};
35800b99b8Sopenharmony_ci} // namespace HiviewDFX
36800b99b8Sopenharmony_ci} // namespace OHOS
37800b99b8Sopenharmony_ci
38800b99b8Sopenharmony_cinamespace {
39800b99b8Sopenharmony_ci/**
40800b99b8Sopenharmony_ci * @tc.name: FaultLoggerPipeTest001
41800b99b8Sopenharmony_ci * @tc.desc: test FaultLoggerPipeMap Check Set Get func
42800b99b8Sopenharmony_ci * @tc.type: FUNC
43800b99b8Sopenharmony_ci */
44800b99b8Sopenharmony_ciHWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest001, TestSize.Level2)
45800b99b8Sopenharmony_ci{
46800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: start.";
47800b99b8Sopenharmony_ci    std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
48800b99b8Sopenharmony_ci    uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
49800b99b8Sopenharmony_ci    int pid = 100;
50800b99b8Sopenharmony_ci    bool check = ptr->Check(pid, time);
51800b99b8Sopenharmony_ci    EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
52800b99b8Sopenharmony_ci
53800b99b8Sopenharmony_ci    ptr->Set(pid, time);
54800b99b8Sopenharmony_ci    auto ret = ptr->Get(pid);
55800b99b8Sopenharmony_ci    EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest001 Get failed";
56800b99b8Sopenharmony_ci
57800b99b8Sopenharmony_ci    time = time + 11000; // 11000 : add 11 seconds
58800b99b8Sopenharmony_ci    check = ptr->Check(pid, time);
59800b99b8Sopenharmony_ci    EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: end.";
62800b99b8Sopenharmony_ci}
63800b99b8Sopenharmony_ci
64800b99b8Sopenharmony_ci/**
65800b99b8Sopenharmony_ci * @tc.name: FaultLoggerPipeTest002
66800b99b8Sopenharmony_ci * @tc.desc: test FaultLoggerPipeMap Del func
67800b99b8Sopenharmony_ci * @tc.type: FUNC
68800b99b8Sopenharmony_ci */
69800b99b8Sopenharmony_ciHWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest002, TestSize.Level2)
70800b99b8Sopenharmony_ci{
71800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: start.";
72800b99b8Sopenharmony_ci    std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
73800b99b8Sopenharmony_ci    uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
74800b99b8Sopenharmony_ci    int pid = 100;
75800b99b8Sopenharmony_ci    ptr->Set(pid, time);
76800b99b8Sopenharmony_ci    auto ret = ptr->Get(pid);
77800b99b8Sopenharmony_ci    EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest002 Get failed";
78800b99b8Sopenharmony_ci    ptr->Del(pid);
79800b99b8Sopenharmony_ci    ret = ptr->Get(pid);
80800b99b8Sopenharmony_ci    EXPECT_EQ(true, ret == nullptr) << "FaultLoggerPipeTest002 Del failed";
81800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: end.";
82800b99b8Sopenharmony_ci}
83800b99b8Sopenharmony_ci
84800b99b8Sopenharmony_ci/**
85800b99b8Sopenharmony_ci * @tc.name: FaultLoggerPipeTest003
86800b99b8Sopenharmony_ci * @tc.desc: test FaultLoggerPipeMap Del func
87800b99b8Sopenharmony_ci * @tc.type: FUNC
88800b99b8Sopenharmony_ci */
89800b99b8Sopenharmony_ciHWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest003, TestSize.Level2)
90800b99b8Sopenharmony_ci{
91800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest003: start.";
92800b99b8Sopenharmony_ci    FaultLoggerPipe faultLoggerPipe;
93800b99b8Sopenharmony_ci    faultLoggerPipe.init_ = false;
94800b99b8Sopenharmony_ci    bool ret = faultLoggerPipe.SetSize(1);
95800b99b8Sopenharmony_ci    EXPECT_FALSE(ret);
96800b99b8Sopenharmony_ci    ret = faultLoggerPipe.Init();
97800b99b8Sopenharmony_ci    EXPECT_TRUE(ret);
98800b99b8Sopenharmony_ci    ret = faultLoggerPipe.SetSize(1);
99800b99b8Sopenharmony_ci    EXPECT_TRUE(ret);
100800b99b8Sopenharmony_ci    int result = faultLoggerPipe.GetWriteFd();
101800b99b8Sopenharmony_ci    EXPECT_NE(-1, result);
102800b99b8Sopenharmony_ci    faultLoggerPipe.write_ = true;
103800b99b8Sopenharmony_ci    result = faultLoggerPipe.GetWriteFd();
104800b99b8Sopenharmony_ci    EXPECT_EQ(-1, result);
105800b99b8Sopenharmony_ci
106800b99b8Sopenharmony_ci    faultLoggerPipe.init_ = false;
107800b99b8Sopenharmony_ci    faultLoggerPipe.Destroy();
108800b99b8Sopenharmony_ci    EXPECT_FALSE(faultLoggerPipe.init_);
109800b99b8Sopenharmony_ci    faultLoggerPipe.init_ = true;
110800b99b8Sopenharmony_ci    faultLoggerPipe.Destroy();
111800b99b8Sopenharmony_ci    EXPECT_FALSE(faultLoggerPipe.init_);
112800b99b8Sopenharmony_ci
113800b99b8Sopenharmony_ci    faultLoggerPipe.Close(-1);
114800b99b8Sopenharmony_ci    faultLoggerPipe.Close(1);
115800b99b8Sopenharmony_ci
116800b99b8Sopenharmony_ci    FaultLoggerPipe2 faultLoggerPipe2(time(nullptr), true);
117800b99b8Sopenharmony_ci
118800b99b8Sopenharmony_ci    FaultLoggerPipeMap faultLoggerPipeMap;
119800b99b8Sopenharmony_ci    ret = faultLoggerPipeMap.Check(1, time(nullptr));
120800b99b8Sopenharmony_ci    EXPECT_FALSE(ret);
121800b99b8Sopenharmony_ci
122800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FaultLoggerPipeTest003: end.";
123800b99b8Sopenharmony_ci}
124800b99b8Sopenharmony_ci}