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 if (remove(testfilename) == 0) {
57 ;
58 } else {
59 FAIL();
60 }
61 }
62
63 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFd, TestSize.Level0)
64 {
65 int fd = open("NOTHISFILE", O_RDWR, 0666);
66
67 UniqueFd ufd2(fd);
68 if (ufd2 == -1) {
69 SUCCEED();
70 }
71 };
72
73 HWTEST_F(UtilsUniqueFd, testUtilsUniqueCtroFromInt, TestSize.Level0)
74 {
75
76 UniqueFd ufd2(open(testfilename, O_RDWR, 0666));
77 if (ufd2 == -1) {
78 FAIL();
79 }
80 };
81
82 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompare, TestSize.Level0)
83 {
84 int fd = open(testfilename, O_RDWR, 0666);
85 UniqueFd ufd2(fd);
86 ASSERT_EQ(fd, ufd2);
87 ASSERT_EQ(ufd2, fd);
88 };
89
90 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNl, 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, testUtilsUniqueFdeqcompareBg, TestSize.Level0)
99 {
100 int fd = open(testfilename, O_RDWR, 0666);
101 UniqueFd ufd2(fd);
102 ASSERT_TRUE(ufd2 > 0);
103 ASSERT_TRUE(0 < ufd2);
104 };
105
106 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNb, 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, testUtilsUniqueFdeqcompareLess, 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 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdeqcompareNeq, TestSize.Level0)
123 {
124 int fd = open(testfilename, O_RDWR, 0666);
125 UniqueFd ufd2(fd);
126 ASSERT_TRUE(ufd2 != 1000000);
127 ASSERT_TRUE(1000000 != ufd2);
128 };
129
130 class NewDeleter {
131 public:
132 static int iflag;
Close(int fd)133 static void Close(int fd)
134 {
135
136 iflag = 10;
137 close(fd);
138 }
139 };
140
141 int NewDeleter::iflag = 0;
142
143 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdDefineDeletor, TestSize.Level0)
144 {
145 int fd = open(testfilename, O_RDWR);
146
147 {
148 UniqueFdAddDeletor<NewDeleter> ufd2(fd);
149 ASSERT_EQ(NewDeleter::iflag, 0);
150 if (ufd2 == -1) {
151 std::cout << "open test.h error" << std::endl;
152 }
153 }
154 ASSERT_EQ(NewDeleter::iflag, 10);
155 };
156
157 HWTEST_F(UtilsUniqueFd, testUtilsUniqueFdDefineDeletorCloseStatus, TestSize.Level0)
158 {
159 int fd = open(testfilename, O_RDWR);
160
161 {
162 UniqueFdAddDeletor<NewDeleter> ufd2(fd);
163 }
164
165 char buf[] = "test";
166 int ret = write(fd, buf, sizeof(buf));
167 ASSERT_EQ(ret, -1);
168 };
169 } // namespace
170 } // namespace OHOS