s4/test-libnet: Helper func to initialize libnet_context
authorKamen Mazdrashki <kamenim@samba.org>
Thu, 10 Jun 2010 10:12:08 +0000 (13:12 +0300)
committerKamen Mazdrashki <kamenim@samba.org>
Thu, 10 Jun 2010 22:20:09 +0000 (01:20 +0300)
and create rpc connections to SAMR and LSA pipes.
This function should be used in cases where we need
'deterministic' connection to the DC we are testing against.

The problem is that most of the test follow the pattern:
1. Connect to server supplied on command line
2. Create user/group through connection in 1.
3. Use 'fresh' libnet_context to query/alter the object in
   step 2.

In singe-DC environment this work well. But in multiple-DC
environment libnet may connect to another DC (step 3), not
the one we use in step 1 and 2. This leads to false error -
object created in step 2 is not found on DC we are querying
in step 3.

source4/torture/libnet/utils.c

index dfc5923f2aa90b7b9a08db9f32df93640f10c290..312b19483f5f4f8ae0b37c8ac7e66e0ad45fc9ec 100644 (file)
@@ -27,6 +27,7 @@
 #include "torture/rpc/torture_rpc.h"
 #include "libnet/libnet.h"
 #include "librpc/gen_ndr/ndr_samr_c.h"
+#include "librpc/gen_ndr/ndr_lsa_c.h"
 #include "torture/libnet/proto.h"
 #include "lib/ldb_wrap.h"
 
@@ -429,6 +430,59 @@ bool test_samr_close_handle(struct torture_context *tctx,
        return true;
 }
 
+/**
+ * Create and initialize libnet_context Context.
+ * Use this function in cases where we need to have SAMR and LSA pipes
+ * of libnet_context to be connected before executing any other
+ * libnet call
+ *
+ * @param rpc_connect [in] Connects SAMR and LSA pipes
+ */
+bool test_libnet_context_init(struct torture_context *tctx,
+                             bool rpc_connect,
+                             struct libnet_context **_net_ctx)
+{
+       NTSTATUS status;
+       bool bret = true;
+       struct libnet_context *net_ctx;
+
+       net_ctx = libnet_context_init(tctx->ev, tctx->lp_ctx);
+       torture_assert(tctx, net_ctx != NULL, "Failed to create libnet_context");
+
+       /* Use command line credentials for testing */
+       net_ctx->cred = cmdline_credentials;
+
+       if (rpc_connect) {
+               /* connect SAMR pipe */
+               status = torture_rpc_connection(tctx,
+                                               &net_ctx->samr.pipe,
+                                               &ndr_table_samr);
+               torture_assert_ntstatus_ok_goto(tctx, status, bret, done,
+                                               "Failed to connect SAMR pipe");
+
+               net_ctx->samr.samr_handle = net_ctx->samr.pipe->binding_handle;
+
+               /* connect LSARPC pipe */
+               status = torture_rpc_connection(tctx,
+                                               &net_ctx->lsa.pipe,
+                                               &ndr_table_lsarpc);
+               torture_assert_ntstatus_ok_goto(tctx, status, bret, done,
+                                               "Failed to connect LSA pipe");
+
+               net_ctx->lsa.lsa_handle = net_ctx->lsa.pipe->binding_handle;
+       }
+
+       *_net_ctx = net_ctx;
+
+done:
+       if (!bret) {
+               /* a previous call has failed,
+                * clean up memory before exit */
+               talloc_free(net_ctx);
+       }
+       return bret;
+}
+
 
 void msg_handler(struct monitor_msg *m)
 {