6f115adc9e8e73e3ad41dcf3168d96f42c2b0a64
[bbaumbach/samba-autobuild/.git] / source4 / torture / raw / notify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    basic raw test suite for change notify
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 "librpc/gen_ndr/ndr_security.h"
23 #include "libcli/raw/libcliraw.h"
24
25 #define BASEDIR "\\test_notify"
26
27 #define CHECK_STATUS(status, correct) do { \
28         if (!NT_STATUS_EQUAL(status, correct)) { \
29                 printf("(%d) Incorrect status %s - should be %s\n", \
30                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
31                 ret = False; \
32                 goto done; \
33         }} while (0)
34
35
36 #define CHECK_VAL(v, correct) do { \
37         if ((v) != (correct)) { \
38                 printf("(%d) wrong value for %s  0x%x - 0x%x\n", \
39                        __LINE__, #v, (int)v, (int)correct); \
40                 ret = False; \
41                 goto done; \
42         }} while (0)
43
44 #define CHECK_WSTR(field, value, flags) do { \
45         if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli)) { \
46                 printf("(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
47                         ret = False; \
48                 goto done; \
49         }} while (0)
50
51
52 /* 
53    basic testing of change notify
54 */
55 BOOL torture_raw_notify(void)
56 {
57         struct smbcli_state *cli;
58         BOOL ret = True;
59         TALLOC_CTX *mem_ctx;
60         NTSTATUS status;
61         struct smb_notify notify;
62         union smb_open io;
63         int fnum = -1;
64         struct smbcli_request *req;
65                 
66         if (!torture_open_connection(&cli)) {
67                 return False;
68         }
69
70         mem_ctx = talloc_init("torture_raw_notify");
71
72         if (!torture_setup_dir(cli, BASEDIR)) {
73                 return False;
74         }
75
76         /*
77           get a handle on the directory
78         */
79         io.generic.level = RAW_OPEN_NTCREATEX;
80         io.ntcreatex.in.root_fid = 0;
81         io.ntcreatex.in.flags = 0;
82         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
83         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
84         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
85         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
86         io.ntcreatex.in.alloc_size = 0;
87         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
88         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
89         io.ntcreatex.in.security_flags = 0;
90         io.ntcreatex.in.fname = BASEDIR;
91
92         status = smb_raw_open(cli->tree, mem_ctx, &io);
93         CHECK_STATUS(status, NT_STATUS_OK);
94         fnum = io.ntcreatex.out.fnum;
95
96         /* ask for a change notify */
97         notify.in.buffer_size = 4096;
98         notify.in.completion_filter = 0xFF;
99         notify.in.fnum = fnum;
100         notify.in.recursive = True;
101
102         printf("testing notify mkdir\n");
103
104         req = smb_raw_changenotify_send(cli->tree, &notify);
105         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
106
107         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
108         CHECK_STATUS(status, NT_STATUS_OK);
109
110         CHECK_VAL(notify.out.num_changes, 1);
111         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
112         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
113
114         printf("testing notify rmdir\n");
115
116         req = smb_raw_changenotify_send(cli->tree, &notify);
117         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
118
119         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
120         CHECK_STATUS(status, NT_STATUS_OK);
121         CHECK_VAL(notify.out.num_changes, 1);
122         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
123         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
124
125         printf("testing notify cancel\n");
126
127         req = smb_raw_changenotify_send(cli->tree, &notify);
128         smb_raw_ntcancel(req);
129         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
130         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
131         CHECK_STATUS(status, NT_STATUS_CANCELLED);
132
133 done:
134         smb_raw_exit(cli->session);
135         smbcli_deltree(cli->tree, BASEDIR);
136         torture_close_connection(cli);
137         talloc_free(mem_ctx);
138         return ret;
139 }