s4:NBT-WINS: pass the expected rcode of the name registration to the test code
[Samba/fernandojvsilva.git] / source4 / torture / nbt / wins.c
blob092215e2f11eb43c8c91472a413415424b208fd1
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_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
70 myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
72 socket_address = socket_address_from_strings(tctx,
73 nbtsock->sock->backend_name,
74 myaddress, lp_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 = lp_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 = lp_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 = lp_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 event_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 EVENT_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 = lp_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 = lp_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 = lp_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 = lp_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 = lp_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 = lp_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 = lp_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;
410 test operations against a WINS server
412 static bool nbt_test_wins(struct torture_context *tctx)
414 struct nbt_name name;
415 uint32_t r = (uint32_t)(random() % (100000));
416 const char *address;
417 bool ret = true;
419 if (!torture_nbt_get_name(tctx, &name, &address))
420 return false;
422 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
424 name.type = NBT_NAME_CLIENT;
425 name.scope = NULL;
426 ret &= nbt_test_wins_name(tctx, address, &name,
427 NBT_NODE_H, true, NBT_RCODE_OK);
429 name.type = NBT_NAME_MASTER;
430 ret &= nbt_test_wins_name(tctx, address, &name,
431 NBT_NODE_H, false, NBT_RCODE_OK);
433 ret &= nbt_test_wins_name(tctx, address, &name,
434 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
436 name.type = NBT_NAME_SERVER;
437 ret &= nbt_test_wins_name(tctx, address, &name,
438 NBT_NODE_H, true, NBT_RCODE_OK);
440 name.type = NBT_NAME_LOGON;
441 ret &= nbt_test_wins_name(tctx, address, &name,
442 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
444 name.type = NBT_NAME_BROWSER;
445 ret &= nbt_test_wins_name(tctx, address, &name,
446 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
448 name.type = NBT_NAME_PDC;
449 ret &= nbt_test_wins_name(tctx, address, &name,
450 NBT_NODE_H, true, NBT_RCODE_OK);
452 name.type = 0xBF;
453 ret &= nbt_test_wins_name(tctx, address, &name,
454 NBT_NODE_H, true, NBT_RCODE_OK);
456 name.type = 0xBE;
457 ret &= nbt_test_wins_name(tctx, address, &name,
458 NBT_NODE_H, false, NBT_RCODE_OK);
460 name.scope = "example";
461 name.type = 0x72;
462 ret &= nbt_test_wins_name(tctx, address, &name,
463 NBT_NODE_H, true, NBT_RCODE_OK);
465 name.scope = "example";
466 name.type = 0x71;
467 ret &= nbt_test_wins_name(tctx, address, &name,
468 NBT_NODE_H | NBT_NM_GROUP, false, NBT_RCODE_OK);
470 name.scope = "foo.example.com";
471 name.type = 0x72;
472 ret &= nbt_test_wins_name(tctx, address, &name,
473 NBT_NODE_H, false, NBT_RCODE_OK);
475 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
476 ret &= nbt_test_wins_name(tctx, address, &name,
477 NBT_NODE_H, false, NBT_RCODE_OK);
479 name.name = "";
480 ret &= nbt_test_wins_name(tctx, address, &name,
481 NBT_NODE_H, false, NBT_RCODE_OK);
483 name.name = talloc_asprintf(tctx, ".");
484 ret &= nbt_test_wins_name(tctx, address, &name,
485 NBT_NODE_H, false, NBT_RCODE_OK);
487 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
488 ret &= nbt_test_wins_name(tctx, address, &name,
489 NBT_NODE_H, false, NBT_RCODE_OK);
491 return ret;
495 test WINS operations
497 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
499 struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
501 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
503 return suite;