s4-torture: ran minimal_includes.pl over source4/torture
[samba.git] / source4 / torture / smb2 / notify.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 notify test suite
5
6    Copyright (C) Stefan Metzmacher 2006
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28
29 #include "lib/events/events.h"
30
31 #define CHECK_STATUS(status, correct) do { \
32         if (!NT_STATUS_EQUAL(status, correct)) { \
33                 printf("(%s) Incorrect status %s - should be %s\n", \
34                        __location__, nt_errstr(status), nt_errstr(correct)); \
35                 ret = false; \
36                 goto done; \
37         }} while (0)
38
39 #define CHECK_VALUE(v, correct) do { \
40         if ((v) != (correct)) { \
41                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
42                        __location__, #v, v, correct); \
43                 ret = false; \
44                 goto done; \
45         }} while (0)
46
47 #define CHECK_WIRE_STR(field, value) do { \
48         if (!field.s || strcmp(field.s, value)) { \
49                 printf("(%s) %s [%s] != %s\n", \
50                           __location__, #field, field.s, value); \
51                 ret = false; \
52                 goto done; \
53         }} while (0)
54
55 #define FNAME "smb2-notify01.dat"
56
57 #define TARGET_IS_WIN7(_tctx) (torture_setting_bool(_tctx, "win7", false))
58
59 static bool test_valid_request(struct torture_context *tctx, struct smb2_tree *tree)
60 {
61         bool ret = true;
62         NTSTATUS status;
63         struct smb2_handle dh;
64         struct smb2_notify n;
65         struct smb2_request *req;
66         uint32_t max_buffer_size = 0x00080000;
67
68         if (TARGET_IS_WIN7(tctx)) {
69                 max_buffer_size = 0x00010000;
70         }
71
72         smb2_util_unlink(tree, FNAME);
73
74         status = smb2_util_roothandle(tree, &dh);
75         CHECK_STATUS(status, NT_STATUS_OK);
76
77         n.in.recursive          = 0x0000;
78         n.in.buffer_size        = max_buffer_size;
79         n.in.file.handle        = dh;
80         n.in.completion_filter  = 0x00000FFF;
81         n.in.unknown            = 0x00000000;
82         req = smb2_notify_send(tree, &n);
83
84         while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
85                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
86                         break;
87                 }
88         }
89
90         status = torture_setup_complex_file(tree, FNAME);
91         CHECK_STATUS(status, NT_STATUS_OK);
92
93         status = smb2_notify_recv(req, tctx, &n);
94         CHECK_STATUS(status, NT_STATUS_OK);
95         CHECK_VALUE(n.out.num_changes, 1);
96         CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_ADDED);
97         CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
98
99         /* 
100          * if the change response doesn't fit in the buffer
101          * NOTIFY_ENUM_DIR is returned.
102          */
103         n.in.buffer_size        = 0x00000000;
104         req = smb2_notify_send(tree, &n);
105
106         while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
107                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
108                         break;
109                 }
110         }
111
112         status = torture_setup_complex_file(tree, FNAME);
113         CHECK_STATUS(status, NT_STATUS_OK);
114
115         status = smb2_notify_recv(req, tctx, &n);
116         CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
117
118         /* 
119          * if the change response fits in the buffer we get
120          * NT_STATUS_OK again
121          */
122         n.in.buffer_size        = max_buffer_size;
123         req = smb2_notify_send(tree, &n);
124
125         while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
126                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
127                         break;
128                 }
129         }
130
131         status = torture_setup_complex_file(tree, FNAME);
132         CHECK_STATUS(status, NT_STATUS_OK);
133
134         status = smb2_notify_recv(req, tctx, &n);
135         CHECK_STATUS(status, NT_STATUS_OK);
136         CHECK_VALUE(n.out.num_changes, 3);
137         CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_REMOVED);
138         CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
139         CHECK_VALUE(n.out.changes[1].action, NOTIFY_ACTION_ADDED);
140         CHECK_WIRE_STR(n.out.changes[1].name, FNAME);
141         CHECK_VALUE(n.out.changes[2].action, NOTIFY_ACTION_MODIFIED);
142         CHECK_WIRE_STR(n.out.changes[2].name, FNAME);
143
144         /* if the first notify returns NOTIFY_ENUM_DIR, all do */
145         status = smb2_util_close(tree, dh);
146         CHECK_STATUS(status, NT_STATUS_OK);
147         status = smb2_util_roothandle(tree, &dh);
148         CHECK_STATUS(status, NT_STATUS_OK);
149
150         n.in.recursive          = 0x0000;
151         n.in.buffer_size        = 0x00000001;
152         n.in.file.handle        = dh;
153         n.in.completion_filter  = 0x00000FFF;
154         n.in.unknown            = 0x00000000;
155         req = smb2_notify_send(tree, &n);
156
157         while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
158                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
159                         break;
160                 }
161         }
162
163         status = torture_setup_complex_file(tree, FNAME);
164         CHECK_STATUS(status, NT_STATUS_OK);
165
166         status = smb2_notify_recv(req, tctx, &n);
167         CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
168
169         n.in.buffer_size        = max_buffer_size;
170         req = smb2_notify_send(tree, &n);
171         while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
172                 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
173                         break;
174                 }
175         }
176
177         status = torture_setup_complex_file(tree, FNAME);
178         CHECK_STATUS(status, NT_STATUS_OK);
179
180         status = smb2_notify_recv(req, tctx, &n);
181         CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
182
183         /* if the buffer size is too large, we get invalid parameter */
184         n.in.recursive          = 0x0000;
185         n.in.buffer_size        = max_buffer_size + 1;
186         n.in.file.handle        = dh;
187         n.in.completion_filter  = 0x00000FFF;
188         n.in.unknown            = 0x00000000;
189         req = smb2_notify_send(tree, &n);
190         status = smb2_notify_recv(req, tctx, &n);
191         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
192
193 done:
194         return ret;
195 }
196
197 /* basic testing of SMB2 notify
198 */
199 bool torture_smb2_notify(struct torture_context *torture)
200 {
201         struct smb2_tree *tree;
202         bool ret = true;
203
204         if (!torture_smb2_connection(torture, &tree)) {
205                 return false;
206         }
207
208         ret &= test_valid_request(torture, tree);
209
210         return ret;
211 }