r13277: print a useful error message when test 17 fails
[samba.git] / source / torture / basic / delete.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    delete on close testing
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/libcli.h"
25 #include "torture/torture.h"
26 #include "system/filesys.h"
27 #include "libcli/raw/libcliraw.h"
28
29 static BOOL check_delete_on_close(struct smbcli_state *cli, int fnum,
30                                   const char *fname, BOOL expect_it)
31 {
32         TALLOC_CTX *mem_ctx = talloc_init("single_search");
33         union smb_search_data data;
34         NTSTATUS status;
35
36         time_t c_time, a_time, m_time;
37         size_t size;
38         uint16_t mode;
39
40         BOOL res = True;
41
42         status = torture_single_search(cli, mem_ctx,
43                                        fname, RAW_SEARCH_FULL_DIRECTORY_INFO,
44                                        FILE_ATTRIBUTE_DIRECTORY,
45                                        &data);
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("(%s) single_search failed (%s)\n", 
48                        __location__, nt_errstr(status));
49                 res = False;
50                 goto done;
51         }
52
53         if (fnum != -1) {
54                 union smb_fileinfo io;
55                 int nlink = expect_it ? 0 : 1;
56
57                 io.all_info.level = RAW_FILEINFO_ALL_INFO;
58                 io.all_info.in.fnum = fnum;
59
60                 status = smb_raw_fileinfo(cli->tree, mem_ctx, &io);
61                 if (!NT_STATUS_IS_OK(status)) {
62                         printf("(%s) qpathinfo failed (%s)\n", __location__,
63                                nt_errstr(status));
64                         res = False;
65                         goto done;
66                 }
67
68                 if (expect_it != io.all_info.out.delete_pending) {
69                         printf("Expected del_on_close flag %d, qfileinfo gave %d\n",
70                                expect_it, io.all_info.out.delete_pending);
71                         res = False;
72                         goto done;
73                 }
74
75                 if (nlink != io.all_info.out.nlink) {
76                         printf("Expected nlink %d, qfileinfo gave %d\n",
77                                nlink, io.all_info.out.nlink);
78                         res = False;
79                         goto done;
80                 }
81
82                 io.standard_info.level = RAW_FILEINFO_STANDARD_INFO;
83                 io.standard_info.in.fnum = fnum;
84
85                 status = smb_raw_fileinfo(cli->tree, mem_ctx, &io);
86                 if (!NT_STATUS_IS_OK(status)) {
87                         printf("(%s) qpathinfo failed (%s)\n", __location__,
88                                nt_errstr(status));
89                         res = False;
90                         goto done;
91                 }
92
93                 if (expect_it != io.standard_info.out.delete_pending) {
94                         printf("Expected del_on_close flag %d, qfileinfo gave %d\n",
95                                expect_it, io.standard_info.out.delete_pending);
96                         res = False;
97                         goto done;
98                 }
99
100                 if (nlink != io.standard_info.out.nlink) {
101                         printf("Expected nlink %d, qfileinfo gave %d\n",
102                                nlink, io.all_info.out.nlink);
103                         res = False;
104                         goto done;
105                 }
106
107         }
108
109         status = smbcli_qpathinfo(cli->tree, fname,
110                                   &c_time, &a_time, &m_time,
111                                   &size, &mode);
112
113         if (expect_it) {
114                 if (!NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
115                         printf("(%s) qpathinfo did not give correct error "
116                                "code (%s) -- NT_STATUS_DELETE_PENDING "
117                                "expected\n", __location__,
118                                nt_errstr(status));
119                         res = False;
120                         goto done;
121                 }
122         } else {
123                 if (!NT_STATUS_IS_OK(status)) {
124                         printf("(%s) qpathinfo failed (%s)\n", __location__,
125                                nt_errstr(status));
126                         res = False;
127                         goto done;
128                 }
129         }
130
131  done:
132         talloc_free(mem_ctx);
133         return res;
134 }
135
136 #define CHECK_STATUS(_cli, _expected) do { \
137         if (!NT_STATUS_EQUAL(_cli->tree->session->transport->error.e.nt_status, _expected)) { \
138                 printf("(%d) Incorrect status %s - should be %s\n", \
139                        __LINE__, nt_errstr(_cli->tree->session->transport->error.e.nt_status), nt_errstr(_expected)); \
140                 correct = False; \
141                 goto fail; \
142         }} while (0)
143
144 /*
145   Test delete on close semantics.
146  */
147 BOOL torture_test_delete(void)
148 {
149         struct smbcli_state *cli1;
150         struct smbcli_state *cli2 = NULL;
151         const char *fname = "\\delete.file";
152         const char *fname_new = "\\delete.new";
153         const char *dirname = "\\delete.dir";
154         int fnum1 = -1;
155         int fnum2 = -1;
156         int dnum1 = -1;
157         BOOL correct = True;
158         NTSTATUS status;
159         
160         printf("starting delete test\n");
161         
162         if (!torture_open_connection(&cli1)) {
163                 return False;
164         }
165
166         if (!torture_open_connection(&cli2)) {
167                 printf("(%s) failed to open second connection.\n",
168                        __location__);
169                 correct = False;
170                 goto fail;
171         }
172
173         smbcli_deltree(cli1->tree, dirname);
174
175         /* Test 1 - this should delete the file on close. */
176         
177         smbcli_setatr(cli1->tree, fname, 0, 0);
178         smbcli_unlink(cli1->tree, fname);
179         
180         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
181                                       SEC_RIGHTS_FILE_ALL,
182                                       FILE_ATTRIBUTE_NORMAL,
183                                       NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 
184                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
185         
186         if (fnum1 == -1) {
187                 printf("(%s) open of %s failed (%s)\n", 
188                        __location__, fname, smbcli_errstr(cli1->tree));
189                 correct = False;
190                 goto fail;
191         }
192         
193         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
194                 printf("(%s) close failed (%s)\n", 
195                        __location__, smbcli_errstr(cli1->tree));
196                 correct = False;
197                 goto fail;
198         }
199
200         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_NONE);
201         if (fnum1 != -1) {
202                 printf("(%s) open of %s succeeded (should fail)\n", 
203                        __location__, fname);
204                 correct = False;
205                 goto fail;
206         }
207         
208         smb_raw_exit(cli1->session);
209         smb_raw_exit(cli2->session);
210
211         printf("first delete on close test succeeded.\n");
212         
213         /* Test 2 - this should delete the file on close. */
214         
215         smbcli_setatr(cli1->tree, fname, 0, 0);
216         smbcli_unlink(cli1->tree, fname);
217         
218         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
219                                       SEC_RIGHTS_FILE_ALL,
220                                       FILE_ATTRIBUTE_NORMAL, NTCREATEX_SHARE_ACCESS_NONE, 
221                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
222         
223         if (fnum1 == -1) {
224                 printf("(%s) open of %s failed (%s)\n", 
225                        __location__, fname, smbcli_errstr(cli1->tree));
226                 correct = False;
227                 goto fail;
228         }
229         
230         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
231                 printf("(%s) setting delete_on_close failed (%s)\n", 
232                        __location__, smbcli_errstr(cli1->tree));
233                 correct = False;
234                 goto fail;
235         }
236         
237         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
238                 printf("(%s) close failed (%s)\n", 
239                        __location__, smbcli_errstr(cli1->tree));
240                 correct = False;
241                 goto fail;
242         }
243         
244         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
245         if (fnum1 != -1) {
246                 printf("(%s) open of %s succeeded should have been deleted on close !\n", 
247                        __location__, fname);
248                 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
249                         printf("(%s) close failed (%s)\n", 
250                                __location__, smbcli_errstr(cli1->tree));
251                         correct = False;
252                         goto fail;
253                 }
254                 smbcli_unlink(cli1->tree, fname);
255         } else
256                 printf("second delete on close test succeeded.\n");
257         
258         smb_raw_exit(cli1->session);
259         smb_raw_exit(cli2->session);
260
261         /* Test 3 - ... */
262         smbcli_setatr(cli1->tree, fname, 0, 0);
263         smbcli_unlink(cli1->tree, fname);
264
265         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
266                                       SEC_RIGHTS_FILE_ALL,
267                                       FILE_ATTRIBUTE_NORMAL,
268                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
269                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
270
271         if (fnum1 == -1) {
272                 printf("(%s) open - 1 of %s failed (%s)\n", 
273                        __location__, fname, smbcli_errstr(cli1->tree));
274                 correct = False;
275                 goto fail;
276         }
277
278         /* This should fail with a sharing violation - open for delete is only compatible
279            with SHARE_DELETE. */
280
281         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
282                                       SEC_RIGHTS_FILE_READ, 
283                                       FILE_ATTRIBUTE_NORMAL,
284                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
285                                       NTCREATEX_DISP_OPEN, 0, 0);
286
287         if (fnum2 != -1) {
288                 printf("(%s) open  - 2 of %s succeeded - should have failed.\n", 
289                        __location__, fname);
290                 correct = False;
291                 goto fail;
292         }
293
294         /* This should succeed. */
295
296         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
297                                       SEC_RIGHTS_FILE_READ, 
298                                       FILE_ATTRIBUTE_NORMAL,
299                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, 
300                                       NTCREATEX_DISP_OPEN, 0, 0);
301
302         if (fnum2 == -1) {
303                 printf("(%s) open  - 2 of %s failed (%s)\n", 
304                        __location__, fname, smbcli_errstr(cli1->tree));
305                 correct = False;
306                 goto fail;
307         }
308
309         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
310                 printf("(%s) setting delete_on_close failed (%s)\n", 
311                        __location__, smbcli_errstr(cli1->tree));
312                 correct = False;
313                 goto fail;
314         }
315         
316         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
317                 printf("(%s) close 1 failed (%s)\n", 
318                        __location__, smbcli_errstr(cli1->tree));
319                 correct = False;
320                 goto fail;
321         }
322         
323         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
324                 printf("(%s) close 2 failed (%s)\n", 
325                        __location__, smbcli_errstr(cli1->tree));
326                 correct = False;
327                 goto fail;
328         }
329         
330         /* This should fail - file should no longer be there. */
331
332         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
333         if (fnum1 != -1) {
334                 printf("(%s) open of %s succeeded should have been deleted on close !\n", 
335                        __location__, fname);
336                 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
337                         printf("(%s) close failed (%s)\n", 
338                                __location__, smbcli_errstr(cli1->tree));
339                 }
340                 smbcli_unlink(cli1->tree, fname);
341                 correct = False;
342                 goto fail;
343         } else
344                 printf("third delete on close test succeeded.\n");
345
346         smb_raw_exit(cli1->session);
347         smb_raw_exit(cli2->session);
348
349         /* Test 4 ... */
350         smbcli_setatr(cli1->tree, fname, 0, 0);
351         status = smbcli_unlink(cli1->tree, fname);
352         if (NT_STATUS_IS_OK(status)) {
353                 printf("(%s) succeeded unlink of %s\n", __location__, fname);
354                 correct = False;
355                 goto fail;
356         }
357
358         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
359                                       SEC_FILE_READ_DATA  | 
360                                       SEC_FILE_WRITE_DATA |
361                                       SEC_STD_DELETE,
362                                       FILE_ATTRIBUTE_NORMAL, 
363                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
364                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
365                                                                 
366         if (fnum1 == -1) {
367                 printf("(%s) open of %s failed (%s)\n", 
368                        __location__, fname, smbcli_errstr(cli1->tree));
369                 correct = False;
370                 goto fail;
371         }
372
373         /* This should succeed. */
374         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
375                                       SEC_RIGHTS_FILE_READ,
376                                       FILE_ATTRIBUTE_NORMAL, 
377                                       NTCREATEX_SHARE_ACCESS_READ  | 
378                                       NTCREATEX_SHARE_ACCESS_WRITE |
379                                       NTCREATEX_SHARE_ACCESS_DELETE, 
380                                       NTCREATEX_DISP_OPEN, 0, 0);
381         if (fnum2 == -1) {
382                 printf("(%s) open  - 2 of %s failed (%s)\n", 
383                        __location__, fname, smbcli_errstr(cli1->tree));
384                 correct = False;
385                 goto fail;
386         }
387         
388         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
389                 printf("(%s) close - 1 failed (%s)\n", 
390                        __location__, smbcli_errstr(cli1->tree));
391                 correct = False;
392                 goto fail;
393         }
394         
395         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
396                 printf("(%s) setting delete_on_close failed (%s)\n", 
397                        __location__, smbcli_errstr(cli1->tree));
398                 correct = False;
399                 goto fail;
400         }
401         
402         /* This should fail - no more opens once delete on close set. */
403         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
404                                       SEC_RIGHTS_FILE_READ,
405                                       FILE_ATTRIBUTE_NORMAL, 
406                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
407                                       NTCREATEX_DISP_OPEN, 0, 0);
408         if (fnum2 != -1) {
409                 printf("(%s) open  - 3 of %s succeeded ! Should have failed.\n",
410                        __location__, fname );
411                 correct = False;
412                 goto fail;
413         }
414         CHECK_STATUS(cli1, NT_STATUS_DELETE_PENDING);
415
416         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
417                 printf("(%s) close - 2 failed (%s)\n", 
418                        __location__, smbcli_errstr(cli1->tree));
419                 correct = False;
420                 goto fail;
421         }
422         
423         smb_raw_exit(cli1->session);
424         smb_raw_exit(cli2->session);
425
426         printf("fourth delete on close test succeeded.\n");
427         
428         /* Test 5 ... */
429         smbcli_setatr(cli1->tree, fname, 0, 0);
430         smbcli_unlink(cli1->tree, fname);
431         
432         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
433         if (fnum1 == -1) {
434                 printf("(%s) open of %s failed (%s)\n", 
435                        __location__, fname, smbcli_errstr(cli1->tree));
436                 correct = False;
437                 goto fail;
438         }
439
440         /* This should fail - only allowed on NT opens with DELETE access. */
441
442         if (NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
443                 printf("(%s) setting delete_on_close on OpenX file succeeded - should fail !\n",
444                        __location__);
445                 correct = False;
446                 goto fail;
447         }
448
449         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
450                 printf("(%s) close - 2 failed (%s)\n", 
451                        __location__, smbcli_errstr(cli1->tree));
452                 correct = False;
453                 goto fail;
454         }
455         
456         smb_raw_exit(cli1->session);
457         smb_raw_exit(cli2->session);
458
459         printf("fifth delete on close test succeeded.\n");
460         
461         /* Test 6 ... */
462         smbcli_setatr(cli1->tree, fname, 0, 0);
463         smbcli_unlink(cli1->tree, fname);
464         
465         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
466                                    SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA,
467                                    FILE_ATTRIBUTE_NORMAL, 
468                                    NTCREATEX_SHARE_ACCESS_READ  |
469                                    NTCREATEX_SHARE_ACCESS_WRITE |
470                                    NTCREATEX_SHARE_ACCESS_DELETE,
471                                    NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
472         
473         if (fnum1 == -1) {
474                 printf("(%s) open of %s failed (%s)\n", 
475                        __location__, fname, smbcli_errstr(cli1->tree));
476                 correct = False;
477                 goto fail;
478         }
479         
480         /* This should fail - only allowed on NT opens with DELETE access. */
481         
482         if (NT_STATUS_IS_OK(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
483                 printf("(%s) setting delete_on_close on file with no delete access succeeded - should fail !\n",
484                        __location__);
485                 correct = False;
486                 goto fail;
487         }
488
489         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
490                 printf("(%s) close - 2 failed (%s)\n", 
491                        __location__, smbcli_errstr(cli1->tree));
492                 correct = False;
493                 goto fail;
494         }
495
496         smb_raw_exit(cli1->session);
497         smb_raw_exit(cli2->session);
498
499         printf("sixth delete on close test succeeded.\n");
500         
501         /* Test 7 ... */
502         smbcli_setatr(cli1->tree, fname, 0, 0);
503         smbcli_unlink(cli1->tree, fname);
504         
505         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
506                                       SEC_FILE_READ_DATA  | 
507                                       SEC_FILE_WRITE_DATA |
508                                       SEC_STD_DELETE,
509                                       FILE_ATTRIBUTE_NORMAL, 0, 
510                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
511                                                                 
512         if (fnum1 == -1) {
513                 printf("(%s) open of %s failed (%s)\n", 
514                        __location__, fname, smbcli_errstr(cli1->tree));
515                 correct = False;
516                 goto fail;
517         }
518
519         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
520                 printf("(%s) setting delete_on_close on file failed !\n",
521                        __location__);
522                 correct = False;
523                 goto fail;
524         }
525
526         correct &= check_delete_on_close(cli1, fnum1, fname, True);
527         
528         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, False))) {
529                 printf("(%s) unsetting delete_on_close on file failed !\n",
530                        __location__);
531                 correct = False;
532                 goto fail;
533         }
534
535         correct &= check_delete_on_close(cli1, fnum1, fname, False);
536         
537         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
538                 printf("(%s) close - 2 failed (%s)\n", 
539                        __location__, smbcli_errstr(cli1->tree));
540                 correct = False;
541                 goto fail;
542         }
543         
544         /* This next open should succeed - we reset the flag. */
545         
546         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
547         if (fnum1 == -1) {
548                 printf("(%s) open of %s failed (%s)\n", 
549                        __location__, fname, smbcli_errstr(cli1->tree));
550                 correct = False;
551                 goto fail;
552         }
553
554         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
555                 printf("(%s) close - 2 failed (%s)\n", 
556                        __location__, smbcli_errstr(cli1->tree));
557                 correct = False;
558                 goto fail;
559         }
560
561         smb_raw_exit(cli1->session);
562         smb_raw_exit(cli2->session);
563
564         printf("seventh delete on close test succeeded.\n");
565         
566         /* Test 7 ... */
567         smbcli_setatr(cli1->tree, fname, 0, 0);
568         smbcli_unlink(cli1->tree, fname);
569         
570         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
571                                       SEC_FILE_READ_DATA|
572                                       SEC_FILE_WRITE_DATA|
573                                       SEC_STD_DELETE,
574                                       FILE_ATTRIBUTE_NORMAL, 
575                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
576                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
577         
578         if (fnum1 == -1) {
579                 printf("(%s) open of %s failed (%s)\n", 
580                        __location__, fname, smbcli_errstr(cli1->tree));
581                 correct = False;
582                 goto fail;
583         }
584
585         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, 
586                                       SEC_FILE_READ_DATA|
587                                       SEC_FILE_WRITE_DATA|
588                                       SEC_STD_DELETE,
589                                       FILE_ATTRIBUTE_NORMAL, 
590                                       NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
591                                       NTCREATEX_DISP_OPEN, 0, 0);
592         
593         if (fnum2 == -1) {
594                 printf("(%s) open of %s failed (%s)\n", 
595                        __location__, fname, smbcli_errstr(cli1->tree));
596                 correct = False;
597                 goto fail;
598         }
599
600         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1, True))) {
601                 printf("(%s) setting delete_on_close on file failed !\n",
602                        __location__);
603                 correct = False;
604                 goto fail;
605         }
606
607         correct &= check_delete_on_close(cli1, fnum1, fname, True);
608         correct &= check_delete_on_close(cli2, fnum2, fname, True);
609
610         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
611                 printf("(%s) close - 1 failed (%s)\n", 
612                        __location__, smbcli_errstr(cli1->tree));
613                 correct = False;
614                 goto fail;
615         }
616
617         correct &= check_delete_on_close(cli1, -1, fname, True);
618         correct &= check_delete_on_close(cli2, fnum2, fname, True);
619         
620         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
621                 printf("(%s) close - 2 failed (%s)\n", 
622                        __location__, smbcli_errstr(cli2->tree));
623                 correct = False;
624                 goto fail;
625         }
626
627         /* This should fail.. */
628         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
629         if (fnum1 != -1) {
630                 printf("(%s) open of %s succeeded should have been deleted on close !\n",
631                        __location__, fname);
632                 goto fail;
633                 correct = False;
634         } else
635                 printf("eighth delete on close test succeeded.\n");
636
637         smb_raw_exit(cli1->session);
638         smb_raw_exit(cli2->session);
639
640         /* This should fail - we need to set DELETE_ACCESS. */
641         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
642                                       SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA,
643                                       FILE_ATTRIBUTE_NORMAL, 
644                                       NTCREATEX_SHARE_ACCESS_NONE, 
645                                       NTCREATEX_DISP_OVERWRITE_IF, 
646                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
647         
648         if (fnum1 != -1) {
649                 printf("(%s) open of %s succeeded should have failed!\n", 
650                        __location__, fname);
651                 correct = False;
652                 goto fail;
653         }
654
655         smb_raw_exit(cli1->session);
656         smb_raw_exit(cli2->session);
657
658         printf("ninth delete on close test succeeded.\n");
659
660         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
661                                       SEC_FILE_READ_DATA|
662                                       SEC_FILE_WRITE_DATA|
663                                       SEC_STD_DELETE,
664                                       FILE_ATTRIBUTE_NORMAL, 
665                                       NTCREATEX_SHARE_ACCESS_NONE, 
666                                       NTCREATEX_DISP_OVERWRITE_IF, 
667                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
668         if (fnum1 == -1) {
669                 printf("(%s) open of %s failed (%s)\n", 
670                        __location__, fname, smbcli_errstr(cli1->tree));
671                 correct = False;
672                 goto fail;
673         }
674
675         /* This should delete the file. */
676         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
677                 printf("(%s) close failed (%s)\n", 
678                        __location__, smbcli_errstr(cli1->tree));
679                 correct = False;
680                 goto fail;
681         }
682
683         /* This should fail.. */
684         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
685         if (fnum1 != -1) {
686                 printf("(%s) open of %s succeeded should have been deleted on close !\n",
687                        __location__, fname);
688                 goto fail;
689                 correct = False;
690         } else
691                 printf("tenth delete on close test succeeded.\n");
692
693         smb_raw_exit(cli1->session);
694         smb_raw_exit(cli2->session);
695
696         /* test 11 - does having read only attribute still allow delete on close. */
697
698         smbcli_setatr(cli1->tree, fname, 0, 0);
699         smbcli_unlink(cli1->tree, fname);
700
701         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
702                                       SEC_RIGHTS_FILE_ALL,
703                                       FILE_ATTRIBUTE_READONLY, 
704                                       NTCREATEX_SHARE_ACCESS_NONE, 
705                                       NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
706         
707         if (fnum1 == -1) {
708                 printf("(%s) open of %s failed (%s)\n", 
709                        __location__, fname, smbcli_errstr(cli1->tree));
710                 correct = False;
711                 goto fail;
712         }
713
714         status = smbcli_nt_delete_on_close(cli1->tree, fnum1, True);
715
716         if (!NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
717                 printf("(%s) setting delete_on_close should fail with NT_STATUS_CANNOT_DELETE. Got %s instead)\n", 
718                        __location__, smbcli_errstr(cli1->tree));
719                 correct = False;
720                 goto fail;
721         }
722
723         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
724                 printf("(%s) close failed (%s)\n", 
725                        __location__, smbcli_errstr(cli1->tree));
726                 correct = False;
727                 goto fail;
728         }
729
730         smbcli_setatr(cli1->tree, fname, 0, 0);
731         smbcli_unlink(cli1->tree, fname);
732
733         smb_raw_exit(cli1->session);
734         smb_raw_exit(cli2->session);
735
736         printf("eleventh delete on close test succeeded.\n");
737
738         /* test 12 - does having read only attribute still allow delete on
739          * close at time of open. */
740
741         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
742                                       SEC_RIGHTS_FILE_ALL,
743                                       FILE_ATTRIBUTE_READONLY,
744                                       NTCREATEX_SHARE_ACCESS_DELETE,
745                                       NTCREATEX_DISP_OVERWRITE_IF, 
746                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
747         
748         if (fnum1 != -1) {
749                 printf("(%s) open of %s succeeded. Should fail with "
750                        "NT_STATUS_CANNOT_DELETE.\n", __location__, fname);
751                 smbcli_close(cli1->tree, fnum1);
752                 correct = False;
753                 goto fail;
754         } else {
755                 status = smbcli_nt_error(cli1->tree);
756                 if (!NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
757                         printf("(%s) setting delete_on_close on open should "
758                                "fail with NT_STATUS_CANNOT_DELETE. Got %s "
759                                "instead)\n", 
760                                __location__, smbcli_errstr(cli1->tree));
761                         correct = False;
762                         goto fail;
763                 }
764         }
765         
766         smb_raw_exit(cli1->session);
767         smb_raw_exit(cli2->session);
768
769         printf("twelvth delete on close test succeeded.\n");
770
771         /* Test 13: Does resetting the delete on close flag affect a second
772          * fd? */
773
774         smbcli_setatr(cli1->tree, fname, 0, 0);
775         smbcli_unlink(cli1->tree, fname);
776         
777         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
778                                       SEC_FILE_READ_DATA|
779                                       SEC_FILE_WRITE_DATA|
780                                       SEC_STD_DELETE,
781                                       FILE_ATTRIBUTE_NORMAL, 
782                                       NTCREATEX_SHARE_ACCESS_READ|
783                                       NTCREATEX_SHARE_ACCESS_WRITE|
784                                       NTCREATEX_SHARE_ACCESS_DELETE,
785                                       NTCREATEX_DISP_OVERWRITE_IF,
786                                       0, 0);
787         
788         if (fnum1 == -1) {
789                 printf("(%s) open of %s failed (%s)\n", 
790                        __location__, fname, smbcli_errstr(cli1->tree));
791                 correct = False;
792                 goto fail;
793         }
794
795         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, 
796                                       SEC_FILE_READ_DATA|
797                                       SEC_FILE_WRITE_DATA|
798                                       SEC_STD_DELETE,
799                                       FILE_ATTRIBUTE_NORMAL, 
800                                       NTCREATEX_SHARE_ACCESS_READ|
801                                       NTCREATEX_SHARE_ACCESS_WRITE|
802                                       NTCREATEX_SHARE_ACCESS_DELETE,
803                                       NTCREATEX_DISP_OPEN, 0, 0);
804         
805         if (fnum2 == -1) {
806                 printf("(%s) open of %s failed (%s)\n", 
807                        __location__, fname, smbcli_errstr(cli2->tree));
808                 correct = False;
809                 goto fail;
810         }
811
812         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, fnum1,
813                                                        True))) {
814                 printf("(%s) setting delete_on_close on file failed !\n",
815                        __location__);
816                 correct = False;
817                 goto fail;
818         }
819
820         correct &= check_delete_on_close(cli1, fnum1, fname, True);
821         correct &= check_delete_on_close(cli2, fnum2, fname, True);
822
823         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli2->tree, fnum2,
824                                                        False))) {
825                 printf("(%s) setting delete_on_close on file failed !\n",
826                        __location__);
827                 correct = False;
828                 goto fail;
829         }
830
831         correct &= check_delete_on_close(cli1, fnum1, fname, False);
832         correct &= check_delete_on_close(cli2, fnum2, fname, False);
833         
834         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
835                 printf("(%s) close - 1 failed (%s)\n", 
836                        __location__, smbcli_errstr(cli1->tree));
837                 correct = False;
838                 goto fail;
839         }
840
841         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
842                 printf("(%s) close - 2 failed (%s)\n", 
843                        __location__, smbcli_errstr(cli2->tree));
844                 correct = False;
845                 goto fail;
846         }
847
848         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_NONE);
849
850         if (fnum1 == -1) {
851                 printf("(%s) open of %s failed!\n", 
852                        __location__, fname);
853                 correct = False;
854                 goto fail;
855         }
856
857         smbcli_close(cli1->tree, fnum1);
858         smbcli_unlink(cli1->tree, fname);
859
860         smb_raw_exit(cli1->session);
861         smb_raw_exit(cli2->session);
862
863         printf("thirteenth delete on close test succeeded.\n");
864
865         /* Test 14 -- directory */
866
867         dnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0,
868                                       SEC_FILE_READ_DATA|
869                                       SEC_FILE_WRITE_DATA|
870                                       SEC_STD_DELETE,
871                                       FILE_ATTRIBUTE_DIRECTORY, 
872                                       NTCREATEX_SHARE_ACCESS_READ|
873                                       NTCREATEX_SHARE_ACCESS_WRITE|
874                                       NTCREATEX_SHARE_ACCESS_DELETE,
875                                       NTCREATEX_DISP_CREATE, 0, 0);
876         if (dnum1 == -1) {
877                 printf("(%s) open of %s failed: %s!\n", 
878                        __location__, dirname, smbcli_errstr(cli1->tree));
879                 correct = False;
880                 goto fail;
881         }
882
883         check_delete_on_close(cli1, dnum1, dirname, False);
884         if (NT_STATUS_IS_ERR(smbcli_nt_delete_on_close(cli1->tree, dnum1, True))) {
885                 printf("(%s) setting delete_on_close on file failed !\n",
886                        __location__);
887                 correct = False;
888                 goto fail;
889         }
890         check_delete_on_close(cli1, dnum1, dirname, True);
891         smbcli_close(cli1->tree, dnum1);
892
893         /* Now it should be gone... */
894
895         dnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0,
896                                       SEC_FILE_READ_DATA|
897                                       SEC_FILE_WRITE_DATA|
898                                       SEC_STD_DELETE,
899                                       FILE_ATTRIBUTE_DIRECTORY, 
900                                       NTCREATEX_SHARE_ACCESS_READ|
901                                       NTCREATEX_SHARE_ACCESS_WRITE|
902                                       NTCREATEX_SHARE_ACCESS_DELETE,
903                                       NTCREATEX_DISP_OPEN, 0, 0);
904         if (dnum1 != -1) {
905                 printf("(%s) setting delete_on_close on file succeeded !\n",
906                        __location__);
907                 correct = False;
908                 goto fail;
909         }
910
911         smb_raw_exit(cli1->session);
912         smb_raw_exit(cli2->session);
913
914         printf("fourteenth delete on close test succeeded.\n");
915
916         /* Test 15: delete on close under rename */
917
918         smbcli_setatr(cli1->tree, fname, 0, 0);
919         smbcli_unlink(cli1->tree, fname);
920         smbcli_unlink(cli1->tree, fname_new);
921         
922         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
923                                       SEC_FILE_READ_DATA,
924                                       FILE_ATTRIBUTE_NORMAL, 
925                                       NTCREATEX_SHARE_ACCESS_READ|
926                                       NTCREATEX_SHARE_ACCESS_WRITE|
927                                       NTCREATEX_SHARE_ACCESS_DELETE,
928                                       NTCREATEX_DISP_OVERWRITE_IF,
929                                       0, 0);
930
931         if (fnum1 == -1) {
932                 printf("(%s) open - 1 of %s failed (%s)\n", 
933                        __location__, fname, smbcli_errstr(cli1->tree));
934                 correct = False;
935                 goto fail;
936         }
937
938         status = smbcli_rename(cli2->tree, fname, fname_new);
939
940         if (!NT_STATUS_IS_OK(status)) {
941                 printf("(%s) renaming failed: %s !\n",
942                        __location__, nt_errstr(status));
943                 correct = False;
944                 goto fail;
945         }
946
947         fnum2 = smbcli_nt_create_full(cli2->tree, fname_new, 0, 
948                                       SEC_GENERIC_ALL,
949                                       FILE_ATTRIBUTE_NORMAL, 
950                                       NTCREATEX_SHARE_ACCESS_READ|
951                                       NTCREATEX_SHARE_ACCESS_WRITE|
952                                       NTCREATEX_SHARE_ACCESS_DELETE,
953                                       NTCREATEX_DISP_OVERWRITE_IF,
954                                       0, 0);
955
956         if (fnum2 == -1) {
957                 printf("(%s) open - 1 of %s failed (%s)\n", 
958                        __location__, fname_new, smbcli_errstr(cli1->tree));
959                 correct = False;
960                 goto fail;
961         }
962
963         status = smbcli_nt_delete_on_close(cli2->tree, fnum2, True);
964
965         if (!NT_STATUS_IS_OK(status)) {
966                 printf("(%s) setting delete_on_close on file failed !\n",
967                        __location__);
968                 correct = False;
969                 goto fail;
970         }
971
972         smbcli_close(cli2->tree, fnum2);
973
974         /* The file should be around under the new name, there's a second
975          * handle open */
976
977         if (!check_delete_on_close(cli1, fnum1, fname_new, True)) {
978                 printf("(%s) checking delete on close on file %s failed!\n",
979                        __location__, fname_new);
980                 correct = False;
981                 goto fail;
982         }
983
984         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, 
985                                       SEC_GENERIC_ALL,
986                                       FILE_ATTRIBUTE_NORMAL, 
987                                       NTCREATEX_SHARE_ACCESS_READ|
988                                       NTCREATEX_SHARE_ACCESS_WRITE|
989                                       NTCREATEX_SHARE_ACCESS_DELETE,
990                                       NTCREATEX_DISP_OVERWRITE_IF,
991                                       0, 0);
992
993         if (fnum2 == -1) {
994                 printf("(%s) open - 1 of %s failed (%s)\n", 
995                        __location__, fname, smbcli_errstr(cli1->tree));
996                 correct = False;
997                 goto fail;
998         }
999
1000         smbcli_close(cli2->tree, fnum2);
1001         smbcli_close(cli1->tree, fnum1);
1002
1003         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1004                                       SEC_FILE_READ_EA,
1005                                       FILE_ATTRIBUTE_NORMAL, 
1006                                       NTCREATEX_SHARE_ACCESS_READ|
1007                                       NTCREATEX_SHARE_ACCESS_WRITE|
1008                                       NTCREATEX_SHARE_ACCESS_DELETE,
1009                                       NTCREATEX_DISP_OPEN,
1010                                       0, 0);
1011
1012         if (fnum1 == -1) {
1013                 printf("(%s) open - 1 of %s failed (%s)\n", 
1014                        __location__, fname, smbcli_errstr(cli1->tree));
1015                 correct = False;
1016                 goto fail;
1017         }
1018
1019         smbcli_close(cli1->tree, fnum1);
1020
1021         fnum1 = smbcli_nt_create_full(cli1->tree, fname_new, 0, 
1022                                       SEC_FILE_READ_EA,
1023                                       FILE_ATTRIBUTE_NORMAL, 
1024                                       NTCREATEX_SHARE_ACCESS_READ|
1025                                       NTCREATEX_SHARE_ACCESS_WRITE|
1026                                       NTCREATEX_SHARE_ACCESS_DELETE,
1027                                       NTCREATEX_DISP_OPEN,
1028                                       0, 0);
1029
1030         if (fnum1 != -1) {
1031                 printf("(%s) smbcli_open succeeded, should have "
1032                        "failed\n", __location__);
1033                 smbcli_close(cli1->tree, fnum1);
1034                 correct = False;
1035                 goto fail;
1036         }
1037
1038         smb_raw_exit(cli1->session);
1039         smb_raw_exit(cli2->session);
1040
1041         printf("fifteenth delete on close test succeeded.\n");
1042
1043         /* Test 16. */
1044
1045         /* Ensure the file doesn't already exist. */
1046         smbcli_close(cli1->tree, fnum1);
1047         smbcli_close(cli1->tree, fnum2);
1048         smbcli_setatr(cli1->tree, fname, 0, 0);
1049         smbcli_unlink(cli1->tree, fname);
1050
1051         /* Firstly create with all access, but delete on close. */
1052         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1053                                       SEC_RIGHTS_FILE_ALL,
1054                                       FILE_ATTRIBUTE_NORMAL,
1055                                       NTCREATEX_SHARE_ACCESS_READ|
1056                                       NTCREATEX_SHARE_ACCESS_WRITE|
1057                                       NTCREATEX_SHARE_ACCESS_DELETE,
1058                                       NTCREATEX_DISP_CREATE,
1059                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1060         
1061         if (fnum1 == -1) {
1062                 printf("(%s) open - 1 of %s failed (%s)\n", 
1063                        __location__, fname, smbcli_errstr(cli1->tree));
1064                 correct = False;
1065                 goto fail;
1066         }
1067
1068         /* The delete on close bit is *not* reported as being set. */
1069         check_delete_on_close(cli1, fnum1, fname, False);
1070
1071         /* Now try opening again for read-only. */
1072         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1073                                       SEC_RIGHTS_FILE_READ,
1074                                       FILE_ATTRIBUTE_NORMAL,
1075                                       NTCREATEX_SHARE_ACCESS_READ|
1076                                       NTCREATEX_SHARE_ACCESS_WRITE|
1077                                       NTCREATEX_SHARE_ACCESS_DELETE,
1078                                       NTCREATEX_DISP_OPEN,
1079                                       0, 0);
1080         
1081
1082         /* Should work. */
1083         if (fnum2 == -1) {
1084                 printf("(%s) open - 1 of %s failed (%s)\n", 
1085                        __location__, fname, smbcli_errstr(cli1->tree));
1086                 correct = False;
1087                 goto fail;
1088         }
1089
1090         /* Now close both.... */
1091         smbcli_close(cli1->tree, fnum1);
1092         smbcli_close(cli1->tree, fnum2);
1093
1094         /* And the file should be deleted ! */
1095         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_NONE);
1096         if (fnum1 != -1) {
1097                 printf("(%s) open of %s succeeded (should fail)\n", 
1098                        __location__, fname);
1099                 correct = False;
1100                 goto fail;
1101         }
1102         
1103         smb_raw_exit(cli1->session);
1104         smb_raw_exit(cli2->session);
1105
1106         printf("sixteenth delete on close test succeeded.\n");
1107
1108         /* Test 17. */
1109
1110         /* Ensure the file doesn't already exist. */
1111         smbcli_close(cli1->tree, fnum1);
1112         smbcli_close(cli1->tree, fnum2);
1113         smbcli_setatr(cli1->tree, fname, 0, 0);
1114         smbcli_unlink(cli1->tree, fname);
1115
1116         /* Firstly open and create with all access */
1117         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1118                                       SEC_RIGHTS_FILE_ALL,
1119                                       FILE_ATTRIBUTE_NORMAL,
1120                                       NTCREATEX_SHARE_ACCESS_READ|
1121                                       NTCREATEX_SHARE_ACCESS_WRITE|
1122                                       NTCREATEX_SHARE_ACCESS_DELETE,
1123                                       NTCREATEX_DISP_CREATE, 
1124                                       0, 0);
1125         if (fnum1 == -1) {
1126                 printf("(%s) open - 1 of %s failed (%s)\n", 
1127                        __location__, fname, smbcli_errstr(cli1->tree));
1128                 correct = False;
1129                 goto fail;
1130         }
1131
1132         /* And close - just to create the file. */
1133         smbcli_close(cli1->tree, fnum1);
1134         
1135         /* Next open with all access, but add delete on close. */
1136         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1137                                       SEC_RIGHTS_FILE_ALL,
1138                                       FILE_ATTRIBUTE_NORMAL,
1139                                       NTCREATEX_SHARE_ACCESS_READ|
1140                                       NTCREATEX_SHARE_ACCESS_WRITE|
1141                                       NTCREATEX_SHARE_ACCESS_DELETE,
1142                                       NTCREATEX_DISP_OPEN,
1143                                       NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1144         
1145         if (fnum1 == -1) {
1146                 printf("(%s) open - 1 of %s failed (%s)\n", 
1147                        __location__, fname, smbcli_errstr(cli1->tree));
1148                 correct = False;
1149                 goto fail;
1150         }
1151
1152         /* The delete on close bit is *not* reported as being set. */
1153         check_delete_on_close(cli1, fnum1, fname, False);
1154
1155         /* Now try opening again for read-only. */
1156         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0, 
1157                                       SEC_RIGHTS_FILE_READ,
1158                                       FILE_ATTRIBUTE_NORMAL,
1159                                       NTCREATEX_SHARE_ACCESS_READ|
1160                                       NTCREATEX_SHARE_ACCESS_WRITE|
1161                                       NTCREATEX_SHARE_ACCESS_DELETE,
1162                                       NTCREATEX_DISP_OPEN,
1163                                       0, 0);
1164         
1165         /* Should work. */
1166         if (fnum2 == -1) {
1167                 printf("(%s) open - 1 of %s failed (%s)\n", 
1168                        __location__, fname, smbcli_errstr(cli1->tree));
1169                 correct = False;
1170                 goto fail;
1171         }
1172
1173         /* Now close both.... */
1174         smbcli_close(cli1->tree, fnum1);
1175         smbcli_close(cli1->tree, fnum2);
1176
1177         /* See if the file is deleted - shouldn't be.... */
1178         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_NONE);
1179         if (fnum1 == -1) {
1180                 printf("(%s) open of %s failed (should succeed) - %s\n", 
1181                        __location__, fname, smbcli_errstr(cli1->tree));
1182                 correct = False;
1183                 goto fail;
1184         }
1185         
1186         smb_raw_exit(cli1->session);
1187         smb_raw_exit(cli2->session);
1188
1189         printf("seventeenth delete on close test succeeded.\n");
1190
1191         /* Test 18. With directories. */
1192
1193         /* Ensure the file doesn't already exist. */
1194         smbcli_close(cli1->tree, fnum1);
1195         smbcli_close(cli1->tree, fnum2);
1196
1197         smbcli_deltree(cli1->tree, dirname);
1198
1199         /* Firstly create with all access, but delete on close. */
1200         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1201                                       SEC_FILE_READ_DATA|
1202                                       SEC_FILE_WRITE_DATA|
1203                                       SEC_STD_DELETE,
1204                                       FILE_ATTRIBUTE_DIRECTORY,
1205                                       NTCREATEX_SHARE_ACCESS_READ|
1206                                       NTCREATEX_SHARE_ACCESS_WRITE|
1207                                       NTCREATEX_SHARE_ACCESS_DELETE,
1208                                       NTCREATEX_DISP_CREATE,
1209                                       NTCREATEX_OPTIONS_DIRECTORY|NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1210         
1211         if (fnum1 == -1) {
1212                 printf("(%s) open - 1 of %s failed (%s)\n", 
1213                        __location__, dirname, smbcli_errstr(cli1->tree));
1214                 correct = False;
1215                 goto fail;
1216         }
1217
1218         /* The delete on close bit is *not* reported as being set. */
1219         check_delete_on_close(cli1, fnum1, dirname, False);
1220
1221         /* Now try opening again for read-only. */
1222         fnum2 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1223                                       SEC_RIGHTS_FILE_READ,
1224                                       FILE_ATTRIBUTE_DIRECTORY,
1225                                       NTCREATEX_SHARE_ACCESS_READ|
1226                                       NTCREATEX_SHARE_ACCESS_WRITE|
1227                                       NTCREATEX_SHARE_ACCESS_DELETE,
1228                                       NTCREATEX_DISP_OPEN,
1229                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1230         
1231
1232         /* Should work. */
1233         if (fnum2 == -1) {
1234                 printf("(%s) open - 1 of %s failed (%s)\n", 
1235                        __location__, dirname, smbcli_errstr(cli1->tree));
1236                 correct = False;
1237                 goto fail;
1238         }
1239
1240         /* Now close both.... */
1241         smbcli_close(cli1->tree, fnum1);
1242         smbcli_close(cli1->tree, fnum2);
1243
1244         /* And the directory should be deleted ! */
1245         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1246                                       SEC_RIGHTS_FILE_READ,
1247                                       FILE_ATTRIBUTE_DIRECTORY,
1248                                       NTCREATEX_SHARE_ACCESS_READ|
1249                                       NTCREATEX_SHARE_ACCESS_WRITE|
1250                                       NTCREATEX_SHARE_ACCESS_DELETE,
1251                                       NTCREATEX_DISP_OPEN,
1252                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1253         if (fnum1 != -1) {
1254                 printf("(%s) open of %s succeeded (should fail)\n", 
1255                        __location__, dirname);
1256                 correct = False;
1257                 goto fail;
1258         }
1259         
1260         smb_raw_exit(cli1->session);
1261         smb_raw_exit(cli2->session);
1262
1263         printf("eighteenth delete on close test succeeded.\n");
1264
1265         /* Test 19. */
1266
1267         smbcli_deltree(cli1->tree, dirname);
1268
1269         /* Firstly open and create with all access */
1270         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1271                                       SEC_FILE_READ_DATA|
1272                                       SEC_FILE_WRITE_DATA|
1273                                       SEC_STD_DELETE,
1274                                       FILE_ATTRIBUTE_DIRECTORY,
1275                                       NTCREATEX_SHARE_ACCESS_READ|
1276                                       NTCREATEX_SHARE_ACCESS_WRITE|
1277                                       NTCREATEX_SHARE_ACCESS_DELETE,
1278                                       NTCREATEX_DISP_CREATE,
1279                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1280         
1281         if (fnum1 == -1) {
1282                 printf("(%s) open - 1 of %s failed (%s)\n", 
1283                        __location__, dirname, smbcli_errstr(cli1->tree));
1284                 correct = False;
1285                 goto fail;
1286         }
1287
1288         /* And close - just to create the directory. */
1289         smbcli_close(cli1->tree, fnum1);
1290         
1291         /* Next open with all access, but add delete on close. */
1292         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1293                                       SEC_FILE_READ_DATA|
1294                                       SEC_FILE_WRITE_DATA|
1295                                       SEC_STD_DELETE,
1296                                       FILE_ATTRIBUTE_DIRECTORY,
1297                                       NTCREATEX_SHARE_ACCESS_READ|
1298                                       NTCREATEX_SHARE_ACCESS_WRITE|
1299                                       NTCREATEX_SHARE_ACCESS_DELETE,
1300                                       NTCREATEX_DISP_OPEN,
1301                                       NTCREATEX_OPTIONS_DIRECTORY|NTCREATEX_OPTIONS_DELETE_ON_CLOSE, 0);
1302         
1303         if (fnum1 == -1) {
1304                 printf("(%s) open - 1 of %s failed (%s)\n", 
1305                        __location__, fname, smbcli_errstr(cli1->tree));
1306                 correct = False;
1307                 goto fail;
1308         }
1309
1310         /* The delete on close bit is *not* reported as being set. */
1311         check_delete_on_close(cli1, fnum1, dirname, False);
1312
1313         /* Now try opening again for read-only. */
1314         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1315                                       SEC_RIGHTS_FILE_READ,
1316                                       FILE_ATTRIBUTE_DIRECTORY,
1317                                       NTCREATEX_SHARE_ACCESS_READ|
1318                                       NTCREATEX_SHARE_ACCESS_WRITE|
1319                                       NTCREATEX_SHARE_ACCESS_DELETE,
1320                                       NTCREATEX_DISP_OPEN,
1321                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1322         
1323         /* Should work. */
1324         if (fnum2 == -1) {
1325                 printf("(%s) open - 1 of %s failed (%s)\n", 
1326                        __location__, dirname, smbcli_errstr(cli1->tree));
1327                 correct = False;
1328                 goto fail;
1329         }
1330
1331         /* Now close both.... */
1332         smbcli_close(cli1->tree, fnum1);
1333         smbcli_close(cli1->tree, fnum2);
1334
1335         /* See if the file is deleted - shouldn't be.... */
1336         fnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0, 
1337                                       SEC_RIGHTS_FILE_READ,
1338                                       FILE_ATTRIBUTE_DIRECTORY,
1339                                       NTCREATEX_SHARE_ACCESS_READ|
1340                                       NTCREATEX_SHARE_ACCESS_WRITE|
1341                                       NTCREATEX_SHARE_ACCESS_DELETE,
1342                                       NTCREATEX_DISP_OPEN,
1343                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1344         if (fnum1 == -1) {
1345                 printf("(%s) open of %s failed (should succeed)\n", 
1346                        __location__, dirname);
1347                 correct = False;
1348                 goto fail;
1349         }
1350         
1351         smb_raw_exit(cli1->session);
1352         smb_raw_exit(cli2->session);
1353
1354         printf("nineteenth delete on close test succeeded.\n");
1355
1356         /* Test 20 -- non-empty directory hardest to get right... */
1357
1358         smbcli_deltree(cli1->tree, dirname);
1359
1360         dnum1 = smbcli_nt_create_full(cli1->tree, dirname, 0,
1361                                       SEC_FILE_READ_DATA|
1362                                       SEC_FILE_WRITE_DATA|
1363                                       SEC_STD_DELETE,
1364                                       FILE_ATTRIBUTE_DIRECTORY, 
1365                                       NTCREATEX_SHARE_ACCESS_READ|
1366                                       NTCREATEX_SHARE_ACCESS_WRITE|
1367                                       NTCREATEX_SHARE_ACCESS_DELETE,
1368                                       NTCREATEX_DISP_CREATE, 
1369                                       NTCREATEX_OPTIONS_DIRECTORY, 0);
1370         if (dnum1 == -1) {
1371                 printf("(%s) open of %s failed: %s!\n", 
1372                        __location__, dirname, smbcli_errstr(cli1->tree));
1373                 correct = False;
1374                 goto fail;
1375         }
1376
1377         check_delete_on_close(cli1, dnum1, dirname, False);
1378         status = smbcli_nt_delete_on_close(cli1->tree, dnum1, True);
1379
1380         {
1381                 char *fullname;
1382                 asprintf(&fullname, "\\%s%s", dirname, fname);
1383                 fnum1 = smbcli_open(cli1->tree, fullname, O_CREAT|O_RDWR,
1384                                     DENY_NONE);
1385                 if (fnum1 != -1) {
1386                         printf("(%s) smbcli_open succeeded, should have "
1387                                "failed: %s\n",
1388                                __location__, smbcli_errstr(cli1->tree));
1389                         correct = False;
1390                         goto fail;
1391                 }
1392
1393                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli1->tree),
1394                                      NT_STATUS_DELETE_PENDING)) {
1395                         printf("(%s) smbcli_open returned %s, expected "
1396                                "NT_STATUS_DELETE_PENDING\n",
1397                                __location__, smbcli_errstr(cli1->tree));
1398                         correct = False;
1399                         goto fail;
1400                 }
1401         }
1402
1403         status = smbcli_nt_delete_on_close(cli1->tree, dnum1, False);
1404         if (!NT_STATUS_IS_OK(status)) {
1405                 printf("(%s) setting delete_on_close on file failed !\n",
1406                        __location__);
1407                 correct = False;
1408                 goto fail;
1409         }
1410                 
1411         {
1412                 char *fullname;
1413                 asprintf(&fullname, "\\%s%s", dirname, fname);
1414                 fnum1 = smbcli_open(cli1->tree, fullname, O_CREAT|O_RDWR,
1415                                     DENY_NONE);
1416                 if (fnum1 == -1) {
1417                         printf("(%s) smbcli_open failed: %s\n",
1418                                __location__, smbcli_errstr(cli1->tree));
1419                         correct = False;
1420                         goto fail;
1421                 }
1422                 smbcli_close(cli1->tree, fnum1);
1423         }
1424
1425         status = smbcli_nt_delete_on_close(cli1->tree, dnum1, True);
1426
1427         if (!NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
1428                 printf("(%s) setting delete_on_close returned %s, expected "
1429                        "NT_STATUS_DIRECTORY_NOT_EMPTY\n", __location__,
1430                        smbcli_errstr(cli1->tree));
1431                 correct = False;
1432                 goto fail;
1433         }
1434
1435         smbcli_close(cli1->tree, dnum1);
1436
1437         smb_raw_exit(cli1->session);
1438         smb_raw_exit(cli2->session);
1439
1440         printf("twentieth delete on close test succeeded.\n");
1441
1442         printf("finished delete test\n");
1443
1444   fail:
1445         /* FIXME: This will crash if we aborted before cli2 got
1446          * intialized, because these functions don't handle
1447          * uninitialized connections. */
1448                 
1449         smbcli_close(cli1->tree, fnum1);
1450         smbcli_close(cli1->tree, fnum2);
1451         smbcli_setatr(cli1->tree, fname, 0, 0);
1452         smbcli_unlink(cli1->tree, fname);
1453
1454         smbcli_deltree(cli1->tree, dirname);
1455
1456         if (!torture_close_connection(cli1)) {
1457                 correct = False;
1458         }
1459         if (!torture_close_connection(cli2)) {
1460                 correct = False;
1461         }
1462         return correct;
1463 }
1464