[PATCH] changes to rpcclient

Christian Ambach ambi at samba.org
Wed May 18 20:35:41 UTC 2016


Hi Andreas,

Am 12.05.16 um 11:41 schrieb Andreas Schneider:
> Thank you very much for the test. As we talked about it yesterday, take a look 
> at https://lwn.net/Articles/558106/ which explains the mocking functionality 
> of cmocka.
> 
> For a start we could just check for cmocka using pkg-config and if we find it 
> enable the tests and run them ...
> 
> Note that you need:
> 
> 	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
> 
> so selftest can parse the results.

I started working with cmocka.
Attached you can find my current work in progress. Does that look as if
its heading into the right direction?

Cheers,
Christian



-------------- next part --------------
>From afc7639312cea9765288e5841a8eb13c33b238cd Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Wed, 18 May 2016 22:07:38 +0200
Subject: [PATCH] WIP: add a cmocka based test for rpcclient

---
 source3/rpcclient/rpcclient.c      |  5 +++
 source3/rpcclient/rpcclient_test.c | 80 ++++++++++++++++++++++++++++++++++++++
 source3/wscript                    |  2 +
 source3/wscript_build              | 57 +++++++++++++++++++++++++++
 4 files changed, 144 insertions(+)
 create mode 100644 source3/rpcclient/rpcclient_test.c

diff --git a/source3/rpcclient/rpcclient.c b/source3/rpcclient/rpcclient.c
index 44d691b..1e3efce 100644
--- a/source3/rpcclient/rpcclient.c
+++ b/source3/rpcclient/rpcclient.c
@@ -35,6 +35,11 @@
 #include "../libcli/smb/smbXcli_base.h"
 #include "messages.h"
 
+#ifdef UNITTESTING
+int rpcclient_main(int argc, char *argv[]);
+#define main rpcclient_main
+#endif
+
 enum pipe_auth_type_spnego {
 	PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
 	PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
diff --git a/source3/rpcclient/rpcclient_test.c b/source3/rpcclient/rpcclient_test.c
new file mode 100644
index 0000000..7bef877
--- /dev/null
+++ b/source3/rpcclient/rpcclient_test.c
@@ -0,0 +1,80 @@
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <cmocka.h>
+#include <string.h>
+#include "includes.h"
+#include "libsmb/libsmb.h"
+
+#define array_length(x) (sizeof(x) / sizeof((x)[0]))
+
+extern int rpcclient_main(int argc, char** argv);
+
+NTSTATUS __wrap_cli_full_connection(struct cli_state **output_cli,
+			     const char *my_name,
+			     const char *dest_host,
+			     const struct sockaddr_storage *dest_ss, int port,
+			     const char *service, const char *service_type,
+			     const char *user, const char *domain,
+			     const char *password, int flags,
+			     int signing_state);
+
+NTSTATUS __wrap_cli_full_connection(struct cli_state **output_cli,
+			     const char *my_name,
+			     const char *dest_host,
+			     const struct sockaddr_storage *dest_ss, int port,
+			     const char *service, const char *service_type,
+			     const char *user, const char *domain,
+			     const char *password, int flags,
+			     int signing_state)
+{
+	return (NTSTATUS) NT_STATUS(mock());
+}
+
+
+static void test_rpcclient_maxprotocol(void **state)
+{
+	const char *args[] = {  "rpcclient", "localhost", "-mSMB3", "-U%",
+			 "-c", "lsaquery" };
+
+	/* make password writable for popt_burn_cmdline_password */
+	args[3] = talloc_strdup(talloc_tos(), args[3]);
+
+	(void) state; /* unused */
+	will_return(__wrap_cli_full_connection,
+		    NT_STATUS_V(NT_STATUS_NOT_FOUND));
+
+	rpcclient_main(ARRAY_SIZE(args), discard_const(args));
+
+	assert_not_in_range(lp_client_max_protocol(),
+			    PROTOCOL_CORE, PROTOCOL_SMB2_24);
+}
+
+
+int main(void)
+{
+	TALLOC_CTX* frame;
+	int rc;
+
+	const struct CMUnitTest tests[] = {
+	cmocka_unit_test(
+		test_rpcclient_maxprotocol),
+	};
+
+	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+
+	if (!(frame = talloc_stackframe())) {
+		fprintf(stderr, "talloc_init() failed\n");
+		return 1;
+	}
+
+	rc = cmocka_run_group_tests(tests, NULL, NULL);
+
+	//talloc_free(frame);
+
+	return rc;
+}
+
diff --git a/source3/wscript b/source3/wscript
index 3b6f8a4..c77f115 100644
--- a/source3/wscript
+++ b/source3/wscript
@@ -95,6 +95,8 @@ def configure(conf):
     # We crash without vfs_default
     required_static_modules.extend(TO_LIST('vfs_default'))
 
+    conf.CHECK_LIB('cmocka');
+
     conf.CHECK_HEADERS('execinfo.h libexc.h libunwind.h netdb.h')
     conf.CHECK_HEADERS('linux/falloc.h linux/ioctl.h')
 
diff --git a/source3/wscript_build b/source3/wscript_build
index ed2424d..5055f51 100755
--- a/source3/wscript_build
+++ b/source3/wscript_build
@@ -1054,6 +1054,63 @@ bld.SAMBA3_BINARY('rpcclient/rpcclient',
                  RPC_NDR_CLUSAPI
                  ''')
 
+
+bld.SAMBA3_BINARY('rpcclient/rpcclient_test',
+                 source='''rpcclient/rpcclient_test.c
+                 rpcclient/rpcclient.c
+                 rpcclient/cmd_lsarpc.c
+                 rpcclient/cmd_samr.c
+                 rpcclient/cmd_spoolss.c
+                 rpcclient/cmd_netlogon.c
+                 rpcclient/cmd_srvsvc.c
+                 rpcclient/cmd_dfs.c
+                 rpcclient/cmd_epmapper.c
+                 rpcclient/cmd_dssetup.c
+                 rpcclient/cmd_echo.c
+                 rpcclient/cmd_shutdown.c
+                 rpcclient/cmd_test.c
+                 rpcclient/cmd_wkssvc.c
+                 rpcclient/cmd_ntsvcs.c
+                 rpcclient/cmd_drsuapi.c
+                 rpcclient/cmd_eventlog.c
+                 rpcclient/cmd_winreg.c
+                 rpcclient/cmd_fss.c
+                 rpcclient/cmd_witness.c
+                 rpcclient/cmd_clusapi.c''',
+                 cflags='-DUNITTESTING',
+                 ldflags='-Wl,--wrap=cli_full_connection',
+                 deps='''
+                 cmocka
+                 talloc
+                 popt_samba3
+                 pdb
+                 libsmb
+                 param
+                 ndr-standard
+                 msrpc3
+                 SMBREADLINE
+                 trusts_util
+                 RPC_NDR_WINREG
+                 RPC_NDR_ECHO
+                 RPC_CLIENT_SCHANNEL
+                 DCUTIL
+                 LIBCLI_SAMR
+                 libcli_lsa3
+                 libcli_netlogon3
+                 cli_spoolss
+                 RPC_NDR_SRVSVC
+                 RPC_NDR_WKSSVC
+                 RPC_NDR_DSSETUP
+                 RPC_NDR_DFS
+                 RPC_NDR_DRSUAPI
+                 RPC_NDR_NTSVCS
+                 RPC_NDR_EVENTLOG
+                 INIT_SAMR
+                 RPC_NDR_FSRVP
+                 RPC_NDR_WITNESS
+                 RPC_NDR_CLUSAPI
+                 ''')
+
 bld.SAMBA3_BINARY('client/smbclient',
                  source='''client/client.c
                  client/clitar.c
-- 
1.9.1



More information about the samba-technical mailing list