da3c956afb1dbdf727a6286eb4cef586a0f76759
[samba.git] / source4 / torture / raw / lock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for various lock operations
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26
27 #define CHECK_STATUS(status, correct) do { \
28         if (!NT_STATUS_EQUAL(status, correct)) { \
29                 printf("(%s) Incorrect status %s - should be %s\n", \
30                        __location__, nt_errstr(status), nt_errstr(correct)); \
31                 ret = False; \
32                 goto done; \
33         }} while (0)
34
35 #define CHECK_VALUE(v, correct) do { \
36         if ((v) != (correct)) { \
37                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
38                        __location__, #v, v, correct); \
39                 ret = False; \
40                 goto done; \
41         }} while (0)
42
43 #define BASEDIR "\\testlock"
44
45
46 /*
47   test SMBlock and SMBunlock ops
48 */
49 static BOOL test_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
50 {
51         union smb_lock io;
52         NTSTATUS status;
53         BOOL ret = True;
54         int fnum;
55         const char *fname = BASEDIR "\\test.txt";
56
57         if (!torture_setup_dir(cli, BASEDIR)) {
58                 return False;
59         }
60
61         printf("Testing RAW_LOCK_LOCK\n");
62         io.generic.level = RAW_LOCK_LOCK;
63         
64         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
65         if (fnum == -1) {
66                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
67                 ret = False;
68                 goto done;
69         }
70
71         printf("Trying 0/0 lock\n");
72         io.lock.level = RAW_LOCK_LOCK;
73         io.lock.in.fnum = fnum;
74         io.lock.in.count = 0;
75         io.lock.in.offset = 0;
76         status = smb_raw_lock(cli->tree, &io);
77         CHECK_STATUS(status, NT_STATUS_OK);
78         cli->session->pid++;
79         status = smb_raw_lock(cli->tree, &io);
80         CHECK_STATUS(status, NT_STATUS_OK);
81         cli->session->pid--;
82         io.lock.level = RAW_LOCK_UNLOCK;
83         status = smb_raw_lock(cli->tree, &io);
84         CHECK_STATUS(status, NT_STATUS_OK);
85
86         printf("Trying 0/1 lock\n");
87         io.lock.level = RAW_LOCK_LOCK;
88         io.lock.in.fnum = fnum;
89         io.lock.in.count = 1;
90         io.lock.in.offset = 0;
91         status = smb_raw_lock(cli->tree, &io);
92         CHECK_STATUS(status, NT_STATUS_OK);
93         cli->session->pid++;
94         status = smb_raw_lock(cli->tree, &io);
95         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
96         cli->session->pid--;
97         io.lock.level = RAW_LOCK_UNLOCK;
98         status = smb_raw_lock(cli->tree, &io);
99         CHECK_STATUS(status, NT_STATUS_OK);
100         io.lock.level = RAW_LOCK_UNLOCK;
101         status = smb_raw_lock(cli->tree, &io);
102         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
103
104         printf("Trying 0xEEFFFFFF lock\n");
105         io.lock.level = RAW_LOCK_LOCK;
106         io.lock.in.fnum = fnum;
107         io.lock.in.count = 4000;
108         io.lock.in.offset = 0xEEFFFFFF;
109         status = smb_raw_lock(cli->tree, &io);
110         CHECK_STATUS(status, NT_STATUS_OK);
111         cli->session->pid++;
112         status = smb_raw_lock(cli->tree, &io);
113         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
114         cli->session->pid--;
115         io.lock.level = RAW_LOCK_UNLOCK;
116         status = smb_raw_lock(cli->tree, &io);
117         CHECK_STATUS(status, NT_STATUS_OK);
118         io.lock.level = RAW_LOCK_UNLOCK;
119         status = smb_raw_lock(cli->tree, &io);
120         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
121
122         printf("Trying 0xEF000000 lock\n");
123         io.lock.level = RAW_LOCK_LOCK;
124         io.lock.in.fnum = fnum;
125         io.lock.in.count = 4000;
126         io.lock.in.offset = 0xEEFFFFFF;
127         status = smb_raw_lock(cli->tree, &io);
128         CHECK_STATUS(status, NT_STATUS_OK);
129         cli->session->pid++;
130         status = smb_raw_lock(cli->tree, &io);
131         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
132         cli->session->pid--;
133         io.lock.level = RAW_LOCK_UNLOCK;
134         status = smb_raw_lock(cli->tree, &io);
135         CHECK_STATUS(status, NT_STATUS_OK);
136         io.lock.level = RAW_LOCK_UNLOCK;
137         status = smb_raw_lock(cli->tree, &io);
138         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
139
140         printf("Trying max lock\n");
141         io.lock.level = RAW_LOCK_LOCK;
142         io.lock.in.fnum = fnum;
143         io.lock.in.count = 4000;
144         io.lock.in.offset = 0xEF000000;
145         status = smb_raw_lock(cli->tree, &io);
146         CHECK_STATUS(status, NT_STATUS_OK);
147         cli->session->pid++;
148         status = smb_raw_lock(cli->tree, &io);
149         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
150         cli->session->pid--;
151         io.lock.level = RAW_LOCK_UNLOCK;
152         status = smb_raw_lock(cli->tree, &io);
153         CHECK_STATUS(status, NT_STATUS_OK);
154         io.lock.level = RAW_LOCK_UNLOCK;
155         status = smb_raw_lock(cli->tree, &io);
156         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
157
158         printf("Trying wrong pid unlock\n");
159         io.lock.level = RAW_LOCK_LOCK;
160         io.lock.in.fnum = fnum;
161         io.lock.in.count = 4002;
162         io.lock.in.offset = 10001;
163         status = smb_raw_lock(cli->tree, &io);
164         CHECK_STATUS(status, NT_STATUS_OK);
165         cli->session->pid++;
166         io.lock.level = RAW_LOCK_UNLOCK;
167         status = smb_raw_lock(cli->tree, &io);
168         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
169         cli->session->pid--;
170         status = smb_raw_lock(cli->tree, &io);
171         CHECK_STATUS(status, NT_STATUS_OK);
172
173 done:
174         smbcli_close(cli->tree, fnum);
175         smb_raw_exit(cli->session);
176         smbcli_deltree(cli->tree, BASEDIR);
177         return ret;
178 }
179
180
181 /*
182   test locking&X ops
183 */
184 static BOOL test_lockx(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
185 {
186         union smb_lock io;
187         struct smb_lock_entry lock[1];
188         NTSTATUS status;
189         BOOL ret = True;
190         int fnum;
191         const char *fname = BASEDIR "\\test.txt";
192
193         if (!torture_setup_dir(cli, BASEDIR)) {
194                 return False;
195         }
196
197         printf("Testing RAW_LOCK_LOCKX\n");
198         io.generic.level = RAW_LOCK_LOCKX;
199         
200         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
201         if (fnum == -1) {
202                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
203                 ret = False;
204                 goto done;
205         }
206
207         io.lockx.level = RAW_LOCK_LOCKX;
208         io.lockx.in.fnum = fnum;
209         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
210         io.lockx.in.timeout = 0;
211         io.lockx.in.ulock_cnt = 0;
212         io.lockx.in.lock_cnt = 1;
213         lock[0].pid = cli->session->pid;
214         lock[0].offset = 10;
215         lock[0].count = 1;
216         io.lockx.in.locks = &lock[0];
217         status = smb_raw_lock(cli->tree, &io);
218         CHECK_STATUS(status, NT_STATUS_OK);
219
220
221         printf("Trying 0xEEFFFFFF lock\n");
222         io.lockx.in.ulock_cnt = 0;
223         io.lockx.in.lock_cnt = 1;
224         lock[0].count = 4000;
225         lock[0].offset = 0xEEFFFFFF;
226         status = smb_raw_lock(cli->tree, &io);
227         CHECK_STATUS(status, NT_STATUS_OK);
228         lock[0].pid++;
229         status = smb_raw_lock(cli->tree, &io);
230         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
231         lock[0].pid--;
232         io.lockx.in.ulock_cnt = 1;
233         io.lockx.in.lock_cnt = 0;
234         status = smb_raw_lock(cli->tree, &io);
235         CHECK_STATUS(status, NT_STATUS_OK);
236         status = smb_raw_lock(cli->tree, &io);
237         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
238
239         printf("Trying 0xEF000000 lock\n");
240         io.lockx.in.ulock_cnt = 0;
241         io.lockx.in.lock_cnt = 1;
242         lock[0].count = 4000;
243         lock[0].offset = 0xEF000000;
244         status = smb_raw_lock(cli->tree, &io);
245         CHECK_STATUS(status, NT_STATUS_OK);
246         lock[0].pid++;
247         status = smb_raw_lock(cli->tree, &io);
248         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
249         lock[0].pid--;
250         io.lockx.in.ulock_cnt = 1;
251         io.lockx.in.lock_cnt = 0;
252         status = smb_raw_lock(cli->tree, &io);
253         CHECK_STATUS(status, NT_STATUS_OK);
254         status = smb_raw_lock(cli->tree, &io);
255         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
256
257         printf("Trying zero lock\n");
258         io.lockx.in.ulock_cnt = 0;
259         io.lockx.in.lock_cnt = 1;
260         lock[0].count = 0;
261         lock[0].offset = ~0;
262         status = smb_raw_lock(cli->tree, &io);
263         CHECK_STATUS(status, NT_STATUS_OK);
264         lock[0].pid++;
265         status = smb_raw_lock(cli->tree, &io);
266         CHECK_STATUS(status, NT_STATUS_OK);
267         lock[0].pid--;
268         io.lockx.in.ulock_cnt = 1;
269         io.lockx.in.lock_cnt = 0;
270         status = smb_raw_lock(cli->tree, &io);
271         CHECK_STATUS(status, NT_STATUS_OK);
272         status = smb_raw_lock(cli->tree, &io);
273         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
274
275         printf("Trying max lock\n");
276         io.lockx.in.ulock_cnt = 0;
277         io.lockx.in.lock_cnt = 1;
278         lock[0].count = 0;
279         lock[0].offset = ~0;
280         status = smb_raw_lock(cli->tree, &io);
281         CHECK_STATUS(status, NT_STATUS_OK);
282         lock[0].pid++;
283         status = smb_raw_lock(cli->tree, &io);
284         CHECK_STATUS(status, NT_STATUS_OK);
285         lock[0].pid--;
286         io.lockx.in.ulock_cnt = 1;
287         io.lockx.in.lock_cnt = 0;
288         status = smb_raw_lock(cli->tree, &io);
289         CHECK_STATUS(status, NT_STATUS_OK);
290         status = smb_raw_lock(cli->tree, &io);
291         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
292
293         printf("Trying 2^63\n");
294         io.lockx.in.ulock_cnt = 0;
295         io.lockx.in.lock_cnt = 1;
296         lock[0].count = 1;
297         lock[0].offset = 1;
298         lock[0].offset <<= 63;
299         status = smb_raw_lock(cli->tree, &io);
300         CHECK_STATUS(status, NT_STATUS_OK);
301         lock[0].pid++;
302         status = smb_raw_lock(cli->tree, &io);
303         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
304         lock[0].pid--;
305         io.lockx.in.ulock_cnt = 1;
306         io.lockx.in.lock_cnt = 0;
307         status = smb_raw_lock(cli->tree, &io);
308         CHECK_STATUS(status, NT_STATUS_OK);
309         status = smb_raw_lock(cli->tree, &io);
310         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
311
312         printf("Trying 2^63 - 1\n");
313         io.lockx.in.ulock_cnt = 0;
314         io.lockx.in.lock_cnt = 1;
315         lock[0].count = 1;
316         lock[0].offset = 1;
317         lock[0].offset <<= 63;
318         lock[0].offset--;
319         status = smb_raw_lock(cli->tree, &io);
320         CHECK_STATUS(status, NT_STATUS_OK);
321         lock[0].pid++;
322         status = smb_raw_lock(cli->tree, &io);
323         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
324         lock[0].pid--;
325         io.lockx.in.ulock_cnt = 1;
326         io.lockx.in.lock_cnt = 0;
327         status = smb_raw_lock(cli->tree, &io);
328         CHECK_STATUS(status, NT_STATUS_OK);
329         status = smb_raw_lock(cli->tree, &io);
330         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
331
332         printf("Trying max lock 2\n");
333         io.lockx.in.ulock_cnt = 0;
334         io.lockx.in.lock_cnt = 1;
335         lock[0].count = 1;
336         lock[0].offset = ~0;
337         status = smb_raw_lock(cli->tree, &io);
338         CHECK_STATUS(status, NT_STATUS_OK);
339         lock[0].pid++;
340         lock[0].count = 2;
341         status = smb_raw_lock(cli->tree, &io);
342         CHECK_STATUS(status, NT_STATUS_OK);
343         lock[0].pid--;
344         io.lockx.in.ulock_cnt = 1;
345         io.lockx.in.lock_cnt = 0;
346         lock[0].count = 1;
347         status = smb_raw_lock(cli->tree, &io);
348         CHECK_STATUS(status, NT_STATUS_OK);
349         status = smb_raw_lock(cli->tree, &io);
350         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
351
352 done:
353         smbcli_close(cli->tree, fnum);
354         smb_raw_exit(cli->session);
355         smbcli_deltree(cli->tree, BASEDIR);
356         return ret;
357 }
358
359
360 /*
361   test high pid
362 */
363 static BOOL test_pidhigh(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
364 {
365         union smb_lock io;
366         struct smb_lock_entry lock[1];
367         NTSTATUS status;
368         BOOL ret = True;
369         int fnum;
370         const char *fname = BASEDIR "\\test.txt";
371         uint8_t c = 1;
372
373         if (!torture_setup_dir(cli, BASEDIR)) {
374                 return False;
375         }
376
377         printf("Testing high pid\n");
378         io.generic.level = RAW_LOCK_LOCKX;
379
380         cli->session->pid = 1;
381         
382         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
383         if (fnum == -1) {
384                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
385                 ret = False;
386                 goto done;
387         }
388
389         if (smbcli_write(cli->tree, fnum, 0, &c, 0, 1) != 1) {
390                 printf("Failed to write 1 byte - %s\n", smbcli_errstr(cli->tree));
391                 ret = False;
392                 goto done;
393         }
394
395         io.lockx.level = RAW_LOCK_LOCKX;
396         io.lockx.in.fnum = fnum;
397         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
398         io.lockx.in.timeout = 0;
399         io.lockx.in.ulock_cnt = 0;
400         io.lockx.in.lock_cnt = 1;
401         lock[0].pid = cli->session->pid;
402         lock[0].offset = 0;
403         lock[0].count = 0xFFFFFFFF;
404         io.lockx.in.locks = &lock[0];
405         status = smb_raw_lock(cli->tree, &io);
406         CHECK_STATUS(status, NT_STATUS_OK);
407
408         if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
409                 printf("Failed to read 1 byte - %s\n", smbcli_errstr(cli->tree));
410                 ret = False;
411                 goto done;
412         }
413
414         cli->session->pid |= 0x10000;
415
416         cli->session->pid = 2;
417
418         if (smbcli_read(cli->tree, fnum, &c, 0, 1) == 1) {
419                 printf("pid is incorrect handled for read with lock!\n");
420                 ret = False;
421                 goto done;
422         }
423
424         cli->session->pid = 0x10001;
425
426         if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
427                 printf("High pid is used on this server!\n");
428                 ret = False;
429         } else {
430                 printf("High pid is not used on this server (correct)\n");
431         }
432
433 done:
434         smbcli_close(cli->tree, fnum);
435         smb_raw_exit(cli->session);
436         smbcli_deltree(cli->tree, BASEDIR);
437         return ret;
438 }
439
440
441 /*
442   test locking&X async operation
443 */
444 static BOOL test_async(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
445 {
446         union smb_lock io;
447         struct smb_lock_entry lock[2];
448         NTSTATUS status;
449         BOOL ret = True;
450         int fnum;
451         const char *fname = BASEDIR "\\test.txt";
452         time_t t;
453         struct smbcli_request *req;
454
455         if (!torture_setup_dir(cli, BASEDIR)) {
456                 return False;
457         }
458
459         printf("Testing LOCKING_ANDX_CANCEL_LOCK\n");
460         io.generic.level = RAW_LOCK_LOCKX;
461         
462         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
463         if (fnum == -1) {
464                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
465                 ret = False;
466                 goto done;
467         }
468
469         io.lockx.level = RAW_LOCK_LOCKX;
470         io.lockx.in.fnum = fnum;
471         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
472         io.lockx.in.timeout = 0;
473         io.lockx.in.ulock_cnt = 0;
474         io.lockx.in.lock_cnt = 1;
475         lock[0].pid = cli->session->pid;
476         lock[0].offset = 100;
477         lock[0].count = 10;
478         io.lockx.in.locks = &lock[0];
479         status = smb_raw_lock(cli->tree, &io);
480         CHECK_STATUS(status, NT_STATUS_OK);
481
482         t = time(NULL);
483
484         printf("testing cancel by CANCEL_LOCK\n");
485
486         /* setup a timed lock */
487         io.lockx.in.timeout = 10000;
488         req = smb_raw_lock_send(cli->tree, &io);
489         if (req == NULL) {
490                 printf("Failed to setup timed lock (%s)\n", __location__);
491                 ret = False;
492                 goto done;
493         }
494
495         /* cancel the wrong range */
496         lock[0].offset = 0;
497         io.lockx.in.timeout = 0;
498         io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
499         status = smb_raw_lock(cli->tree, &io);
500         CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
501
502         /* cancel with the wrong bits set */
503         lock[0].offset = 100;
504         io.lockx.in.timeout = 0;
505         io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
506         status = smb_raw_lock(cli->tree, &io);
507         CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
508
509         /* cancel the right range */
510         lock[0].offset = 100;
511         io.lockx.in.timeout = 0;
512         io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
513         status = smb_raw_lock(cli->tree, &io);
514         CHECK_STATUS(status, NT_STATUS_OK);
515
516         /* receive the failed lock request */
517         status = smbcli_request_simple_recv(req);
518         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
519
520         if (time(NULL) > t+2) {
521                 printf("lock cancel was not immediate (%s)\n", __location__);
522                 ret = False;
523                 goto done;
524         }
525
526         printf("testing cancel by unlock\n");
527         io.lockx.in.ulock_cnt = 0;
528         io.lockx.in.lock_cnt = 1;
529         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
530         io.lockx.in.timeout = 0;
531         status = smb_raw_lock(cli->tree, &io);
532         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
533
534         io.lockx.in.timeout = 5000;
535         req = smb_raw_lock_send(cli->tree, &io);
536         if (req == NULL) {
537                 printf("Failed to setup timed lock (%s)\n", __location__);
538                 ret = False;
539                 goto done;
540         }
541
542         io.lockx.in.ulock_cnt = 1;
543         io.lockx.in.lock_cnt = 0;
544         status = smb_raw_lock(cli->tree, &io);
545         CHECK_STATUS(status, NT_STATUS_OK);
546
547         t = time(NULL);
548         status = smbcli_request_simple_recv(req);
549         CHECK_STATUS(status, NT_STATUS_OK);
550
551         if (time(NULL) > t+2) {
552                 printf("lock cancel by unlock was not immediate (%s) - took %d secs\n", 
553                        __location__, (int)(time(NULL)-t));
554                 ret = False;
555                 goto done;
556         }
557
558
559         printf("testing cancel by close\n");
560         io.lockx.in.ulock_cnt = 0;
561         io.lockx.in.lock_cnt = 1;
562         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
563         io.lockx.in.timeout = 0;
564         status = smb_raw_lock(cli->tree, &io);
565         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
566
567         io.lockx.in.timeout = 10000;
568         req = smb_raw_lock_send(cli->tree, &io);
569         if (req == NULL) {
570                 printf("Failed to setup timed lock (%s)\n", __location__);
571                 ret = False;
572                 goto done;
573         }
574
575         smbcli_close(cli->tree, fnum);
576
577         status = smbcli_request_simple_recv(req);
578         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
579
580         if (time(NULL) > t+2) {
581                 printf("lock cancel by unlock was not immediate (%s)\n", __location__);
582                 ret = False;
583                 goto done;
584         }
585         
586
587 done:
588         smbcli_close(cli->tree, fnum);
589         smb_raw_exit(cli->session);
590         smbcli_deltree(cli->tree, BASEDIR);
591         return ret;
592 }
593
594
595 /*
596   test LOCKING_ANDX_CHANGE_LOCKTYPE
597 */
598 static BOOL test_changetype(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
599 {
600         union smb_lock io;
601         struct smb_lock_entry lock[2];
602         NTSTATUS status;
603         BOOL ret = True;
604         int fnum;
605         uint8_t c = 0;
606         const char *fname = BASEDIR "\\test.txt";
607
608         if (!torture_setup_dir(cli, BASEDIR)) {
609                 return False;
610         }
611
612         printf("Testing LOCKING_ANDX_CHANGE_LOCKTYPE\n");
613         io.generic.level = RAW_LOCK_LOCKX;
614         
615         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
616         if (fnum == -1) {
617                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
618                 ret = False;
619                 goto done;
620         }
621
622         io.lockx.level = RAW_LOCK_LOCKX;
623         io.lockx.in.fnum = fnum;
624         io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
625         io.lockx.in.timeout = 0;
626         io.lockx.in.ulock_cnt = 0;
627         io.lockx.in.lock_cnt = 1;
628         lock[0].pid = cli->session->pid;
629         lock[0].offset = 100;
630         lock[0].count = 10;
631         io.lockx.in.locks = &lock[0];
632         status = smb_raw_lock(cli->tree, &io);
633         CHECK_STATUS(status, NT_STATUS_OK);
634
635         if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
636                 printf("allowed write on read locked region (%s)\n", __location__);
637                 ret = False;
638                 goto done;
639         }
640
641         /* windows server don't seem to support this */
642         io.lockx.in.mode = LOCKING_ANDX_CHANGE_LOCKTYPE;
643         status = smb_raw_lock(cli->tree, &io);
644         CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
645
646         if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
647                 printf("allowed write after lock change (%s)\n", __location__);
648                 ret = False;
649                 goto done;
650         }
651
652 done:
653         smbcli_close(cli->tree, fnum);
654         smb_raw_exit(cli->session);
655         smbcli_deltree(cli->tree, BASEDIR);
656         return ret;
657 }
658
659
660 /* 
661    basic testing of lock calls
662 */
663 BOOL torture_raw_lock(void)
664 {
665         struct smbcli_state *cli;
666         BOOL ret = True;
667         TALLOC_CTX *mem_ctx;
668
669         if (!torture_open_connection(&cli)) {
670                 return False;
671         }
672
673         mem_ctx = talloc_init("torture_raw_lock");
674
675         ret &= test_lockx(cli, mem_ctx);
676         ret &= test_lock(cli, mem_ctx);
677         ret &= test_pidhigh(cli, mem_ctx);
678         ret &= test_async(cli, mem_ctx);
679         ret &= test_changetype(cli, mem_ctx);
680
681         torture_close_connection(cli);
682         talloc_free(mem_ctx);
683         return ret;
684 }