digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
[gnulib.git] / tests / test-flock.c
1 /* Test of flock() function.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include <sys/file.h>
26
27 #define ASSERT(expr) \
28   do                                                                    \
29     {                                                                   \
30       if (!(expr))                                                      \
31         {                                                               \
32           fprintf (stderr, "%s:%d: assertion failed, errno = %d\n",     \
33                    __FILE__, __LINE__, errno);                          \
34           fflush (stderr);                                              \
35           abort ();                                                     \
36         }                                                               \
37     }                                                                   \
38   while (0)
39
40 static void
41 test_shared (const char *file, int fd)
42 {
43   /* Should be able to acquire several shared locks on a file, through
44    * different file table entries.
45    */
46   int fd2, r;
47
48   ASSERT (flock (fd, LOCK_SH) == 0);
49
50   fd2 = open (file, O_RDWR, 0644);
51   ASSERT (fd2 >= 0);
52
53   r = flock (fd2, LOCK_SH | LOCK_NB);
54   ASSERT (r == 0);              /* Was able to acquire a second shared lock. */
55
56   ASSERT (flock (fd, LOCK_UN) == 0);
57   ASSERT (close (fd2) == 0);
58 }
59
60 static void
61 test_exclusive (const char *file, int fd)
62 {
63   /* Should not be able to acquire more than one exclusive lock on a file. */
64   int fd2, r;
65
66   ASSERT (flock (fd, LOCK_EX) == 0);
67
68   fd2 = open (file, O_RDWR, 0644);
69   ASSERT (fd2 >= 0);
70
71   r = flock (fd2, LOCK_SH | LOCK_NB);
72   ASSERT (r == -1);             /* Was unable to acquire a second exclusive lock. */
73
74   ASSERT (flock (fd, LOCK_UN) == 0);
75   ASSERT (close (fd2) == 0);
76 }
77
78 int
79 main (int argc, char *argv[])
80 {
81   int fd;
82   const char *file = "test-flock.txt";
83
84   /* Open a non-empty file for testing. */
85   fd = open (file, O_RDWR | O_CREAT | O_TRUNC, 0644);
86   ASSERT (fd >= 0);
87   ASSERT (write (fd, "hello", 5) == 5);
88
89   /* Some impossible operation codes which should never be accepted. */
90   ASSERT (flock (fd, LOCK_SH | LOCK_EX) == -1);
91   ASSERT (errno == EINVAL);
92   ASSERT (flock (fd, LOCK_SH | LOCK_UN) == -1);
93   ASSERT (errno == EINVAL);
94   ASSERT (flock (fd, LOCK_EX | LOCK_UN) == -1);
95   ASSERT (errno == EINVAL);
96   ASSERT (flock (fd, 0) == -1);
97   ASSERT (errno == EINVAL);
98
99   test_shared (file, fd);
100   test_exclusive (file, fd);
101
102   /* Close and remove the test file. */
103   ASSERT (close (fd) == 0);
104   ASSERT (unlink (file) == 0);
105
106   return 0;
107 }