[PATCH] Add libsmbclient list shares test

Andreas Schneider asn at samba.org
Mon Aug 27 15:29:22 UTC 2018


Hi,

the attached patch adds a test for listing shares using libsmbclient and fixes 
teh tests to run against a target with smbd.


Please review and push if OK.


Thanks,



	Andreas


-- 
Andreas Schneider                      asn at samba.org
Samba Team                             www.samba.org
GPG-ID:     8DFF53E18F2ABC8D8F3C92237EE0FC4DCC014E3D
-------------- next part --------------
>From 50cd8fe7e1796dcb21eaa7eac787c90ebaea57d6 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Thu, 23 Aug 2018 14:00:29 +0200
Subject: [PATCH 1/3] s4:torture: Improve torture_libsmbclient_init_context()

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 source4/torture/libsmbclient/libsmbclient.c | 25 +++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/source4/torture/libsmbclient/libsmbclient.c b/source4/torture/libsmbclient/libsmbclient.c
index e84ce018a69..b8fab225d90 100644
--- a/source4/torture/libsmbclient/libsmbclient.c
+++ b/source4/torture/libsmbclient/libsmbclient.c
@@ -43,11 +43,23 @@ static void debug_callback(void *private_ptr, int level, const char *msg)
 bool torture_libsmbclient_init_context(struct torture_context *tctx,
 				       SMBCCTX **ctx_p)
 {
-	SMBCCTX *ctx;
+	SMBCCTX *ctx = NULL;
+	SMBCCTX *p = NULL;
+	bool ok = true;
 
 	ctx = smbc_new_context();
-	torture_assert(tctx, ctx, "failed to get new context");
-	torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
+	torture_assert_not_null_goto(tctx,
+				     ctx,
+				     ok,
+				     out,
+				     "Failed to create new context");
+
+	p = smbc_init_context(ctx);
+	torture_assert_not_null_goto(tctx,
+				     p,
+				     ok,
+				     out,
+				     "Failed to initialize context");
 
 	smbc_setDebug(ctx, DEBUGLEVEL);
 	smbc_setOptionDebugToStderr(ctx, 1);
@@ -57,7 +69,12 @@ bool torture_libsmbclient_init_context(struct torture_context *tctx,
 
 	*ctx_p = ctx;
 
-	return true;
+out:
+	if (!ok) {
+		smbc_free_context(ctx, 1);
+	}
+
+	return ok;
 }
 
 static bool torture_libsmbclient_version(struct torture_context *tctx)
-- 
2.18.0


>From c215fca7dcf83f11f47c214a926c8e7ce77d876b Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Thu, 23 Aug 2018 14:12:43 +0200
Subject: [PATCH 2/3] selftest: Schedule libsmbclient tests against ad_dc

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 source4/selftest/tests.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py
index f884d272cc7..b0584928c3a 100755
--- a/source4/selftest/tests.py
+++ b/source4/selftest/tests.py
@@ -322,12 +322,14 @@ base = smbtorture4_testsuites("base.")
 
 netapi = smbtorture4_testsuites("netapi.")
 
-libsmbclient = smbtorture4_testsuites("libsmbclient.")
-libsmbclient_testargs = ["--option=torture:smburl=smb://$USERNAME:$PASSWORD@$SERVER/tmp",
-                         "--option=torture:replace_smbconf=%s/testdata/samba3/smb_new.conf" % srcdir()]
+for t in base + raw + smb2 + netapi:
+    plansmbtorture4testsuite(t, "ad_dc_ntvfs", ['//$SERVER/tmp', '-U$USERNAME%$PASSWORD'] + ntvfsargs)
 
-for t in base + raw + smb2 + netapi + libsmbclient:
-    plansmbtorture4testsuite(t, "ad_dc_ntvfs", ['//$SERVER/tmp', '-U$USERNAME%$PASSWORD'] + ntvfsargs + libsmbclient_testargs)
+libsmbclient = smbtorture4_testsuites("libsmbclient.")
+for t in libsmbclient:
+    libsmbclient_testargs = ["--option=torture:smburl=smb://$USERNAME:$PASSWORD@$SERVER/tmp",
+                             "--option=torture:replace_smbconf=%s/testdata/samba3/smb_new.conf" % srcdir()]
+    plansmbtorture4testsuite(t, "ad_dc", ['//$SERVER/tmp', '-U$USERNAME%$PASSWORD'] + libsmbclient_testargs)
 
 plansmbtorture4testsuite("raw.qfileinfo.ipc", "ad_dc_ntvfs", '//$SERVER/ipc\$ -U$USERNAME%$PASSWORD')
 
-- 
2.18.0


>From d91261a1b7e752505fc361328d3f8f2def438588 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 27 Aug 2018 15:59:59 +0200
Subject: [PATCH 3/3] s4:torture: Add a test for listing shares

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 source4/selftest/tests.py                   |  6 ++-
 source4/torture/libsmbclient/libsmbclient.c | 57 +++++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py
index b0584928c3a..b0d597017f3 100755
--- a/source4/selftest/tests.py
+++ b/source4/selftest/tests.py
@@ -327,7 +327,11 @@ for t in base + raw + smb2 + netapi:
 
 libsmbclient = smbtorture4_testsuites("libsmbclient.")
 for t in libsmbclient:
-    libsmbclient_testargs = ["--option=torture:smburl=smb://$USERNAME:$PASSWORD@$SERVER/tmp",
+    url = "smb://$USERNAME:$PASSWORD@$SERVER/tmp"
+    if "list_shares" in t:
+        url = "smb://$USERNAME:$PASSWORD@$SERVER"
+
+    libsmbclient_testargs = ["--option=torture:smburl=" + url,
                              "--option=torture:replace_smbconf=%s/testdata/samba3/smb_new.conf" % srcdir()]
     plansmbtorture4testsuite(t, "ad_dc", ['//$SERVER/tmp', '-U$USERNAME%$PASSWORD'] + libsmbclient_testargs)
 
diff --git a/source4/torture/libsmbclient/libsmbclient.c b/source4/torture/libsmbclient/libsmbclient.c
index b8fab225d90..fb27ddd0326 100644
--- a/source4/torture/libsmbclient/libsmbclient.c
+++ b/source4/torture/libsmbclient/libsmbclient.c
@@ -522,6 +522,62 @@ done:
 	return ok;
 }
 
+static bool torture_libsmbclient_list_shares(struct torture_context *tctx)
+{
+	const char *smburl = torture_setting_string(tctx, "smburl", NULL);
+	struct smbc_dirent *dirent = NULL;
+	SMBCCTX *ctx = NULL;
+	int dhandle = -1;
+	bool ipc_share_found = false;
+	bool ok = true;
+
+	if (smburl == NULL) {
+		torture_fail(tctx,
+			     "option --option=torture:smburl="
+			     "smb://user:password@server missing\n");
+	}
+
+	ok = torture_libsmbclient_init_context(tctx, &ctx);
+	torture_assert_goto(tctx,
+			    ok,
+			    ok,
+			    out,
+			    "Failed to init context");
+	smbc_set_context(ctx);
+
+	torture_comment(tctx, "Listing: %s\n", smburl);
+	dhandle = smbc_opendir(smburl);
+	torture_assert_int_not_equal_goto(tctx,
+					  dhandle,
+					  -1,
+					  ok,
+					  out,
+					  "Failed to open smburl");
+
+	while((dirent = smbc_readdir(dhandle)) != NULL) {
+		torture_comment(tctx, "DIR: %s\n", dirent->name);
+		torture_assert_not_null_goto(tctx,
+					     dirent->name,
+					     ok,
+					     out,
+					     "Failed to read name");
+
+		if (strequal(dirent->name, "IPC$")) {
+			ipc_share_found = true;
+		}
+	}
+
+	torture_assert_goto(tctx,
+			    ipc_share_found,
+			    ok,
+			    out,
+			    "Failed to list IPC$ share");
+
+out:
+	smbc_closedir(dhandle);
+	return ok;
+}
+
 NTSTATUS torture_libsmbclient_init(TALLOC_CTX *ctx)
 {
 	struct torture_suite *suite;
@@ -534,6 +590,7 @@ NTSTATUS torture_libsmbclient_init(TALLOC_CTX *ctx)
 	torture_suite_add_simple_test(suite, "setConfiguration", torture_libsmbclient_setConfiguration);
 	torture_suite_add_simple_test(suite, "options", torture_libsmbclient_options);
 	torture_suite_add_simple_test(suite, "opendir", torture_libsmbclient_opendir);
+	torture_suite_add_simple_test(suite, "list_shares", torture_libsmbclient_list_shares);
 	torture_suite_add_simple_test(suite, "readdirplus",
 		torture_libsmbclient_readdirplus);
 
-- 
2.18.0



More information about the samba-technical mailing list