1 /*
2    Unix SMB/CIFS implementation.
3 
4    local test for messaging code
5 
6    Copyright (C) Andrew Tridgell 2004
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/messaging/irpc.h"
26 #include "torture/torture.h"
27 
28 
29 static uint32_t msg_pong;
30 
ping_message(struct messaging_context * msg,void * private,uint32_t msg_type,uint32_t src,DATA_BLOB * data)31 static void ping_message(struct messaging_context *msg, void *private,
32 			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
33 {
34 	NTSTATUS status;
35 	status = messaging_send(msg, src, msg_pong, data);
36 	if (!NT_STATUS_IS_OK(status)) {
37 		printf("pong failed - %s\n", nt_errstr(status));
38 	}
39 }
40 
pong_message(struct messaging_context * msg,void * private,uint32_t msg_type,uint32_t src,DATA_BLOB * data)41 static void pong_message(struct messaging_context *msg, void *private,
42 			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
43 {
44 	int *count = private;
45 	(*count)++;
46 }
47 
exit_message(struct messaging_context * msg,void * private,uint32_t msg_type,uint32_t src,DATA_BLOB * data)48 static void exit_message(struct messaging_context *msg, void *private,
49 			 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
50 {
51 	talloc_free(private);
52 	exit(0);
53 }
54 
55 /*
56   test ping speed
57 */
test_ping_speed(struct torture_context * tctx)58 static bool test_ping_speed(struct torture_context *tctx)
59 {
60 	struct event_context *ev;
61 	struct messaging_context *msg_client_ctx;
62 	struct messaging_context *msg_server_ctx;
63 	int ping_count = 0;
64 	int pong_count = 0;
65 	struct timeval tv;
66 	int timelimit = torture_setting_int(tctx, "timelimit", 10);
67 	uint32_t msg_ping, msg_exit;
68 	TALLOC_CTX *mem_ctx = tctx;
69 
70 	lp_set_cmdline("lock dir", "lockdir.tmp");
71 
72 	ev = event_context_init(mem_ctx);
73 
74 	msg_server_ctx = messaging_init(mem_ctx, 1, ev);
75 
76 	torture_assert(tctx, msg_server_ctx != NULL, "Failed to init ping messaging context");
77 
78 	messaging_register_tmp(msg_server_ctx, NULL, ping_message, &msg_ping);
79 	messaging_register_tmp(msg_server_ctx, mem_ctx, exit_message, &msg_exit);
80 
81 	msg_client_ctx = messaging_init(mem_ctx, 2, ev);
82 
83 	torture_assert(tctx, msg_client_ctx != NULL, "msg_client_ctx messaging_init() failed");
84 
85 	messaging_register_tmp(msg_client_ctx, &pong_count, pong_message, &msg_pong);
86 
87 	tv = timeval_current();
88 
89 	torture_comment(tctx, "Sending pings for %d seconds", timelimit);
90 	while (timeval_elapsed(&tv) < timelimit) {
91 		DATA_BLOB data;
92 		NTSTATUS status1, status2;
93 
94 		data.data = discard_const_p(uint8_t, "testing");
95 		data.length = strlen((const char *)data.data);
96 
97 		status1 = messaging_send(msg_client_ctx, 1, msg_ping, &data);
98 		status2 = messaging_send(msg_client_ctx, 1, msg_ping, NULL);
99 
100 		torture_assert_ntstatus_ok(tctx, status1, "msg1 failed");
101 		ping_count++;
102 
103 		torture_assert_ntstatus_ok(tctx, status2, "msg2 failed");
104 		ping_count++;
105 
106 		while (ping_count > pong_count + 20) {
107 			event_loop_once(ev);
108 		}
109 	}
110 
111 	torture_comment(tctx, "waiting for %d remaining replies (done %d)",
112 	       ping_count - pong_count, pong_count);
113 	while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
114 		event_loop_once(ev);
115 	}
116 
117 	torture_comment(tctx, "sending exit");
118 	messaging_send(msg_client_ctx, 1, msg_exit, NULL);
119 
120 	torture_assert_int_equal(tctx, ping_count, pong_count, "ping test failed");
121 
122 	torture_comment(tctx, "ping rate of %.0f messages/sec",
123 	       (ping_count+pong_count)/timeval_elapsed(&tv));
124 
125 	talloc_free(msg_client_ctx);
126 	talloc_free(msg_server_ctx);
127 
128 	talloc_free(ev);
129 	return true;
130 }
131 
torture_local_messaging(TALLOC_CTX * mem_ctx)132 struct torture_suite *torture_local_messaging(TALLOC_CTX *mem_ctx)
133 {
134 	struct torture_suite *s = torture_suite_create(mem_ctx, "MESSAGING");
135 	torture_suite_add_simple_test(s, "ping_speed", test_ping_speed);
136 	return s;
137 }
138