]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/regression/lib/libutil/test-flopen.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / regression / lib / libutil / test-flopen.c
1 /*-
2  * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <sys/fcntl.h>
35
36 #include <errno.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <libutil.h>
44
45 /*
46  * Test that flopen() can create a file.
47  */
48 const char *
49 test_flopen_create(void)
50 {
51         const char *fn = "test_flopen_create";
52         const char *result = NULL;
53         int fd;
54
55         unlink(fn);
56         fd = flopen(fn, O_RDWR|O_CREAT, 0640);
57         if (fd < 0) {
58                 result = strerror(errno);
59         } else {
60                 close(fd);
61         }
62         unlink(fn);
63         return (result);
64 }
65
66 /*
67  * Test that flopen() can open an existing file.
68  */
69 const char *
70 test_flopen_open(void)
71 {
72         const char *fn = "test_flopen_open";
73         const char *result = NULL;
74         int fd;
75
76         fd = open(fn, O_RDWR|O_CREAT, 0640);
77         if (fd < 0) {
78                 result = strerror(errno);
79         } else {
80                 close(fd);
81                 fd = flopen(fn, O_RDWR);
82                 if (fd < 0) {
83                         result = strerror(errno);
84                 } else {
85                         close(fd);
86                 }
87         }
88         unlink(fn);
89         return (result);
90 }
91
92 /*
93  * Test that flopen() can lock against itself
94  */
95 const char *
96 test_flopen_lock_self(void)
97 {
98         const char *fn = "test_flopen_lock";
99         const char *result = NULL;
100         int fd1, fd2;
101
102         unlink(fn);
103         fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
104         if (fd1 < 0) {
105                 result = strerror(errno);
106         } else {
107                 fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
108                 if (fd2 >= 0) {
109                         result = "second open succeeded";
110                         close(fd2);
111                 }
112                 close(fd1);
113         }
114         unlink(fn);
115         return (result);
116 }
117
118 /*
119  * Test that flopen() can lock against other processes
120  */
121 const char *
122 test_flopen_lock_other(void)
123 {
124         const char *fn = "test_flopen_lock";
125         const char *result = NULL;
126         volatile int fd1, fd2;
127
128         unlink(fn);
129         fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
130         if (fd1 < 0) {
131                 result = strerror(errno);
132         } else {
133                 fd2 = -42;
134                 if (vfork() == 0) {
135                         fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
136                         close(fd2);
137                         _exit(0);
138                 }
139                 if (fd2 == -42)
140                         result = "vfork() doesn't work as expected";
141                 if (fd2 >= 0)
142                         result = "second open succeeded";
143                 close(fd1);
144         }
145         unlink(fn);
146         return (result);
147 }
148
149 static struct test {
150         const char *name;
151         const char *(*func)(void);
152 } t[] = {
153         { "flopen_create", test_flopen_create },
154         { "flopen_open", test_flopen_open },
155         { "flopen_lock_self", test_flopen_lock_self },
156         { "flopen_lock_other", test_flopen_lock_other },
157 };
158
159 int
160 main(void)
161 {
162         const char *result;
163         int i, nt;
164
165         nt = sizeof(t) / sizeof(*t);
166         printf("1..%d\n", nt);
167         for (i = 0; i < nt; ++i) {
168                 if ((result = t[i].func()) != NULL)
169                         printf("not ok %d - %s # %s\n", i + 1,
170                             t[i].name, result);
171                 else
172                         printf("ok %d - %s\n", i + 1,
173                             t[i].name);
174         }
175         exit(0);
176 }