s3:torture:delete: add a comment
[kai/samba.git] / source3 / torture / test_msg.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test msg_stream API
4    Copyright (C) Volker Lendecke 2012
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 "lib/util/tevent_unix.h"
23 #include "msg_channel.h"
24
25 struct msg_test_state {
26         struct tevent_context *ev;
27         struct messaging_context *msg;
28         struct msg_channel *channel;
29 };
30
31 static void msg_test_got_channel(struct tevent_req *subreq);
32 static void msg_test_got_msg(struct tevent_req *subreq);
33
34 static struct tevent_req *msg_test_send(TALLOC_CTX *mem_ctx,
35                                         struct tevent_context *ev)
36 {
37         struct tevent_req *req, *subreq;
38         struct msg_test_state *state;
39
40         req = tevent_req_create(mem_ctx, &state, struct msg_test_state);
41         if (req == NULL) {
42                 return NULL;
43         }
44         state->ev = ev;
45
46         state->msg = messaging_init(state, state->ev);
47         if (tevent_req_nomem(state->msg, req)) {
48                 return tevent_req_post(req, ev);
49         }
50         subreq = msg_channel_init_send(state, state->ev, state->msg, MSG_PING);
51         if (tevent_req_nomem(subreq, req)) {
52                 return tevent_req_post(req, ev);
53         }
54         tevent_req_set_callback(subreq, msg_test_got_channel, req);
55         return req;
56 }
57
58 static void msg_test_got_channel(struct tevent_req *subreq)
59 {
60         struct tevent_req *req = tevent_req_callback_data(
61                 subreq, struct tevent_req);
62         struct msg_test_state *state = tevent_req_data(
63                 req, struct msg_test_state);
64         int ret;
65
66         ret = msg_channel_init_recv(subreq, state, &state->channel);
67         TALLOC_FREE(subreq);
68         if (tevent_req_error(req, ret)) {
69                 return;
70         }
71         subreq = msg_read_send(state, state->ev, state->channel);
72         if (tevent_req_nomem(subreq, req)) {
73                 return;
74         }
75         tevent_req_set_callback(subreq, msg_test_got_msg, req);
76 }
77
78 static void msg_test_got_msg(struct tevent_req *subreq)
79 {
80         struct tevent_req *req = tevent_req_callback_data(
81                 subreq, struct tevent_req);
82         struct msg_test_state *state = tevent_req_data(
83                 req, struct msg_test_state);
84         struct messaging_rec *msg;
85         int ret;
86
87         ret = msg_read_recv(subreq, state, &msg);
88         TALLOC_FREE(subreq);
89         if (tevent_req_error(req, ret)) {
90                 return;
91         }
92         tevent_req_done(req);
93 }
94
95 static int msg_test_recv(struct tevent_req *req)
96 {
97         int err;
98
99         if (tevent_req_is_unix_error(req, &err)) {
100                 return err;
101         }
102         return 0;
103 }
104
105 bool run_msg_test(int dummy)
106 {
107         struct tevent_context *ev;
108         struct tevent_req *req;
109         int ret;
110
111         ev = tevent_context_init(talloc_tos());
112         if (ev == NULL) {
113                 fprintf(stderr, "tevent_context_init failed\n");
114                 return false;
115         }
116         req = msg_test_send(ev, ev);
117         if (req == NULL) {
118                 fprintf(stderr, "msg_test_send failed\n");
119                 return false;
120         }
121         if (!tevent_req_poll(req, ev)) {
122                 fprintf(stderr, "tevent_req_poll failed\n");
123                 return false;
124         }
125         ret = msg_test_recv(req);
126         TALLOC_FREE(req);
127         printf("msg_test_recv returned %s\n",
128                ret ? strerror(ret) : "success");
129         TALLOC_FREE(ev);
130         return (ret == 0);
131 }