param: add extra default parameter to get_parametrics_by_service
[Samba.git] / source3 / torture / test_messaging_read.c
blob387ebfde8784cec5d83a8fb2d4806303108623fb
1 /*
2 Unix SMB/CIFS implementation.
3 Test for a messaging_read bug
4 Copyright (C) Volker Lendecke 2014
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 "messages.h"
25 struct msg_count_state {
26 struct tevent_context *ev;
27 struct messaging_context *msg_ctx;
28 uint32_t msg_type;
29 unsigned *count;
32 static void msg_count_done(struct tevent_req *subreq);
34 static struct tevent_req *msg_count_send(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev,
36 struct messaging_context *msg_ctx,
37 uint32_t msg_type,
38 unsigned *count)
40 struct tevent_req *req, *subreq;
41 struct msg_count_state *state;
43 req = tevent_req_create(mem_ctx, &state, struct msg_count_state);
44 if (req == NULL) {
45 return NULL;
47 state->ev = ev;
48 state->msg_ctx = msg_ctx;
49 state->msg_type = msg_type;
50 state->count = count;
52 subreq = messaging_read_send(state, state->ev, state->msg_ctx,
53 state->msg_type);
54 if (tevent_req_nomem(subreq, req)) {
55 return tevent_req_post(req, ev);
57 tevent_req_set_callback(subreq, msg_count_done, req);
58 return req;
61 static void msg_count_done(struct tevent_req *subreq)
63 struct tevent_req *req = tevent_req_callback_data(
64 subreq, struct tevent_req);
65 struct msg_count_state *state = tevent_req_data(
66 req, struct msg_count_state);
67 int ret;
69 ret = messaging_read_recv(subreq, NULL, NULL);
70 TALLOC_FREE(subreq);
71 if (tevent_req_error(req, ret)) {
72 return;
74 *state->count += 1;
76 subreq = messaging_read_send(state, state->ev, state->msg_ctx,
77 state->msg_type);
78 if (tevent_req_nomem(subreq, req)) {
79 return;
81 tevent_req_set_callback(subreq, msg_count_done, req);
84 bool run_messaging_read1(int dummy)
86 struct tevent_context *ev = NULL;
87 struct messaging_context *msg_ctx = NULL;
88 struct tevent_req *req1 = NULL;
89 unsigned count1 = 0;
90 struct tevent_req *req2 = NULL;
91 unsigned count2 = 0;
92 NTSTATUS status;
93 bool retval = false;
95 ev = samba_tevent_context_init(talloc_tos());
96 if (ev == NULL) {
97 fprintf(stderr, "tevent_context_init failed\n");
98 goto fail;
100 msg_ctx = messaging_init(ev, ev);
101 if (msg_ctx == NULL) {
102 fprintf(stderr, "messaging_init failed\n");
103 goto fail;
106 req1 = msg_count_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY, &count1);
107 if (req1 == NULL) {
108 fprintf(stderr, "msg_count_send failed\n");
109 goto fail;
111 req2 = msg_count_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY, &count2);
112 if (req1 == NULL) {
113 fprintf(stderr, "msg_count_send failed\n");
114 goto fail;
116 status = messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
117 MSG_SMB_NOTIFY, NULL, 0);
118 if (!NT_STATUS_IS_OK(status)) {
119 fprintf(stderr, "messaging_send_buf failed: %s\n",
120 nt_errstr(status));
121 goto fail;
124 if (tevent_loop_once(ev) != 0) {
125 fprintf(stderr, "tevent_loop_once failed\n");
126 goto fail;
129 printf("%u/%u\n", count1, count2);
131 if ((count1 != 1) || (count2 != 1)){
132 fprintf(stderr, "Got %u/%u msgs, expected 1 each\n",
133 count1, count2);
134 goto fail;
137 retval = true;
138 fail:
139 TALLOC_FREE(req1);
140 TALLOC_FREE(req2);
141 TALLOC_FREE(msg_ctx);
142 TALLOC_FREE(ev);
143 return retval;
146 struct msg_free_state {
147 struct tevent_req **to_free;
150 static void msg_free_done(struct tevent_req *subreq);
152 static struct tevent_req *msg_free_send(TALLOC_CTX *mem_ctx,
153 struct tevent_context *ev,
154 struct messaging_context *msg_ctx,
155 uint32_t msg_type,
156 struct tevent_req **to_free)
158 struct tevent_req *req, *subreq;
159 struct msg_free_state *state;
161 req = tevent_req_create(mem_ctx, &state, struct msg_free_state);
162 if (req == NULL) {
163 return NULL;
165 state->to_free = to_free;
167 subreq = messaging_read_send(state, ev, msg_ctx, msg_type);
168 if (tevent_req_nomem(subreq, req)) {
169 return tevent_req_post(req, ev);
171 tevent_req_set_callback(subreq, msg_free_done, req);
172 return req;
175 static void msg_free_done(struct tevent_req *subreq)
177 struct tevent_req *req = tevent_req_callback_data(
178 subreq, struct tevent_req);
179 struct msg_free_state *state = tevent_req_data(
180 req, struct msg_free_state);
181 int ret;
183 ret = messaging_read_recv(subreq, NULL, NULL);
184 TALLOC_FREE(subreq);
185 if (tevent_req_error(req, ret)) {
186 return;
188 TALLOC_FREE(*state->to_free);
189 tevent_req_done(req);
192 bool run_messaging_read2(int dummy)
194 struct tevent_context *ev = NULL;
195 struct messaging_context *msg_ctx = NULL;
196 struct tevent_req *req1 = NULL;
197 struct tevent_req *req2 = NULL;
198 unsigned count = 0;
199 NTSTATUS status;
200 bool retval = false;
202 ev = samba_tevent_context_init(talloc_tos());
203 if (ev == NULL) {
204 fprintf(stderr, "tevent_context_init failed\n");
205 goto fail;
207 msg_ctx = messaging_init(ev, ev);
208 if (msg_ctx == NULL) {
209 fprintf(stderr, "messaging_init failed\n");
210 goto fail;
213 req1 = msg_free_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY, &req2);
214 if (req1 == NULL) {
215 fprintf(stderr, "msg_count_send failed\n");
216 goto fail;
218 req2 = msg_count_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY, &count);
219 if (req1 == NULL) {
220 fprintf(stderr, "msg_count_send failed\n");
221 goto fail;
223 status = messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
224 MSG_SMB_NOTIFY, NULL, 0);
225 if (!NT_STATUS_IS_OK(status)) {
226 fprintf(stderr, "messaging_send_buf failed: %s\n",
227 nt_errstr(status));
228 goto fail;
231 if (!tevent_req_poll(req1, ev) != 0) {
232 fprintf(stderr, "tevent_req_poll failed\n");
233 goto fail;
236 if (count != 0) {
237 fprintf(stderr, "Got %u msgs, expected none\n", count);
238 goto fail;
241 retval = true;
242 fail:
243 TALLOC_FREE(req1);
244 TALLOC_FREE(msg_ctx);
245 TALLOC_FREE(ev);
246 return retval;