NBT-WINS: we don't need the slow low port tests for every name
[samba.git] / source4 / torture / nbt / wins.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT WINS server testing
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
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"
31
32 #define CHECK_VALUE(tctx, v, correct) \
33         torture_assert_int_equal(tctx, v, correct, "Incorrect value")
34
35 #define CHECK_STRING(tctx, v, correct) \
36         torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
37
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); \
42 } while (0)
43
44
45 /*
46   test operations against a WINS server
47 */
48 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
49                                struct nbt_name *name, uint16_t nb_flags,
50                                bool try_low_port)
51 {
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;
56         NTSTATUS status;
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;
62
63         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
64
65         myaddress = talloc_strdup(tctx, iface_best_ip(ifaces, address));
66
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");
72
73         /* we do the listen here to ensure the WINS server receives the packets from
74            the right IP */
75         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
76         talloc_free(socket_address);
77         if (!NT_STATUS_IS_OK(status)) {
78                 low_port = false;
79                 socket_address = socket_address_from_strings(tctx,
80                                                              nbtsock->sock->backend_name,
81                                                              myaddress, 0);
82                 torture_assert(tctx, socket_address != NULL,
83                                "Error getting address");
84
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");
89         }
90
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);
93
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;
103
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);
107
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");
112         } else {
113                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
114                 io.in.name = *name;
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;
119                 io.in.ttl = 300000;
120
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",
125                                                 address));
126                 }
127                 torture_assert_ntstatus_ok(tctx, status,
128                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
129                                         address));
130
131                 CHECK_STRING(tctx, io.out.wins_server, address);
132                 CHECK_VALUE(tctx, io.out.rcode, 0);
133         }
134
135         torture_comment(tctx, "register the name correct address\n");
136         io.in.name = *name;
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;
141         io.in.ttl = 300000;
142         
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));
145         
146         CHECK_STRING(tctx, io.out.wins_server, address);
147         CHECK_VALUE(tctx, io.out.rcode, 0);
148
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",
157                         address));
158                 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
159         }
160
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;
169
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));
175                 return true;
176         }
177         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
178         
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");
184         } else {
185                 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
186         }
187
188
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));
195         }
196
197         query.in.name = *name;
198         if (name->scope) {
199                 query.in.name.scope = strupper_talloc(tctx, name->scope);
200         }
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));
206         }
207
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;
215         
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",
220                                         address));
221         }
222         torture_assert_ntstatus_ok(tctx, status,
223                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
224                                 address));
225
226         CHECK_STRING(tctx, refresh.out.wins_server, address);
227         CHECK_VALUE(tctx, refresh.out.rcode, 0);
228
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;
238
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",
243                                         address));
244         }
245         torture_assert_ntstatus_ok(tctx, status,
246                 talloc_asprintf(tctx, "Bad response from %s for name release",
247                                 address));
248
249         CHECK_NAME(tctx, release.out.name, *name);
250         CHECK_VALUE(tctx, release.out.rcode, 0);
251
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");
256         } else {
257                 torture_comment(tctx, "register the name with a wrong address (makes the next request slow!)\n");
258                 io.in.name = *name;
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;
263                 io.in.ttl = 300000;
264         
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",
269                                                 address));
270                 }
271                 torture_assert_ntstatus_ok(tctx, status,
272                         talloc_asprintf(tctx, "Bad response from %s for name register\n",
273                                         address));
274
275                 CHECK_STRING(tctx, io.out.wins_server, address);
276                 CHECK_VALUE(tctx, io.out.rcode, 0);
277         }
278
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;
286
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",
291                                         address));
292         }
293         torture_assert_ntstatus_ok(tctx, status,
294                 talloc_asprintf(tctx, "Bad response from %s for name refresh",
295                                 address));
296
297         CHECK_STRING(tctx, refresh.out.wins_server, address);
298         CHECK_VALUE(tctx, refresh.out.rcode, 0);
299
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;
309
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));
312         
313         CHECK_NAME(tctx, release.out.name, *name);
314         CHECK_VALUE(tctx, release.out.rcode, 0);
315
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",
320                        address));
321         
322         CHECK_NAME(tctx, release.out.name, *name);
323         CHECK_VALUE(tctx, release.out.rcode, 0);
324
325
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");
333         } else {
334                 torture_assert_ntstatus_equal(tctx, status, 
335                                                                           NT_STATUS_OBJECT_NAME_NOT_FOUND,
336                                 "Incorrect response to name query");
337         }
338         
339         return true;
340 }
341
342
343
344 /*
345   test operations against a WINS server
346 */
347 static bool nbt_test_wins(struct torture_context *tctx)
348 {
349         struct nbt_name name;
350         uint32_t r = (uint32_t)(random() % (100000));
351         const char *address;
352         bool ret = true;
353
354         if (!torture_nbt_get_name(tctx, &name, &address))
355                 return false;
356
357         name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
358
359         name.type = NBT_NAME_CLIENT;
360         name.scope = NULL;
361         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
362
363         name.type = NBT_NAME_MASTER;
364         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
365
366         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
367
368         name.type = NBT_NAME_SERVER;
369         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
370
371         name.type = NBT_NAME_LOGON;
372         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
373
374         name.type = NBT_NAME_BROWSER;
375         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
376
377         name.type = NBT_NAME_PDC;
378         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
379
380         name.type = 0xBF;
381         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
382
383         name.type = 0xBE;
384         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
385
386         name.scope = "example";
387         name.type = 0x72;
388         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, true);
389
390         name.scope = "example";
391         name.type = 0x71;
392         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP, false);
393
394         name.scope = "foo.example.com";
395         name.type = 0x72;
396         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
397
398         name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
399         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
400
401         name.name = "";
402         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
403
404         name.name = talloc_asprintf(tctx, ".");
405         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
406
407         name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
408         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H, false);
409
410         return ret;
411 }
412
413 /*
414   test WINS operations
415 */
416 struct torture_suite *torture_nbt_wins(TALLOC_CTX *mem_ctx)
417 {
418         struct torture_suite *suite = torture_suite_create(mem_ctx, "WINS");
419
420         torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
421
422         return suite;
423 }