s4:torture: send the TCONX_FLAG_EXTENDED_RESPONSE flag
[Samba/gebeck_regimport.git] / source4 / torture / nbt / wins.c
blobbd023294cbf17023031364a5714e67c56c80192e
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;
68 load_interface_list(tctx, tctx->lp_ctx, &ifaces);
70 myaddress = talloc_strdup(tctx, iface_list_best_ip(ifaces, address));
72 socket_address = socket_address_from_strings(tctx,
73 nbtsock->sock->backend_name,
74 myaddress, lpcfg_nbt_port(tctx->lp_ctx));
75 torture_assert(tctx, socket_address != NULL,
76 "Error getting address");
78 /* we do the listen here to ensure the WINS server receives the packets from
79 the right IP */
80 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
81 talloc_free(socket_address);
82 if (!NT_STATUS_IS_OK(status)) {
83 low_port = false;
84 socket_address = socket_address_from_strings(tctx,
85 nbtsock->sock->backend_name,
86 myaddress, 0);
87 torture_assert(tctx, socket_address != NULL,
88 "Error getting address");
90 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
91 talloc_free(socket_address);
92 torture_assert_ntstatus_ok(tctx, status,
93 "socket_listen for WINS failed");
96 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
97 nbt_name_string(tctx, name), myaddress, nb_flags);
99 torture_comment(tctx, "release the name\n");
100 release.in.name = *name;
101 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
102 release.in.dest_addr = address;
103 release.in.address = myaddress;
104 release.in.nb_flags = nb_flags;
105 release.in.broadcast = false;
106 release.in.timeout = 3;
107 release.in.retries = 0;
109 status = nbt_name_release(nbtsock, tctx, &release);
110 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
111 CHECK_VALUE(tctx, release.out.rcode, 0);
113 if (nb_flags & NBT_NM_GROUP) {
114 /* ignore this for group names */
115 } else if (!low_port) {
116 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
117 } else {
118 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
119 io.in.name = *name;
120 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
121 io.in.wins_servers = const_str_list(
122 str_list_make_single(tctx, address));
123 io.in.addresses = const_str_list(
124 str_list_make_single(tctx, "127.64.64.1"));
125 io.in.nb_flags = nb_flags;
126 io.in.ttl = 300000;
128 status = nbt_name_register_wins(nbtsock, tctx, &io);
129 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
130 torture_assert_ntstatus_ok(tctx, status,
131 talloc_asprintf(tctx, "No response from %s for name register\n",
132 address));
134 torture_assert_ntstatus_ok(tctx, status,
135 talloc_asprintf(tctx, "Bad response from %s for name register\n",
136 address));
138 CHECK_STRING(tctx, io.out.wins_server, address);
139 CHECK_VALUE(tctx, io.out.rcode, 0);
141 torture_comment(tctx, "register the name correct address\n");
142 name_register.in.name = *name;
143 name_register.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
144 name_register.in.dest_addr = address;
145 name_register.in.address = myaddress;
146 name_register.in.nb_flags = nb_flags;
147 name_register.in.register_demand= false;
148 name_register.in.broadcast = false;
149 name_register.in.multi_homed = true;
150 name_register.in.ttl = 300000;
151 name_register.in.timeout = 3;
152 name_register.in.retries = 2;
155 * test if the server ignores resent requests
157 req = nbt_name_register_send(nbtsock, &name_register);
158 while (true) {
159 tevent_loop_once(nbtsock->event_ctx);
160 if (req->state != NBT_REQUEST_WAIT) {
161 break;
163 if (req->received_wack) {
165 * if we received the wack response
166 * we resend the request and the
167 * server should ignore that
168 * and not handle it as new request
170 req->state = NBT_REQUEST_SEND;
171 DLIST_ADD_END(nbtsock->send_queue, req,
172 struct nbt_name_request *);
173 TEVENT_FD_WRITEABLE(nbtsock->fde);
174 break;
178 status = nbt_name_register_recv(req, tctx, &name_register);
179 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
180 torture_assert_ntstatus_ok(tctx, status,
181 talloc_asprintf(tctx, "No response from %s for name register\n",
182 address));
184 torture_assert_ntstatus_ok(tctx, status,
185 talloc_asprintf(tctx, "Bad response from %s for name register\n",
186 address));
188 CHECK_VALUE(tctx, name_register.out.rcode, 0);
189 CHECK_STRING(tctx, name_register.out.reply_addr, myaddress);
192 torture_comment(tctx, "register the name correct address\n");
193 io.in.name = *name;
194 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
195 io.in.wins_servers = (const char **)str_list_make_single(tctx, address);
196 io.in.addresses = (const char **)str_list_make_single(tctx, myaddress);
197 io.in.nb_flags = nb_flags;
198 io.in.ttl = 300000;
200 status = nbt_name_register_wins(nbtsock, tctx, &io);
201 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
203 CHECK_STRING(tctx, io.out.wins_server, address);
204 CHECK_VALUE(tctx, io.out.rcode, register_rcode);
206 if (register_rcode != NBT_RCODE_OK) {
207 return true;
210 if (name->type != NBT_NAME_MASTER &&
211 name->type != NBT_NAME_LOGON &&
212 name->type != NBT_NAME_BROWSER &&
213 (nb_flags & NBT_NM_GROUP)) {
214 torture_comment(tctx, "Try to register as non-group\n");
215 io.in.nb_flags &= ~NBT_NM_GROUP;
216 status = nbt_name_register_wins(nbtsock, tctx, &io);
217 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
218 address));
219 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
222 torture_comment(tctx, "query the name to make sure its there\n");
223 query.in.name = *name;
224 query.in.dest_addr = address;
225 query.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
226 query.in.broadcast = false;
227 query.in.wins_lookup = true;
228 query.in.timeout = 3;
229 query.in.retries = 0;
231 status = nbt_name_query(nbtsock, tctx, &query);
232 if (name->type == NBT_NAME_MASTER) {
233 torture_assert_ntstatus_equal(
234 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
235 talloc_asprintf(tctx, "Bad response from %s for name query", address));
236 return true;
238 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
240 CHECK_NAME(tctx, query.out.name, *name);
241 CHECK_VALUE(tctx, query.out.num_addrs, 1);
242 if (name->type != NBT_NAME_LOGON &&
243 (nb_flags & NBT_NM_GROUP)) {
244 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
245 } else {
246 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
250 query.in.name.name = strupper_talloc(tctx, name->name);
251 if (query.in.name.name &&
252 strcmp(query.in.name.name, name->name) != 0) {
253 torture_comment(tctx, "check case sensitivity\n");
254 status = nbt_name_query(nbtsock, tctx, &query);
255 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
258 query.in.name = *name;
259 if (name->scope) {
260 query.in.name.scope = strupper_talloc(tctx, name->scope);
262 if (query.in.name.scope &&
263 strcmp(query.in.name.scope, name->scope) != 0) {
264 torture_comment(tctx, "check case sensitivity on scope\n");
265 status = nbt_name_query(nbtsock, tctx, &query);
266 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
269 torture_comment(tctx, "refresh the name\n");
270 refresh.in.name = *name;
271 refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
272 refresh.in.wins_servers = (const char **)str_list_make_single(tctx, address);
273 refresh.in.addresses = (const char **)str_list_make_single(tctx, myaddress);
274 refresh.in.nb_flags = nb_flags;
275 refresh.in.ttl = 12345;
277 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
278 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
279 torture_assert_ntstatus_ok(tctx, status,
280 talloc_asprintf(tctx, "No response from %s for name refresh",
281 address));
283 torture_assert_ntstatus_ok(tctx, status,
284 talloc_asprintf(tctx, "Bad response from %s for name refresh",
285 address));
287 CHECK_STRING(tctx, refresh.out.wins_server, address);
288 CHECK_VALUE(tctx, refresh.out.rcode, 0);
290 printf("release the name\n");
291 release.in.name = *name;
292 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
293 release.in.dest_addr = address;
294 release.in.address = myaddress;
295 release.in.nb_flags = nb_flags;
296 release.in.broadcast = false;
297 release.in.timeout = 3;
298 release.in.retries = 0;
300 status = nbt_name_release(nbtsock, tctx, &release);
301 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
302 torture_assert_ntstatus_ok(tctx, status,
303 talloc_asprintf(tctx, "No response from %s for name release",
304 address));
306 torture_assert_ntstatus_ok(tctx, status,
307 talloc_asprintf(tctx, "Bad response from %s for name release",
308 address));
310 CHECK_NAME(tctx, release.out.name, *name);
311 CHECK_VALUE(tctx, release.out.rcode, 0);
313 if (nb_flags & NBT_NM_GROUP) {
314 /* ignore this for group names */
315 } else if (!low_port) {
316 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
317 } else {
318 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
319 io.in.name = *name;
320 io.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
321 io.in.wins_servers = const_str_list(
322 str_list_make_single(tctx, address));
323 io.in.addresses = const_str_list(
324 str_list_make_single(tctx, "127.64.64.1"));
325 io.in.nb_flags = nb_flags;
326 io.in.ttl = 300000;
328 status = nbt_name_register_wins(nbtsock, tctx, &io);
329 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
330 torture_assert_ntstatus_ok(tctx, status,
331 talloc_asprintf(tctx, "No response from %s for name register\n",
332 address));
334 torture_assert_ntstatus_ok(tctx, status,
335 talloc_asprintf(tctx, "Bad response from %s for name register\n",
336 address));
338 CHECK_STRING(tctx, io.out.wins_server, address);
339 CHECK_VALUE(tctx, io.out.rcode, 0);
342 torture_comment(tctx, "refresh the name with the correct address\n");
343 refresh.in.name = *name;
344 refresh.in.wins_port = lpcfg_nbt_port(tctx->lp_ctx);
345 refresh.in.wins_servers = const_str_list(
346 str_list_make_single(tctx, address));
347 refresh.in.addresses = const_str_list(
348 str_list_make_single(tctx, myaddress));
349 refresh.in.nb_flags = nb_flags;
350 refresh.in.ttl = 12345;
352 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
353 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
354 torture_assert_ntstatus_ok(tctx, status,
355 talloc_asprintf(tctx, "No response from %s for name refresh",
356 address));
358 torture_assert_ntstatus_ok(tctx, status,
359 talloc_asprintf(tctx, "Bad response from %s for name refresh",
360 address));
362 CHECK_STRING(tctx, refresh.out.wins_server, address);
363 CHECK_VALUE(tctx, refresh.out.rcode, 0);
365 torture_comment(tctx, "release the name\n");
366 release.in.name = *name;
367 release.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
368 release.in.dest_addr = address;
369 release.in.address = myaddress;
370 release.in.nb_flags = nb_flags;
371 release.in.broadcast = false;
372 release.in.timeout = 3;
373 release.in.retries = 0;
375 status = nbt_name_release(nbtsock, tctx, &release);
376 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
378 CHECK_NAME(tctx, release.out.name, *name);
379 CHECK_VALUE(tctx, release.out.rcode, 0);
381 torture_comment(tctx, "release again\n");
382 status = nbt_name_release(nbtsock, tctx, &release);
383 torture_assert_ntstatus_ok(tctx, status,
384 talloc_asprintf(tctx, "Bad response from %s for name query",
385 address));
387 CHECK_NAME(tctx, release.out.name, *name);
388 CHECK_VALUE(tctx, release.out.rcode, 0);
391 torture_comment(tctx, "query the name to make sure its gone\n");
392 query.in.name = *name;
393 status = nbt_name_query(nbtsock, tctx, &query);
394 if (name->type != NBT_NAME_LOGON &&
395 (nb_flags & NBT_NM_GROUP)) {
396 torture_assert_ntstatus_ok(tctx, status,
397 "ERROR: Name query failed after group release");
398 } else {
399 torture_assert_ntstatus_equal(tctx, status,
400 NT_STATUS_OBJECT_NAME_NOT_FOUND,
401 "Incorrect response to name query");
404 return true;
408 static char *test_nbt_wins_scope_string(TALLOC_CTX *mem_ctx, uint8_t count)
410 char *res;
411 uint8_t i;
413 res = talloc_array(mem_ctx, char, count+1);
414 if (res == NULL) {
415 return NULL;
418 for (i=0; i < count; i++) {
419 switch (i) {
420 case 63:
421 case 63 + 1 + 63:
422 case 63 + 1 + 63 + 1 + 63:
423 res[i] = '.';
424 break;
425 default:
426 res[i] = '0' + (i%10);
427 break;
431 res[count] = '\0';
433 talloc_set_name_const(res, res);
435 return res;
439 test operations against a WINS server
441 static bool nbt_test_wins(struct torture_context *tctx)
443 struct nbt_name name;
444 uint32_t r = (uint32_t)(random() % (100000));
445 const char *address;
446 bool ret = true;
448 if (!torture_nbt_get_name(tctx, &name, &address))
449 return false;
451 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
453 name.type = NBT_NAME_CLIENT;
454 name.scope = NULL;
455 ret &= nbt_test_wins_name(tctx, address, &name,
456 NBT_NODE_H, true, NBT_RCODE_OK);
458 name.type = NBT_NAME_MASTER;
459 ret &= nbt_test_wins_name(tctx, address, &name,
460 NBT_NODE_H, false, NBT_RCODE_OK);
462 ret &= nbt_test_wins_name(tctx, address, &name,
463 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
465 name.type = NBT_NAME_SERVER;
466 ret &= nbt_test_wins_name(tctx, address, &name,
467 NBT_NODE_H, true, NBT_RCODE_OK);
469 name.type = NBT_NAME_LOGON;
470 ret &= nbt_test_wins_name(tctx, address, &name,
471 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
473 name.type = NBT_NAME_BROWSER;
474 ret &= nbt_test_wins_name(tctx, address, &name,
475 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
477 name.type = NBT_NAME_PDC;
478 ret &= nbt_test_wins_name(tctx, address, &name,
479 NBT_NODE_H, true, NBT_RCODE_OK);
481 name.type = 0xBF;
482 ret &= nbt_test_wins_name(tctx, address, &name,
483 NBT_NODE_H, true, NBT_RCODE_OK);
485 name.type = 0xBE;
486 ret &= nbt_test_wins_name(tctx, address, &name,
487 NBT_NODE_H, false, NBT_RCODE_OK);
489 name.scope = "example";
490 name.type = 0x72;
491 ret &= nbt_test_wins_name(tctx, address, &name,
492 NBT_NODE_H, true, NBT_RCODE_OK);
494 name.scope = "example";
495 name.type = 0x71;
496 ret &= nbt_test_wins_name(tctx, address, &name,
497 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
499 name.scope = "foo.example.com";
500 name.type = 0x72;
501 ret &= nbt_test_wins_name(tctx, address, &name,
502 NBT_NODE_H, false, NBT_RCODE_OK);
504 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
505 ret &= nbt_test_wins_name(tctx, address, &name,
506 NBT_NODE_H, false, NBT_RCODE_OK);
508 name.name = "";
509 ret &= nbt_test_wins_name(tctx, address, &name,
510 NBT_NODE_H, false, NBT_RCODE_OK);
512 name.name = talloc_asprintf(tctx, ".");
513 ret &= nbt_test_wins_name(tctx, address, &name,
514 NBT_NODE_H, false, NBT_RCODE_OK);
516 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
517 ret &= nbt_test_wins_name(tctx, address, &name,
518 NBT_NODE_H, false, NBT_RCODE_OK);
520 name.scope = test_nbt_wins_scope_string(tctx, 237);
521 ret &= nbt_test_wins_name(tctx, address, &name,
522 NBT_NODE_H, false, NBT_RCODE_OK);
524 name.scope = test_nbt_wins_scope_string(tctx, 238);
525 ret &= nbt_test_wins_name(tctx, address, &name,
526 NBT_NODE_H, false, NBT_RCODE_SVR);
528 return ret;
532 test WINS operations
534 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
536 struct torture_suite *suite = torture_suite_create(mem_ctx, "wins");
538 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
540 return suite;