]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/lib/libc/stdio/test-mkostemp.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / lib / libc / stdio / test-mkostemp.c
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 __FBSDID("$FreeBSD$");
33
34 #include <sys/stat.h>
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 static const char template[] = _PATH_TMP "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 static void
135 test_badflags(void)
136 {
137         char tmpf[sizeof(template)];
138
139         memcpy(tmpf, template, sizeof(tmpf));
140         if (mkostemp(tmpf, O_CREAT) == -1)
141                 printf("ok %d - mkostemp(O_CREAT) correctly failed\n",
142                     testnum++);
143         else
144                 printf("not ok %d - mkostemp(O_CREAT) wrongly succeeded\n",
145                     testnum++);
146 }
147
148 int
149 main(int argc, char *argv[])
150 {
151         int i;
152         const char *e;
153
154         printf("1..5\n");
155         testnum = 1;
156
157         test_one(0);
158         test_one(O_CLOEXEC);
159         test_one(O_APPEND);
160         test_one(O_APPEND | O_CLOEXEC);
161         test_badflags();
162
163         return (0);
164 }