2 * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/signal.h>
34 #include <sys/types.h>
35 #include <sys/fcntl.h>
47 * Test that flopen() can create a file.
50 test_flopen_create(void)
52 const char *fn = "test_flopen_create";
53 const char *result = NULL;
57 fd = flopen(fn, O_RDWR|O_CREAT, 0640);
59 result = strerror(errno);
68 * Test that flopen() can open an existing file.
71 test_flopen_open(void)
73 const char *fn = "test_flopen_open";
74 const char *result = NULL;
77 fd = open(fn, O_RDWR|O_CREAT, 0640);
79 result = strerror(errno);
82 fd = flopen(fn, O_RDWR);
84 result = strerror(errno);
94 * Test that flopen() can lock against itself
97 test_flopen_lock_self(void)
99 const char *fn = "test_flopen_lock_self";
100 const char *result = NULL;
104 fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
106 result = strerror(errno);
108 fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
110 result = "second open succeeded";
120 * Test that flopen() can lock against other processes
123 test_flopen_lock_other(void)
125 const char *fn = "test_flopen_lock_other";
126 const char *result = NULL;
127 volatile int fd1, fd2;
130 fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
132 result = strerror(errno);
136 fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
141 result = "vfork() doesn't work as expected";
143 result = "second open succeeded";
151 * Test that child processes inherit the lock
154 test_flopen_lock_child(void)
156 const char *fn = "test_flopen_lock_child";
157 const char *result = NULL;
159 volatile int fd1, fd2;
162 fd1 = flopen(fn, O_RDWR|O_CREAT, 0640);
164 result = strerror(errno);
166 if ((pid = fork()) == 0) {
167 select(0, 0, 0, 0, 0);
173 fd2 = flopen(fn, O_RDWR|O_NONBLOCK);
178 result = "vfork() doesn't work as expected";
180 result = "second open succeeded";
189 const char *(*func)(void);
191 { "flopen_create", test_flopen_create },
192 { "flopen_open", test_flopen_open },
193 { "flopen_lock_self", test_flopen_lock_self },
194 { "flopen_lock_other", test_flopen_lock_other },
195 { "flopen_lock_child", test_flopen_lock_child },
204 nt = sizeof(t) / sizeof(*t);
205 printf("1..%d\n", nt);
206 for (i = 0; i < nt; ++i) {
207 if ((result = t[i].func()) != NULL)
208 printf("not ok %d - %s # %s\n", i + 1,
211 printf("ok %d - %s\n", i + 1,