]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/unit_tests/fcntlrangecheck_tests_11202484_src/fcntlrangecheck_tests_11202484.c
be921a3d521bfdba6e3a89cf672d0ef63e48b16c
12 int do_fcntl_lock(int fd
, int cmd
, short lock_type
, off_t start
, short when
, off_t len
, int ret
){
14 bzero(&fl
, sizeof(fl
));
17 fl
.l_type
= lock_type
;
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
);
26 printf("[FAILED] fcntl test failed\n");
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)
50 char *tmpfile
="/tmp/fcntltry.txt";
53 fd
= creat(tmpfile
, S_IRWXU
);
59 /* fcntl with seek position set to 1 */
60 if (lseek(fd
, (off_t
)1, SEEK_SET
) != 1){
64 off_t lock_start
= 0, lock_len
= 0;
66 printf("Testing with SEEK_SET\n");
68 /* testing F_GETLK for SEEK_SET with lock_start = constant and len changes */
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
);
74 /* testing F_GETLK for SEEK_SET with len fixed 0 and lock_start changing */
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
);
80 /* testing F_GETLK for SEEK_SET with len fixed max and lock_start changing */
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
);
88 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
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
);
94 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
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
);
100 /* testing F_GETLK for SEEK_SET with len fixed min and lock_start changing */
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
);
106 printf("Testing with SEEK_CUR with offset 1 \n");
108 /* testing F_GETLK for SEEK_CUR with lock_start = constant and len changes */
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
);
114 /* testing F_GETLK for SEEK_CUR with len fixed 0 and lock_start changing */
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
);
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
);
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
);
135 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
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
);
141 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
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
);
150 fd
= creat(tmpfile
, S_IRWXU
);
156 /* fcntl with seek position set to 1 */
157 if (lseek(fd
, (off_t
)LLONG_MAX
- 1, SEEK_SET
) != (LLONG_MAX
- 1)){
163 printf("Testing with SEEK_CUR with offset LLONG_MAX - 1\n");
165 /* testing F_GETLK for SEEK_CUR with lock_start = constant and len changes */
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
);
172 /* testing F_GETLK for SEEK_CUR with len fixed 0 and lock_start changing */
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
);
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
);
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
);
192 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
194 is_read_lock(fd
, 0, SEEK_CUR
, lock_len
, FAILURE
);
195 is_read_lock(fd
, -100, SEEK_CUR
, lock_len
, SUCCESS
);
197 /* testing F_GETLK for SEEK_CUR with len fixed min and lock_start changing */
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
);
204 printf("[PASSED] fcntl test passed \n");
207 printf("[FAILED] fcntl test failed\n");