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,
52 struct nbt_name_register_wins io;
53 struct nbt_name_query query;
54 struct nbt_name_refresh_wins refresh;
55 struct nbt_name_release release;
57 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
58 const char *myaddress;
59 struct socket_address *socket_address;
60 struct interface *ifaces;
61 bool low_port = try_low_port;
63 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
65 myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
67 socket_address = socket_address_from_strings(tctx,
68 nbtsock->sock->backend_name,
69 myaddress, lp_nbt_port(tctx->lp_ctx));
70 torture_assert(tctx, socket_address != NULL,
71 "Error getting address");
73 /* we do the listen here to ensure the WINS server receives the packets from
75 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
76 talloc_free(socket_address);
77 if (!NT_STATUS_IS_OK(status)) {
79 socket_address = socket_address_from_strings(tctx,
80 nbtsock->sock->backend_name,
82 torture_assert(tctx, socket_address != NULL,
83 "Error getting address");
85 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
86 talloc_free(socket_address);
87 torture_assert_ntstatus_ok(tctx, status,
88 "socket_listen for WINS failed");
91 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
92 nbt_name_string(tctx, name), myaddress, nb_flags);
94 torture_comment(tctx, "release the name\n");
95 release.in.name = *name;
96 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
97 release.in.dest_addr = address;
98 release.in.address = myaddress;
99 release.in.nb_flags = nb_flags;
100 release.in.broadcast = false;
101 release.in.timeout = 3;
102 release.in.retries = 0;
104 status = nbt_name_release(nbtsock, tctx, &release);
105 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
106 CHECK_VALUE(tctx, release.out.rcode, 0);
108 if (nb_flags & NBT_NM_GROUP) {
109 /* ignore this for group names */
110 } else if (!low_port) {
111 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
113 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
115 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
116 io.in.wins_servers = str_list_make(tctx, address, NULL);
117 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
118 io.in.nb_flags = nb_flags;
121 status = nbt_name_register_wins(nbtsock, tctx, &io);
122 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
123 torture_assert_ntstatus_ok(tctx, status,
124 talloc_asprintf(tctx, "No response from %s for name register\n",
127 torture_assert_ntstatus_ok(tctx, status,
128 talloc_asprintf(tctx, "Bad response from %s for name register\n",
131 CHECK_STRING(tctx, io.out.wins_server, address);
132 CHECK_VALUE(tctx, io.out.rcode, 0);
135 torture_comment(tctx, "register the name correct address\n");
137 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
138 io.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
139 io.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
140 io.in.nb_flags = nb_flags;
143 status = nbt_name_register_wins(nbtsock, tctx, &io);
144 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
146 CHECK_STRING(tctx, io.out.wins_server, address);
147 CHECK_VALUE(tctx, io.out.rcode, 0);
149 if (name->type != NBT_NAME_MASTER &&
150 name->type != NBT_NAME_LOGON &&
151 name->type != NBT_NAME_BROWSER &&
152 (nb_flags & NBT_NM_GROUP)) {
153 torture_comment(tctx, "Try to register as non-group\n");
154 io.in.nb_flags &= ~NBT_NM_GROUP;
155 status = nbt_name_register_wins(nbtsock, tctx, &io);
156 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
158 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
161 torture_comment(tctx, "query the name to make sure its there\n");
162 query.in.name = *name;
163 query.in.dest_addr = address;
164 query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
165 query.in.broadcast = false;
166 query.in.wins_lookup = true;
167 query.in.timeout = 3;
168 query.in.retries = 0;
170 status = nbt_name_query(nbtsock, tctx, &query);
171 if (name->type == NBT_NAME_MASTER) {
172 torture_assert_ntstatus_equal(
173 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
174 talloc_asprintf(tctx, "Bad response from %s for name query", address));
177 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
179 CHECK_NAME(tctx, query.out.name, *name);
180 CHECK_VALUE(tctx, query.out.num_addrs, 1);
181 if (name->type != NBT_NAME_LOGON &&
182 (nb_flags & NBT_NM_GROUP)) {
183 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
185 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
189 query.in.name.name = strupper_talloc(tctx, name->name);
190 if (query.in.name.name &&
191 strcmp(query.in.name.name, name->name) != 0) {
192 torture_comment(tctx, "check case sensitivity\n");
193 status = nbt_name_query(nbtsock, tctx, &query);
194 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
197 query.in.name = *name;
199 query.in.name.scope = strupper_talloc(tctx, name->scope);
201 if (query.in.name.scope &&
202 strcmp(query.in.name.scope, name->scope) != 0) {
203 torture_comment(tctx, "check case sensitivity on scope\n");
204 status = nbt_name_query(nbtsock, tctx, &query);
205 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
208 torture_comment(tctx, "refresh the name\n");
209 refresh.in.name = *name;
210 refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
211 refresh.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
212 refresh.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
213 refresh.in.nb_flags = nb_flags;
214 refresh.in.ttl = 12345;
216 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
217 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
218 torture_assert_ntstatus_ok(tctx, status,
219 talloc_asprintf(tctx, "No response from %s for name refresh",
222 torture_assert_ntstatus_ok(tctx, status,
223 talloc_asprintf(tctx, "Bad response from %s for name refresh",
226 CHECK_STRING(tctx, refresh.out.wins_server, address);
227 CHECK_VALUE(tctx, refresh.out.rcode, 0);
229 printf("release the name\n");
230 release.in.name = *name;
231 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
232 release.in.dest_addr = address;
233 release.in.address = myaddress;
234 release.in.nb_flags = nb_flags;
235 release.in.broadcast = false;
236 release.in.timeout = 3;
237 release.in.retries = 0;
239 status = nbt_name_release(nbtsock, tctx, &release);
240 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
241 torture_assert_ntstatus_ok(tctx, status,
242 talloc_asprintf(tctx, "No response from %s for name release",
245 torture_assert_ntstatus_ok(tctx, status,
246 talloc_asprintf(tctx, "Bad response from %s for name release",
249 CHECK_NAME(tctx, release.out.name, *name);
250 CHECK_VALUE(tctx, release.out.rcode, 0);
252 if (nb_flags & NBT_NM_GROUP) {
253 /* ignore this for group names */
254 } else if (!low_port) {
255 torture_comment(tctx, "no low port - skip: register the name with a wrong address\n");
257 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
259 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
260 io.in.wins_servers = str_list_make(tctx, address, NULL);
261 io.in.addresses = str_list_make(tctx, "127.64.64.1", NULL);
262 io.in.nb_flags = nb_flags;
265 status = nbt_name_register_wins(nbtsock, tctx, &io);
266 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
267 torture_assert_ntstatus_ok(tctx, status,
268 talloc_asprintf(tctx, "No response from %s for name register\n",
271 torture_assert_ntstatus_ok(tctx, status,
272 talloc_asprintf(tctx, "Bad response from %s for name register\n",
275 CHECK_STRING(tctx, io.out.wins_server, address);
276 CHECK_VALUE(tctx, io.out.rcode, 0);
279 torture_comment(tctx, "refresh the name with the correct address\n");
280 refresh.in.name = *name;
281 refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
282 refresh.in.wins_servers = str_list_make(tctx, address, NULL);
283 refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
284 refresh.in.nb_flags = nb_flags;
285 refresh.in.ttl = 12345;
287 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
288 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
289 torture_assert_ntstatus_ok(tctx, status,
290 talloc_asprintf(tctx, "No response from %s for name refresh",
293 torture_assert_ntstatus_ok(tctx, status,
294 talloc_asprintf(tctx, "Bad response from %s for name refresh",
297 CHECK_STRING(tctx, refresh.out.wins_server, address);
298 CHECK_VALUE(tctx, refresh.out.rcode, 0);
300 torture_comment(tctx, "release the name\n");
301 release.in.name = *name;
302 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
303 release.in.dest_addr = address;
304 release.in.address = myaddress;
305 release.in.nb_flags = nb_flags;
306 release.in.broadcast = false;
307 release.in.timeout = 3;
308 release.in.retries = 0;
310 status = nbt_name_release(nbtsock, tctx, &release);
311 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
313 CHECK_NAME(tctx, release.out.name, *name);
314 CHECK_VALUE(tctx, release.out.rcode, 0);
316 torture_comment(tctx, "release again\n");
317 status = nbt_name_release(nbtsock, tctx, &release);
318 torture_assert_ntstatus_ok(tctx, status,
319 talloc_asprintf(tctx, "Bad response from %s for name query",
322 CHECK_NAME(tctx, release.out.name, *name);
323 CHECK_VALUE(tctx, release.out.rcode, 0);
326 torture_comment(tctx, "query the name to make sure its gone\n");
327 query.in.name = *name;
328 status = nbt_name_query(nbtsock, tctx, &query);
329 if (name->type != NBT_NAME_LOGON &&
330 (nb_flags & NBT_NM_GROUP)) {
331 torture_assert_ntstatus_ok(tctx, status,
332 "ERROR: Name query failed after group release");
334 torture_assert_ntstatus_equal(tctx, status,
335 NT_STATUS_OBJECT_NAME_NOT_FOUND,
336 "Incorrect response to name query");
345 test operations against a WINS server
347 static bool nbt_test_wins(struct torture_context *tctx)
349 struct nbt_name name;
350 uint32_t r = (uint32_t)(random() % (100000));
354 if (!torture_nbt_get_name(tctx, &name, &address))
357 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
359 name.type = NBT_NAME_CLIENT;
361 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
363 name.type = NBT_NAME_MASTER;
364 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
366 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
368 name.type = NBT_NAME_SERVER;
369 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
371 name.type = NBT_NAME_LOGON;
372 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
374 name.type = NBT_NAME_BROWSER;
375 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
377 name.type = NBT_NAME_PDC;
378 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
381 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
384 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
386 name.scope = "example";
388 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
390 name.scope = "example";
392 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
394 name.scope = "foo.example.com";
396 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
398 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
399 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
402 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
404 name.name = talloc_asprintf(tctx, ".");
405 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
407 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
408 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
416 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
418 struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
420 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);