selftest split $PERL into multiple arguments for Test::More check
[Samba.git] / source3 / torture / test_async_echo.c
blobdec5e748feeea518651f1263eb1beaef4bad1d9f
1 /*
2 Unix SMB/CIFS implementation.
3 Run the async echo responder
4 Copyright (C) Volker Lendecke 2010
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.
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.
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/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "librpc/gen_ndr/ndr_echo_c.h"
24 static void rpccli_sleep_done(struct tevent_req *req)
26 int *done = (int *)tevent_req_callback_data_void(req);
27 NTSTATUS status;
28 uint32_t result = UINT32_MAX;
30 status = dcerpc_echo_TestSleep_recv(req, talloc_tos(), &result);
31 TALLOC_FREE(req);
32 printf("sleep returned %s, %d\n", nt_errstr(status), (int)result);
33 *done -= 1;
36 static void cli_echo_done(struct tevent_req *req)
38 int *done = (int *)tevent_req_callback_data_void(req);
39 NTSTATUS status;
41 status = cli_echo_recv(req);
42 TALLOC_FREE(req);
43 printf("echo returned %s\n", nt_errstr(status));
44 *done -= 1;
47 static void cli_close_done(struct tevent_req *req)
49 int *done = (int *)tevent_req_callback_data_void(req);
50 NTSTATUS status;
51 size_t written;
53 status = cli_write_andx_recv(req, &written);
54 TALLOC_FREE(req);
55 printf("close returned %s\n", nt_errstr(status));
56 *done -= 1;
59 bool run_async_echo(int dummy)
61 struct cli_state *cli = NULL;
62 struct rpc_pipe_client *p;
63 struct dcerpc_binding_handle *b;
64 struct tevent_context *ev;
65 struct tevent_req *req;
66 NTSTATUS status;
67 bool ret = false;
68 int i, num_reqs;
69 uint8_t buf[32768];
71 printf("Starting ASYNC_ECHO\n");
73 ev = tevent_context_init(talloc_tos());
74 if (ev == NULL) {
75 printf("tevent_context_init failed\n");
76 goto fail;
79 if (!torture_open_connection(&cli, 0)) {
80 printf("torture_open_connection failed\n");
81 goto fail;
83 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_rpcecho.syntax_id,
84 &p);
85 if (!NT_STATUS_IS_OK(status)) {
86 printf("Could not open echo pipe: %s\n", nt_errstr(status));
87 goto fail;
89 b = p->binding_handle;
91 num_reqs = 0;
93 req = dcerpc_echo_TestSleep_send(ev, ev, b, 15);
94 if (req == NULL) {
95 printf("rpccli_echo_TestSleep_send failed\n");
96 goto fail;
98 tevent_req_set_callback(req, rpccli_sleep_done, &num_reqs);
99 num_reqs += 1;
101 req = cli_echo_send(ev, ev, cli, 1, data_blob_const("hello", 5));
102 if (req == NULL) {
103 printf("cli_echo_send failed\n");
104 goto fail;
106 tevent_req_set_callback(req, cli_echo_done, &num_reqs);
107 num_reqs += 1;
109 memset(buf, 0, sizeof(buf));
111 for (i=0; i<10; i++) {
112 req = cli_write_andx_send(ev, ev, cli, 4711, 0, buf, 0, sizeof(buf));
113 if (req == NULL) {
114 printf("cli_close_send failed\n");
115 goto fail;
117 tevent_req_set_callback(req, cli_close_done, &num_reqs);
118 num_reqs += 1;
120 req = cli_echo_send(ev, ev, cli, 1, data_blob_const("hello", 5));
121 if (req == NULL) {
122 printf("cli_echo_send failed\n");
123 goto fail;
125 tevent_req_set_callback(req, cli_echo_done, &num_reqs);
126 num_reqs += 1;
129 while (num_reqs > 0) {
130 if (tevent_loop_once(ev) != 0) {
131 printf("tevent_loop_once failed\n");
132 goto fail;
136 TALLOC_FREE(p);
138 ret = true;
139 fail:
140 if (cli != NULL) {
141 torture_close_connection(cli);
143 return ret;