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 = nbt_name_socket_init(tctx, NULL);
57 const char *myaddress = talloc_strdup(tctx, iface_best_ip(address));
58 struct socket_address *socket_address;
60 socket_address = socket_address_from_strings(tctx,
61 nbtsock->sock->backend_name,
63 torture_assert(tctx, socket_address != NULL,
64 "Error getting address");
66 /* we do the listen here to ensure the WINS server receives the packets from
68 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
69 torture_assert_ntstatus_ok(tctx, status,
70 "socket_listen for WINS failed");
71 talloc_free(socket_address);
73 torture_comment(tctx, "Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
74 nbt_name_string(tctx, name), myaddress, nb_flags);
76 torture_comment(tctx, "release the name\n");
77 release.in.name = *name;
78 release.in.dest_addr = address;
79 release.in.address = myaddress;
80 release.in.nb_flags = nb_flags;
81 release.in.broadcast = false;
82 release.in.timeout = 3;
83 release.in.retries = 0;
85 status = nbt_name_release(nbtsock, tctx, &release);
86 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
87 CHECK_VALUE(tctx, release.out.rcode, 0);
89 torture_comment(tctx, "register the name\n");
91 io.in.wins_servers = str_list_make(tctx, address, NULL);
92 io.in.addresses = str_list_make(tctx, myaddress, NULL);
93 io.in.nb_flags = nb_flags;
96 status = nbt_name_register_wins(nbtsock, tctx, &io);
97 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register", address));
99 CHECK_STRING(tctx, io.out.wins_server, address);
100 CHECK_VALUE(tctx, io.out.rcode, 0);
102 if (name->type != NBT_NAME_MASTER &&
103 name->type != NBT_NAME_LOGON &&
104 name->type != NBT_NAME_BROWSER &&
105 (nb_flags & NBT_NM_GROUP)) {
106 torture_comment(tctx, "Try to register as non-group\n");
107 io.in.nb_flags &= ~NBT_NM_GROUP;
108 status = nbt_name_register_wins(nbtsock, tctx, &io);
109 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name register\n",
111 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
114 torture_comment(tctx, "query the name to make sure its there\n");
115 query.in.name = *name;
116 query.in.dest_addr = address;
117 query.in.dest_port = lp_nbt_port(tctx->lp_ctx);
118 query.in.broadcast = false;
119 query.in.wins_lookup = true;
120 query.in.timeout = 3;
121 query.in.retries = 0;
123 status = nbt_name_query(nbtsock, tctx, &query);
124 if (name->type == NBT_NAME_MASTER) {
125 torture_assert_ntstatus_equal(
126 tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND,
127 talloc_asprintf(tctx, "Bad response from %s for name query", address));
130 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
132 CHECK_NAME(tctx, query.out.name, *name);
133 CHECK_VALUE(tctx, query.out.num_addrs, 1);
134 if (name->type != NBT_NAME_LOGON &&
135 (nb_flags & NBT_NM_GROUP)) {
136 CHECK_STRING(tctx, query.out.reply_addrs[0], "255.255.255.255");
138 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
142 query.in.name.name = strupper_talloc(tctx, name->name);
143 if (query.in.name.name &&
144 strcmp(query.in.name.name, name->name) != 0) {
145 torture_comment(tctx, "check case sensitivity\n");
146 status = nbt_name_query(nbtsock, tctx, &query);
147 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
150 query.in.name = *name;
152 query.in.name.scope = strupper_talloc(tctx, name->scope);
154 if (query.in.name.scope &&
155 strcmp(query.in.name.scope, name->scope) != 0) {
156 torture_comment(tctx, "check case sensitivity on scope\n");
157 status = nbt_name_query(nbtsock, tctx, &query);
158 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OBJECT_NAME_NOT_FOUND, talloc_asprintf(tctx, "Bad response from %s for name query", address));
161 torture_comment(tctx, "refresh the name\n");
162 refresh.in.name = *name;
163 refresh.in.wins_servers = str_list_make(tctx, address, NULL);
164 refresh.in.addresses = str_list_make(tctx, myaddress, NULL);
165 refresh.in.nb_flags = nb_flags;
166 refresh.in.ttl = 12345;
168 status = nbt_name_refresh_wins(nbtsock, tctx, &refresh);
169 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
171 CHECK_STRING(tctx, refresh.out.wins_server, address);
172 CHECK_VALUE(tctx, refresh.out.rcode, 0);
174 torture_comment(tctx, "release the name\n");
175 release.in.name = *name;
176 release.in.dest_addr = address;
177 release.in.address = myaddress;
178 release.in.nb_flags = nb_flags;
179 release.in.broadcast = false;
180 release.in.timeout = 3;
181 release.in.retries = 0;
183 status = nbt_name_release(nbtsock, tctx, &release);
184 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
186 CHECK_NAME(tctx, release.out.name, *name);
187 CHECK_VALUE(tctx, release.out.rcode, 0);
189 torture_comment(tctx, "release again\n");
190 status = nbt_name_release(nbtsock, tctx, &release);
191 torture_assert_ntstatus_ok(tctx, status,
192 talloc_asprintf(tctx, "Bad response from %s for name query",
195 CHECK_NAME(tctx, release.out.name, *name);
196 CHECK_VALUE(tctx, release.out.rcode, 0);
199 torture_comment(tctx, "query the name to make sure its gone\n");
200 query.in.name = *name;
201 status = nbt_name_query(nbtsock, tctx, &query);
202 if (name->type != NBT_NAME_LOGON &&
203 (nb_flags & NBT_NM_GROUP)) {
204 torture_assert_ntstatus_ok(tctx, status,
205 "ERROR: Name query failed after group release");
207 torture_assert_ntstatus_equal(tctx, status,
208 NT_STATUS_OBJECT_NAME_NOT_FOUND,
209 "Incorrect response to name query");
218 test operations against a WINS server
220 static bool nbt_test_wins(struct torture_context *tctx)
222 struct nbt_name name;
223 uint32_t r = (uint32_t)(random() % (100000));
227 if (!torture_nbt_get_name(tctx, &name, &address))
230 name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
232 name.type = NBT_NAME_CLIENT;
234 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
236 name.type = NBT_NAME_MASTER;
237 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
239 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
241 name.type = NBT_NAME_SERVER;
242 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
244 name.type = NBT_NAME_LOGON;
245 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
247 name.type = NBT_NAME_BROWSER;
248 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
250 name.type = NBT_NAME_PDC;
251 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
254 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
257 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
259 name.scope = "example";
261 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
263 name.scope = "example";
265 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
267 name.scope = "foo.example.com";
269 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
271 name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
272 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
275 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
277 name.name = talloc_asprintf(tctx, ".");
278 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
280 name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
281 ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
289 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
291 struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
293 torture_suite_add_simple_test(suite, "wins", nbt_test_wins);