Merge branch 'master' of ssh://git.samba.org/data/git/samba into selftest
[gd/samba-autobuild/.git] / source4 / torture / unix / unix_info2.c
1 /*
2    Test the SMB_QUERY_FILE_UNIX_INFO2 Unix extension.
3
4    Copyright (C) 2007 James Peach
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libcli/libcli.h"
22 #include "libcli/raw/interfaces.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "torture/torture.h"
25 #include "torture/util.h"
26 #include "torture/basic/proto.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "auth/credentials/credentials.h"
29 #include "libcli/resolve/resolve.h"
30 #include "param/param.h"
31
32 struct unix_info2 {
33         uint64_t end_of_file;
34         uint64_t num_bytes;
35         NTTIME status_change_time;
36         NTTIME access_time;
37         NTTIME change_time;
38         uint64_t uid;
39         uint64_t gid;
40         uint32_t file_type;
41         uint64_t dev_major;
42         uint64_t dev_minor;
43         uint64_t unique_id;
44         uint64_t permissions;
45         uint64_t nlink;
46         NTTIME create_time;
47         uint32_t file_flags;
48         uint32_t flags_mask;
49 };
50
51 static struct smbcli_state *connect_to_server(struct torture_context *tctx)
52 {
53         NTSTATUS status;
54         struct smbcli_state *cli;
55
56         const char *host = torture_setting_string(tctx, "host", NULL);
57         const char *share = torture_setting_string(tctx, "share", NULL);
58         struct smbcli_options options;
59         struct smbcli_session_options session_options;
60
61         lp_smbcli_options(tctx->lp_ctx, &options);
62         lp_smbcli_session_options(tctx->lp_ctx, &session_options);
63
64         status = smbcli_full_connection(tctx, &cli, host, 
65                                         lp_smb_ports(tctx->lp_ctx),
66                                         share, NULL,
67                                         cmdline_credentials, 
68                                         lp_resolve_context(tctx->lp_ctx),
69                                         tctx->ev, &options, &session_options);
70
71         if (!NT_STATUS_IS_OK(status)) {
72                 printf("failed to connect to //%s/%s: %s\n",
73                         host, share, nt_errstr(status));
74                 return NULL;
75         }
76
77         return cli;
78 }
79
80 static bool check_unix_info2(struct torture_context *torture,
81                         struct unix_info2 *info2)
82 {
83         printf("\tcreate_time=0x%016llu flags=0x%08x mask=0x%08x\n",
84                         (unsigned long long)info2->create_time,
85                         info2->file_flags, info2->flags_mask);
86
87         if (info2->file_flags == 0) {
88                 return true;
89         }
90
91         /* If we have any file_flags set, they must be within the range
92          * defined by flags_mask.
93          */
94         if ((info2->flags_mask & info2->file_flags) == 0) {
95                 torture_result(torture, TORTURE_FAIL,
96                         __location__": UNIX_INFO2 flags field 0x%08x, "
97                         "does not match mask 0x%08x\n",
98                         info2->file_flags, info2->flags_mask);
99         }
100
101         return true;
102 }
103
104 static NTSTATUS set_path_info2(void *mem_ctx,
105                                 struct smbcli_state *cli,
106                                 const char *fname,
107                                 struct unix_info2 *info2)
108 {
109         union smb_setfileinfo sfinfo;
110
111         ZERO_STRUCT(sfinfo.basic_info.in);
112         sfinfo.generic.level = RAW_SFILEINFO_UNIX_INFO2;
113         sfinfo.generic.in.file.path = fname;
114
115         sfinfo.unix_info2.in.end_of_file = info2->end_of_file;
116         sfinfo.unix_info2.in.num_bytes = info2->num_bytes;
117         sfinfo.unix_info2.in.status_change_time = info2->status_change_time;
118         sfinfo.unix_info2.in.access_time = info2->access_time;
119         sfinfo.unix_info2.in.change_time = info2->change_time;
120         sfinfo.unix_info2.in.uid = info2->uid;
121         sfinfo.unix_info2.in.gid = info2->gid;
122         sfinfo.unix_info2.in.file_type = info2->file_type;
123         sfinfo.unix_info2.in.dev_major = info2->dev_major;
124         sfinfo.unix_info2.in.dev_minor = info2->dev_minor;
125         sfinfo.unix_info2.in.unique_id = info2->unique_id;
126         sfinfo.unix_info2.in.permissions = info2->permissions;
127         sfinfo.unix_info2.in.nlink = info2->nlink;
128         sfinfo.unix_info2.in.create_time = info2->create_time;
129         sfinfo.unix_info2.in.file_flags = info2->file_flags;
130         sfinfo.unix_info2.in.flags_mask = info2->flags_mask;
131
132         return smb_raw_setpathinfo(cli->tree, &sfinfo);
133 }
134
135 static bool query_file_path_info2(void *mem_ctx,
136                         struct torture_context *torture,
137                         struct smbcli_state *cli,
138                         int fnum,
139                         const char *fname,
140                         struct unix_info2 *info2)
141 {
142         NTSTATUS result;
143         union smb_fileinfo finfo;
144
145         finfo.generic.level = RAW_FILEINFO_UNIX_INFO2;
146
147         if (fname) {
148                 finfo.generic.in.file.path = fname;
149                 result = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
150         } else {
151                 finfo.generic.in.file.fnum = fnum;
152                 result = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
153         }
154
155         torture_assert_ntstatus_equal(torture, result, NT_STATUS_OK,
156                         smbcli_errstr(cli->tree));
157
158         info2->end_of_file = finfo.unix_info2.out.end_of_file;
159         info2->num_bytes = finfo.unix_info2.out.num_bytes;
160         info2->status_change_time = finfo.unix_info2.out.status_change_time;
161         info2->access_time = finfo.unix_info2.out.access_time;
162         info2->change_time = finfo.unix_info2.out.change_time;
163         info2->uid = finfo.unix_info2.out.uid;
164         info2->gid = finfo.unix_info2.out.gid;
165         info2->file_type = finfo.unix_info2.out.file_type;
166         info2->dev_major = finfo.unix_info2.out.dev_major;
167         info2->dev_minor = finfo.unix_info2.out.dev_minor;
168         info2->unique_id = finfo.unix_info2.out.unique_id;
169         info2->permissions = finfo.unix_info2.out.permissions;
170         info2->nlink = finfo.unix_info2.out.nlink;
171         info2->create_time = finfo.unix_info2.out.create_time;
172         info2->file_flags = finfo.unix_info2.out.file_flags;
173         info2->flags_mask = finfo.unix_info2.out.flags_mask;
174
175         if (!check_unix_info2(torture, info2)) {
176                 return false;
177         }
178
179         return true;
180 }
181
182 static bool query_file_info2(void *mem_ctx,
183                         struct torture_context *torture,
184                         struct smbcli_state *cli,
185                         int fnum,
186                         struct unix_info2 *info2)
187 {
188         return query_file_path_info2(mem_ctx, torture, cli, 
189                         fnum, NULL, info2);
190 }
191
192 static bool query_path_info2(void *mem_ctx,
193                         struct torture_context *torture,
194                         struct smbcli_state *cli,
195                         const char *fname,
196                         struct unix_info2 *info2)
197 {
198         return query_file_path_info2(mem_ctx, torture, cli, 
199                         -1, fname, info2);
200 }
201
202 static bool search_callback(void *private, const union smb_search_data *fdata)
203 {
204         struct unix_info2 *info2 = (struct unix_info2 *)private;
205
206         info2->end_of_file = fdata->unix_info2.end_of_file;
207         info2->num_bytes = fdata->unix_info2.num_bytes;
208         info2->status_change_time = fdata->unix_info2.status_change_time;
209         info2->access_time = fdata->unix_info2.access_time;
210         info2->change_time = fdata->unix_info2.change_time;
211         info2->uid = fdata->unix_info2.uid;
212         info2->gid = fdata->unix_info2.gid;
213         info2->file_type = fdata->unix_info2.file_type;
214         info2->dev_major = fdata->unix_info2.dev_major;
215         info2->dev_minor = fdata->unix_info2.dev_minor;
216         info2->unique_id = fdata->unix_info2.unique_id;
217         info2->permissions = fdata->unix_info2.permissions;
218         info2->nlink = fdata->unix_info2.nlink;
219         info2->create_time = fdata->unix_info2.create_time;
220         info2->file_flags = fdata->unix_info2.file_flags;
221         info2->flags_mask = fdata->unix_info2.flags_mask;
222
223         return true;
224 }
225
226 static bool find_single_info2(void *mem_ctx,
227                         struct torture_context *torture,
228                         struct smbcli_state *cli,
229                         const char *fname,
230                         struct unix_info2 *info2)
231 {
232         union smb_search_first search;
233         NTSTATUS status;
234
235         /* Set up a new search for a single item, not using resume keys. */
236         ZERO_STRUCT(search);
237         search.t2ffirst.level = RAW_SEARCH_TRANS2;
238         search.t2ffirst.data_level = SMB_FIND_UNIX_INFO2;
239         search.t2ffirst.in.max_count = 1;
240         search.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
241         search.t2ffirst.in.pattern = fname;
242
243         status = smb_raw_search_first(cli->tree, mem_ctx,
244                                       &search, info2, search_callback);
245         torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
246                         smbcli_errstr(cli->tree));
247
248         torture_assert_int_equal(torture, search.t2ffirst.out.count, 1,
249                         "expected exactly one result");
250         torture_assert_int_equal(torture, search.t2ffirst.out.end_of_search, 1,
251                         "expected end_of_search to be true");
252
253         return check_unix_info2(torture, info2);
254 }
255
256 #define ASSERT_FLAGS_MATCH(info2, expected) \
257         if ((info2)->file_flags != (1 << i)) { \
258                 torture_result(torture, TORTURE_FAIL, \
259                         __location__": INFO2 flags field was 0x%08x, "\
260                                 "expected 0x%08x\n",\
261                                 (info2)->file_flags, expected); \
262         }
263
264 static void set_no_metadata_change(struct unix_info2 *info2)
265 {
266         info2->uid = SMB_UID_NO_CHANGE;
267         info2->gid = SMB_GID_NO_CHANGE;
268         info2->permissions = SMB_MODE_NO_CHANGE;
269
270         info2->end_of_file =
271                 ((uint64_t)SMB_SIZE_NO_CHANGE_HI << 32) | SMB_SIZE_NO_CHANGE_LO;
272
273         info2->status_change_time =
274                 info2->access_time =
275                 info2->change_time =
276                 info2->create_time =
277                 ((uint64_t)SMB_SIZE_NO_CHANGE_HI << 32) | SMB_SIZE_NO_CHANGE_LO;
278 }
279
280 static bool verify_setinfo_flags(void *mem_ctx,
281                         struct torture_context *torture,
282                         struct smbcli_state *cli,
283                         const char *fname)
284 {
285         struct unix_info2 info2;
286         uint32_t smb_fmask;
287         int i;
288
289         bool ret = true;
290         NTSTATUS status;
291
292         if (!query_path_info2(mem_ctx, torture, cli, fname, &info2)) {
293                 return false;
294         }
295
296         smb_fmask = info2.flags_mask;
297
298         /* For each possible flag, ask to set exactly 1 flag, making sure
299          * that flag is in our requested mask.
300          */
301         for (i = 0; i < 32; ++i) {
302                 info2.file_flags = (1 << i);
303                 info2.flags_mask = smb_fmask | info2.file_flags;
304
305                 set_no_metadata_change(&info2);
306                 status = set_path_info2(mem_ctx, cli, fname, &info2);
307
308                 if (info2.file_flags & smb_fmask) {
309                         torture_assert_ntstatus_equal(torture,
310                                         status, NT_STATUS_OK,
311                                         "setting valid UNIX_INFO2 flag");
312
313                         if (!query_path_info2(mem_ctx, torture, cli,
314                                                 fname, &info2)) {
315                                 return false;
316                         }
317
318                         ASSERT_FLAGS_MATCH(&info2, 1 << i);
319
320
321                 } else {
322                         /* We tried to set a flag the server doesn't
323                          * understand.
324                          */
325                         torture_assert_ntstatus_equal(torture,
326                                         status, NT_STATUS_INVALID_PARAMETER,
327                                         "setting invalid UNIX_INFO2 flag");
328                 }
329         }
330
331         /* Make sure that a zero flags field does nothing. */
332         set_no_metadata_change(&info2);
333         info2.file_flags = 0xFFFFFFFF;
334         info2.flags_mask = 0;
335         status = set_path_info2(mem_ctx, cli, fname, &info2);
336         torture_assert_ntstatus_equal(torture, status, NT_STATUS_OK,
337                         "setting empty flags mask");
338
339         return ret;
340
341 }
342
343 static int create_file(struct smbcli_state *cli, const char * fname)
344 {
345
346         return smbcli_nt_create_full(cli->tree, fname, 0,
347                 SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
348                 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF,
349                 0, 0);
350 }
351
352 static bool match_info2(struct torture_context *torture,
353                 const struct unix_info2 *pinfo,
354                 const struct unix_info2 *finfo)
355 {
356         printf("checking results match\n");
357
358         torture_assert_u64_equal(torture, finfo->end_of_file, 0,
359                         "end_of_file should be 0");
360         torture_assert_u64_equal(torture, finfo->num_bytes, 0,
361                         "num_bytes should be 0");
362
363         torture_assert_u64_equal(torture, finfo->end_of_file,
364                         pinfo->end_of_file, "end_of_file mismatch");
365         torture_assert_u64_equal(torture, finfo->num_bytes, pinfo->num_bytes,
366                         "num_bytes mismatch");
367
368         /* Don't match access_time. */
369
370         torture_assert_u64_equal(torture, finfo->status_change_time,
371                         pinfo->status_change_time,
372                         "status_change_time mismatch");
373         torture_assert_u64_equal(torture, finfo->change_time,
374                         pinfo->change_time, "change_time mismatch");
375
376         torture_assert_u64_equal(torture, finfo->uid, pinfo->uid,
377                         "UID mismatch");
378         torture_assert_u64_equal(torture, finfo->gid, pinfo->gid,
379                         "GID mismatch");
380         torture_assert_int_equal(torture, finfo->file_type, pinfo->file_type,
381                         "file_type mismatch");
382         torture_assert_u64_equal(torture, finfo->dev_major, pinfo->dev_major,
383                         "dev_major mismatch");
384         torture_assert_u64_equal(torture, finfo->dev_minor, pinfo->dev_minor,
385                         "dev_minor mismatch");
386         torture_assert_u64_equal(torture, finfo->unique_id, pinfo->unique_id,
387                         "unique_id mismatch");
388         torture_assert_u64_equal(torture, finfo->permissions,
389                         pinfo->permissions, "permissions mismatch");
390         torture_assert_u64_equal(torture, finfo->nlink, pinfo->nlink,
391                         "nlink mismatch");
392         torture_assert_u64_equal(torture, finfo->create_time, pinfo->create_time,
393                         "create_time mismatch");
394
395         return true;
396 }
397
398
399 #define FILENAME "\\smb_unix_info2.txt"
400
401 bool unix_torture_unix_info2(struct torture_context *torture)
402 {
403         void *mem_ctx;
404         struct smbcli_state *cli;
405         int fnum;
406
407         struct unix_info2 pinfo, finfo;
408
409         mem_ctx = talloc_init("smb_query_unix_info2");
410         torture_assert(torture, mem_ctx != NULL, "out of memory");
411
412         if (!(cli = connect_to_server(torture))) {
413                 talloc_free(mem_ctx);
414                 return false;
415         }
416
417         smbcli_unlink(cli->tree, FILENAME);
418
419         fnum = create_file(cli, FILENAME);
420         torture_assert(torture, fnum != -1, smbcli_errstr(cli->tree));
421
422         printf("checking SMB_QFILEINFO_UNIX_INFO2 for QueryFileInfo\n");
423         if (!query_file_info2(mem_ctx, torture, cli, fnum, &finfo)) {
424                 goto fail;
425         }
426
427         printf("checking SMB_QFILEINFO_UNIX_INFO2 for QueryPathInfo\n");
428         if (!query_path_info2(mem_ctx, torture, cli, FILENAME, &pinfo)) {
429                 goto fail;
430         }
431
432         if (!match_info2(torture, &pinfo, &finfo)) {
433                 goto fail;
434         }
435
436         printf("checking SMB_FIND_UNIX_INFO2 for FindFirst\n");
437         if (!find_single_info2(mem_ctx, torture, cli, FILENAME, &pinfo)) {
438                 goto fail;
439         }
440
441         if (!match_info2(torture, &pinfo, &finfo)) {
442                 goto fail;
443         }
444
445         /* XXX: should repeat this test with SetFileInfo. */
446         printf("checking SMB_SFILEINFO_UNIX_INFO2 for SetPathInfo\n");
447         if (!verify_setinfo_flags(mem_ctx, torture, cli, FILENAME)) {
448                 goto fail;
449         }
450
451         smbcli_close(cli->tree, fnum);
452         smbcli_unlink(cli->tree, FILENAME);
453         torture_close_connection(cli);
454         talloc_free(mem_ctx);
455         return true;
456
457 fail:
458
459         smbcli_close(cli->tree, fnum);
460         smbcli_unlink(cli->tree, FILENAME);
461         torture_close_connection(cli);
462         talloc_free(mem_ctx);
463         return false;
464
465 }
466
467 /* vim: set sts=8 sw=8 : */