1 /*
2 * Copyright (c) 2021 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 "unique_fd.h"
17 #include <fstream>
18 #include <iostream>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <gtest/gtest.h>
23 using namespace testing::ext;
24 using namespace std;
25
26 namespace OHOS {
27 namespace {
28 class UtilsUniqueFd : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32
33 static char const* testfilename;
34 };
35
36 char const* UtilsUniqueFd::testfilename = "testfilename.test";
37
SetUpTestCase(void)38 void UtilsUniqueFd::SetUpTestCase(void)
39 {
40
41 ofstream outfile;
42 outfile.open(testfilename, ios::out | ios::trunc);
43 outfile << "testdata\n"
44 << std::endl;
45 outfile.close();
46 }
47
TearDownTestCase(void)48 void UtilsUniqueFd::TearDownTestCase(void)
49 {
50
51 ifstream inputfile;
52 inputfile.open(testfilename, ios::in);
53 std::string testStr;
54 inputfile >> testStr;
55 inputfile.close();
56 EXPECT_EQ(remove(testfilename), 0);
57 }
58
59 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFd, TestSize.Level0)
60 {
61 int fd = open("NOTHISFILE", O_RDWR, 0666);
62
63 UniqueFd ufd2(fd);
64 EXPECT_EQ(ufd2, -1);
65 };
66
67 HWTEST_F(UtilsUniqueFd, testUtilsUniqueCtroFromInt, TestSize.Level0)
68 {
69
70 UniqueFd ufd2(open(testfilename, O_RDWR, 0666));
71 EXPECT_NE(ufd2, -1);
72 };
73
74 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompare, TestSize.Level0)
75 {
76 int fd = open(testfilename, O_RDWR, 0666);
77 UniqueFd ufd2(fd);
78 ASSERT_EQ(fd, ufd2);
79 ASSERT_EQ(ufd2, fd);
80 };
81
82 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNl, TestSize.Level0)
83 {
84 int fd = open(testfilename, O_RDWR, 0666);
85 UniqueFd ufd2(fd);
86 ASSERT_TRUE(ufd2 >= 0);
87 ASSERT_TRUE(0 <= ufd2);
88 };
89
90 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareBg, TestSize.Level0)
91 {
92 int fd = open(testfilename, O_RDWR, 0666);
93 UniqueFd ufd2(fd);
94 ASSERT_TRUE(ufd2 > 0);
95 ASSERT_TRUE(0 < ufd2);
96 };
97
98 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNb, TestSize.Level0)
99 {
100 int fd = open(testfilename, O_RDWR, 0666);
101 UniqueFd ufd2(fd);
102 ASSERT_TRUE(ufd2 <= 1000000);
103 ASSERT_TRUE(1000000 >= ufd2);
104 };
105
106 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareLess, TestSize.Level0)
107 {
108 int fd = open(testfilename, O_RDWR, 0666);
109 UniqueFd ufd2(fd);
110 ASSERT_TRUE(ufd2 < 1000000);
111 ASSERT_TRUE(1000000 > ufd2);
112 };
113
114 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNeq, TestSize.Level0)
115 {
116 int fd = open(testfilename, O_RDWR, 0666);
117 UniqueFd ufd2(fd);
118 ASSERT_TRUE(ufd2 != 1000000);
119 ASSERT_TRUE(1000000 != ufd2);
120 };
121
122 class NewDeleter {
123 public:
124 static int iflag;
Close(int fd)125 static void Close(int fd)
126 {
127
128 iflag = 10;
129 close(fd);
130 }
131 };
132
133 int NewDeleter::iflag = 0;
134
135 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdDefineDeletor, TestSize.Level0)
136 {
137 int fd = open(testfilename, O_RDWR);
138
139 {
140 UniqueFdAddDeletor<NewDeleter> ufd2(fd);
141 ASSERT_EQ(NewDeleter::iflag, 0);
142 if (ufd2 == -1) {
143 std::cout << "open test.h error" << std::endl;
144 }
145 }
146 ASSERT_EQ(NewDeleter::iflag, 10);
147 };
148
149 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdDefineDeletorCloseStatus, TestSize.Level0)
150 {
151 int fd = open(testfilename, O_RDWR);
152
153 {
154 UniqueFdAddDeletor<NewDeleter> ufd2(fd);
155 }
156
157 char buf[] = "test";
158 int ret = write(fd, buf, sizeof(buf));
159 ASSERT_EQ(ret, -1);
160 };
161 } // namespace
162 } // namespace OHOS
163