xref: /freebsd/lib/libc/tests/stdio/mkostemp_test.c (revision 315ee00f)
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 
27 /*
28  * Test program for mkostemp().
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/stat.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <paths.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <atf-c.h>
43 
44 static const char template[] = "mkostemp.XXXXXXXX";
45 static int testnum;
46 
47 #define MISCFLAGS (O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC)
48 
49 static void
50 test_one(int oflags)
51 {
52 	char tmpf[sizeof(template)];
53 	struct stat st1, st2;
54 	int fd;
55 
56 	memcpy(tmpf, template, sizeof(tmpf));
57 	fd = mkostemp(tmpf, oflags);
58 	if (fd < 0) {
59 		printf("not ok %d - oflags=%#x "
60 		    "mkostemp() reported failure: %s\n",
61 		    testnum++, oflags, strerror(errno));
62 		return;
63 	}
64 	if (memcmp(tmpf, template, sizeof(tmpf) - 8 - 1) != 0) {
65 		printf("not ok %d - oflags=%#x "
66 		    "returned pathname does not match template: %s\n",
67 		    testnum++, oflags, tmpf);
68 		return;
69 	}
70 	do {
71 		if (fcntl(fd, F_GETFD) !=
72 		    (oflags & O_CLOEXEC ? FD_CLOEXEC : 0)) {
73 			printf("not ok %d - oflags=%#x "
74 			    "close-on-exec flag incorrect\n",
75 			    testnum++, oflags);
76 			break;
77 		}
78 		if ((fcntl(fd, F_GETFL) & MISCFLAGS) != (oflags & MISCFLAGS)) {
79 			printf("not ok %d - oflags=%#x "
80 			    "open flags incorrect\n",
81 			    testnum++, oflags);
82 			break;
83 		}
84 		if (stat(tmpf, &st1) == -1) {
85 			printf("not ok %d - oflags=%#x "
86 			    "cannot stat returned pathname %s: %s\n",
87 			    testnum++, oflags, tmpf, strerror(errno));
88 			break;
89 		}
90 		if (fstat(fd, &st2) == -1) {
91 			printf("not ok %d - oflags=%#x "
92 			    "cannot fstat returned fd %d: %s\n",
93 			    testnum++, oflags, fd, strerror(errno));
94 			break;
95 		}
96 		if (!S_ISREG(st1.st_mode) || (st1.st_mode & 0777) != 0600 ||
97 		    st1.st_nlink != 1 || st1.st_size != 0) {
98 			printf("not ok %d - oflags=%#x "
99 			    "named file attributes incorrect\n",
100 			    testnum++, oflags);
101 			break;
102 		}
103 		if (!S_ISREG(st2.st_mode) || (st2.st_mode & 0777) != 0600 ||
104 		    st2.st_nlink != 1 || st2.st_size != 0) {
105 			printf("not ok %d - oflags=%#x "
106 			    "opened file attributes incorrect\n",
107 			    testnum++, oflags);
108 			break;
109 		}
110 		if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
111 			printf("not ok %d - oflags=%#x "
112 			    "named and opened file do not match\n",
113 			    testnum++, oflags);
114 			break;
115 		}
116 		(void)unlink(tmpf);
117 		if (fstat(fd, &st2) == -1)
118 			printf("not ok %d - oflags=%#x "
119 			    "cannot fstat returned fd %d again: %s\n",
120 			    testnum++, oflags, fd, strerror(errno));
121 		else if (st2.st_nlink != 0)
122 			printf("not ok %d - oflags=%#x "
123 			    "st_nlink is not 0 after unlink\n",
124 			    testnum++, oflags);
125 		else
126 			printf("ok %d - oflags=%#x\n", testnum++, oflags);
127 		(void)close(fd);
128 		return;
129 	} while (0);
130 	(void)close(fd);
131 	(void)unlink(tmpf);
132 }
133 
134 ATF_TC_WITHOUT_HEAD(zero);
135 ATF_TC_BODY(zero, tc)
136 {
137 
138 	test_one(0);
139 }
140 
141 ATF_TC_WITHOUT_HEAD(O_CLOEXEC);
142 ATF_TC_BODY(O_CLOEXEC, tc)
143 {
144 
145 	test_one(O_CLOEXEC);
146 }
147 
148 ATF_TC_WITHOUT_HEAD(O_APPEND);
149 ATF_TC_BODY(O_APPEND, tc)
150 {
151 
152 	test_one(O_APPEND);
153 }
154 
155 ATF_TC_WITHOUT_HEAD(O_APPEND__O_CLOEXEC);
156 ATF_TC_BODY(O_APPEND__O_CLOEXEC, tc)
157 {
158 
159 	test_one(O_APPEND|O_CLOEXEC);
160 }
161 
162 ATF_TC_WITHOUT_HEAD(bad_flags);
163 ATF_TC_BODY(bad_flags, tc)
164 {
165 
166 	char tmpf[sizeof(template)];
167 
168 	memcpy(tmpf, template, sizeof(tmpf));
169 	ATF_REQUIRE_MSG(mkostemp(tmpf, O_CREAT) == -1,
170 		"mkostemp(O_CREAT) succeeded unexpectedly");
171 }
172 
173 ATF_TP_ADD_TCS(tp)
174 {
175 
176 	ATF_TP_ADD_TC(tp, zero);
177 	ATF_TP_ADD_TC(tp, O_CLOEXEC);
178 	ATF_TP_ADD_TC(tp, O_APPEND);
179 	ATF_TP_ADD_TC(tp, O_APPEND__O_CLOEXEC);
180 	ATF_TP_ADD_TC(tp, bad_flags);
181 
182 	return (atf_no_error());
183 }
184