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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19 
20 #include "dfsu_fd_guard.h"
21 
22 namespace OHOS::Storage::DistributedFile::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 class DfsuFdGuardTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32 };
33 
SetUpTestCase(void)34 void DfsuFdGuardTest::SetUpTestCase(void)
35 {
36     GTEST_LOG_(INFO) << "SetUpTestCase";
37 }
38 
TearDownTestCase(void)39 void DfsuFdGuardTest::TearDownTestCase(void)
40 {
41     GTEST_LOG_(INFO) << "TearDownTestCase";
42 }
43 
SetUp(void)44 void DfsuFdGuardTest::SetUp(void)
45 {
46     GTEST_LOG_(INFO) << "SetUp";
47 }
48 
TearDown(void)49 void DfsuFdGuardTest::TearDown(void)
50 {
51     GTEST_LOG_(INFO) << "TearDown";
52 }
53 
54 /**
55  * @tc.name: GetFDTest001
56  * @tc.desc: Verify the GetFD function
57  * @tc.type: FUNC
58  * @tc.require: I6JPKG
59  */
60 HWTEST_F(DfsuFdGuardTest, GetFDTest001, TestSize.Level1)
61 {
62     GTEST_LOG_(INFO) << "GetFDTest001 Begin";
63     try {
64         int fd = 0;
65         bool autoClose = true;
66         DfsuFDGuard dfsuFDGuard(fd, autoClose);
67         auto ret = dfsuFDGuard.GetFD();
68         EXPECT_EQ(ret, 0);
69     } catch (...) {
70         EXPECT_TRUE(false);
71         GTEST_LOG_(INFO) << " GetFDTest001 ERROR";
72     }
73 
74     GTEST_LOG_(INFO) << "GetFDTest001 End";
75 }
76 
77 /**
78  * @tc.name: GetFDTest002
79  * @tc.desc: Verify the GetFD function
80  * @tc.type: FUNC
81  * @tc.require: I6JPKG
82  */
83 HWTEST_F(DfsuFdGuardTest, GetFDTest002, TestSize.Level1)
84 {
85     GTEST_LOG_(INFO) << "GetFDTest002 Begin";
86     try {
87         int fd = 0;
88         bool autoClose = true;
89         DfsuFDGuard dfsuFDGuard(fd, autoClose);
90         if (dfsuFDGuard.GetFD()) {
91             EXPECT_TRUE(true);
92         }
93     } catch (...) {
94         EXPECT_TRUE(false);
95         GTEST_LOG_(INFO) << " GetFDTest002 ERROR";
96     }
97 
98     GTEST_LOG_(INFO) << "GetFDTest002 End";
99 }
100 
101 /**
102  * @tc.name: SetFDTest001
103  * @tc.desc: Verify the SetFD function
104  * @tc.type: FUNC
105  * @tc.require: I6JPKG
106  */
107 HWTEST_F(DfsuFdGuardTest, SetFDTest001, TestSize.Level1)
108 {
109     GTEST_LOG_(INFO) << "SetFDTest001 Begin";
110     try {
111         int fd = 1;
112         bool autoClose = true;
113         DfsuFDGuard dfsuFDGuard(fd, autoClose);
114         dfsuFDGuard.SetFD(fd, autoClose);
115         EXPECT_TRUE(true);
116     } catch (...) {
117         EXPECT_TRUE(false);
118         GTEST_LOG_(INFO) << " SetFDTest001 ERROR";
119     }
120 
121     GTEST_LOG_(INFO) << "SetFDTest001 End";
122 }
123 
124 /**
125  * @tc.name: SetFDTest002
126  * @tc.desc: Verify the SetFD function
127  * @tc.type: FUNC
128  * @tc.require: I6JPKG
129  */
130 HWTEST_F(DfsuFdGuardTest, SetFDTest002, TestSize.Level1)
131 {
132     GTEST_LOG_(INFO) << "SetFDTest002 Begin";
133     try {
134         int fd = 1;
135         bool autoClose = true;
136         DfsuFDGuard dfsuFDGuard(fd, autoClose);
137         dfsuFDGuard.ClearFD();
138         EXPECT_TRUE(true);
139     } catch (...) {
140         EXPECT_TRUE(false);
141         GTEST_LOG_(INFO) << " SetFDTest002 ERROR";
142     }
143 
144     GTEST_LOG_(INFO) << "SetFDTest002 End";
145 }
146 } // namespace OHOS::Storage::DistributedFile::Test
147