1 /*
2 * Copyright (c) 2025 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 "distributed_file_fd_guard.h"
17
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20 #include "dfs_error.h"
21 #include "utils_log.h"
22
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 namespace Test {
27 using namespace OHOS::FileManagement;
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace std;
31
32 class DistributedFileFdGuardTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void DistributedFileFdGuardTest::SetUpTestCase(void)
41 {
42 GTEST_LOG_(INFO) << "SetUpTestCase";
43 }
44
TearDownTestCase(void)45 void DistributedFileFdGuardTest::TearDownTestCase(void)
46 {
47 GTEST_LOG_(INFO) << "TearDownTestCase";
48 }
49
SetUp(void)50 void DistributedFileFdGuardTest::SetUp(void)
51 {
52 GTEST_LOG_(INFO) << "SetUp";
53 }
54
TearDown(void)55 void DistributedFileFdGuardTest::TearDown(void)
56 {
57 GTEST_LOG_(INFO) << "TearDown";
58 }
59
60 /**
61 * @tc.name: DistributedFileFdGuard_GetFD_0001
62 * @tc.desc: test GetFD function.
63 * @tc.type: FUNC
64 * @tc.require: I7TDJK
65 */
66 HWTEST_F(DistributedFileFdGuardTest, DistributedFileFdGuard_GetFD_0001, TestSize.Level1)
67 {
68 GTEST_LOG_(INFO) << "DistributedFileFdGuard_GetFD_0001 Start";
69
70 int fd = 10;
71 bool autoClose = false;
72 FDGuard dfsuFDGuard(fd, autoClose);
73 dfsuFDGuard.SetFD(fd, autoClose);
74 int32_t ret = dfsuFDGuard.GetFD();
75 EXPECT_EQ(ret, fd);
76
77 GTEST_LOG_(INFO) << "DistributedFileFdGuard_GetFD_0001 End";
78 }
79
80 /**
81 * @tc.name: DistributedFileFdGuard_GetFD_0002
82 * @tc.desc: test GetFD function.
83 * @tc.type: FUNC
84 * @tc.require: I7TDJK
85 */
86 HWTEST_F(DistributedFileFdGuardTest, DistributedFileFdGuard_GetFD_0002, TestSize.Level1)
87 {
88 GTEST_LOG_(INFO) << "DistributedFileFdGuard_GetFD_0002 Start";
89
90 int fd = 5;
91 bool autoClose = false;
92 FDGuard dfsuFDGuard(fd, autoClose);
93 dfsuFDGuard.SetFD(fd, autoClose);
94 int32_t ret = dfsuFDGuard.GetFD();
95 EXPECT_EQ(ret, fd);
96
97 GTEST_LOG_(INFO) << "DistributedFileFdGuard_GetFD_0002 End";
98 }
99
100 /**
101 * @tc.name: DistributedFileFdGuard_ClearFD_0001
102 * @tc.desc: test ClearFD function.
103 * @tc.type: FUNC
104 * @tc.require: I7TDJK
105 */
106 HWTEST_F(DistributedFileFdGuardTest, DistributedFileFdGuard_ClearFD_0001, TestSize.Level1)
107 {
108 GTEST_LOG_(INFO) << "DistributedFileFdGuard_ClearFD_0001 Start";
109
110 int fd = 4;
111 bool autoClose = false;
112 FDGuard dfsuFDGuard(fd, autoClose);
113 dfsuFDGuard.SetFD(fd, autoClose);
114 dfsuFDGuard.ClearFD();
115 int32_t ret = dfsuFDGuard.GetFD();
116 EXPECT_NE(ret, fd);
117
118 GTEST_LOG_(INFO) << "DistributedFileFdGuard_ClearFD_0001 End";
119 }
120 } // namespace Test
121 } // namespace DistributedFile
122 } // namespace Storage
123 } // namespace OHOS