lib/util: add debug_set_forced_log_priority()
[Samba.git] / source4 / torture / raw / eas.c
blob59baae53ad620adaf0aa02bf4534b07282bf720e
1 /*
2 Unix SMB/CIFS implementation.
4 test DOS extended attributes
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Guenter Kukkukk 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28 #include "torture/raw/proto.h"
30 #define BASEDIR "\\testeas"
32 #define CHECK_STATUS(status, correct) do { \
33 torture_assert_ntstatus_equal_goto(tctx, status, correct, ret, done, "Incorrect status"); \
34 } while (0)
36 static bool maxeadebug; /* need that here, to allow no file delete in debug case */
38 static bool check_ea(struct smbcli_state *cli,
39 const char *fname, const char *eaname, const char *value)
41 NTSTATUS status = torture_check_ea(cli, fname, eaname, value);
42 return NT_STATUS_IS_OK(status);
45 static char bad_ea_chars[] = "\"*+,/:;<=>?[\\]|";
47 static bool test_eas(struct smbcli_state *cli, struct torture_context *tctx)
49 NTSTATUS status;
50 union smb_setfileinfo setfile;
51 union smb_open io;
52 const char *fname = BASEDIR "\\ea.txt";
53 bool ret = true;
54 char bad_ea_name[7];
55 int i;
56 int fnum = -1;
58 torture_comment(tctx, "TESTING SETFILEINFO EA_SET\n");
60 io.generic.level = RAW_OPEN_NTCREATEX;
61 io.ntcreatex.in.root_fid.fnum = 0;
62 io.ntcreatex.in.flags = 0;
63 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
64 io.ntcreatex.in.create_options = 0;
65 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
66 io.ntcreatex.in.share_access =
67 NTCREATEX_SHARE_ACCESS_READ |
68 NTCREATEX_SHARE_ACCESS_WRITE;
69 io.ntcreatex.in.alloc_size = 0;
70 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
71 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
72 io.ntcreatex.in.security_flags = 0;
73 io.ntcreatex.in.fname = fname;
74 status = smb_raw_open(cli->tree, tctx, &io);
75 CHECK_STATUS(status, NT_STATUS_OK);
76 fnum = io.ntcreatex.out.file.fnum;
78 ret &= check_ea(cli, fname, "EAONE", NULL);
80 torture_comment(tctx, "Adding first two EAs\n");
81 setfile.generic.level = RAW_SFILEINFO_EA_SET;
82 setfile.generic.in.file.fnum = fnum;
83 setfile.ea_set.in.num_eas = 2;
84 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
85 setfile.ea_set.in.eas[0].flags = 0;
86 setfile.ea_set.in.eas[0].name.s = "EAONE";
87 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE1");
88 setfile.ea_set.in.eas[1].flags = 0;
89 setfile.ea_set.in.eas[1].name.s = "SECONDEA";
90 setfile.ea_set.in.eas[1].value = data_blob_string_const("ValueTwo");
92 status = smb_raw_setfileinfo(cli->tree, &setfile);
93 CHECK_STATUS(status, NT_STATUS_OK);
95 ret &= check_ea(cli, fname, "EAONE", "VALUE1");
96 ret &= check_ea(cli, fname, "SECONDEA", "ValueTwo");
98 torture_comment(tctx, "Modifying 2nd EA\n");
99 setfile.ea_set.in.num_eas = 1;
100 setfile.ea_set.in.eas[0].name.s = "SECONDEA";
101 setfile.ea_set.in.eas[0].value = data_blob_string_const(" Changed Value");
102 status = smb_raw_setfileinfo(cli->tree, &setfile);
103 CHECK_STATUS(status, NT_STATUS_OK);
105 ret &= check_ea(cli, fname, "EAONE", "VALUE1");
106 ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
108 torture_comment(tctx, "Setting a NULL EA\n");
109 setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
110 setfile.ea_set.in.eas[0].name.s = "NULLEA";
111 status = smb_raw_setfileinfo(cli->tree, &setfile);
112 CHECK_STATUS(status, NT_STATUS_OK);
114 ret &= check_ea(cli, fname, "EAONE", "VALUE1");
115 ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
116 ret &= check_ea(cli, fname, "NULLEA", NULL);
118 torture_comment(tctx, "Deleting first EA\n");
119 setfile.ea_set.in.eas[0].flags = 0;
120 setfile.ea_set.in.eas[0].name.s = "EAONE";
121 setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
122 status = smb_raw_setfileinfo(cli->tree, &setfile);
123 CHECK_STATUS(status, NT_STATUS_OK);
125 ret &= check_ea(cli, fname, "EAONE", NULL);
126 ret &= check_ea(cli, fname, "SECONDEA", " Changed Value");
128 torture_comment(tctx, "Deleting second EA\n");
129 setfile.ea_set.in.eas[0].flags = 0;
130 setfile.ea_set.in.eas[0].name.s = "SECONDEA";
131 setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
132 status = smb_raw_setfileinfo(cli->tree, &setfile);
133 CHECK_STATUS(status, NT_STATUS_OK);
135 ret &= check_ea(cli, fname, "EAONE", NULL);
136 ret &= check_ea(cli, fname, "SECONDEA", NULL);
138 /* Check EA name containing colon. All EA's set
139 must be ignored, not just the one with the bad
140 name. */
142 torture_comment(tctx, "Adding bad EA name\n");
143 setfile.generic.level = RAW_SFILEINFO_EA_SET;
144 setfile.generic.in.file.fnum = fnum;
145 setfile.ea_set.in.num_eas = 3;
146 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 3);
147 setfile.ea_set.in.eas[0].flags = 0;
148 setfile.ea_set.in.eas[0].name.s = "EAONE";
149 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE1");
150 setfile.ea_set.in.eas[1].flags = 0;
151 setfile.ea_set.in.eas[1].name.s = "SECOND:EA";
152 setfile.ea_set.in.eas[1].value = data_blob_string_const("ValueTwo");
153 setfile.ea_set.in.eas[2].flags = 0;
154 setfile.ea_set.in.eas[2].name.s = "THIRDEA";
155 setfile.ea_set.in.eas[2].value = data_blob_string_const("ValueThree");
157 status = smb_raw_setfileinfo(cli->tree, &setfile);
158 CHECK_STATUS(status, STATUS_INVALID_EA_NAME);
160 ret &= check_ea(cli, fname, "EAONE", NULL);
161 ret &= check_ea(cli, fname, "THIRDEA", NULL);
163 setfile.generic.level = RAW_SFILEINFO_EA_SET;
164 setfile.generic.in.file.fnum = fnum;
165 setfile.ea_set.in.num_eas = 1;
166 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 1);
167 setfile.ea_set.in.eas[0].flags = 0;
168 strlcpy(bad_ea_name, "TEST_X", sizeof(bad_ea_name));
169 setfile.ea_set.in.eas[0].name.s = bad_ea_name;
171 torture_comment(tctx, "Testing bad EA name range.\n");
173 for (i = 1; i < 256; i++) {
174 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE1");
175 bad_ea_name[5] = (char)i;
176 torture_comment(tctx, "Testing bad EA name %d.\n", i);
177 status = smb_raw_setfileinfo(cli->tree, &setfile);
178 if (i < 32 || strchr(bad_ea_chars, i)) {
179 CHECK_STATUS(status, STATUS_INVALID_EA_NAME);
180 } else {
181 CHECK_STATUS(status, NT_STATUS_OK);
183 /* Now delete the EA we just set to make
184 sure we don't run out of room. */
185 setfile.ea_set.in.eas[0].value = data_blob(NULL, 0);
186 status = smb_raw_setfileinfo(cli->tree, &setfile);
187 CHECK_STATUS(status, NT_STATUS_OK);
191 done:
192 smbcli_close(cli->tree, fnum);
193 return ret;
198 * Helper function to retrieve the max. ea size for one ea name
200 static int test_one_eamax(struct torture_context *tctx,
201 struct smbcli_state *cli, const int fnum,
202 const char *eaname, DATA_BLOB eablob,
203 const int eastart, const int eadebug)
205 NTSTATUS status;
206 struct ea_struct eastruct;
207 union smb_setfileinfo setfile;
208 int i, high, low, maxeasize;
210 setfile.generic.level = RAW_SFILEINFO_EA_SET;
211 setfile.generic.in.file.fnum = fnum;
212 setfile.ea_set.in.num_eas = 1;
213 setfile.ea_set.in.eas = &eastruct;
214 setfile.ea_set.in.eas->flags = 0;
215 setfile.ea_set.in.eas->name.s = eaname;
216 setfile.ea_set.in.eas->value = eablob;
218 maxeasize = eablob.length;
219 i = eastart;
220 low = 0;
221 high = maxeasize;
223 do {
224 if (eadebug) {
225 torture_comment(tctx, "Testing EA size: %d\n", i);
227 setfile.ea_set.in.eas->value.length = i;
229 status = smb_raw_setfileinfo(cli->tree, &setfile);
231 if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
232 if (eadebug) {
233 torture_comment(tctx, "[%s] EA size %d succeeded! "
234 "(high=%d low=%d)\n",
235 eaname, i, high, low);
237 low = i;
238 if (low == maxeasize) {
239 torture_comment(tctx, "Max. EA size for \"%s\"=%d "
240 "[but could be possibly larger]\n",
241 eaname, low);
242 break;
244 if (high - low == 1 && high != maxeasize) {
245 torture_comment(tctx, "Max. EA size for \"%s\"=%d\n",
246 eaname, low);
247 break;
249 i += (high - low + 1) / 2;
250 } else {
251 if (eadebug) {
252 torture_comment(tctx, "[%s] EA size %d failed! "
253 "(high=%d low=%d) [%s]\n",
254 eaname, i, high, low,
255 nt_errstr(status));
257 high = i;
258 if (high - low <= 1) {
259 torture_comment(tctx, "Max. EA size for \"%s\"=%d\n",
260 eaname, low);
261 break;
263 i -= (high - low + 1) / 2;
265 } while (true);
267 return low;
271 * Test for maximum ea size - more than one ea name is checked.
273 * Additional parameters can be passed, to allow further testing:
275 * default
276 * maxeasize 65536 limit the max. size for a single EA name
277 * maxeanames 101 limit of the number of tested names
278 * maxeastart 1 this EA size is used to test for the 1st EA (atm)
279 * maxeadebug 0 if set true, further debug output is done - in addition
280 * the testfile is not deleted for further inspection!
282 * Set some/all of these options on the cmdline with:
283 * --option torture:maxeasize=1024 --option torture:maxeadebug=1 ...
286 static bool test_max_eas(struct smbcli_state *cli, struct torture_context *tctx)
288 NTSTATUS status;
289 union smb_open io;
290 const char *fname = BASEDIR "\\ea_max.txt";
291 int fnum = -1;
292 bool ret = true;
293 bool err = false;
295 int i, j, k, last;
296 size_t total;
297 DATA_BLOB eablob;
298 char *eaname = NULL;
299 int maxeasize;
300 int maxeanames;
301 int maxeastart;
303 torture_comment(tctx, "TESTING SETFILEINFO MAX. EA_SET\n");
305 maxeasize = torture_setting_int(tctx, "maxeasize", 65536);
306 maxeanames = torture_setting_int(tctx, "maxeanames", 101);
307 maxeastart = torture_setting_int(tctx, "maxeastart", 1);
308 maxeadebug = torture_setting_bool(tctx, "maxeadebug", false);
310 /* Do some sanity check on possibly passed parms */
311 if (maxeasize <= 0) {
312 torture_comment(tctx, "Invalid parameter 'maxeasize=%d'",maxeasize);
313 err = true;
315 if (maxeanames <= 0) {
316 torture_comment(tctx, "Invalid parameter 'maxeanames=%d'",maxeanames);
317 err = true;
319 if (maxeastart <= 0) {
320 torture_comment(tctx, "Invalid parameter 'maxeastart=%d'",maxeastart);
321 err = true;
323 if (err) {
324 torture_comment(tctx, "\n\n");
325 goto done;
327 if (maxeastart > maxeasize) {
328 maxeastart = maxeasize;
329 torture_comment(tctx, "'maxeastart' outside range - corrected to %d\n",
330 maxeastart);
332 torture_comment(tctx, "MAXEA parms: maxeasize=%d maxeanames=%d maxeastart=%d"
333 " maxeadebug=%d\n", maxeasize, maxeanames, maxeastart,
334 maxeadebug);
336 io.generic.level = RAW_OPEN_NTCREATEX;
337 io.ntcreatex.in.root_fid.fnum = 0;
338 io.ntcreatex.in.flags = 0;
339 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
340 io.ntcreatex.in.create_options = 0;
341 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
342 io.ntcreatex.in.share_access =
343 NTCREATEX_SHARE_ACCESS_READ |
344 NTCREATEX_SHARE_ACCESS_WRITE;
345 io.ntcreatex.in.alloc_size = 0;
346 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
347 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
348 io.ntcreatex.in.security_flags = 0;
349 io.ntcreatex.in.fname = fname;
350 status = smb_raw_open(cli->tree, tctx, &io);
351 CHECK_STATUS(status, NT_STATUS_OK);
352 fnum = io.ntcreatex.out.file.fnum;
354 eablob = data_blob_talloc(tctx, NULL, maxeasize);
355 if (eablob.data == NULL) {
356 goto done;
359 * Fill in some EA data - the offset could be easily checked
360 * during a hexdump.
362 for (i = 0, k = 0; i < eablob.length / 4; i++, k+=4) {
363 eablob.data[k] = k & 0xff;
364 eablob.data[k+1] = (k >> 8) & 0xff;
365 eablob.data[k+2] = (k >> 16) & 0xff;
366 eablob.data[k+3] = (k >> 24) & 0xff;
369 i = eablob.length % 4;
370 if (i-- > 0) {
371 eablob.data[k] = k & 0xff;
372 if (i-- > 0) {
373 eablob.data[k+1] = (k >> 8) & 0xff;
374 if (i-- > 0) {
375 eablob.data[k+2] = (k >> 16) & 0xff;
380 * Filesystems might allow max. EAs data for different EA names.
381 * So more than one EA name should be checked.
383 total = 0;
384 last = maxeastart;
386 for (i = 0; i < maxeanames; i++) {
387 if (eaname != NULL) {
388 talloc_free(eaname);
390 eaname = talloc_asprintf(tctx, "MAX%d", i);
391 if(eaname == NULL) {
392 goto done;
394 j = test_one_eamax(tctx, cli, fnum, eaname, eablob, last, maxeadebug);
395 if (j <= 0) {
396 break;
398 total += j;
399 last = j;
402 torture_comment(tctx, "Total EA size:%zu\n", total);
403 if (i == maxeanames) {
404 torture_comment(tctx, "NOTE: More EAs could be available!\n");
406 if (total == 0) {
407 ret = false;
409 done:
410 smbcli_close(cli->tree, fnum);
411 return ret;
415 test using NTTRANS CREATE to create a file with an initial EA set
417 static bool test_nttrans_create(struct smbcli_state *cli, struct torture_context *tctx)
419 NTSTATUS status;
420 union smb_open io;
421 const char *fname = BASEDIR "\\ea2.txt";
422 const char *fname_bad = BASEDIR "\\ea2_bad.txt";
423 bool ret = true;
424 int fnum = -1;
425 struct ea_struct eas[3];
426 struct smb_ea_list ea_list;
428 torture_comment(tctx, "TESTING NTTRANS CREATE WITH EAS\n");
430 io.generic.level = RAW_OPEN_NTTRANS_CREATE;
431 io.ntcreatex.in.root_fid.fnum = 0;
432 io.ntcreatex.in.flags = 0;
433 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
434 io.ntcreatex.in.create_options = 0;
435 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
436 io.ntcreatex.in.share_access =
437 NTCREATEX_SHARE_ACCESS_READ |
438 NTCREATEX_SHARE_ACCESS_WRITE;
439 io.ntcreatex.in.alloc_size = 0;
440 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
441 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
442 io.ntcreatex.in.security_flags = 0;
443 io.ntcreatex.in.fname = fname;
445 ea_list.num_eas = 3;
446 ea_list.eas = eas;
448 eas[0].flags = 0;
449 eas[0].name.s = "1st EA";
450 eas[0].value = data_blob_string_const("Value One");
452 eas[1].flags = 0;
453 eas[1].name.s = "2nd EA";
454 eas[1].value = data_blob_string_const("Second Value");
456 eas[2].flags = 0;
457 eas[2].name.s = "and 3rd";
458 eas[2].value = data_blob_string_const("final value");
460 io.ntcreatex.in.ea_list = &ea_list;
461 io.ntcreatex.in.sec_desc = NULL;
463 status = smb_raw_open(cli->tree, tctx, &io);
464 CHECK_STATUS(status, NT_STATUS_OK);
465 fnum = io.ntcreatex.out.file.fnum;
467 ret &= check_ea(cli, fname, "EAONE", NULL);
468 ret &= check_ea(cli, fname, "1st EA", "Value One");
469 ret &= check_ea(cli, fname, "2nd EA", "Second Value");
470 ret &= check_ea(cli, fname, "and 3rd", "final value");
472 smbcli_close(cli->tree, fnum);
474 torture_comment(tctx, "Trying to add EAs on non-create\n");
475 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
476 io.ntcreatex.in.fname = fname;
478 ea_list.num_eas = 1;
479 eas[0].flags = 0;
480 eas[0].name.s = "Fourth EA";
481 eas[0].value = data_blob_string_const("Value Four");
483 status = smb_raw_open(cli->tree, tctx, &io);
484 CHECK_STATUS(status, NT_STATUS_OK);
485 fnum = io.ntcreatex.out.file.fnum;
487 ret &= check_ea(cli, fname, "1st EA", "Value One");
488 ret &= check_ea(cli, fname, "2nd EA", "Second Value");
489 ret &= check_ea(cli, fname, "and 3rd", "final value");
490 ret &= check_ea(cli, fname, "Fourth EA", NULL);
492 torture_comment(tctx, "TESTING NTTRANS CREATE WITH BAD EA NAMES\n");
494 io.generic.level = RAW_OPEN_NTTRANS_CREATE;
495 io.ntcreatex.in.root_fid.fnum = 0;
496 io.ntcreatex.in.flags = 0;
497 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
498 io.ntcreatex.in.create_options = 0;
499 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
500 io.ntcreatex.in.share_access =
501 NTCREATEX_SHARE_ACCESS_READ |
502 NTCREATEX_SHARE_ACCESS_WRITE;
503 io.ntcreatex.in.alloc_size = 0;
504 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
505 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
506 io.ntcreatex.in.security_flags = 0;
507 io.ntcreatex.in.fname = fname_bad;
509 ea_list.num_eas = 3;
510 ea_list.eas = eas;
512 eas[0].flags = 0;
513 eas[0].name.s = "1st EA";
514 eas[0].value = data_blob_string_const("Value One");
516 eas[1].flags = 0;
517 eas[1].name.s = "2nd:BAD:EA";
518 eas[1].value = data_blob_string_const("Second Value");
520 eas[2].flags = 0;
521 eas[2].name.s = "and 3rd";
522 eas[2].value = data_blob_string_const("final value");
524 io.ntcreatex.in.ea_list = &ea_list;
525 io.ntcreatex.in.sec_desc = NULL;
527 status = smb_raw_open(cli->tree, tctx, &io);
528 CHECK_STATUS(status, STATUS_INVALID_EA_NAME);
530 /* File must not exist. */
531 io.generic.level = RAW_OPEN_NTCREATEX;
532 io.ntcreatex.in.root_fid.fnum = 0;
533 io.ntcreatex.in.flags = 0;
534 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
535 io.ntcreatex.in.create_options = 0;
536 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
537 io.ntcreatex.in.share_access =
538 NTCREATEX_SHARE_ACCESS_READ |
539 NTCREATEX_SHARE_ACCESS_WRITE;
540 io.ntcreatex.in.alloc_size = 0;
541 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
542 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
543 io.ntcreatex.in.security_flags = 0;
544 io.ntcreatex.in.fname = fname_bad;
545 status = smb_raw_open(cli->tree, tctx, &io);
546 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
548 done:
549 smbcli_close(cli->tree, fnum);
550 return ret;
554 basic testing of EA calls
556 bool torture_raw_eas(struct torture_context *torture, struct smbcli_state *cli)
558 bool ret = true;
560 torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
562 ret &= test_eas(cli, torture);
563 ret &= test_nttrans_create(cli, torture);
565 smb_raw_exit(cli->session);
567 return ret;
571 test max EA size
573 bool torture_max_eas(struct torture_context *torture)
575 struct smbcli_state *cli;
576 bool ret = true;
578 if (!torture_open_connection(&cli, torture, 0)) {
579 return false;
582 torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
584 ret &= test_max_eas(cli, torture);
586 smb_raw_exit(cli->session);
587 if (!maxeadebug) {
588 /* in no ea debug case, all files are gone now */
589 smbcli_deltree(cli->tree, BASEDIR);
592 torture_close_connection(cli);
593 return ret;