1 /*
2 * Copyright (c) 2022 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 <fcntl.h>
17 #include "functionalext.h"
18
19 const int FAILED = -1;
20
21 /**
22 * @tc.name : dup3_0100
23 * @tc.desc : The parameter flags is O_CLOEXEC, which can assign the old file descriptor to the new file descriptor
24 * @tc.level : Level 0
25 */
dup3_0100(void)26 void dup3_0100(void)
27 {
28 int fd = open("/data/dup3test.txt", O_RDWR | O_CREAT, TEST_MODE);
29 EXPECT_TRUE("dup3_0100", fd >= 0);
30 int ret = dup3(fd, fileno(stderr), O_CLOEXEC);
31 EXPECT_EQ("dup3_0100", ret, fileno(stderr));
32 close(fd);
33 remove("/data/dup3test.txt");
34 }
35
36 /**
37 * @tc.name : dup3_0200
38 * @tc.desc : The parameter old is invalid, the old file descriptor cannot be assigned to the new file descriptor.
39 * @tc.level : Level 2
40 */
dup3_0200(void)41 void dup3_0200(void)
42 {
43 int fd = -1;
44 int ret = dup3(fd, fileno(stderr), O_CLOEXEC);
45 EXPECT_EQ("dup3_0200", ret, FAILED);
46 }
47
48 /**
49 * @tc.name : dup3_0300
50 * @tc.desc : The parameter new is invalid, the old file descriptor cannot be assigned to the new file descriptor.
51 * @tc.level : Level 2
52 */
dup3_0300(void)53 void dup3_0300(void)
54 {
55 int newfd = -1;
56 int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
57 EXPECT_TRUE("dup3_0300", fd >= 0);
58 int ret = dup3(fd, newfd, O_CLOEXEC);
59 EXPECT_EQ("dup3_0300", ret, FAILED);
60 close(fd);
61 remove("/data/readtest.txt");
62 }
63
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 dup3_0100();
67 dup3_0200();
68 dup3_0300();
69 return t_status;
70 }