s3-rpc_server: run minimal_includes.pl.
[samba.git] / source3 / torture / test_async_echo.c
1 /*
2    Unix SMB/CIFS implementation.
3    Run the async echo responder
4    Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "rpc_client/cli_pipe.h"
23 #include "librpc/gen_ndr/ndr_echo_c.h"
24
25 static void rpccli_sleep_done(struct tevent_req *req)
26 {
27         int *done = (int *)tevent_req_callback_data_void(req);
28         NTSTATUS status;
29         uint32_t result = UINT32_MAX;
30
31         status = dcerpc_echo_TestSleep_recv(req, talloc_tos(), &result);
32         TALLOC_FREE(req);
33         printf("sleep returned %s, %d\n", nt_errstr(status), (int)result);
34         *done -= 1;
35 }
36
37 static void cli_echo_done(struct tevent_req *req)
38 {
39         int *done = (int *)tevent_req_callback_data_void(req);
40         NTSTATUS status;
41
42         status = cli_echo_recv(req);
43         TALLOC_FREE(req);
44         printf("echo returned %s\n", nt_errstr(status));
45         *done -= 1;
46 }
47
48 static void cli_close_done(struct tevent_req *req)
49 {
50         int *done = (int *)tevent_req_callback_data_void(req);
51         NTSTATUS status;
52         size_t written;
53
54         status = cli_write_andx_recv(req, &written);
55         TALLOC_FREE(req);
56         printf("close returned %s\n", nt_errstr(status));
57         *done -= 1;
58 }
59
60 bool run_async_echo(int dummy)
61 {
62         struct cli_state *cli = NULL;
63         struct rpc_pipe_client *p;
64         struct dcerpc_binding_handle *b;
65         struct tevent_context *ev;
66         struct tevent_req *req;
67         NTSTATUS status;
68         bool ret = false;
69         int i, num_reqs;
70         uint8_t buf[32768];
71
72         printf("Starting ASYNC_ECHO\n");
73
74         ev = tevent_context_init(talloc_tos());
75         if (ev == NULL) {
76                 printf("tevent_context_init failed\n");
77                 goto fail;
78         }
79
80         if (!torture_open_connection(&cli, 0)) {
81                 printf("torture_open_connection failed\n");
82                 goto fail;
83         }
84         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_rpcecho.syntax_id,
85                                           &p);
86         if (!NT_STATUS_IS_OK(status)) {
87                 printf("Could not open echo pipe: %s\n", nt_errstr(status));
88                 goto fail;
89         }
90         b = p->binding_handle;
91
92         num_reqs = 0;
93
94         req = dcerpc_echo_TestSleep_send(ev, ev, b, 15);
95         if (req == NULL) {
96                 printf("rpccli_echo_TestSleep_send failed\n");
97                 goto fail;
98         }
99         tevent_req_set_callback(req, rpccli_sleep_done, &num_reqs);
100         num_reqs += 1;
101
102         req = cli_echo_send(ev, ev, cli, 1, data_blob_const("hello", 5));
103         if (req == NULL) {
104                 printf("cli_echo_send failed\n");
105                 goto fail;
106         }
107         tevent_req_set_callback(req, cli_echo_done, &num_reqs);
108         num_reqs += 1;
109
110         memset(buf, 0, sizeof(buf));
111
112         for (i=0; i<10; i++) {
113                 req = cli_write_andx_send(ev, ev, cli, 4711, 0, buf, 0, sizeof(buf));
114                 if (req == NULL) {
115                         printf("cli_close_send failed\n");
116                         goto fail;
117                 }
118                 tevent_req_set_callback(req, cli_close_done, &num_reqs);
119                 num_reqs += 1;
120
121                 req = cli_echo_send(ev, ev, cli, 1, data_blob_const("hello", 5));
122                 if (req == NULL) {
123                         printf("cli_echo_send failed\n");
124                         goto fail;
125                 }
126                 tevent_req_set_callback(req, cli_echo_done, &num_reqs);
127                 num_reqs += 1;
128         }
129
130         while (num_reqs > 0) {
131                 if (tevent_loop_once(ev) != 0) {
132                         printf("tevent_loop_once failed\n");
133                         goto fail;
134                 }
135         }
136
137         TALLOC_FREE(p);
138
139         ret = true;
140 fail:
141         if (cli != NULL) {
142                 torture_close_connection(cli);
143         }
144         return ret;
145 }