xref: /freebsd/tests/sys/file/fcntlflags_test.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2013 Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 /*
36  * O_ACCMODE is currently defined incorrectly. This is what it should be.
37  * Various code depends on the incorrect value.
38  */
39 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
40 
41 static int testnum;
42 
43 static void
44 subtests(const char *path, int omode, const char *omodetext)
45 {
46 	int fd, flags1, flags2, flags3;
47 
48 	fd = open(path, omode);
49 	if (fd == -1)
50 		printf("not ok %d - open(\"%s\", %s) failed\n",
51 		    testnum++, path, omodetext);
52 	else
53 		printf("ok %d - open(\"%s\", %s) succeeded\n",
54 		    testnum++, path, omodetext);
55 	flags1 = fcntl(fd, F_GETFL);
56 	if (flags1 == -1)
57 		printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
58 	else if ((flags1 & CORRECT_O_ACCMODE) == omode)
59 		printf("ok %d - fcntl(F_GETFL) gave correct result\n",
60 		    testnum++);
61 	else
62 		printf("not ok %d - fcntl(F_GETFL) gave incorrect result "
63 		    "(%#x & %#x != %#x)\n",
64 		    testnum++, flags1, CORRECT_O_ACCMODE, omode);
65 	if (fcntl(fd, F_SETFL, flags1) == -1)
66 		printf("not ok %d - fcntl(F_SETFL) same flags failed\n",
67 		    testnum++);
68 	else
69 		printf("ok %d - fcntl(F_SETFL) same flags succeeded\n",
70 		    testnum++);
71 	flags2 = fcntl(fd, F_GETFL);
72 	if (flags2 == -1)
73 		printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
74 	else if (flags2 == flags1)
75 		printf("ok %d - fcntl(F_GETFL) gave same result\n",
76 		    testnum++);
77 	else
78 		printf("not ok %d - fcntl(F_SETFL) caused fcntl(F_GETFL) to "
79 		    "change from %#x to %#x\n",
80 		    testnum++, flags1, flags2);
81 	if (fcntl(fd, F_SETFL, flags2 | O_NONBLOCK) == -1)
82 		printf("not ok %d - fcntl(F_SETFL) O_NONBLOCK failed\n",
83 		    testnum++);
84 	else
85 		printf("ok %d - fcntl(F_SETFL) O_NONBLOCK succeeded\n",
86 		    testnum++);
87 	flags3 = fcntl(fd, F_GETFL);
88 	if (flags3 == -1)
89 		printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
90 	else if (flags3 == (flags2 | O_NONBLOCK))
91 		printf("ok %d - fcntl(F_GETFL) gave expected result\n",
92 		    testnum++);
93 	else
94 		printf("not ok %d - fcntl(F_SETFL) gave unexpected result "
95 		    "(%#x != %#x)\n",
96 		    testnum++, flags3, flags2 | O_NONBLOCK);
97 	(void)close(fd);
98 }
99 
100 int
101 main(int argc __unused, char **argv __unused)
102 {
103 	printf("1..24\n");
104 	testnum = 1;
105 	subtests("/dev/null", O_RDONLY, "O_RDONLY");
106 	subtests("/dev/null", O_WRONLY, "O_WRONLY");
107 	subtests("/dev/null", O_RDWR, "O_RDWR");
108 	subtests("/bin/sh", O_EXEC, "O_EXEC");
109 	return (0);
110 }
111