param: change init_printer_values to take an lp ctx
[Samba.git] / source4 / torture / nbt / wins.c
blobd0ae9cd635e4173aa966c555884abe40b1b081a7
1 /*
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/>.
22 #include "includes.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/events/events.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "system/network.h"
28 #include "lib/socket/netif.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30 #include "torture/torture.h"
31 #include "torture/nbt/proto.h"
32 #include "param/param.h"
34 #define CHECK_VALUE(tctx, v, correct) \
35 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
37 #define CHECK_STRING(tctx, v, correct) \
38 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
40 #define CHECK_NAME(tctx, _name, correct) do { \
41 CHECK_STRING(tctx, (_name).name, (correct).name); \
42 CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
43 CHECK_STRING(tctx, (_name).scope, (correct).scope); \
44 } while (0)
48 test operations against a WINS server
50 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
51 struct nbt_name *name, uint16_t nb_flags,
52 bool try_low_port,
53 uint8_t register_rcode)
55 struct nbt_name_register_wins io;
56 struct nbt_name_register name_register;
57 struct nbt_name_query query;
58 struct nbt_name_refresh_wins refresh;
59 struct nbt_name_release release;
60 struct nbt_name_request *req;
61 NTSTATUS status;
62 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
63 const char *myaddress;
64 struct socket_address *socket_address;
65 struct interface *ifaces;
66 bool low_port = try_low_port;
67 char **l;
69 load_interface_list(tctx, tctx->lp_ctx, &ifaces);
71 myaddress = talloc_strdup(tctx, iface_list_best_ip(ifaces, address));
73 socket_address = socket_address_from_strings(tctx,
74 nbtsock->sock->backend_name,
75 myaddress, lpcfg_nbt_port(tctx->lp_ctx));
76 torture_assert(tctx, socket_address != NULL,
77 "Error getting address");
79 /* we do the listen here to ensure the WINS server receives the packets from
80 the right IP */
81 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
82 talloc_free(socket_address);
83 if (!NT_STATUS_IS_OK(status)) {
84 low_port = false;
85 socket_address = socket_address_from_strings(tctx,
86 nbtsock->sock->backend_name,
87 myaddress, 0);
88 torture_assert(tctx, socket_address != NULL,
89 "Error getting address");
91 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
92 talloc_free(socket_address);
93 torture_assert_ntstatus_ok(tctx, status,
94 "socket_listen for WINS failed");
97 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
98 nbt_name_string(tctx, name), myaddress, nb_flags);
100 torture_comment(tctx, "release the name\n");
101 release.in.name = *name;
102 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
103 release.in.dest_addr = address;
104 release.in.address = myaddress;
105 release.in.nb_flags = nb_flags;
106 release.in.broadcast = false;
107 release.in.timeout = 3;
108 release.in.retries = 0;
110 status = nbt_name_release(nbtsock, tctx, &release);
111 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
112 CHECK_VALUE(tctx, release.out.rcode, 0);
114 if (nb_flags & NBT_NM_GROUP) {
115 /* ignore this for group names */
116 } else if (!low_port) {
117 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
118 } else {
119 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
120 io.in.name = *name;
121 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
122 io.in.wins_servers = const_str_list(
123 str_list_make_single(tctx, address));
124 io.in.addresses = const_str_list(
125 str_list_make_single(tctx, "127.64.64.1"));
126 io.in.nb_flags = nb_flags;
127 io.in.ttl = 300000;
129 status = nbt_name_register_wins(nbtsock, tctx, &io);
130 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
131 torture_assert_ntstatus_ok(tctx, status,
132 talloc_asprintf(tctx, "No response from %s for name register\n",
133 address));
135 torture_assert_ntstatus_ok(tctx, status,
136 talloc_asprintf(tctx, "Bad response from %s for name register\n",
137 address));
139 CHECK_STRING(tctx, io.out.wins_server, address);
140 CHECK_VALUE(tctx, io.out.rcode, 0);
142 torture_comment(tctx, "register the name correct address\n");
143 name_register.in.name = *name;
144 name_register.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
145 name_register.in.dest_addr = address;
146 name_register.in.address = myaddress;
147 name_register.in.nb_flags = nb_flags;
148 name_register.in.register_demand= false;
149 name_register.in.broadcast = false;
150 name_register.in.multi_homed = true;
151 name_register.in.ttl = 300000;
152 name_register.in.timeout = 3;
153 name_register.in.retries = 2;
156 * test if the server ignores resent requests
158 req = nbt_name_register_send(nbtsock, &name_register);
159 while (true) {
160 tevent_loop_once(nbtsock->event_ctx);
161 if (req->state != NBT_REQUEST_WAIT) {
162 break;
164 if (req->received_wack) {
166 * if we received the wack response
167 * we resend the request and the
168 * server should ignore that
169 * and not handle it as new request
171 req->state = NBT_REQUEST_SEND;
172 DLIST_ADD_END(nbtsock->send_queue, req,
173 struct nbt_name_request *);
174 TEVENT_FD_WRITEABLE(nbtsock->fde);
175 break;
179 status = nbt_name_register_recv(req, tctx, &name_register);
180 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
181 torture_assert_ntstatus_ok(tctx, status,
182 talloc_asprintf(tctx, "No response from %s for name register\n",
183 address));
185 torture_assert_ntstatus_ok(tctx, status,
186 talloc_asprintf(tctx, "Bad response from %s for name register\n",
187 address));
189 CHECK_VALUE(tctx, name_register.out.rcode, 0);
190 CHECK_STRING(tctx, name_register.out.reply_addr, myaddress);
193 torture_comment(tctx, "register the name correct address\n");
194 io.in.name = *name;
195 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
196 l = str_list_make_single(tctx, address);
197 io.in.wins_servers = discard_const_p(const char *, l);
198 l = str_list_make_single(tctx, myaddress);
199 io.in.addresses = discard_const_p(const char *, l);
200 io.in.nb_flags = nb_flags;
201 io.in.ttl = 300000;
203 status = nbt_name_register_wins(nbtsock, tctx, &io);
204 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
206 CHECK_STRING(tctx, io.out.wins_server, address);
207 CHECK_VALUE(tctx, io.out.rcode, register_rcode);
209 if (register_rcode != NBT_RCODE_OK) {
210 return true;
213 if (name->type != NBT_NAME_MASTER &&
214 name->type != NBT_NAME_LOGON &&
215 name->type != NBT_NAME_BROWSER &&
216 (nb_flags & NBT_NM_GROUP)) {
217 torture_comment(tctx, "Try to register as non-group\n");
218 io.in.nb_flags &= ~NBT_NM_GROUP;
219 status = nbt_name_register_wins(nbtsock, tctx, &io);
220 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
221 address));
222 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
225 torture_comment(tctx, "query the name to make sure its there\n");
226 query.in.name = *name;
227 query.in.dest_addr = address;
228 query.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
229 query.in.broadcast = false;
230 query.in.wins_lookup = true;
231 query.in.timeout = 3;
232 query.in.retries = 0;
234 status = nbt_name_query(nbtsock, tctx, &query);
235 if (name->type == NBT_NAME_MASTER) {
236 torture_assert_ntstatus_equal(
237 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
238 talloc_asprintf(tctx, "Bad response from %s for name query", address));
239 return true;
241 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
243 CHECK_NAME(tctx, query.out.name, *name);
244 CHECK_VALUE(tctx, query.out.num_addrs, 1);
245 if (name->type != NBT_NAME_LOGON &&
246 (nb_flags & NBT_NM_GROUP)) {
247 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
248 } else {
249 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
253 query.in.name.name = strupper_talloc(tctx, name->name);
254 if (query.in.name.name &&
255 strcmp(query.in.name.name, name->name) != 0) {
256 torture_comment(tctx, "check case sensitivity\n");
257 status = nbt_name_query(nbtsock, tctx, &query);
258 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
261 query.in.name = *name;
262 if (name->scope) {
263 query.in.name.scope = strupper_talloc(tctx, name->scope);
265 if (query.in.name.scope &&
266 strcmp(query.in.name.scope, name->scope) != 0) {
267 torture_comment(tctx, "check case sensitivity on scope\n");
268 status = nbt_name_query(nbtsock, tctx, &query);
269 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
272 torture_comment(tctx, "refresh the name\n");
273 refresh.in.name = *name;
274 refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
275 l = str_list_make_single(tctx, address);
276 refresh.in.wins_servers = discard_const_p(const char *, l);
277 l = str_list_make_single(tctx, myaddress);
278 refresh.in.addresses = discard_const_p(const char *, l);
279 refresh.in.nb_flags = nb_flags;
280 refresh.in.ttl = 12345;
282 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
283 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
284 torture_assert_ntstatus_ok(tctx, status,
285 talloc_asprintf(tctx, "No response from %s for name refresh",
286 address));
288 torture_assert_ntstatus_ok(tctx, status,
289 talloc_asprintf(tctx, "Bad response from %s for name refresh",
290 address));
292 CHECK_STRING(tctx, refresh.out.wins_server, address);
293 CHECK_VALUE(tctx, refresh.out.rcode, 0);
295 printf("release the name\n");
296 release.in.name = *name;
297 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
298 release.in.dest_addr = address;
299 release.in.address = myaddress;
300 release.in.nb_flags = nb_flags;
301 release.in.broadcast = false;
302 release.in.timeout = 3;
303 release.in.retries = 0;
305 status = nbt_name_release(nbtsock, tctx, &release);
306 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
307 torture_assert_ntstatus_ok(tctx, status,
308 talloc_asprintf(tctx, "No response from %s for name release",
309 address));
311 torture_assert_ntstatus_ok(tctx, status,
312 talloc_asprintf(tctx, "Bad response from %s for name release",
313 address));
315 CHECK_NAME(tctx, release.out.name, *name);
316 CHECK_VALUE(tctx, release.out.rcode, 0);
318 if (nb_flags & NBT_NM_GROUP) {
319 /* ignore this for group names */
320 } else if (!low_port) {
321 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
322 } else {
323 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
324 io.in.name = *name;
325 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
326 io.in.wins_servers = const_str_list(
327 str_list_make_single(tctx, address));
328 io.in.addresses = const_str_list(
329 str_list_make_single(tctx, "127.64.64.1"));
330 io.in.nb_flags = nb_flags;
331 io.in.ttl = 300000;
333 status = nbt_name_register_wins(nbtsock, tctx, &io);
334 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
335 torture_assert_ntstatus_ok(tctx, status,
336 talloc_asprintf(tctx, "No response from %s for name register\n",
337 address));
339 torture_assert_ntstatus_ok(tctx, status,
340 talloc_asprintf(tctx, "Bad response from %s for name register\n",
341 address));
343 CHECK_STRING(tctx, io.out.wins_server, address);
344 CHECK_VALUE(tctx, io.out.rcode, 0);
347 torture_comment(tctx, "refresh the name with the correct address\n");
348 refresh.in.name = *name;
349 refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
350 refresh.in.wins_servers = const_str_list(
351 str_list_make_single(tctx, address));
352 refresh.in.addresses = const_str_list(
353 str_list_make_single(tctx, myaddress));
354 refresh.in.nb_flags = nb_flags;
355 refresh.in.ttl = 12345;
357 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
358 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
359 torture_assert_ntstatus_ok(tctx, status,
360 talloc_asprintf(tctx, "No response from %s for name refresh",
361 address));
363 torture_assert_ntstatus_ok(tctx, status,
364 talloc_asprintf(tctx, "Bad response from %s for name refresh",
365 address));
367 CHECK_STRING(tctx, refresh.out.wins_server, address);
368 CHECK_VALUE(tctx, refresh.out.rcode, 0);
370 torture_comment(tctx, "release the name\n");
371 release.in.name = *name;
372 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
373 release.in.dest_addr = address;
374 release.in.address = myaddress;
375 release.in.nb_flags = nb_flags;
376 release.in.broadcast = false;
377 release.in.timeout = 3;
378 release.in.retries = 0;
380 status = nbt_name_release(nbtsock, tctx, &release);
381 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
383 CHECK_NAME(tctx, release.out.name, *name);
384 CHECK_VALUE(tctx, release.out.rcode, 0);
386 torture_comment(tctx, "release again\n");
387 status = nbt_name_release(nbtsock, tctx, &release);
388 torture_assert_ntstatus_ok(tctx, status,
389 talloc_asprintf(tctx, "Bad response from %s for name query",
390 address));
392 CHECK_NAME(tctx, release.out.name, *name);
393 CHECK_VALUE(tctx, release.out.rcode, 0);
396 torture_comment(tctx, "query the name to make sure its gone\n");
397 query.in.name = *name;
398 status = nbt_name_query(nbtsock, tctx, &query);
399 if (name->type != NBT_NAME_LOGON &&
400 (nb_flags & NBT_NM_GROUP)) {
401 torture_assert_ntstatus_ok(tctx, status,
402 "ERROR: Name query failed after group release");
403 } else {
404 torture_assert_ntstatus_equal(tctx, status,
405 NT_STATUS_OBJECT_NAME_NOT_FOUND,
406 "Incorrect response to name query");
409 return true;
413 static char *test_nbt_wins_scope_string(TALLOC_CTX *mem_ctx, uint8_t count)
415 char *res;
416 uint8_t i;
418 res = talloc_array(mem_ctx, char, count+1);
419 if (res == NULL) {
420 return NULL;
423 for (i=0; i < count; i++) {
424 switch (i) {
425 case 63:
426 case 63 + 1 + 63:
427 case 63 + 1 + 63 + 1 + 63:
428 res[i] = '.';
429 break;
430 default:
431 res[i] = '0' + (i%10);
432 break;
436 res[count] = '\0';
438 talloc_set_name_const(res, res);
440 return res;
444 test operations against a WINS server
446 static bool nbt_test_wins(struct torture_context *tctx)
448 struct nbt_name name;
449 uint32_t r = (uint32_t)(random() % (100000));
450 const char *address;
451 bool ret = true;
453 if (!torture_nbt_get_name(tctx, &name, &address))
454 return false;
456 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
458 name.type = NBT_NAME_CLIENT;
459 name.scope = NULL;
460 ret &= nbt_test_wins_name(tctx, address, &name,
461 NBT_NODE_H, true, NBT_RCODE_OK);
463 name.type = NBT_NAME_MASTER;
464 ret &= nbt_test_wins_name(tctx, address, &name,
465 NBT_NODE_H, false, NBT_RCODE_OK);
467 ret &= nbt_test_wins_name(tctx, address, &name,
468 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
470 name.type = NBT_NAME_SERVER;
471 ret &= nbt_test_wins_name(tctx, address, &name,
472 NBT_NODE_H, true, NBT_RCODE_OK);
474 name.type = NBT_NAME_LOGON;
475 ret &= nbt_test_wins_name(tctx, address, &name,
476 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
478 name.type = NBT_NAME_BROWSER;
479 ret &= nbt_test_wins_name(tctx, address, &name,
480 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
482 name.type = NBT_NAME_PDC;
483 ret &= nbt_test_wins_name(tctx, address, &name,
484 NBT_NODE_H, true, NBT_RCODE_OK);
486 name.type = 0xBF;
487 ret &= nbt_test_wins_name(tctx, address, &name,
488 NBT_NODE_H, true, NBT_RCODE_OK);
490 name.type = 0xBE;
491 ret &= nbt_test_wins_name(tctx, address, &name,
492 NBT_NODE_H, false, NBT_RCODE_OK);
494 name.scope = "example";
495 name.type = 0x72;
496 ret &= nbt_test_wins_name(tctx, address, &name,
497 NBT_NODE_H, true, NBT_RCODE_OK);
499 name.scope = "example";
500 name.type = 0x71;
501 ret &= nbt_test_wins_name(tctx, address, &name,
502 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
504 name.scope = "foo.example.com";
505 name.type = 0x72;
506 ret &= nbt_test_wins_name(tctx, address, &name,
507 NBT_NODE_H, false, NBT_RCODE_OK);
509 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
510 ret &= nbt_test_wins_name(tctx, address, &name,
511 NBT_NODE_H, false, NBT_RCODE_OK);
513 name.name = "";
514 ret &= nbt_test_wins_name(tctx, address, &name,
515 NBT_NODE_H, false, NBT_RCODE_OK);
517 name.name = talloc_asprintf(tctx, ".");
518 ret &= nbt_test_wins_name(tctx, address, &name,
519 NBT_NODE_H, false, NBT_RCODE_OK);
521 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
522 ret &= nbt_test_wins_name(tctx, address, &name,
523 NBT_NODE_H, false, NBT_RCODE_OK);
525 name.scope = test_nbt_wins_scope_string(tctx, 237);
526 ret &= nbt_test_wins_name(tctx, address, &name,
527 NBT_NODE_H, false, NBT_RCODE_OK);
529 name.scope = test_nbt_wins_scope_string(tctx, 238);
530 ret &= nbt_test_wins_name(tctx, address, &name,
531 NBT_NODE_H, false, NBT_RCODE_SVR);
533 return ret;
537 test WINS operations
539 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
541 struct torture_suite *suite = torture_suite_create(mem_ctx, "wins");
543 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
545 return suite;