2 Unix SMB/CIFS implementation.
4 NBT WINS server 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/>.
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 "librpc/gen_ndr/ndr_nbt.h"
28 #include "torture/torture.h"
29 #include "torture/nbt/proto.h"
30 #include "param/param.h"
32 #define CHECK_VALUE(tctx, v, correct) \
33 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
35 #define CHECK_STRING(tctx, v, correct) \
36 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
38 #define CHECK_NAME(tctx, _name, correct) do { \
39 CHECK_STRING(tctx, (_name).name, (correct).name); \
40 CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
41 CHECK_STRING(tctx, (_name).scope, (correct).scope); \
46 test operations against a WINS server
48 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
49 struct nbt_name *name, uint16_t nb_flags)
51 struct nbt_name_register_wins io;
52 struct nbt_name_query query;
53 struct nbt_name_refresh_wins refresh;
54 struct nbt_name_release release;
56 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
57 const char *myaddress;
58 struct socket_address *socket_address;
59 struct interface *ifaces;
62 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
64 myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
66 socket_address = socket_address_from_strings(tctx,
67 nbtsock->sock->backend_name,
68 myaddress, lp_nbt_port(tctx->lp_ctx));
69 torture_assert(tctx, socket_address != NULL,
70 "Error getting address");
72 /* we do the listen here to ensure the WINS server receives the packets from
74 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
75 talloc_free(socket_address);
76 if (!NT_STATUS_IS_OK(status)) {
78 socket_address = socket_address_from_strings(tctx,
79 nbtsock->sock->backend_name,
81 torture_assert(tctx, socket_address != NULL,
82 "Error getting address");
84 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
85 talloc_free(socket_address);
86 torture_assert_ntstatus_ok(tctx, status,
87 "socket_listen for WINS failed");
90 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
91 nbt_name_string(tctx, name), myaddress, nb_flags);
93 torture_comment(tctx, "release the name\n");
94 release.in.name = *name;
95 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
96 release.in.dest_addr = address;
97 release.in.address = myaddress;
98 release.in.nb_flags = nb_flags;
99 release.in.broadcast = false;
100 release.in.timeout = 3;
101 release.in.retries = 0;
103 status = nbt_name_release(nbtsock, tctx, &release);
104 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
105 CHECK_VALUE(tctx, release.out.rcode, 0);
107 if (nb_flags & NBT_NM_GROUP) {
108 /* ignore this for group names */
109 } else if (!low_port) {
110 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
112 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
114 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
115 io.in.wins_servers = str_list_make(tctx, address, NULL);
116 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
117 io.in.nb_flags = nb_flags;
120 status = nbt_name_register_wins(nbtsock, tctx, &io);
121 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
122 torture_assert_ntstatus_ok(tctx, status,
123 talloc_asprintf(tctx, "No response from %s for name register\n",
126 torture_assert_ntstatus_ok(tctx, status,
127 talloc_asprintf(tctx, "Bad response from %s for name register\n",
130 CHECK_STRING(tctx, io.out.wins_server, address);
131 CHECK_VALUE(tctx, io.out.rcode, 0);
134 torture_comment(tctx, "register the name correct address\n");
136 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
137 io.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
138 io.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
139 io.in.nb_flags = nb_flags;
142 status = nbt_name_register_wins(nbtsock, tctx, &io);
143 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
145 CHECK_STRING(tctx, io.out.wins_server, address);
146 CHECK_VALUE(tctx, io.out.rcode, 0);
148 if (name->type != NBT_NAME_MASTER &&
149 name->type != NBT_NAME_LOGON &&
150 name->type != NBT_NAME_BROWSER &&
151 (nb_flags & NBT_NM_GROUP)) {
152 torture_comment(tctx, "Try to register as non-group\n");
153 io.in.nb_flags &= ~NBT_NM_GROUP;
154 status = nbt_name_register_wins(nbtsock, tctx, &io);
155 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
157 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
160 torture_comment(tctx, "query the name to make sure its there\n");
161 query.in.name = *name;
162 query.in.dest_addr = address;
163 query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
164 query.in.broadcast = false;
165 query.in.wins_lookup = true;
166 query.in.timeout = 3;
167 query.in.retries = 0;
169 status = nbt_name_query(nbtsock, tctx, &query);
170 if (name->type == NBT_NAME_MASTER) {
171 torture_assert_ntstatus_equal(
172 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
173 talloc_asprintf(tctx, "Bad response from %s for name query", address));
176 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
178 CHECK_NAME(tctx, query.out.name, *name);
179 CHECK_VALUE(tctx, query.out.num_addrs, 1);
180 if (name->type != NBT_NAME_LOGON &&
181 (nb_flags & NBT_NM_GROUP)) {
182 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
184 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
188 query.in.name.name = strupper_talloc(tctx, name->name);
189 if (query.in.name.name &&
190 strcmp(query.in.name.name, name->name) != 0) {
191 torture_comment(tctx, "check case sensitivity\n");
192 status = nbt_name_query(nbtsock, tctx, &query);
193 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
196 query.in.name = *name;
198 query.in.name.scope = strupper_talloc(tctx, name->scope);
200 if (query.in.name.scope &&
201 strcmp(query.in.name.scope, name->scope) != 0) {
202 torture_comment(tctx, "check case sensitivity on scope\n");
203 status = nbt_name_query(nbtsock, tctx, &query);
204 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
207 torture_comment(tctx, "refresh the name\n");
208 refresh.in.name = *name;
209 refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
210 refresh.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
211 refresh.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
212 refresh.in.nb_flags = nb_flags;
213 refresh.in.ttl = 12345;
215 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
216 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
217 torture_assert_ntstatus_ok(tctx, status,
218 talloc_asprintf(tctx, "No response from %s for name refresh",
221 torture_assert_ntstatus_ok(tctx, status,
222 talloc_asprintf(tctx, "Bad response from %s for name refresh",
225 CHECK_STRING(tctx, refresh.out.wins_server, address);
226 CHECK_VALUE(tctx, refresh.out.rcode, 0);
228 printf("release the name\n");
229 release.in.name = *name;
230 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
231 release.in.dest_addr = address;
232 release.in.address = myaddress;
233 release.in.nb_flags = nb_flags;
234 release.in.broadcast = false;
235 release.in.timeout = 3;
236 release.in.retries = 0;
238 status = nbt_name_release(nbtsock, tctx, &release);
239 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
240 torture_assert_ntstatus_ok(tctx, status,
241 talloc_asprintf(tctx, "No response from %s for name release",
244 torture_assert_ntstatus_ok(tctx, status,
245 talloc_asprintf(tctx, "Bad response from %s for name release",
248 CHECK_NAME(tctx, release.out.name, *name);
249 CHECK_VALUE(tctx, release.out.rcode, 0);
251 if (nb_flags & NBT_NM_GROUP) {
252 /* ignore this for group names */
253 } else if (!low_port) {
254 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
256 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
258 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
259 io.in.wins_servers = str_list_make(tctx, address, NULL);
260 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
261 io.in.nb_flags = nb_flags;
264 status = nbt_name_register_wins(nbtsock, tctx, &io);
265 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
266 torture_assert_ntstatus_ok(tctx, status,
267 talloc_asprintf(tctx, "No response from %s for name register\n",
270 torture_assert_ntstatus_ok(tctx, status,
271 talloc_asprintf(tctx, "Bad response from %s for name register\n",
274 CHECK_STRING(tctx, io.out.wins_server, address);
275 CHECK_VALUE(tctx, io.out.rcode, 0);
278 torture_comment(tctx, "refresh the name with the correct address\n");
279 refresh.in.name = *name;
280 refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
281 refresh.in.wins_servers = str_list_make(tctx, address, NULL);
282 refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
283 refresh.in.nb_flags = nb_flags;
284 refresh.in.ttl = 12345;
286 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
287 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
288 torture_assert_ntstatus_ok(tctx, status,
289 talloc_asprintf(tctx, "No response from %s for name refresh",
292 torture_assert_ntstatus_ok(tctx, status,
293 talloc_asprintf(tctx, "Bad response from %s for name refresh",
296 CHECK_STRING(tctx, refresh.out.wins_server, address);
297 CHECK_VALUE(tctx, refresh.out.rcode, 0);
299 torture_comment(tctx, "release the name\n");
300 release.in.name = *name;
301 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
302 release.in.dest_addr = address;
303 release.in.address = myaddress;
304 release.in.nb_flags = nb_flags;
305 release.in.broadcast = false;
306 release.in.timeout = 3;
307 release.in.retries = 0;
309 status = nbt_name_release(nbtsock, tctx, &release);
310 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
312 CHECK_NAME(tctx, release.out.name, *name);
313 CHECK_VALUE(tctx, release.out.rcode, 0);
315 torture_comment(tctx, "release again\n");
316 status = nbt_name_release(nbtsock, tctx, &release);
317 torture_assert_ntstatus_ok(tctx, status,
318 talloc_asprintf(tctx, "Bad response from %s for name query",
321 CHECK_NAME(tctx, release.out.name, *name);
322 CHECK_VALUE(tctx, release.out.rcode, 0);
325 torture_comment(tctx, "query the name to make sure its gone\n");
326 query.in.name = *name;
327 status = nbt_name_query(nbtsock, tctx, &query);
328 if (name->type != NBT_NAME_LOGON &&
329 (nb_flags & NBT_NM_GROUP)) {
330 torture_assert_ntstatus_ok(tctx, status,
331 "ERROR: Name query failed after group release");
333 torture_assert_ntstatus_equal(tctx, status,
334 NT_STATUS_OBJECT_NAME_NOT_FOUND,
335 "Incorrect response to name query");
344 test operations against a WINS server
346 static bool nbt_test_wins(struct torture_context *tctx)
348 struct nbt_name name;
349 uint32_t r = (uint32_t)(random() % (100000));
353 if (!torture_nbt_get_name(tctx, &name, &address))
356 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
358 name.type = NBT_NAME_CLIENT;
360 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
362 name.type = NBT_NAME_MASTER;
363 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
365 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
367 name.type = NBT_NAME_SERVER;
368 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
370 name.type = NBT_NAME_LOGON;
371 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
373 name.type = NBT_NAME_BROWSER;
374 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
376 name.type = NBT_NAME_PDC;
377 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
380 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
383 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
385 name.scope = "example";
387 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
389 name.scope = "example";
391 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
393 name.scope = "foo.example.com";
395 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
397 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
398 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
401 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
403 name.name = talloc_asprintf(tctx, ".");
404 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
406 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
407 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
415 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
417 struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
419 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);