]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/unit_tests/fcntlrangecheck_tests_11202484_src/fcntlrangecheck_tests_11202484.c
be921a3d521bfdba6e3a89cf672d0ef63e48b16c
[apple/xnu.git] / tools / tests / unit_tests / fcntlrangecheck_tests_11202484_src / fcntlrangecheck_tests_11202484.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <limits.h>
8
9 #define SUCCESS 0
10 #define FAILURE -1
11
12 int do_fcntl_lock(int fd, int cmd, short lock_type, off_t start, short when, off_t len, int ret){
13 struct flock fl;
14 bzero(&fl, sizeof(fl));
15 fl.l_start = start;
16 fl.l_len = len;
17 fl.l_type = lock_type;
18 fl.l_whence = when;
19 errno = 0;
20 int retval = fcntl(fd, cmd, &fl);
21 printf ("fcntl with flock(%lld,%lld,%d,%d) returned %d and errno %d \n", start, len, lock_type, when, retval, errno);
22 if ( retval < 0)
23 perror("fcntl");
24
25 if (retval != ret) {
26 printf("[FAILED] fcntl test failed\n");
27 exit(-1);
28 }
29 return retval;
30 }
31
32 #define read_lock(fd, offset, whence, len, ret) \
33 do_fcntl_lock(fd, F_SETLK, F_RDLCK, offset, whence, len, ret)
34 #define readw_lock(fd, offset, whence, len, ret) \
35 do_fcntl_lock(fd, F_SETLKW, F_RDLCK, offset, whence, len, ret)
36 #define write_lock(fd, offset, whence, len, ret) \
37 do_fcntl_lock(fd, F_SETLK, F_WRLCK, offset, whence, len, ret)
38 #define writew_lock(fd, offset, whence, len, ret) \
39 do_fcntl_lock(fd, F_SETLKW, F_WRLCK, offset, whence, len, ret)
40 #define un_lock(fd, offset, whence, len, ret) \
41 do_fcntl_lock(fd, F_SETLK, F_UNLCK, offset, whence, len, ret)
42 #define is_read_lock(fd, offset, whence, len, ret) \
43 do_fcntl_lock(fd, F_GETLK, F_RDLCK, offset, whence, len, ret)
44 #define is_write_lock(fd, offset, whence, len, ret) \
45 do_fcntl_lock(fd, F_GETLK, F_WRLCK, offset, whence, len, ret)
46
47
48 int main(){
49 int fd = 0;
50 char *tmpfile ="/tmp/fcntltry.txt";
51
52 unlink(tmpfile);
53 fd = creat(tmpfile, S_IRWXU);
54 if (fd < 0) {
55 perror("creat");
56 goto failed;
57 }
58
59 /* fcntl with seek position set to 1 */
60 if (lseek(fd, (off_t)1, SEEK_SET) != 1){
61 perror("lseek");
62 goto failed;
63 }
64 off_t lock_start = 0, lock_len = 0;
65
66 printf("Testing with SEEK_SET\n");
67
68 /* testing F_GETLK for SEEK_SET with lock_start = constant and len changes */
69 lock_start = 0;
70 is_read_lock(fd, lock_start, SEEK_SET, 0, SUCCESS);
71 is_read_lock(fd, lock_start, SEEK_SET, LLONG_MAX, SUCCESS);
72 is_read_lock(fd, lock_start, SEEK_SET, LLONG_MIN, FAILURE);
73
74 /* testing F_GETLK for SEEK_SET with len fixed 0 and lock_start changing */
75 lock_len = 0;
76 is_read_lock(fd, 0, SEEK_SET, lock_len, SUCCESS);
77 is_read_lock(fd, LLONG_MAX, SEEK_SET, lock_len, SUCCESS);
78 is_read_lock(fd, LLONG_MIN, SEEK_SET, lock_len, FAILURE);
79
80 /* testing F_GETLK for SEEK_SET with len fixed max and lock_start changing */
81 lock_len = LLONG_MAX;
82 is_read_lock(fd, 0, SEEK_SET, lock_len, SUCCESS);
83 is_read_lock(fd, 1, SEEK_SET, lock_len, SUCCESS);
84 is_read_lock(fd, 2, SEEK_SET, lock_len, FAILURE);
85 is_read_lock(fd, LLONG_MAX, SEEK_SET, lock_len, FAILURE);
86 is_read_lock(fd, LLONG_MIN, SEEK_SET, lock_len, FAILURE);
87
88 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
89 lock_len = LLONG_MIN;
90 is_read_lock(fd, 0, SEEK_SET, lock_len, FAILURE);
91 is_read_lock(fd, LLONG_MAX, SEEK_SET, lock_len, FAILURE);
92 is_read_lock(fd, LLONG_MIN, SEEK_SET, lock_len, FAILURE);
93
94 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
95 lock_len = 20;
96 is_read_lock(fd, 0, SEEK_SET, lock_len, SUCCESS);
97 is_read_lock(fd, 100, SEEK_SET, lock_len, SUCCESS);
98 is_read_lock(fd, -100, SEEK_SET, lock_len, FAILURE);
99
100 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
101 lock_len = -20;
102 is_read_lock(fd, 0, SEEK_SET, lock_len, FAILURE);
103 is_read_lock(fd, 100, SEEK_SET, lock_len, SUCCESS);
104 is_read_lock(fd, -100, SEEK_SET, lock_len, FAILURE);
105
106 printf("Testing with SEEK_CUR with offset 1 \n");
107
108 /* testing F_GETLK for SEEK_CUR with lock_start = constant and len changes */
109 lock_start = 0;
110 is_read_lock(fd, lock_start, SEEK_CUR, 0, SUCCESS);
111 is_read_lock(fd, lock_start, SEEK_CUR, LLONG_MAX, SUCCESS);
112 is_read_lock(fd, lock_start, SEEK_CUR, LLONG_MIN, FAILURE);
113
114 /* testing F_GETLK for SEEK_CUR with len fixed 0 and lock_start changing */
115 lock_len = 0;
116 is_read_lock(fd, 0, SEEK_CUR, lock_len, SUCCESS);
117 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
118 is_read_lock(fd, LLONG_MAX - 1, SEEK_CUR, lock_len, SUCCESS);
119 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
120
121 /* testing F_GETLK for SEEK_CUR with len fixed max and lock_start changing */
122 lock_len = LLONG_MAX;
123 is_read_lock(fd, 0, SEEK_CUR, lock_len, SUCCESS);
124 is_read_lock(fd, 1, SEEK_CUR, lock_len, FAILURE);
125 is_read_lock(fd, 2, SEEK_CUR, lock_len, FAILURE);
126 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
127 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
128
129 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
130 lock_len = LLONG_MIN;
131 is_read_lock(fd, 0, SEEK_CUR, lock_len, FAILURE);
132 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
133 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
134
135 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
136 lock_len = 20;
137 is_read_lock(fd, 0, SEEK_CUR, lock_len, SUCCESS);
138 is_read_lock(fd, 100, SEEK_CUR, lock_len, SUCCESS);
139 is_read_lock(fd, -100, SEEK_CUR, lock_len, FAILURE);
140
141 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
142 lock_len = -20;
143 is_read_lock(fd, 0, SEEK_CUR, lock_len, FAILURE);
144 is_read_lock(fd, 100, SEEK_CUR, lock_len, SUCCESS);
145 is_read_lock(fd, -100, SEEK_CUR, lock_len, FAILURE);
146
147 close(fd);
148
149 unlink(tmpfile);
150 fd = creat(tmpfile, S_IRWXU);
151 if (fd < 0) {
152 perror("creat");
153 goto failed;
154 }
155
156 /* fcntl with seek position set to 1 */
157 if (lseek(fd, (off_t)LLONG_MAX - 1, SEEK_SET) != (LLONG_MAX - 1)){
158 perror("lseek");
159 goto failed;
160 }
161
162
163 printf("Testing with SEEK_CUR with offset LLONG_MAX - 1\n");
164
165 /* testing F_GETLK for SEEK_CUR with lock_start = constant and len changes */
166 lock_start = 0;
167 is_read_lock(fd, lock_start, SEEK_CUR, 0, SUCCESS);
168 is_read_lock(fd, lock_start, SEEK_CUR, LLONG_MAX, FAILURE);
169 is_read_lock(fd, lock_start, SEEK_CUR, LLONG_MIN, FAILURE);
170 is_read_lock(fd, lock_start, SEEK_CUR, LLONG_MIN + 2, SUCCESS);
171
172 /* testing F_GETLK for SEEK_CUR with len fixed 0 and lock_start changing */
173 lock_len = 0;
174 is_read_lock(fd, 0, SEEK_CUR, lock_len, SUCCESS);
175 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
176 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
177 is_read_lock(fd, LLONG_MIN + 2, SEEK_CUR, lock_len, SUCCESS);
178
179 /* testing F_GETLK for SEEK_CUR with len fixed max and lock_start changing */
180 lock_len = LLONG_MAX;
181 is_read_lock(fd, 0, SEEK_CUR, lock_len, FAILURE);
182 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
183 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
184 is_read_lock(fd, LLONG_MIN + 2, SEEK_CUR, lock_len, SUCCESS);
185
186 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
187 lock_len = LLONG_MIN;
188 is_read_lock(fd, 0, SEEK_CUR, lock_len, FAILURE);
189 is_read_lock(fd, LLONG_MAX, SEEK_CUR, lock_len, FAILURE);
190 is_read_lock(fd, LLONG_MIN, SEEK_CUR, lock_len, FAILURE);
191
192 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
193 lock_len = 20;
194 is_read_lock(fd, 0, SEEK_CUR, lock_len, FAILURE);
195 is_read_lock(fd, -100, SEEK_CUR, lock_len, SUCCESS);
196
197 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
198 lock_len = -20;
199 is_read_lock(fd, 0, SEEK_CUR, lock_len, SUCCESS);
200 is_read_lock(fd, 100, SEEK_CUR, lock_len, FAILURE);
201 is_read_lock(fd, -100, SEEK_CUR, lock_len, SUCCESS);
202
203
204 printf("[PASSED] fcntl test passed \n");
205 return 0;
206 failed:
207 printf("[FAILED] fcntl test failed\n");
208 return -1;
209
210 }