r23792: convert Samba4 to GPLv3
[mdw/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
31 #define CHECK_VALUE(tctx, v, correct) \
32         torture_assert_int_equal(tctx, v, correct, "Incorrect value")
33
34 #define CHECK_STRING(tctx, v, correct) \
35         torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
36
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); \
41 } while (0)
42
43
44 /*
45   test operations against a WINS server
46 */
47 static bool nbt_test_wins_name(struct torture_context *tctx, const char *address,
48                                struct nbt_name *name, uint16_t nb_flags)
49 {
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;
54         NTSTATUS status;
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;
58
59         socket_address = socket_address_from_strings(tctx, 
60                                                      nbtsock->sock->backend_name,
61                                                      myaddress, 0);
62         torture_assert(tctx, socket_address != NULL, 
63                                    "Error getting address");
64
65         /* we do the listen here to ensure the WINS server receives the packets from
66            the right IP */
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);
71
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);
74
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;
83
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);
87
88         torture_comment(tctx, "register the name\n");
89         io.in.name = *name;
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;
93         io.in.ttl = 300000;
94         
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));
97         
98         CHECK_STRING(tctx, io.out.wins_server, address);
99         CHECK_VALUE(tctx, io.out.rcode, 0);
100
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",
109                         address));
110                 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
111         }
112
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;
120
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));
126                 return true;
127         }
128         torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx, "Bad response from %s for name query", address));
129         
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");
135         } else {
136                 CHECK_STRING(tctx, query.out.reply_addrs[0], myaddress);
137         }
138
139
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));
146         }
147
148         query.in.name = *name;
149         if (name->scope) {
150                 query.in.name.scope = strupper_talloc(tctx, name->scope);
151         }
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));
157         }
158
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;
165         
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));
168         
169         CHECK_STRING(tctx, refresh.out.wins_server, address);
170         CHECK_VALUE(tctx, refresh.out.rcode, 0);
171
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;
180
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));
183         
184         CHECK_NAME(tctx, release.out.name, *name);
185         CHECK_VALUE(tctx, release.out.rcode, 0);
186
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",
191                        address));
192         
193         CHECK_NAME(tctx, release.out.name, *name);
194         CHECK_VALUE(tctx, release.out.rcode, 0);
195
196
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");
204         } else {
205                 torture_assert_ntstatus_equal(tctx, status, 
206                                                                           NT_STATUS_OBJECT_NAME_NOT_FOUND,
207                                 "Incorrect response to name query");
208         }
209         
210         return true;
211 }
212
213
214
215 /*
216   test operations against a WINS server
217 */
218 static bool nbt_test_wins(struct torture_context *tctx)
219 {
220         struct nbt_name name;
221         uint32_t r = (uint32_t)(random() % (100000));
222         const char *address;
223         bool ret = true;
224
225         if (!torture_nbt_get_name(tctx, &name, &address))
226                 return false;
227
228         name.name = talloc_asprintf(tctx, "_TORTURE-%5u", r);
229
230         name.type = NBT_NAME_CLIENT;
231         name.scope = NULL;
232         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
233
234         name.type = NBT_NAME_MASTER;
235         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
236
237         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
238
239         name.type = NBT_NAME_SERVER;
240         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
241
242         name.type = NBT_NAME_LOGON;
243         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
244
245         name.type = NBT_NAME_BROWSER;
246         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
247
248         name.type = NBT_NAME_PDC;
249         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
250
251         name.type = 0xBF;
252         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
253
254         name.type = 0xBE;
255         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
256
257         name.scope = "example";
258         name.type = 0x72;
259         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
260
261         name.scope = "example";
262         name.type = 0x71;
263         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
264
265         name.scope = "foo.example.com";
266         name.type = 0x72;
267         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
268
269         name.name = talloc_asprintf(tctx, "_T\01-%5u.foo", r);
270         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
271
272         name.name = "";
273         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
274
275         name.name = talloc_asprintf(tctx, ".");
276         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
277
278         name.name = talloc_asprintf(tctx, "%5u-\377\200\300FOO", r);
279         ret &= nbt_test_wins_name(tctx, address, &name, NBT_NODE_H);
280
281         return ret;
282 }
283
284 /*
285   test WINS operations
286 */
287 struct torture_suite *torture_nbt_wins(void)
288 {
289         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), 
290                                                                                                            "WINS");
291
292         torture_suite_add_simple_test(suite, "wins", nbt_test_wins);
293
294         return suite;
295 }