1 /*
2    Unix SMB/CIFS implementation.
3 
4    Example echo torture tests
5 
6    Copyright (C) 2010 Kai Blin  <kai@samba.org>
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "includes.h"
23 #include "torture/smbtorture.h"
24 #include "libcli/resolve/resolve.h"
25 #include <tevent.h>
26 #include "libcli/util/ntstatus.h"
27 #include "libcli/echo/libecho.h"
28 
29 NTSTATUS torture_libcli_echo_init(TALLOC_CTX *);
30 
31 /* Basic test function that sends an echo request and checks the reply */
32 static bool echo_udp_basic(struct torture_context *tctx, const char *address)
33 {
34 	struct tevent_req *req;
35 	NTSTATUS status;
36 	const char *msg_send = "This is a test string\n";
37 	char *msg_recv;
38 
39 	req = echo_request_send(tctx, tctx->ev, address, msg_send);
40 	torture_assert(tctx, req != NULL,
41 		       "echo_request_send returned non-null tevent_req");
42 
43 	while(tevent_req_is_in_progress(req)) {
44 		tevent_loop_once(tctx->ev);
45 	}
46 
47 	status = echo_request_recv(req, tctx, &msg_recv);
48 	torture_assert_ntstatus_ok(tctx, status,
49 				   "echo_request_recv returned ok");
50 
51 	torture_assert_str_equal(tctx, msg_recv, msg_send,
52 				 "Echo server echoed request string");
53 
54 	return true;
55 }
56 
57 /*Test case to set up the environment and perform UDP-based echo tests */
58 static bool torture_echo_udp(struct torture_context *tctx)
59 {
60 	const char *address;
61 	struct nbt_name name;
62 	NTSTATUS status;
63 	bool ret = true;
64 
65 	make_nbt_name_server(&name,
66 			     torture_setting_string(tctx, "host", NULL));
67 	status = resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
68 				 0, 0,
69 				 &name, tctx,
70 				 &address, tctx->ev);
71 	if (!NT_STATUS_IS_OK(status)) {
72 		printf("Failed to resolve %s - %s\n", name.name,
73 		       nt_errstr(status));
74 		return false;
75 	}
76 
77 	/* All tests are now called here */
78 	ret &= echo_udp_basic(tctx, address);
79 
80 	return ret;
81 }
82 
83 /* Test suite that bundles all the libecho tests */
84 NTSTATUS torture_libcli_echo_init(TALLOC_CTX *ctx)
85 {
86 	struct torture_suite *suite;
87 
88 	suite = torture_suite_create(ctx, "echo");
89 	NT_STATUS_HAVE_NO_MEMORY(suite);
90 
91 	torture_suite_add_simple_test(suite, "udp", torture_echo_udp);
92 
93 	suite->description = talloc_strdup(suite, "libcli/echo interface tests");
94 	torture_register_suite(ctx, suite);
95 
96 	return NT_STATUS_OK;
97 }
98