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