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