libsmb: Convert cli_NetServerEnum() away from cli_api()
[samba4-gss.git] / source3 / torture / test_async_echo.c
blobf21daa4485cd18453a3bdb8b349f1b66c514e8b7
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 "libsmb/libsmb.h"
23 #include "rpc_client/cli_pipe.h"
24 #include "librpc/gen_ndr/ndr_echo_c.h"
26 static void rpccli_sleep_done(struct tevent_req *req)
28 int *done = (int *)tevent_req_callback_data_void(req);
29 NTSTATUS status;
30 uint32_t result = UINT32_MAX;
32 status = dcerpc_echo_TestSleep_recv(req, talloc_tos(), &result);
33 TALLOC_FREE(req);
34 printf("sleep returned %s, %d\n", nt_errstr(status), (int)result);
35 *done -= 1;
38 static void cli_echo_done(struct tevent_req *req)
40 int *done = (int *)tevent_req_callback_data_void(req);
41 NTSTATUS status;
43 status = cli_echo_recv(req);
44 TALLOC_FREE(req);
45 printf("echo returned %s\n", nt_errstr(status));
46 *done -= 1;
49 static void write_andx_done(struct tevent_req *req)
51 int *done = (int *)tevent_req_callback_data_void(req);
52 NTSTATUS status;
53 size_t written;
55 status = cli_write_andx_recv(req, &written);
56 TALLOC_FREE(req);
57 printf("cli_write_andx returned %s\n", nt_errstr(status));
58 *done -= 1;
61 bool run_async_echo(int dummy)
63 struct cli_state *cli = NULL;
64 struct rpc_pipe_client *p;
65 struct dcerpc_binding_handle *b;
66 struct tevent_context *ev;
67 struct tevent_req *req;
68 NTSTATUS status;
69 bool ret = false;
70 int i, num_reqs;
71 uint8_t buf[65536];
73 printf("Starting ASYNC_ECHO\n");
75 ev = samba_tevent_context_init(talloc_tos());
76 if (ev == NULL) {
77 printf("tevent_context_init failed\n");
78 goto fail;
81 if (!torture_open_connection(&cli, 0)) {
82 printf("torture_open_connection failed\n");
83 goto fail;
85 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_rpcecho,
86 &p);
87 if (!NT_STATUS_IS_OK(status)) {
88 printf("Could not open echo pipe: %s\n", nt_errstr(status));
89 goto fail;
91 b = p->binding_handle;
93 num_reqs = 0;
95 req = dcerpc_echo_TestSleep_send(ev, ev, b, 15);
96 if (req == NULL) {
97 printf("rpccli_echo_TestSleep_send failed\n");
98 goto fail;
100 tevent_req_set_callback(req, rpccli_sleep_done, &num_reqs);
101 num_reqs += 1;
103 req = cli_echo_send(ev, ev, cli, 1, data_blob_const("hello", 5));
104 if (req == NULL) {
105 printf("cli_echo_send failed\n");
106 goto fail;
108 tevent_req_set_callback(req, cli_echo_done, &num_reqs);
109 num_reqs += 1;
111 memset(buf, 0, sizeof(buf));
113 for (i=0; i<10; i++) {
114 req = cli_write_andx_send(ev, ev, cli, 4711, 0, buf, 0,
115 sizeof(buf));
116 if (req == NULL) {
117 printf("cli_write_andx_send failed\n");
118 goto fail;
120 tevent_req_set_callback(req, write_andx_done, &num_reqs);
121 num_reqs += 1;
123 req = cli_echo_send(ev, ev, cli, 1,
124 data_blob_const("hello", 5));
125 if (req == NULL) {
126 printf("cli_echo_send failed\n");
127 goto fail;
129 tevent_req_set_callback(req, cli_echo_done, &num_reqs);
130 num_reqs += 1;
133 while (num_reqs > 0) {
134 if (tevent_loop_once(ev) != 0) {
135 printf("tevent_loop_once failed\n");
136 goto fail;
140 TALLOC_FREE(p);
142 ret = true;
143 fail:
144 if (cli != NULL) {
145 torture_close_connection(cli);
147 return ret;