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"
31 #define CHECK_VALUE(tctx, v, correct) \
32 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
34 #define CHECK_STRING(tctx, v, correct) \
35 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
37 #define CHECK_NAME(tctx, _name, correct) do { \
38 CHECK_STRING(tctx, (_name).name, (correct).name); \
39 CHECK_VALUE(tctx, (uint8_t)(_name).type, (uint8_t)(correct).type); \
40 CHECK_STRING(tctx, (_name).scope, (correct).scope); \
45 test operations against a WINS server
47 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
48 struct nbt_name *name, uint16_t nb_flags)
50 struct nbt_name_register_wins io;
51 struct nbt_name_query query;
52 struct nbt_name_refresh_wins refresh;
53 struct nbt_name_release release;
55 struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
56 const char *myaddress = talloc_strdup(tctx, iface_best_ip(address));
57 struct socket_address *socket_address;
59 socket_address = socket_address_from_strings(tctx,
60 nbtsock->sock->backend_name,
62 torture_assert(tctx, socket_address != NULL,
63 "Error getting address");
65 /* we do the listen here to ensure the WINS server receives the packets from
67 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
68 torture_assert_ntstatus_ok(tctx, status,
69 "socket_listen for WINS failed");
70 talloc_free(socket_address);
72 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
73 nbt_name_string(tctx, name), myaddress, nb_flags);
75 torture_comment(tctx, "release the name\n");
76 release.in.name = *name;
77 release.in.dest_addr = address;
78 release.in.address = myaddress;
79 release.in.nb_flags = nb_flags;
80 release.in.broadcast = False;
81 release.in.timeout = 3;
82 release.in.retries = 0;
84 status = nbt_name_release(nbtsock, tctx, &release);
85 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
86 CHECK_VALUE(tctx, release.out.rcode, 0);
88 torture_comment(tctx, "register the name\n");
90 io.in.wins_servers = str_list_make(tctx, address, NULL);
91 io.in.addresses = str_list_make(tctx, myaddress, NULL);
92 io.in.nb_flags = nb_flags;
95 status = nbt_name_register_wins(nbtsock, tctx, &io);
96 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
98 CHECK_STRING(tctx, io.out.wins_server, address);
99 CHECK_VALUE(tctx, io.out.rcode, 0);
101 if (name->type != NBT_NAME_MASTER &&
102 name->type != NBT_NAME_LOGON &&
103 name->type != NBT_NAME_BROWSER &&
104 (nb_flags & NBT_NM_GROUP)) {
105 torture_comment(tctx, "Try to register as non-group\n");
106 io.in.nb_flags &= ~NBT_NM_GROUP;
107 status = nbt_name_register_wins(nbtsock, tctx, &io);
108 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
110 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
113 torture_comment(tctx, "query the name to make sure its there\n");
114 query.in.name = *name;
115 query.in.dest_addr = address;
116 query.in.broadcast = False;
117 query.in.wins_lookup = True;
118 query.in.timeout = 3;
119 query.in.retries = 0;
121 status = nbt_name_query(nbtsock, tctx, &query);
122 if (name->type == NBT_NAME_MASTER) {
123 torture_assert_ntstatus_equal(
124 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
125 talloc_asprintf(tctx, "Bad response from %s for name query", address));
128 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
130 CHECK_NAME(tctx, query.out.name, *name);
131 CHECK_VALUE(tctx, query.out.num_addrs, 1);
132 if (name->type != NBT_NAME_LOGON &&
133 (nb_flags & NBT_NM_GROUP)) {
134 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
136 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
140 query.in.name.name = strupper_talloc(tctx, name->name);
141 if (query.in.name.name &&
142 strcmp(query.in.name.name, name->name) != 0) {
143 torture_comment(tctx, "check case sensitivity\n");
144 status = nbt_name_query(nbtsock, tctx, &query);
145 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
148 query.in.name = *name;
150 query.in.name.scope = strupper_talloc(tctx, name->scope);
152 if (query.in.name.scope &&
153 strcmp(query.in.name.scope, name->scope) != 0) {
154 torture_comment(tctx, "check case sensitivity on scope\n");
155 status = nbt_name_query(nbtsock, tctx, &query);
156 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
159 torture_comment(tctx, "refresh the name\n");
160 refresh.in.name = *name;
161 refresh.in.wins_servers = str_list_make(tctx, address, NULL);
162 refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
163 refresh.in.nb_flags = nb_flags;
164 refresh.in.ttl = 12345;
166 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
167 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
169 CHECK_STRING(tctx, refresh.out.wins_server, address);
170 CHECK_VALUE(tctx, refresh.out.rcode, 0);
172 torture_comment(tctx, "release the name\n");
173 release.in.name = *name;
174 release.in.dest_addr = address;
175 release.in.address = myaddress;
176 release.in.nb_flags = nb_flags;
177 release.in.broadcast = False;
178 release.in.timeout = 3;
179 release.in.retries = 0;
181 status = nbt_name_release(nbtsock, tctx, &release);
182 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
184 CHECK_NAME(tctx, release.out.name, *name);
185 CHECK_VALUE(tctx, release.out.rcode, 0);
187 torture_comment(tctx, "release again\n");
188 status = nbt_name_release(nbtsock, tctx, &release);
189 torture_assert_ntstatus_ok(tctx, status,
190 talloc_asprintf(tctx, "Bad response from %s for name query",
193 CHECK_NAME(tctx, release.out.name, *name);
194 CHECK_VALUE(tctx, release.out.rcode, 0);
197 torture_comment(tctx, "query the name to make sure its gone\n");
198 query.in.name = *name;
199 status = nbt_name_query(nbtsock, tctx, &query);
200 if (name->type != NBT_NAME_LOGON &&
201 (nb_flags & NBT_NM_GROUP)) {
202 torture_assert_ntstatus_ok(tctx, status,
203 "ERROR: Name query failed after group release");
205 torture_assert_ntstatus_equal(tctx, status,
206 NT_STATUS_OBJECT_NAME_NOT_FOUND,
207 "Incorrect response to name query");
216 test operations against a WINS server
218 static bool nbt_test_wins(struct torture_context *tctx)
220 struct nbt_name name;
221 uint32_t r = (uint32_t)(random() % (100000));
225 if (!torture_nbt_get_name(tctx, &name, &address))
228 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
230 name.type = NBT_NAME_CLIENT;
232 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
234 name.type = NBT_NAME_MASTER;
235 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
237 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
239 name.type = NBT_NAME_SERVER;
240 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
242 name.type = NBT_NAME_LOGON;
243 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
245 name.type = NBT_NAME_BROWSER;
246 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
248 name.type = NBT_NAME_PDC;
249 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
252 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
255 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
257 name.scope = "example";
259 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
261 name.scope = "example";
263 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
265 name.scope = "foo.example.com";
267 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
269 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
270 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
273 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
275 name.name = talloc_asprintf(tctx, ".");
276 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
278 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
279 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
287 struct torture_suite *torture_nbt_wins(void)
289 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(),
292 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);