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)
51 struct nbt_name_register_wins io;
52 struct nbt_name_query query;
53 struct nbt_name_refresh_wins refresh;
54 struct nbt_name_release release;
56 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
57 const char *myaddress;
58 struct socket_address *socket_address;
59 struct interface *ifaces;
61 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
63 myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
65 socket_address = socket_address_from_strings(tctx,
66 nbtsock->sock->backend_name,
68 torture_assert(tctx, socket_address != NULL,
69 "Error getting address");
71 /* we do the listen here to ensure the WINS server receives the packets from
73 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
74 torture_assert_ntstatus_ok(tctx, status,
75 "socket_listen for WINS failed");
76 talloc_free(socket_address);
78 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
79 nbt_name_string(tctx, name), myaddress, nb_flags);
81 torture_comment(tctx, "release the name\n");
82 release.in.name = *name;
83 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
84 release.in.dest_addr = address;
85 release.in.address = myaddress;
86 release.in.nb_flags = nb_flags;
87 release.in.broadcast = false;
88 release.in.timeout = 3;
89 release.in.retries = 0;
91 status = nbt_name_release(nbtsock, tctx, &release);
92 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
93 CHECK_VALUE(tctx, release.out.rcode, 0);
95 torture_comment(tctx, "register the name\n");
97 io.in.wins_port = lp_nbt_port(tctx->lp_ctx);
98 io.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
99 io.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
100 io.in.nb_flags = nb_flags;
103 status = nbt_name_register_wins(nbtsock, tctx, &io);
104 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
106 CHECK_STRING(tctx, io.out.wins_server, address);
107 CHECK_VALUE(tctx, io.out.rcode, 0);
109 if (name->type != NBT_NAME_MASTER &&
110 name->type != NBT_NAME_LOGON &&
111 name->type != NBT_NAME_BROWSER &&
112 (nb_flags & NBT_NM_GROUP)) {
113 torture_comment(tctx, "Try to register as non-group\n");
114 io.in.nb_flags &= ~NBT_NM_GROUP;
115 status = nbt_name_register_wins(nbtsock, tctx, &io);
116 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
118 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
121 torture_comment(tctx, "query the name to make sure its there\n");
122 query.in.name = *name;
123 query.in.dest_addr = address;
124 query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
125 query.in.broadcast = false;
126 query.in.wins_lookup = true;
127 query.in.timeout = 3;
128 query.in.retries = 0;
130 status = nbt_name_query(nbtsock, tctx, &query);
131 if (name->type == NBT_NAME_MASTER) {
132 torture_assert_ntstatus_equal(
133 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
134 talloc_asprintf(tctx, "Bad response from %s for name query", address));
137 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
139 CHECK_NAME(tctx, query.out.name, *name);
140 CHECK_VALUE(tctx, query.out.num_addrs, 1);
141 if (name->type != NBT_NAME_LOGON &&
142 (nb_flags & NBT_NM_GROUP)) {
143 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
145 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
149 query.in.name.name = strupper_talloc(tctx, name->name);
150 if (query.in.name.name &&
151 strcmp(query.in.name.name, name->name) != 0) {
152 torture_comment(tctx, "check case sensitivity\n");
153 status = nbt_name_query(nbtsock, tctx, &query);
154 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
157 query.in.name = *name;
159 query.in.name.scope = strupper_talloc(tctx, name->scope);
161 if (query.in.name.scope &&
162 strcmp(query.in.name.scope, name->scope) != 0) {
163 torture_comment(tctx, "check case sensitivity on scope\n");
164 status = nbt_name_query(nbtsock, tctx, &query);
165 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
168 torture_comment(tctx, "refresh the name\n");
169 refresh.in.name = *name;
170 refresh.in.wins_port = lp_nbt_port(tctx->lp_ctx);
171 refresh.in.wins_servers = (const char **)str_list_make(tctx, address, NULL);
172 refresh.in.addresses = (const char **)str_list_make(tctx, myaddress, NULL);
173 refresh.in.nb_flags = nb_flags;
174 refresh.in.ttl = 12345;
176 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
177 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
179 CHECK_STRING(tctx, refresh.out.wins_server, address);
180 CHECK_VALUE(tctx, refresh.out.rcode, 0);
182 torture_comment(tctx, "release the name\n");
183 release.in.name = *name;
184 release.in.dest_port = lp_nbt_port(tctx->lp_ctx);
185 release.in.dest_addr = address;
186 release.in.address = myaddress;
187 release.in.nb_flags = nb_flags;
188 release.in.broadcast = false;
189 release.in.timeout = 3;
190 release.in.retries = 0;
192 status = nbt_name_release(nbtsock, tctx, &release);
193 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
195 CHECK_NAME(tctx, release.out.name, *name);
196 CHECK_VALUE(tctx, release.out.rcode, 0);
198 torture_comment(tctx, "release again\n");
199 status = nbt_name_release(nbtsock, tctx, &release);
200 torture_assert_ntstatus_ok(tctx, status,
201 talloc_asprintf(tctx, "Bad response from %s for name query",
204 CHECK_NAME(tctx, release.out.name, *name);
205 CHECK_VALUE(tctx, release.out.rcode, 0);
208 torture_comment(tctx, "query the name to make sure its gone\n");
209 query.in.name = *name;
210 status = nbt_name_query(nbtsock, tctx, &query);
211 if (name->type != NBT_NAME_LOGON &&
212 (nb_flags & NBT_NM_GROUP)) {
213 torture_assert_ntstatus_ok(tctx, status,
214 "ERROR: Name query failed after group release");
216 torture_assert_ntstatus_equal(tctx, status,
217 NT_STATUS_OBJECT_NAME_NOT_FOUND,
218 "Incorrect response to name query");
227 test operations against a WINS server
229 static bool nbt_test_wins(struct torture_context *tctx)
231 struct nbt_name name;
232 uint32_t r = (uint32_t)(random() % (100000));
236 if (!torture_nbt_get_name(tctx, &name, &address))
239 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
241 name.type = NBT_NAME_CLIENT;
243 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
245 name.type = NBT_NAME_MASTER;
246 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
248 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
250 name.type = NBT_NAME_SERVER;
251 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
253 name.type = NBT_NAME_LOGON;
254 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
256 name.type = NBT_NAME_BROWSER;
257 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
259 name.type = NBT_NAME_PDC;
260 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
263 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
266 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
268 name.scope = "example";
270 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
272 name.scope = "example";
274 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
276 name.scope = "foo.example.com";
278 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
280 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
281 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
284 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
286 name.name = talloc_asprintf(tctx, ".");
287 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
289 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
290 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
298 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
300 struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
302 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);