77a7dfa677f545934e92deb860154939f1bde34c
[metze/samba/wip.git] / libcli / echo / tests / echo.c
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 /* Basic test function that sends an echo request and checks the reply */
30 static bool echo_udp_basic(struct torture_context *tctx, const char *address)
31 {
32         struct tevent_req *req;
33         NTSTATUS status;
34         const char *msg_send = "This is a test string\n";
35         char *msg_recv;
36
37         req = echo_request_send(tctx, tctx->ev, address, msg_send);
38         torture_assert(tctx, req != NULL,
39                        "echo_request_send returned non-null tevent_req");
40
41         while(tevent_req_is_in_progress(req)) {
42                 tevent_loop_once(tctx->ev);
43         }
44
45         status = echo_request_recv(req, tctx, &msg_recv);
46         torture_assert_ntstatus_ok(tctx, status,
47                                    "echo_request_recv returned ok");
48
49         torture_assert_str_equal(tctx, msg_recv, msg_send,
50                                  "Echo server echoed request string");
51
52         return true;
53 }
54
55 /*Test case to set up the environment and perform UDP-based echo tests */
56 static bool torture_echo_udp(struct torture_context *tctx)
57 {
58         const char *address;
59         struct nbt_name name;
60         NTSTATUS status;
61         bool ret = true;
62
63         make_nbt_name_server(&name,
64                              torture_setting_string(tctx, "host", NULL));
65         status = resolve_name(lpcfg_resolve_context(tctx->lp_ctx), &name, tctx,
66                               &address, tctx->ev);
67         if (!NT_STATUS_IS_OK(status)) {
68                 printf("Failed to resolve %s - %s\n", name.name,
69                        nt_errstr(status));
70                 return false;
71         }
72
73         /* All tests are now called here */
74         ret &= echo_udp_basic(tctx, address);
75
76         return ret;
77 }
78
79 /* Test suite that bundles all the libecho tests */
80 NTSTATUS torture_libcli_echo_init(void)
81 {
82         struct torture_suite *suite;
83
84         suite = torture_suite_create(talloc_autofree_context(), "echo");
85         NT_STATUS_HAVE_NO_MEMORY(suite);
86
87         torture_suite_add_simple_test(suite, "udp", torture_echo_udp);
88
89         suite->description = talloc_strdup(suite, "libcli/echo interface tests");
90         torture_register_suite(suite);
91
92         return NT_STATUS_OK;
93 }