s3-libsmb: Don't leak memory on error.
[Samba.git] / source3 / torture / test_msg.c
blob2171598ab8f92e0a89449d96af07a0b9662fbc24
1 /*
2 Unix SMB/CIFS implementation.
3 Test msg_stream API
4 Copyright (C) Volker Lendecke 2012
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 "lib/util/tevent_unix.h"
23 #include "msg_channel.h"
25 struct msg_test_state {
26 struct tevent_context *ev;
27 struct messaging_context *msg;
28 struct msg_channel *channel;
31 static void msg_test_got_channel(struct tevent_req *subreq);
32 static void msg_test_got_msg(struct tevent_req *subreq);
34 static struct tevent_req *msg_test_send(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev)
37 struct tevent_req *req, *subreq;
38 struct msg_test_state *state;
40 req = tevent_req_create(mem_ctx, &state, struct msg_test_state);
41 if (req == NULL) {
42 return NULL;
44 state->ev = ev;
46 state->msg = messaging_init(state, state->ev);
47 if (tevent_req_nomem(state->msg, req)) {
48 return tevent_req_post(req, ev);
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);
54 tevent_req_set_callback(subreq, msg_test_got_channel, req);
55 return req;
58 static void msg_test_got_channel(struct tevent_req *subreq)
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;
66 ret = msg_channel_init_recv(subreq, state, &state->channel);
67 TALLOC_FREE(subreq);
68 if (tevent_req_error(req, ret)) {
69 return;
71 subreq = msg_read_send(state, state->ev, state->channel);
72 if (tevent_req_nomem(subreq, req)) {
73 return;
75 tevent_req_set_callback(subreq, msg_test_got_msg, req);
78 static void msg_test_got_msg(struct tevent_req *subreq)
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;
87 ret = msg_read_recv(subreq, state, &msg);
88 TALLOC_FREE(subreq);
89 if (tevent_req_error(req, ret)) {
90 return;
92 tevent_req_done(req);
95 static int msg_test_recv(struct tevent_req *req)
97 int err;
99 if (tevent_req_is_unix_error(req, &err)) {
100 return err;
102 return 0;
105 bool run_msg_test(int dummy)
107 struct tevent_context *ev;
108 struct tevent_req *req;
109 int ret;
111 ev = samba_tevent_context_init(talloc_tos());
112 if (ev == NULL) {
113 fprintf(stderr, "tevent_context_init failed\n");
114 return false;
116 req = msg_test_send(ev, ev);
117 if (req == NULL) {
118 fprintf(stderr, "msg_test_send failed\n");
119 return false;
121 if (!tevent_req_poll(req, ev)) {
122 fprintf(stderr, "tevent_req_poll failed\n");
123 return false;
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);