Debian Codegraph @

source4/torture/libsmbclient/libsmbclient.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Guenther Deschner 2010
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 "system/dir.h"
22 #include "torture/smbtorture.h"
23 #include "auth/credentials/credentials.h"
24 #include "lib/cmdline/cmdline.h"
25 #include <libsmbclient.h>
26 #include "torture/libsmbclient/proto.h"
27 #include "lib/param/loadparm.h"
28 #include "lib/param/param_global.h"
29 #include "libcli/smb/smb_constants.h"
30 #include "dynconfig.h"
31 #include "lib/util/time.h"
32
33 /* test string to compare with when debug_callback is called */
34 #define TEST_STRING "smbc_setLogCallback test"
35
36 /* Dummy log callback function */
37 static void debug_callback(void *private_ptr, int level, const char *msg)
38 {
39 bool *found = private_ptr;
40 if (strstr(msg, TEST_STRING) != NULL) {
41 *found = true;
42 }
43 return;
44 }
45
46 static void auth_callback(const char *srv,
47 const char *shr,
48 char *wg, int wglen,
49 char *un, int unlen,
50 char *pw, int pwlen)
51 {
52 const char *workgroup =
53 cli_credentials_get_domain(samba_cmdline_get_creds());
54 const char *username =
55 cli_credentials_get_username(samba_cmdline_get_creds());
56 const char *password =
57 cli_credentials_get_password(samba_cmdline_get_creds());
58 ssize_t ret;
59
60 if (workgroup != NULL) {
61 ret = strlcpy(wg, workgroup, wglen);
62 if (ret >= wglen) {
63 abort();
64 }
65 }
66
67 if (username != NULL) {
68 ret = strlcpy(un, username, unlen);
69 if (ret >= unlen) {
70 abort();
71 }
72 }
73
74 if (password != NULL) {
75 ret = strlcpy(pw, password, pwlen);
76 if (ret >= pwlen) {
77 abort();
78 }
79 }
80 };
81
82 bool torture_libsmbclient_init_context(struct torture_context *tctx,
83 SMBCCTX **ctx_p)
84 {
85 const char *workgroup =
86 cli_credentials_get_domain(samba_cmdline_get_creds());
87 const char *username =
88 cli_credentials_get_username(samba_cmdline_get_creds());
89 const char *client_proto =
90 torture_setting_string(tctx, "clientprotocol", NULL);
91 SMBCCTX *ctx = NULL;
92 SMBCCTX *p = NULL;
93 bool ok = true;
94 int dbglevel = DEBUGLEVEL;
95
96 ctx = smbc_new_context();
97 torture_assert_not_null_goto(tctx,
98 ctx,
99 ok,
100 out,
101 "Failed to create new context");
102
103 p = smbc_init_context(ctx);
104 torture_assert_not_null_goto(tctx,
105 p,
106 ok,
107 out,
108 "Failed to initialize context");
109
110 smbc_setDebug(ctx, dbglevel);
111 smbc_setOptionDebugToStderr(ctx, 1);
112
113 if (workgroup != NULL) {
114 smbc_setWorkgroup(ctx, workgroup);
115 }
116 if (username != NULL) {
117 smbc_setUser(ctx, username);
118 }
119
120 smbc_setFunctionAuthData(ctx, auth_callback);
121
122 if (client_proto != NULL) {
123 smbc_setOptionProtocols(ctx, client_proto, client_proto);
124 }
125
126 *ctx_p = ctx;
127
128 out:
129 if (!ok) {
130 smbc_free_context(ctx, 1);
131 }
132
133 return ok;
134 }
135
136 static bool torture_libsmbclient_version(struct torture_context *tctx)
137 {
138 torture_comment(tctx, "Testing smbc_version\n");
139
140 torture_assert(tctx, smbc_version(), "failed to get version");
141
142 return true;
143 }
144
145 static bool torture_libsmbclient_initialize(struct torture_context *tctx)
146 {
147 SMBCCTX *ctx;
148 bool ret = false;
149
150 torture_comment(tctx, "Testing smbc_new_context\n");
151
152 ctx = smbc_new_context();
153 torture_assert(tctx, ctx, "failed to get new context");
154
155 torture_comment(tctx, "Testing smbc_init_context\n");
156
157 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
158
159 smbc_setLogCallback(ctx, &ret, debug_callback);
160 DEBUG(0, (TEST_STRING"\n"));
161 torture_assert(tctx, ret, "Failed debug_callback not called");
162 ret = false;
163 smbc_setLogCallback(ctx, NULL, NULL);
164 DEBUG(0, (TEST_STRING"\n"));
165 torture_assert(tctx, !ret, "Failed debug_callback called");
166
167 smbc_free_context(ctx, 1);
168
169 return true;
170 }
171
172 static bool torture_libsmbclient_setConfiguration(struct torture_context *tctx)
173 {
174 SMBCCTX *ctx;
175 struct loadparm_global *global_config = NULL;
176 const char *new_smb_conf = torture_setting_string(tctx,
177 "replace_smbconf",
178 "");
179
180 ctx = smbc_new_context();
181 torture_assert_not_null(tctx, ctx, "failed to get new context");
182
183 torture_assert_not_null(
184 tctx, smbc_init_context(ctx), "failed to init context");
185
186 torture_comment(tctx, "Testing smbc_setConfiguration - new file %s\n",
187 new_smb_conf);
188
189 global_config = get_globals();
190 torture_assert(tctx, global_config, "Global Config is NULL");
191
192 /* check configuration before smbc_setConfiguration call */
193 torture_comment(tctx, "'workgroup' before setConfiguration %s\n",
194 global_config->workgroup);
195 torture_comment(tctx, "'client min protocol' before "
196 "setConfiguration %d\n",
197 global_config->client_min_protocol);
198 torture_comment(tctx, "'client max protocol' before "
199 "setConfiguration %d\n",
200 global_config->_client_max_protocol);
201 torture_comment(tctx, "'client signing' before setConfiguration %d\n",
202 global_config->client_signing);
203 torture_comment(tctx, "'deadtime' before setConfiguration %d\n",
204 global_config->deadtime);
205
206 torture_assert_int_equal(tctx, smbc_setConfiguration(ctx, new_smb_conf),
207 0, "setConfiguration conf file not found");
208
209 /* verify configuration */
210 torture_assert_str_equal(tctx, global_config->workgroup,
211 "NEW_WORKGROUP",
212 "smbc_setConfiguration failed, "
213 "'workgroup' not updated");
214 torture_assert_int_equal(tctx, global_config->client_min_protocol, PROTOCOL_NT1,
215 "smbc_setConfiguration failed, 'client min protocol' "
216 "not updated");
217 torture_assert_int_equal(tctx, global_config->_client_max_protocol, PROTOCOL_SMB3_00,
218 "smbc_setConfiguration failed, 'client max protocol' "
219 "not updated");
220 torture_assert_int_equal(tctx, global_config->client_signing, 1,
221 "smbc_setConfiguration failed, 'client signing' "
222 "not updated");
223 torture_assert_int_equal(tctx, global_config->deadtime, 5,
224 "smbc_setConfiguration failed, 'deadtime' not updated");
225
226 /* Restore configuration to default */
227 smbc_setConfiguration(ctx, get_dyn_CONFIGFILE());
228
229 smbc_free_context(ctx, 1);
230
231 return true;
232 }
233
234 static bool test_opendir(struct torture_context *tctx,
235 SMBCCTX *ctx,
236 const char *fname,
237 bool expect_success)
238 {
239 int handle, ret;
240
241 torture_comment(tctx, "Testing smbc_opendir(%s)\n", fname);
242
243 handle = smbc_opendir(fname);
244 if (!expect_success) {
245 return true;
246 }
247 if (handle < 0) {
248 torture_fail(tctx, talloc_asprintf(tctx, "failed to obain file handle for '%s'", fname));
249 }
250
251 ret = smbc_closedir(handle);
252 torture_assert_int_equal(tctx, ret, 0,
253 talloc_asprintf(tctx, "failed to close file handle for '%s'", fname));
254
255 return true;
256 }
257
258 static bool torture_libsmbclient_opendir(struct torture_context *tctx)
259 {
260 size_t i;
261 SMBCCTX *ctx;
262 bool ret = true;
263 const char *bad_urls[] = {
264 "",
265 NULL,
266 "smb",
267 "smb:",
268 "smb:/",
269 "smb:///",
270 "bms://",
271 ":",
272 ":/",
273 "://",
274 ":///",
275 "/",
276 "//",
277 "///"
278 };
279 const char *good_urls[] = {
280 "smb://",
281 "smb://WORKGROUP",
282 "smb://WORKGROUP/"
283 };
284
285 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
286 smbc_set_context(ctx);
287
288 for (i=0; i < ARRAY_SIZE(bad_urls); i++) {
289 ret &= test_opendir(tctx, ctx, bad_urls[i], false);
290 }
291 for (i=0; i < ARRAY_SIZE(good_urls); i++) {
292 ret &= test_opendir(tctx, ctx, good_urls[i], true);
293 }
294
295 smbc_free_context(ctx, 1);
296
297 return ret;
298 }
299
300 static bool torture_libsmbclient_readdirplus(struct torture_context *tctx)
301 {
302 SMBCCTX *ctx;
303 int ret = -1;
304 int dhandle = -1;
305 int fhandle = -1;
306 bool found = false;
307 const char *filename = NULL;
308 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
309
310 if (smburl == NULL) {
311 torture_fail(tctx,
312 "option --option=torture:smburl="
313 "smb://user:password@server/share missing\n");
314 }
315
316 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
317 smbc_set_context(ctx);
318
319 filename = talloc_asprintf(tctx,
320 "%s/test_readdirplus.txt",
321 smburl);
322 if (filename == NULL) {
323 torture_fail(tctx,
324 "talloc fail\n");
325 }
326 /* Ensure the file doesn't exist. */
327 smbc_unlink(filename);
328
329 /* Create it. */
330 fhandle = smbc_creat(filename, 0666);
331 if (fhandle < 0) {
332 torture_fail(tctx,
333 talloc_asprintf(tctx,
334 "failed to create file '%s': %s",
335 filename,
336 strerror(errno)));
337 }
338 ret = smbc_close(fhandle);
339 torture_assert_int_equal(tctx,
340 ret,
341 0,
342 talloc_asprintf(tctx,
343 "failed to close handle for '%s'",
344 filename));
345
346 dhandle = smbc_opendir(smburl);
347 if (dhandle < 0) {
348 int saved_errno = errno;
349 smbc_unlink(filename);
350 torture_fail(tctx,
351 talloc_asprintf(tctx,
352 "failed to obtain "
353 "directory handle for '%s' : %s",
354 smburl,
355 strerror(saved_errno)));
356 }
357
358 /* Readdirplus to ensure we see the new file. */
359 for (;;) {
360 const struct libsmb_file_info *exstat =
361 smbc_readdirplus(dhandle);
362 if (exstat == NULL) {
363 break;
364 }
365 if (strcmp(exstat->name, "test_readdirplus.txt") == 0) {
366 found = true;
367 break;
368 }
369 }
370
371 /* Remove it again. */
372 smbc_unlink(filename);
373 ret = smbc_closedir(dhandle);
374 torture_assert_int_equal(tctx,
375 ret,
376 0,
377 talloc_asprintf(tctx,
378 "failed to close directory handle for '%s'",
379 smburl));
380
381 smbc_free_context(ctx, 1);
382
383 if (!found) {
384 torture_fail(tctx,
385 talloc_asprintf(tctx,
386 "failed to find file '%s'",
387 filename));
388 }
389
390 return true;
391 }
392
393 static bool torture_libsmbclient_readdirplus_seek(struct torture_context *tctx)
394 {
395 SMBCCTX *ctx;
396 int ret = -1;
397 int dhandle = -1;
398 int fhandle = -1;
399 const char *dname = NULL;
400 const char *full_filename[100] = {0};
401 const char *filename[100] = {0};
402 const struct libsmb_file_info *direntries[102] = {0};
403 unsigned int i = 0;
404 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
405 bool success = false;
406 off_t telldir_50 = (off_t)-1;
407 off_t telldir_20 = (off_t)-1;
408 size_t getdentries_size = 0;
409 struct smbc_dirent *getdentries = NULL;
410 struct smbc_dirent *dirent_20 = NULL;
411 const struct libsmb_file_info *direntries_20 = NULL;
412 const struct libsmb_file_info *direntriesplus_20 = NULL;
413 const char *plus2_stat_path = NULL;
414 struct stat st = {0};
415 struct stat st2 = {0};
416
417 torture_assert_not_null(
418 tctx,
419 smburl,
420 "option --option=torture:smburl="
421 "smb://user:password@server/share missing\n");
422
423 DEBUG(0,("torture_libsmbclient_readdirplus_seek start\n"));
424
425 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
426 smbc_set_context(ctx);
427
428 dname = talloc_asprintf(tctx,
429 "%s/rd_seek",
430 smburl);
431 torture_assert_not_null_goto(
432 tctx, dname, success, done, "talloc fail\n");
433
434 /* Ensure the files don't exist. */
435 for (i = 0; i < 100; i++) {
436 filename[i] = talloc_asprintf(tctx,
437 "test_readdirplus_%u.txt",
438 i);
439 torture_assert_not_null_goto(
440 tctx, filename[i], success, done, "talloc fail");
441 full_filename[i] = talloc_asprintf(tctx,
442 "%s/%s",
443 dname,
444 filename[i]);
445 torture_assert_not_null_goto(
446 tctx, full_filename[i], success, done, "talloc fail");
447 (void)smbc_unlink(full_filename[i]);
448 }
449 /* Ensure the directory doesn't exist. */
450 (void)smbc_rmdir(dname);
451
452 /* Create containing directory. */
453 ret = smbc_mkdir(dname, 0777);
454 torture_assert_goto(
455 tctx,
456 ret == 0,
457 success,
458 done,
459 talloc_asprintf(tctx,
460 "failed to create directory '%s': %s",
461 dname,
462 strerror(errno)));
463
464 DEBUG(0,("torture_libsmbclient_readdirplus_seek create\n"));
465
466 /* Create them. */
467 for (i = 0; i < 100; i++) {
468 fhandle = smbc_creat(full_filename[i], 0666);
469 if (fhandle < 0) {
470 torture_fail_goto(tctx,
471 done,
472 talloc_asprintf(tctx,
473 "failed to create file '%s': %s",
474 full_filename[i],
475 strerror(errno)));
476 }
477 ret = smbc_close(fhandle);
478 torture_assert_int_equal_goto(tctx,
479 ret,
480 0,
481 success,
482 done,
483 talloc_asprintf(tctx,
484 "failed to close handle for '%s'",
485 full_filename[i]));
486 }
487
488 DEBUG(0,("torture_libsmbclient_readdirplus_seek enum\n"));
489
490 /* Now enumerate the directory. */
491 dhandle = smbc_opendir(dname);
492 torture_assert_goto(
493 tctx,
494 dhandle >= 0,
495 success,
496 done,
497 talloc_asprintf(tctx,
498 "failed to obtain "
499 "directory handle for '%s' : %s",
500 dname,
501 strerror(errno)));
502
503 /* Read all the files. 100 we created plus . and .. */
504 for (i = 0; i < 102; i++) {
505 bool found = false;
506 unsigned int j;
507
508 direntries[i] = smbc_readdirplus(dhandle);
509 if (direntries[i] == NULL) {
510 break;
511 }
512
513 /* Store at offset 50. */
514 if (i == 50) {
515 telldir_50 = smbc_telldir(dhandle);
516 torture_assert_goto(
517 tctx,
518 telldir_50 != (off_t)-1,
519 success,
520 done,
521 talloc_asprintf(tctx,
522 "telldir failed file %s\n",
523 direntries[i]->name));
524 }
525
526 if (ISDOT(direntries[i]->name)) {
527 continue;
528 }
529 if (ISDOTDOT(direntries[i]->name)) {
530 continue;
531 }
532
533 /* Ensure all our files exist. */
534 for (j = 0; j < 100; j++) {
535 if (strcmp(direntries[i]->name,
536 filename[j]) == 0) {
537 found = true;
538 }
539 }
540 torture_assert_goto(
541 tctx,
542 found,
543 success,
544 done,
545 talloc_asprintf(tctx,
546 "failed to find file %s\n",
547 direntries[i]->name));
548 }
549
550 /*
551 * We're seeking on in-memory lists here, so
552 * whilst the handle is open we really should
553 * get the same files back in the same order.
554 */
555
556 ret = smbc_lseekdir(dhandle, telldir_50);
557 torture_assert_int_equal_goto(tctx,
558 ret,
559 0,
560 success,
561 done,
562 talloc_asprintf(tctx,
563 "failed to seek (50) directory handle for '%s'",
564 dname));
565
566 DEBUG(0,("torture_libsmbclient_readdirplus_seek seek\n"));
567
568 for (i = 51; i < 102; i++) {
569 const struct libsmb_file_info *entry =
570 smbc_readdirplus(dhandle);
571 torture_assert_goto(
572 tctx,
573 entry == direntries[i],
574 success,
575 done,
576 talloc_asprintf(tctx,
577 "after seek - failed to find "
578 "file %s - got %s\n",
579 direntries[i]->name,
580 entry->name));
581 }
582
583 /* Seek back to the start. */
584 ret = smbc_lseekdir(dhandle, 0);
585 torture_assert_int_equal_goto(tctx,
586 ret,
587 0,
588 success,
589 done,
590 talloc_asprintf(tctx,
591 "failed to seek directory handle to start for '%s'",
592 dname));
593
594 /*
595 * Mix getdents/readdir/readdirplus with lseek to ensure
596 * we get the same result.
597 */
598
599 /* Allocate the space for 20 entries.
600 * Tricky as we need to allocate 20 struct smbc_dirent's + space
601 * for the name lengths.
602 */
603 getdentries_size = 20 * (sizeof(struct smbc_dirent) +
604 strlen("test_readdirplus_1000.txt") + 1);
605
606 getdentries = (struct smbc_dirent *)talloc_array_size(tctx,
607 getdentries_size,
608 1);
609 torture_assert_not_null_goto(
610 tctx,
611 getdentries,
612 success,
613 done,
614 "talloc fail");
615
616 ret = smbc_getdents(dhandle, getdentries, getdentries_size);
617 torture_assert_goto(tctx,
618 (ret != -1),
619 success,
620 done,
621 talloc_asprintf(tctx,
622 "smbd_getdents(1) for '%s' failed\n",
623 dname));
624
625 telldir_20 = smbc_telldir(dhandle);
626 torture_assert_goto(
627 tctx,
628 telldir_20 != (off_t)-1,
629 success,
630 done,
631 "telldir (20) failed\n");
632
633 /* Read another 20. */
634 ret = smbc_getdents(dhandle, getdentries, getdentries_size);
635 torture_assert_goto(tctx,
636 (ret != -1),
637 success,
638 done,
639 talloc_asprintf(tctx,
640 "smbd_getdents(2) for '%s' failed\n",
641 dname));
642
643 /* Seek back to 20. */
644 ret = smbc_lseekdir(dhandle, telldir_20);
645 torture_assert_int_equal_goto(tctx,
646 ret,
647 0,
648 success,
649 done,
650 talloc_asprintf(tctx,
651 "failed to seek (20) directory handle for '%s'",
652 dname));
653
654 /* Read with readdir. */
655 dirent_20 = smbc_readdir(dhandle);
656 torture_assert_not_null_goto(
657 tctx,
658 dirent_20,
659 success,
660 done,
661 "smbc_readdir (20) failed\n");
662
663 /* Ensure the getdents and readdir names are the same. */
664 ret = strcmp(dirent_20->name, getdentries[0].name);
665 torture_assert_goto(
666 tctx,
667 ret == 0,
668 success,
669 done,
670 talloc_asprintf(tctx,
671 "after seek (20) readdir name mismatch "
672 "file %s - got %s\n",
673 dirent_20->name,
674 getdentries[0].name));
675
676 /* Seek back to 20. */
677 ret = smbc_lseekdir(dhandle, telldir_20);
678 torture_assert_int_equal_goto(tctx,
679 ret,
680 0,
681 success,
682 done,
683 talloc_asprintf(tctx,
684 "failed to seek (20) directory handle for '%s'",
685 dname));
686 /* Read with readdirplus. */
687 direntries_20 = smbc_readdirplus(dhandle);
688 torture_assert_not_null_goto(
689 tctx,
690 direntries_20,
691 success,
692 done,
693 "smbc_readdirplus (20) failed\n");
694
695 /* Ensure the readdirplus and readdir names are the same. */
696 ret = strcmp(dirent_20->name, direntries_20->name);
697 torture_assert_goto(
698 tctx,
699 ret == 0,
700 success,
701 done,
702 talloc_asprintf(tctx,
703 "after seek (20) readdirplus name mismatch "
704 "file %s - got %s\n",
705 dirent_20->name,
706 direntries_20->name));
707
708 /* Seek back to 20. */
709 ret = smbc_lseekdir(dhandle, telldir_20);
710 torture_assert_int_equal_goto(tctx,
711 ret,
712 0,
713 success,
714 done,
715 talloc_asprintf(tctx,
716 "failed to seek (20) directory handle for '%s'",
717 dname));
718
719 /* Read with readdirplus2. */
720 direntriesplus_20 = smbc_readdirplus2(dhandle, &st2);
721 torture_assert_not_null_goto(
722 tctx,
723 direntriesplus_20,
724 success,
725 done,
726 "smbc_readdirplus2 (20) failed\n");
727
728 /* Ensure the readdirplus2 and readdirplus names are the same. */
729 ret = strcmp(direntries_20->name, direntriesplus_20->name);
730 torture_assert_goto(
731 tctx,
732 ret == 0,
733 success,
734 done,
735 talloc_asprintf(tctx,
736 "after seek (20) readdirplus2 name mismatch "
737 "file %s - got %s\n",
738 dirent_20->name,
739 direntries_20->name));
740
741 /* Ensure doing stat gets the same data. */
742 plus2_stat_path = talloc_asprintf(tctx,
743 "%s/%s",
744 dname,
745 direntriesplus_20->name);
746 torture_assert_not_null_goto(
747 tctx,
748 plus2_stat_path,
749 success,
750 done,
751 "talloc fail\n");
752
753 ret = smbc_stat(plus2_stat_path, &st);
754 torture_assert_int_equal_goto(tctx,
755 ret,
756 0,
757 success,
758 done,
759 talloc_asprintf(tctx,
760 "failed to stat file '%s'",
761 plus2_stat_path));
762
763 torture_assert_int_equal(tctx,
764 st.st_ino,
765 st2.st_ino,
766 talloc_asprintf(tctx,
767 "file %s mismatched ino value "
768 "stat got %"PRIx64" readdirplus2 got %"PRIx64"" ,
769 plus2_stat_path,
770 (uint64_t)st.st_ino,
771 (uint64_t)st2.st_ino));
772
773 torture_assert_int_equal(tctx,
774 st.st_dev,
775 st2.st_dev,
776 talloc_asprintf(tctx,
777 "file %s mismatched dev value "
778 "stat got %"PRIx64" readdirplus2 got %"PRIx64"" ,
779 plus2_stat_path,
780 (uint64_t)st.st_dev,
781 (uint64_t)st2.st_dev));
782
783 ret = smbc_closedir(dhandle);
784 torture_assert_int_equal(tctx,
785 ret,
786 0,
787 talloc_asprintf(tctx,
788 "failed to close directory handle for '%s'",
789 dname));
790
791 dhandle = -1;
792 success = true;
793
794 done:
795
796 /* Clean up. */
797 if (dhandle != -1) {
798 smbc_closedir(dhandle);
799 }
800 for (i = 0; i < 100; i++) {
801 if (full_filename[i] != NULL) {
802 smbc_unlink(full_filename[i]);
803 }
804 }
805 if (dname != NULL) {
806 smbc_rmdir(dname);
807 }
808
809 smbc_free_context(ctx, 1);
810
811 return success;
812 }
813
814 #ifndef SMBC_FILE_MODE
815 #define SMBC_FILE_MODE (S_IFREG | 0444)
816 #endif
817
818 static bool torture_libsmbclient_readdirplus2(struct torture_context *tctx)
819 {
820 SMBCCTX *ctx = NULL;
821 int dhandle = -1;
822 int fhandle = -1;
823 bool found = false;
824 bool success = false;
825 const char *filename = NULL;
826 struct stat st2 = {0};
827 struct stat st = {0};
828 int ret;
829 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
830
831 if (smburl == NULL) {
832 torture_fail(tctx,
833 "option --option=torture:smburl="
834 "smb://user:password@server/share missing\n");
835 }
836
837 torture_assert_goto(tctx, torture_libsmbclient_init_context(tctx, &ctx), success, done, "");
838 smbc_set_context(ctx);
839
840 filename = talloc_asprintf(tctx,
841 "%s/test_readdirplus.txt",
842 smburl);
843 if (filename == NULL) {
844 torture_fail_goto(tctx, done, "talloc fail\n");
845 }
846
847 /* Ensure the file doesn't exist. */
848 smbc_unlink(filename);
849
850 /* Create it. */
851 fhandle = smbc_creat(filename, 0666);
852 if (fhandle < 0) {
853 torture_fail_goto(tctx,
854 done,
855 talloc_asprintf(tctx,
856 "failed to create file '%s': %s",
857 filename,
858 strerror(errno)));
859 }
860 ret = smbc_close(fhandle);
861 torture_assert_int_equal_goto(tctx,
862 ret,
863 0,
864 success,
865 done,
866 talloc_asprintf(tctx,
867 "failed to close handle for '%s'",
868 filename));
869
870 dhandle = smbc_opendir(smburl);
871 if (dhandle < 0) {
872 int saved_errno = errno;
873 smbc_unlink(filename);
874 torture_fail_goto(tctx,
875 done,
876 talloc_asprintf(tctx,
877 "failed to obtain "
878 "directory handle for '%s' : %s",
879 smburl,
880 strerror(saved_errno)));
881 }
882
883 /* readdirplus2 to ensure we see the new file. */
884 for (;;) {
885 const struct libsmb_file_info *exstat =
886 smbc_readdirplus2(dhandle, &st2);
887 if (exstat == NULL) {
888 break;
889 }
890
891 if (strcmp(exstat->name, "test_readdirplus.txt") == 0) {
892 found = true;
893 break;
894 }
895 }
896
897 if (!found) {
898 smbc_unlink(filename);
899 torture_fail_goto(tctx,
900 done,
901 talloc_asprintf(tctx,
902 "failed to find file '%s'",
903 filename));
904 }
905
906 /* Ensure mode is as expected. */
907 /*
908 * New file gets SMBC_FILE_MODE plus
909 * archive bit -> S_IXUSR
910 * !READONLY -> S_IWUSR.
911 */
912 torture_assert_int_equal_goto(tctx,
913 st2.st_mode,
914 SMBC_FILE_MODE|S_IXUSR|S_IWUSR,
915 success,
916 done,
917 talloc_asprintf(tctx,
918 "file %s st_mode should be 0%o, got 0%o'",
919 filename,
920 SMBC_FILE_MODE|S_IXUSR|S_IWUSR,
921 (unsigned int)st2.st_mode));
922
923 /* Ensure smbc_stat() gets the same data. */
924 ret = smbc_stat(filename, &st);
925 torture_assert_int_equal_goto(tctx,
926 ret,
927 0,
928 success,
929 done,
930 talloc_asprintf(tctx,
931 "failed to stat file '%s'",
932 filename));
933
934 torture_assert_int_equal_goto(tctx,
935 st2.st_ino,
936 st.st_ino,
937 success,
938 done,
939 talloc_asprintf(tctx,
940 "filename '%s' ino mismatch. "
941 "From smbc_readdirplus2 = %"PRIx64" "
942 "From smbc_stat = %"PRIx64"",
943 filename,
944 (uint64_t)st2.st_ino,
945 (uint64_t)st.st_ino));
946
947
948 /* Remove it again. */
949 smbc_unlink(filename);
950 ret = smbc_closedir(dhandle);
951 torture_assert_int_equal_goto(tctx,
952 ret,
953 0,
954 success,
955 done,
956 talloc_asprintf(tctx,
957 "failed to close directory handle for '%s'",
958 filename));
959 success = true;
960
961 done:
962 smbc_free_context(ctx, 1);
963 return success;
964 }
965
966 bool torture_libsmbclient_configuration(struct torture_context *tctx)
967 {
968 SMBCCTX *ctx;
969 bool ok = true;
970
971 ctx = smbc_new_context();
972 torture_assert(tctx, ctx, "failed to get new context");
973 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
974
975 torture_comment(tctx, "Testing smbc_(set|get)Debug\n");
976 smbc_setDebug(ctx, DEBUGLEVEL);
977 torture_assert_int_equal_goto(tctx,
978 smbc_getDebug(ctx),
979 DEBUGLEVEL,
980 ok,
981 done,
982 "failed to set DEBUGLEVEL");
983
984 torture_comment(tctx, "Testing smbc_(set|get)NetbiosName\n");
985 smbc_setNetbiosName(ctx, discard_const("torture_netbios"));
986 torture_assert_str_equal_goto(tctx,
987 smbc_getNetbiosName(ctx),
988 "torture_netbios",
989 ok,
990 done,
991 "failed to set NetbiosName");
992
993 torture_comment(tctx, "Testing smbc_(set|get)Workgroup\n");
994 smbc_setWorkgroup(ctx, discard_const("torture_workgroup"));
995 torture_assert_str_equal_goto(tctx,
996 smbc_getWorkgroup(ctx),
997 "torture_workgroup",
998 ok,
999 done,
1000 "failed to set Workgroup");
1001
1002 torture_comment(tctx, "Testing smbc_(set|get)User\n");
1003 smbc_setUser(ctx, "torture_user");
1004 torture_assert_str_equal_goto(tctx,
1005 smbc_getUser(ctx),
1006 "torture_user",
1007 ok,
1008 done,
1009 "failed to set User");
1010
1011 torture_comment(tctx, "Testing smbc_(set|get)Timeout\n");
1012 smbc_setTimeout(ctx, 12345);
1013 torture_assert_int_equal_goto(tctx,
1014 smbc_getTimeout(ctx),
1015 12345,
1016 ok,
1017 done,
1018 "failed to set Timeout");
1019
1020 done:
1021 smbc_free_context(ctx, 1);
1022
1023 return ok;
1024 }
1025
1026 bool torture_libsmbclient_options(struct torture_context *tctx)
1027 {
1028 SMBCCTX *ctx;
1029 bool ok = true;
1030
1031 ctx = smbc_new_context();
1032 torture_assert(tctx, ctx, "failed to get new context");
1033 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
1034
1035 torture_comment(tctx, "Testing smbc_(set|get)OptionDebugToStderr\n");
1036 smbc_setOptionDebugToStderr(ctx, true);
1037 torture_assert_goto(tctx,
1038 smbc_getOptionDebugToStderr(ctx),
1039 ok,
1040 done,
1041 "failed to set OptionDebugToStderr");
1042
1043 torture_comment(tctx, "Testing smbc_(set|get)OptionFullTimeNames\n");
1044 smbc_setOptionFullTimeNames(ctx, true);
1045 torture_assert_goto(tctx,
1046 smbc_getOptionFullTimeNames(ctx),
1047 ok,
1048 done,
1049 "failed to set OptionFullTimeNames");
1050
1051 torture_comment(tctx, "Testing smbc_(set|get)OptionOpenShareMode\n");
1052 smbc_setOptionOpenShareMode(ctx, SMBC_SHAREMODE_DENY_ALL);
1053 torture_assert_int_equal_goto(tctx,
1054 smbc_getOptionOpenShareMode(ctx),
1055 SMBC_SHAREMODE_DENY_ALL,
1056 ok,
1057 done,
1058 "failed to set OptionOpenShareMode");
1059
1060 torture_comment(tctx, "Testing smbc_(set|get)OptionUserData\n");
1061 smbc_setOptionUserData(ctx, (void *)discard_const("torture_user_data"));
1062 torture_assert_str_equal_goto(tctx,
1063 (const char*)smbc_getOptionUserData(ctx),
1064 "torture_user_data",
1065 ok,
1066 done,
1067 "failed to set OptionUserData");
1068
1069 torture_comment(tctx,
1070 "Testing smbc_(set|get)OptionSmbEncryptionLevel\n");
1071 smbc_setOptionSmbEncryptionLevel(ctx, SMBC_ENCRYPTLEVEL_REQUEST);
1072 torture_assert_int_equal_goto(tctx,
1073 smbc_getOptionSmbEncryptionLevel(ctx),
1074 SMBC_ENCRYPTLEVEL_REQUEST,
1075 ok,
1076 done,
1077 "failed to set OptionSmbEncryptionLevel");
1078
1079 torture_comment(tctx, "Testing smbc_(set|get)OptionCaseSensitive\n");
1080 smbc_setOptionCaseSensitive(ctx, false);
1081 torture_assert_goto(tctx,
1082 !smbc_getOptionCaseSensitive(ctx),
1083 ok,
1084 done,
1085 "failed to set OptionCaseSensitive");
1086
1087 torture_comment(tctx,
1088 "Testing smbc_(set|get)OptionBrowseMaxLmbCount\n");
1089 smbc_setOptionBrowseMaxLmbCount(ctx, 2);
1090 torture_assert_int_equal_goto(tctx,
1091 smbc_getOptionBrowseMaxLmbCount(ctx),
1092 2,
1093 ok,
1094 done,
1095 "failed to set OptionBrowseMaxLmbCount");
1096
1097 torture_comment(tctx,
1098 "Testing smbc_(set|get)OptionUrlEncodeReaddirEntries\n");
1099 smbc_setOptionUrlEncodeReaddirEntries(ctx, true);
1100 torture_assert_goto(tctx,
1101 smbc_getOptionUrlEncodeReaddirEntries(ctx),
1102 ok,
1103 done,
1104 "failed to set OptionUrlEncodeReaddirEntries");
1105
1106 torture_comment(tctx,
1107 "Testing smbc_(set|get)OptionOneSharePerServer\n");
1108 smbc_setOptionOneSharePerServer(ctx, true);
1109 torture_assert_goto(tctx,
1110 smbc_getOptionOneSharePerServer(ctx),
1111 ok,
1112 done,
1113 "failed to set OptionOneSharePerServer");
1114
1115 torture_comment(tctx, "Testing smbc_(set|get)OptionUseKerberos\n");
1116 smbc_setOptionUseKerberos(ctx, false);
1117 torture_assert_goto(tctx,
1118 !smbc_getOptionUseKerberos(ctx),
1119 ok,
1120 done,
1121 "failed to set OptionUseKerberos");
1122
1123 torture_comment(tctx,
1124 "Testing smbc_(set|get)OptionFallbackAfterKerberos\n");
1125 smbc_setOptionFallbackAfterKerberos(ctx, false);
1126 torture_assert_goto(tctx,
1127 !smbc_getOptionFallbackAfterKerberos(ctx),
1128 ok,
1129 done,
1130 "failed to set OptionFallbackAfterKerberos");
1131
1132 torture_comment(tctx,
1133 "Testing smbc_(set|get)OptionNoAutoAnonymousLogin\n");
1134 smbc_setOptionNoAutoAnonymousLogin(ctx, true);
1135 torture_assert_goto(tctx,
1136 smbc_getOptionNoAutoAnonymousLogin(ctx),
1137 ok,
1138 done,
1139 "failed to set OptionNoAutoAnonymousLogin");
1140
1141 torture_comment(tctx, "Testing smbc_(set|get)OptionUseCCache\n");
1142 smbc_setOptionUseCCache(ctx, true);
1143 torture_assert_goto(tctx,
1144 smbc_getOptionUseCCache(ctx),
1145 ok,
1146 done,
1147 "failed to set OptionUseCCache");
1148
1149 done:
1150 smbc_free_context(ctx, 1);
1151
1152 return ok;
1153 }
1154
1155 static bool torture_libsmbclient_list_shares(struct torture_context *tctx)
1156 {
1157 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1158 struct smbc_dirent *dirent = NULL;
1159 SMBCCTX *ctx = NULL;
1160 int dhandle = -1;
1161 bool ipc_share_found = false;
1162 bool ok = true;
1163
1164 if (smburl == NULL) {
1165 torture_fail(tctx,
1166 "option --option=torture:smburl="
1167 "smb://user:password@server missing\n");
1168 }
1169
1170 ok = torture_libsmbclient_init_context(tctx, &ctx);
1171 torture_assert_goto(tctx,
1172 ok,
1173 ok,
1174 out,
1175 "Failed to init context");
1176 smbc_set_context(ctx);
1177
1178 torture_comment(tctx, "Listing: %s\n", smburl);
1179 dhandle = smbc_opendir(smburl);
1180 torture_assert_int_not_equal_goto(tctx,
1181 dhandle,
1182 -1,
1183 ok,
1184 out,
1185 "Failed to open smburl");
1186
1187 while((dirent = smbc_readdir(dhandle)) != NULL) {
1188 torture_comment(tctx, "DIR: %s\n", dirent->name);
1189 torture_assert_not_null_goto(tctx,
1190 dirent->name,
1191 ok,
1192 out,
1193 "Failed to read name");
1194
1195 if (strequal(dirent->name, "IPC$")) {
1196 ipc_share_found = true;
1197 }
1198 }
1199
1200 torture_assert_goto(tctx,
1201 ipc_share_found,
1202 ok,
1203 out,
1204 "Failed to list IPC$ share");
1205
1206 out:
1207 smbc_closedir(dhandle);
1208 return ok;
1209 }
1210
1211 static bool torture_libsmbclient_utimes(struct torture_context *tctx)
1212 {
1213 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1214 SMBCCTX *ctx = NULL;
1215 struct stat st;
1216 int fhandle, ret;
1217 struct timeval tbuf[2];
1218 bool ok;
1219
1220 if (smburl == NULL) {
1221 torture_fail(tctx,
1222 "option --option=torture:smburl="
1223 "smb://user:password@server missing\n");
1224 }
1225
1226 ok = torture_libsmbclient_init_context(tctx, &ctx);
1227 torture_assert(tctx, ok, "Failed to init context");
1228 smbc_set_context(ctx);
1229
1230 fhandle = smbc_open(smburl, O_RDWR|O_CREAT, 0644);
1231 torture_assert_int_not_equal(tctx, fhandle, -1, "smbc_open failed");
1232
1233 ret = smbc_fstat(fhandle, &st);
1234 torture_assert_int_not_equal(tctx, ret, -1, "smbc_fstat failed");
1235
1236 tbuf[0] = convert_timespec_to_timeval(get_atimespec(&st));
1237 tbuf[1] = convert_timespec_to_timeval(get_mtimespec(&st));
1238
1239 tbuf[1] = timeval_add(&tbuf[1], 0, 100000); /* 100 msec */
1240
1241 ret = smbc_utimes(smburl, tbuf);
1242 torture_assert_int_not_equal(tctx, ret, -1, "smbc_utimes failed");
1243
1244 ret = smbc_fstat(fhandle, &st);
1245 torture_assert_int_not_equal(tctx, ret, -1, "smbc_fstat failed");
1246
1247 torture_assert_int_equal(
1248 tctx,
1249 get_mtimensec(&st) / 1000,
1250 tbuf[1].tv_usec,
1251 "smbc_utimes did not update msec");
1252
1253 smbc_close(fhandle);
1254 smbc_unlink(smburl);
1255 return true;
1256 }
1257
1258 static bool torture_libsmbclient_noanon_list(struct torture_context *tctx)
1259 {
1260 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1261 struct smbc_dirent *dirent = NULL;
1262 SMBCCTX *ctx = NULL;
1263 int dhandle = -1;
1264 bool ok = true;
1265
1266 if (smburl == NULL) {
1267 torture_fail(tctx,
1268 "option --option=torture:smburl="
1269 "smb://user:password@server missing\n");
1270 }
1271
1272 ok = torture_libsmbclient_init_context(tctx, &ctx);
1273 torture_assert_goto(tctx,
1274 ok,
1275 ok,
1276 out,
1277 "Failed to init context");
1278 torture_comment(tctx,
1279 "Testing smbc_setOptionNoAutoAnonymousLogin\n");
1280 smbc_setOptionNoAutoAnonymousLogin(ctx, true);
1281 smbc_set_context(ctx);
1282
1283 torture_comment(tctx, "Listing: %s\n", smburl);
1284 dhandle = smbc_opendir(smburl);
1285 torture_assert_int_not_equal_goto(tctx,
1286 dhandle,
1287 -1,
1288 ok,
1289 out,
1290 "Failed to open smburl");
1291
1292 while((dirent = smbc_readdir(dhandle)) != NULL) {
1293 torture_comment(tctx, "DIR: %s\n", dirent->name);
1294 torture_assert_not_null_goto(tctx,
1295 dirent->name,
1296 ok,
1297 out,
1298 "Failed to read name");
1299 }
1300
1301 out:
1302 smbc_closedir(dhandle);
1303 return ok;
1304 }
1305
1306 static bool torture_libsmbclient_rename(struct torture_context *tctx)
1307 {
1308 SMBCCTX *ctx = NULL;
1309 int fhandle = -1;
1310 bool success = false;
1311 const char *filename_src = NULL;
1312 const char *filename_dst = NULL;
1313 int ret;
1314 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1315
1316 if (smburl == NULL) {
1317 torture_fail(tctx,
1318 "option --option=torture:smburl="
1319 "smb://user:password@server/share missing\n");
1320 }
1321
1322 torture_assert_goto(tctx,
1323 torture_libsmbclient_init_context(tctx, &ctx),
1324 success,
1325 done,
1326 "");
1327
1328 smbc_set_context(ctx);
1329
1330 filename_src = talloc_asprintf(tctx,
1331 "%s/src",
1332 smburl);
1333 if (filename_src == NULL) {
1334 torture_fail_goto(tctx, done, "talloc fail\n");
1335 }
1336
1337 filename_dst = talloc_asprintf(tctx,
1338 "%s/dst",
1339 smburl);
1340 if (filename_dst == NULL) {
1341 torture_fail_goto(tctx, done, "talloc fail\n");
1342 }
1343
1344 /* Ensure the files don't exist. */
1345 smbc_unlink(filename_src);
1346 smbc_unlink(filename_dst);
1347
1348 /* Create them. */
1349 fhandle = smbc_creat(filename_src, 0666);
1350 if (fhandle < 0) {
1351 torture_fail_goto(tctx,
1352 done,
1353 talloc_asprintf(tctx,
1354 "failed to create file '%s': %s",
1355 filename_src,
1356 strerror(errno)));
1357 }
1358 ret = smbc_close(fhandle);
1359 torture_assert_int_equal_goto(tctx,
1360 ret,
1361 0,
1362 success,
1363 done,
1364 talloc_asprintf(tctx,
1365 "failed to close handle for '%s'",
1366 filename_src));
1367
1368 fhandle = smbc_creat(filename_dst, 0666);
1369 if (fhandle < 0) {
1370 torture_fail_goto(tctx,
1371 done,
1372 talloc_asprintf(tctx,
1373 "failed to create file '%s': %s",
1374 filename_dst,
1375 strerror(errno)));
1376 }
1377 ret = smbc_close(fhandle);
1378 torture_assert_int_equal_goto(tctx,
1379 ret,
1380 0,
1381 success,
1382 done,
1383 talloc_asprintf(tctx,
1384 "failed to close handle for '%s'",
1385 filename_dst));
1386
1387 ret = smbc_rename(filename_src, filename_dst);
1388
1389 /*
1390 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=14938
1391 * gives ret == -1, but errno = 0 for overwrite renames
1392 * over SMB2.
1393 */
1394 torture_assert_int_equal_goto(tctx,
1395 ret,
1396 0,
1397 success,
1398 done,
1399 talloc_asprintf(tctx,
1400 "smbc_rename '%s' -> '%s' failed with %s\n",
1401 filename_src,
1402 filename_dst,
1403 strerror(errno)));
1404
1405 /* Remove them again. */
1406 smbc_unlink(filename_src);
1407 smbc_unlink(filename_dst);
1408 success = true;
1409
1410 done:
1411 smbc_free_context(ctx, 1);
1412 return success;
1413 }
1414
1415 static bool torture_libsmbclient_getatr(struct torture_context *tctx)
1416 {
1417 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1418 SMBCCTX *ctx = NULL;
1419 char *getatr_name = NULL;
1420 struct stat st = {0};
1421 bool ok;
1422 int ret = 0;
1423 int err = 0;
1424
1425 if (smburl == NULL) {
1426 torture_fail(tctx,
1427 "option --option=torture:smburl="
1428 "smb://user:password@server missing\n");
1429 }
1430
1431 ok = torture_libsmbclient_init_context(tctx, &ctx);
1432 torture_assert(tctx, ok, "Failed to init context");
1433 smbc_set_context(ctx);
1434
1435 getatr_name = talloc_asprintf(tctx,
1436 "%s/noexist",
1437 smburl);
1438 if (getatr_name == NULL) {
1439 torture_result(tctx,
1440 TORTURE_FAIL,
1441 __location__": %s",
1442 "talloc fail\n");
1443 return false;
1444 }
1445 /* Ensure the file doesn't exist. */
1446 smbc_unlink(getatr_name);
1447 /*
1448 * smbc_stat() internally uses SMBC_getatr().
1449 * Make sure doing getatr on a non-existent file gives
1450 * an error of -1, errno = ENOENT.
1451 */
1452
1453 ret = smbc_stat(getatr_name, &st);
1454 if (ret == -1) {
1455 err = errno;
1456 }
1457 torture_assert_int_equal(tctx,
1458 ret,
1459 -1,
1460 talloc_asprintf(tctx,
1461 "smbc_stat on '%s' should "
1462 "get -1, got %d\n",
1463 getatr_name,
1464 ret));
1465 torture_assert_int_equal(tctx,
1466 err,
1467 ENOENT,
1468 talloc_asprintf(tctx,
1469 "smbc_stat on '%s' should "
1470 "get errno = ENOENT, got %s\n",
1471 getatr_name,
1472 strerror(err)));
1473 return true;
1474 }
1475
1476 static bool torture_libsmbclient_getxattr(struct torture_context *tctx)
1477 {
1478 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1479 int fhandle = -1;
1480 SMBCCTX *ctx = NULL;
1481 char *getxattr_name = NULL;
1482 char value[4096];
1483 bool ok = false;
1484 int ret = -1;
1485
1486 if (smburl == NULL) {
1487 torture_fail(tctx,
1488 "option --option=torture:smburl="
1489 "smb://user:password@server missing\n");
1490 }
1491
1492 ok = torture_libsmbclient_init_context(tctx, &ctx);
1493 torture_assert(tctx, ok, "Failed to init context");
1494 smbc_set_context(ctx);
1495
1496 getxattr_name = talloc_asprintf(tctx,
1497 "%s/getxattr",
1498 smburl);
1499 if (getxattr_name == NULL) {
1500 torture_result(tctx,
1501 TORTURE_FAIL,
1502 __location__": %s",
1503 "talloc fail\n");
1504 return false;
1505 }
1506 /* Ensure the file doesn't exist. */
1507 smbc_unlink(getxattr_name);
1508
1509 /* Create testfile. */
1510 fhandle = smbc_creat(getxattr_name, 0666);
1511 if (fhandle < 0) {
1512 torture_fail_goto(tctx,
1513 done,
1514 talloc_asprintf(tctx,
1515 "failed to create file '%s': %s",
1516 getxattr_name,
1517 strerror(errno)));
1518 }
1519 ret = smbc_close(fhandle);
1520 torture_assert_int_equal_goto(tctx,
1521 ret,
1522 0,
1523 ok,
1524 done,
1525 talloc_asprintf(tctx,
1526 "failed to close handle for '%s'",
1527 getxattr_name));
1528
1529 /*
1530 * Ensure getting a non-existent attribute returns -1.
1531 */
1532 ret = smbc_getxattr(getxattr_name, "foobar", value, sizeof(value));
1533 torture_assert_int_equal_goto(tctx,
1534 ret,
1535 -1,
1536 ok,
1537 done,
1538 talloc_asprintf(tctx,
1539 "smbc_getxattr(foobar) on '%s' should "
1540 "get -1, got %d\n",
1541 getxattr_name,
1542 ret));
1543
1544 /*
1545 * Ensure getting a valid attribute computes its size.
1546 */
1547 ret = smbc_getxattr(getxattr_name, "system.*", NULL, 0);
1548 torture_assert_goto(tctx,
1549 ret >= 0,
1550 ok,
1551 done,
1552 talloc_asprintf(tctx,
1553 "smbc_getxattr(foobar, NULL) on '%s' should "
1554 "get >=0, got %d\n",
1555 getxattr_name,
1556 ret));
1557
1558 /*
1559 * Ensure getting a valid attribute returns its size.
1560 */
1561 ret = smbc_getxattr(getxattr_name, "system.*", value, sizeof(value));
1562 torture_assert_goto(tctx,
1563 ret >= 0,
1564 ok,
1565 done,
1566 talloc_asprintf(tctx,
1567 "smbc_getxattr(foobar, value) on '%s' should "
1568 "get >=0, got %d\n",
1569 getxattr_name,
1570 ret));
1571
1572 ok = true;
1573
1574 done:
1575
1576 smbc_unlink(getxattr_name);
1577 smbc_free_context(ctx, 1);
1578 return ok;
1579 }
1580
1581 static bool torture_libsmbclient_init_posix_context(struct torture_context *tctx,
1582 SMBCCTX **ctx_p)
1583 {
1584 SMBCCTX *ctx = NULL;
1585 SMBCCTX *p = NULL;
1586 bool ok = true;
1587
1588 ctx = smbc_new_context();
1589 torture_assert_not_null_goto(
1590 tctx, ctx, ok, out, "Failed to create new context");
1591
1592 torture_comment(tctx, "Enabling POSIX extensions\n");
1593 smbc_setOptionPosixExtensions(ctx, true);
1594
1595 p = smbc_init_context(ctx);
1596 torture_assert_not_null_goto(tctx,
1597 p,
1598 ok,
1599 out,
1600 "Failed to initialize context");
1601
1602 smbc_setFunctionAuthData(ctx, auth_callback);
1603 smbc_set_context(ctx);
1604
1605 *ctx_p = ctx;
1606
1607 out:
1608 if (!ok) {
1609 smbc_free_context(ctx, true);
1610 }
1611
1612 return ok;
1613 }
1614
1615 static bool torture_libsmbclient_posix_extensions(struct torture_context *tctx)
1616 {
1617 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1618 int fhandle = -1;
1619 SMBCCTX *ctx = NULL;
1620 char *posix_name = NULL;
1621 char buf[2] = {'\0'};
1622 bool ok = false;
1623 int ret = -1;
1624
1625 if (smburl == NULL) {
1626 torture_fail(tctx,
1627 "option --option=torture:smburl="
1628 "smb://user:password@server missing\n");
1629 }
1630
1631 torture_assert_goto(tctx,
1632 torture_libsmbclient_init_posix_context(tctx, &ctx),
1633 ok,
1634 done,
1635 "Failed to init POSIX context");
1636
1637 posix_name = talloc_asprintf(tctx,
1638 "%s/posix_test_original.txt",
1639 smburl);
1640 torture_assert_not_null_goto(tctx,
1641 posix_name,
1642 ok,
1643 done,
1644 "talloc_asprintf failed\n");
1645
1646 /* Open the test file */
1647 fhandle = smbc_open(posix_name, O_RDONLY, 0);
1648 torture_assert_goto(tctx,
1649 fhandle >= 0,
1650 ok,
1651 done,
1652 talloc_asprintf(tctx,
1653 "failed to open file '%s': %s",
1654 posix_name,
1655 strerror(errno)));
1656
1657 /*
1658 * Check if we got POSIX extensions by reading the
1659 * "posix.attr.enabled" extended attribute.
1660 * This should return "1" if POSIX extensions are active.
1661 */
1662 torture_comment(tctx, "Checking for POSIX extensions\n");
1663 ret = smbc_fgetxattr(fhandle, "posix.attr.enabled", buf, sizeof(buf));
1664
1665 torture_assert_int_equal_goto(
1666 tctx,
1667 ret,
1668 1,
1669 ok,
1670 done,
1671 "smbc_fgetxattr(posix.attr.enabled) failed");
1672
1673 torture_assert_str_equal_goto(
1674 tctx, buf, "1", ok, done, "POSIX extensions not enabled.");
1675 torture_comment(tctx, "POSIX extensions are enabled!\n");
1676
1677 ok = true;
1678
1679 done:
1680 if (fhandle >= 0) {
1681 smbc_close(fhandle);
1682 }
1683 if (ctx != NULL) {
1684 smbc_free_context(ctx, 1);
1685 }
1686
1687 return ok;
1688 }
1689
1690 static bool torture_libsmbclient_posix_hardlinks(struct torture_context *tctx)
1691 {
1692 const char *smburl = torture_setting_string(tctx, "smburl", NULL);
1693 int fhandle = -1;
1694 SMBCCTX *ctx = NULL;
1695 char *posix_name = NULL;
1696 char buf[sizeof(struct stat) + 4]; /* +4 bytes for DOS attributes */
1697 struct stat st_posix;
1698 bool ok = false;
1699 int ret = -1;
1700
1701 if (smburl == NULL) {
1702 torture_fail(tctx,
1703 "option --option=torture:smburl="
1704 "smb://user:password@server missing\n");
1705 }
1706
1707 torture_assert_goto(tctx,
1708 torture_libsmbclient_init_posix_context(tctx, &ctx),
1709 ok,
1710 done,
1711 "Failed to init POSIX context");
1712
1713 posix_name = talloc_asprintf(tctx,
1714 "%s/posix_test_original.txt",
1715 smburl);
1716 torture_assert_not_null_goto(tctx,
1717 posix_name,
1718 ok,
1719 done,
1720 "talloc_asprintf failed\n");
1721
1722 /* Open the file with hardlinks */
1723 fhandle = smbc_open(posix_name, O_RDONLY, 0);
1724 torture_assert_goto(tctx,
1725 fhandle >= 0,
1726 ok,
1727 done,
1728 talloc_asprintf(tctx,
1729 "failed to open file '%s': %s",
1730 posix_name,
1731 strerror(errno)));
1732
1733 /*
1734 * Get POSIX stat information including hardlink count
1735 */
1736 torture_comment(tctx, "Getting POSIX stat info for hardlink test\n");
1737 ret = smbc_fgetxattr(fhandle,
1738 "smb311_posix.statinfo",
1739 buf,
1740 sizeof(buf));
1741
1742 torture_assert_int_equal_goto(
1743 tctx,
1744 ret,
1745 sizeof(struct stat),
1746 ok,
1747 done,
1748 talloc_asprintf(tctx,
1749 "smbc_fgetxattr(smb311_posix.statinfo) "
1750 "returned %d, expected %zu",
1751 ret,
1752 sizeof(struct stat)));
1753
1754 memcpy(&st_posix, buf, sizeof(struct stat));
1755
1756 torture_comment(tctx,
1757 "File has %lu hardlinks\n",
1758 (unsigned long)st_posix.st_nlink);
1759
1760 torture_assert_int_equal_goto(
1761 tctx,
1762 st_posix.st_nlink,
1763 3,
1764 ok,
1765 done,
1766 talloc_asprintf(tctx,
1767 "Expected 3 hardlinks, got %lu",
1768 (unsigned long)st_posix.st_nlink));
1769
1770 ok = true;
1771
1772 done:
1773 if (fhandle >= 0) {
1774 smbc_close(fhandle);
1775 }
1776 if (ctx != NULL) {
1777 smbc_free_context(ctx, 1);
1778 }
1779
1780 return ok;
1781 }
1782
1783 NTSTATUS torture_libsmbclient_init(TALLOC_CTX *ctx)
1784 {
1785 struct torture_suite *suite;
1786
1787 suite = torture_suite_create(ctx, "libsmbclient");
1788
1789 torture_suite_add_simple_test(suite, "version", torture_libsmbclient_version);
1790 torture_suite_add_simple_test(suite, "initialize", torture_libsmbclient_initialize);
1791 torture_suite_add_simple_test(suite, "configuration", torture_libsmbclient_configuration);
1792 torture_suite_add_simple_test(suite, "setConfiguration", torture_libsmbclient_setConfiguration);
1793 torture_suite_add_simple_test(suite, "options", torture_libsmbclient_options);
1794 torture_suite_add_simple_test(suite, "opendir", torture_libsmbclient_opendir);
1795 torture_suite_add_simple_test(suite, "list_shares", torture_libsmbclient_list_shares);
1796 torture_suite_add_simple_test(suite, "readdirplus",
1797 torture_libsmbclient_readdirplus);
1798 torture_suite_add_simple_test(suite, "readdirplus_seek",
1799 torture_libsmbclient_readdirplus_seek);
1800 torture_suite_add_simple_test(suite, "readdirplus2",
1801 torture_libsmbclient_readdirplus2);
1802 torture_suite_add_simple_test(
1803 suite, "utimes", torture_libsmbclient_utimes);
1804 torture_suite_add_simple_test(
1805 suite, "noanon_list", torture_libsmbclient_noanon_list);
1806 torture_suite_add_simple_test(suite,
1807 "rename",
1808 torture_libsmbclient_rename);
1809 torture_suite_add_simple_test(suite, "getatr",
1810 torture_libsmbclient_getatr);
1811 torture_suite_add_simple_test(suite, "getxattr",
1812 torture_libsmbclient_getxattr);
1813 torture_suite_add_simple_test(suite, "posix_extensions",
1814 torture_libsmbclient_posix_extensions);
1815 torture_suite_add_simple_test(suite, "posix_hardlinks",
1816 torture_libsmbclient_posix_hardlinks);
1817
1818 suite->description = talloc_strdup(suite, "libsmbclient interface tests");
1819
1820 torture_register_suite(ctx, suite);
1821
1822 return NT_STATUS_OK;
1823 }