Remove more event_context_init() uses from function calls within deep down the code.
[Samba/aatanasov.git] / source4 / torture / nbt / register.c
bloba8681f828f7373e10b20fd5ffb642247f8164a7a
1 /*
2 Unix SMB/CIFS implementation.
4 NBT name registration testing
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
22 #include "includes.h"
23 #include "lib/socket/socket.h"
24 #include "libcli/resolve/resolve.h"
25 #include "system/network.h"
26 #include "lib/socket/netif.h"
27 #include "torture/torture.h"
28 #include "torture/nbt/proto.h"
29 #include "param/param.h"
31 #define CHECK_VALUE(tctx, v, correct) \
32 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
34 #define CHECK_STRING(tctx, v, correct) \
35 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
41 test that a server responds correctly to attempted registrations of its name
43 static bool nbt_register_own(struct torture_context *tctx)
45 struct nbt_name_register io;
46 NTSTATUS status;
47 struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, tctx->ev,
48 lp_iconv_convenience(tctx->lp_ctx));
49 struct socket_address *socket_address;
50 struct nbt_name name;
51 const char *address;
52 const char *myaddress;
53 struct interface *ifaces;
55 if (!torture_nbt_get_name(tctx, &name, &address))
56 return false;
58 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
60 myaddress = iface_best_ip(ifaces, address);
62 socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
63 myaddress, 0);
64 torture_assert(tctx, socket_address != NULL, "Unable to get address");
66 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
67 torture_assert_ntstatus_ok(tctx, status,
68 "socket_listen for nbt_register_own failed");
70 torture_comment(tctx, "Testing name defense to name registration\n");
72 io.in.name = name;
73 io.in.dest_addr = address;
74 io.in.dest_port = lp_nbt_port(tctx->lp_ctx);
75 io.in.address = myaddress;
76 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
77 io.in.register_demand = false;
78 io.in.broadcast = true;
79 io.in.multi_homed = false;
80 io.in.ttl = 1234;
81 io.in.timeout = 3;
82 io.in.retries = 0;
84 status = nbt_name_register(nbtsock, tctx, &io);
85 torture_assert_ntstatus_ok(tctx, status,
86 talloc_asprintf(tctx, "Bad response from %s for name register",
87 address));
89 CHECK_STRING(tctx, io.out.name.name, name.name);
90 CHECK_VALUE(tctx, io.out.name.type, name.type);
91 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
93 /* check a register demand */
94 io.in.address = myaddress;
95 io.in.register_demand = true;
97 status = nbt_name_register(nbtsock, tctx, &io);
99 torture_assert_ntstatus_ok(tctx, status,
100 talloc_asprintf(tctx, "Bad response from %s for name register demand", address));
102 CHECK_STRING(tctx, io.out.name.name, name.name);
103 CHECK_VALUE(tctx, io.out.name.type, name.type);
104 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
106 return true;
111 test that a server responds correctly to attempted name refresh requests
113 static bool nbt_refresh_own(struct torture_context *tctx)
115 struct nbt_name_refresh io;
116 NTSTATUS status;
117 struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, tctx->ev,
118 lp_iconv_convenience(tctx->lp_ctx));
119 const char *myaddress;
120 struct socket_address *socket_address;
121 struct nbt_name name;
122 const char *address;
123 struct interface *ifaces;
125 if (!torture_nbt_get_name(tctx, &name, &address))
126 return false;
128 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
130 myaddress = iface_best_ip(ifaces, address);
132 socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
133 myaddress, 0);
134 torture_assert(tctx, socket_address != NULL,
135 "Can't parse socket address");
137 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
138 torture_assert_ntstatus_ok(tctx, status,
139 "socket_listen for nbt_referesh_own failed");
141 torture_comment(tctx, "Testing name defense to name refresh\n");
143 io.in.name = name;
144 io.in.dest_addr = address;
145 io.in.dest_port = lp_nbt_port(tctx->lp_ctx);
146 io.in.address = myaddress;
147 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
148 io.in.broadcast = false;
149 io.in.ttl = 1234;
150 io.in.timeout = 3;
151 io.in.retries = 0;
153 status = nbt_name_refresh(nbtsock, tctx, &io);
155 torture_assert_ntstatus_ok(tctx, status,
156 talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
158 CHECK_STRING(tctx, io.out.name.name, name.name);
159 CHECK_VALUE(tctx, io.out.name.type, name.type);
160 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
162 return true;
167 test name registration to a server
169 struct torture_suite *torture_nbt_register(TALLOC_CTX *mem_ctx)
171 struct torture_suite *suite;
173 suite = torture_suite_create(mem_ctx, "REGISTER");
174 torture_suite_add_simple_test(suite, "register_own", nbt_register_own);
175 torture_suite_add_simple_test(suite, "refresh_own", nbt_refresh_own);
177 return suite;