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