r5325: - expanded the NBT-WINS test to include scopes
[jra/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26 #include "lib/socket/socket.h"
27
28 #define CHECK_VALUE(v, correct) do { \
29         if ((v) != (correct)) { \
30                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
31                        __location__, #v, v, correct); \
32                 ret = False; \
33         }} while (0)
34
35 #define CHECK_STRING(v, correct) do { \
36         if ((v) != (correct) && \
37             ((v)==NULL || (correct)==NULL || StrCaseCmp(v, correct) != 0)) { \
38                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
39                        __location__, #v, v, correct); \
40                 ret = False; \
41         }} while (0)
42
43 #define CHECK_NAME(_name, correct) do { \
44         CHECK_STRING((_name).name, (correct).name); \
45         CHECK_VALUE((_name).type, (correct).type); \
46         CHECK_STRING((_name).scope, (correct).scope); \
47 } while (0)
48
49 /*
50   test operations against a WINS server
51 */
52 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
53                                struct nbt_name *name)
54 {
55         struct nbt_name_register_wins io;
56         struct nbt_name_query query;
57         struct nbt_name_refresh_wins refresh;
58         struct nbt_name_release release;
59         NTSTATUS status;
60         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
61         BOOL ret = True;
62         const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
63
64         /* we do the listen here to ensure the WINS server receives the packets from
65            the right IP */
66         socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
67
68         printf("Testing name registration to WINS with name %s<%02x> at %s\n", 
69                name->name, name->type, myaddress);
70         if (name->scope) {
71                 printf("scope is %s\n", name->scope);
72         }
73
74         io.in.name = *name;
75         io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
76         io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
77         io.in.nb_flags = NBT_NODE_H;
78         io.in.ttl = 300000;
79         
80         status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
81         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
82                 printf("No response from %s for name register\n", address);
83                 return False;
84         }
85         if (!NT_STATUS_IS_OK(status)) {
86                 printf("Bad response from %s for name register - %s\n",
87                        address, nt_errstr(status));
88                 return False;
89         }
90         
91         CHECK_STRING(io.out.wins_server, address);
92         CHECK_VALUE(io.out.rcode, 0);
93
94         printf("query the name to make sure its there\n");
95         query.in.name = *name;
96         query.in.dest_addr = address;
97         query.in.broadcast = False;
98         query.in.wins_lookup = True;
99         query.in.timeout = 3;
100         query.in.retries = 0;
101
102         status = nbt_name_query(nbtsock, mem_ctx, &query);
103         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
104                 printf("No response from %s for name query\n", address);
105                 return False;
106         }
107         if (!NT_STATUS_IS_OK(status)) {
108                 printf("Bad response from %s for name query - %s\n",
109                        address, nt_errstr(status));
110                 return False;
111         }
112         
113         CHECK_NAME(query.out.name, *name);
114         CHECK_VALUE(query.out.num_addrs, 1);
115         CHECK_STRING(query.out.reply_addrs[0], myaddress);
116
117         printf("refresh the name\n");
118         refresh.in.name = *name;
119         refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
120         refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
121         refresh.in.nb_flags = NBT_NODE_H;
122         refresh.in.ttl = 12345;
123         
124         status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
125         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
126                 printf("No response from %s for name refresh\n", address);
127                 return False;
128         }
129         if (!NT_STATUS_IS_OK(status)) {
130                 printf("Bad response from %s for name refresh - %s\n",
131                        address, nt_errstr(status));
132                 return False;
133         }
134         
135         CHECK_STRING(io.out.wins_server, address);
136         CHECK_VALUE(io.out.rcode, 0);
137
138         printf("release the name\n");
139         release.in.name = *name;
140         release.in.dest_addr = address;
141         release.in.address = myaddress;
142         release.in.nb_flags = NBT_NODE_H;
143         release.in.broadcast = False;
144         release.in.timeout = 3;
145         release.in.retries = 0;
146
147         status = nbt_name_release(nbtsock, mem_ctx, &release);
148         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
149                 printf("No response from %s for name release\n", address);
150                 return False;
151         }
152         if (!NT_STATUS_IS_OK(status)) {
153                 printf("Bad response from %s for name query - %s\n",
154                        address, nt_errstr(status));
155                 return False;
156         }
157         
158         CHECK_NAME(release.out.name, *name);
159         CHECK_VALUE(release.out.rcode, 0);
160
161
162         printf("release again\n");
163         status = nbt_name_release(nbtsock, mem_ctx, &release);
164         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165                 printf("No response from %s for name release\n", address);
166                 return False;
167         }
168         if (!NT_STATUS_IS_OK(status)) {
169                 printf("Bad response from %s for name query - %s\n",
170                        address, nt_errstr(status));
171                 return False;
172         }
173         
174         CHECK_NAME(release.out.name, *name);
175         CHECK_VALUE(release.out.rcode, 0);
176
177
178         printf("query the name to make sure its gone\n");
179         status = nbt_name_query(nbtsock, mem_ctx, &query);
180         if (NT_STATUS_IS_OK(status)) {
181                 printf("ERROR: Name query success after release\n");
182                 return False;
183         }
184         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
185                 printf("Incorrect response to name query - %s\n", nt_errstr(status));
186                 return False;
187         }
188         
189         return ret;
190 }
191
192
193
194 /*
195   test operations against a WINS server
196 */
197 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
198 {
199         struct nbt_name name;
200         BOOL ret = True;
201         uint32_t r = (unsigned)(random() % (100000));
202
203         name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
204                                     
205         name.type = NBT_NAME_CLIENT;
206         name.scope = NULL;
207         ret &= nbt_test_wins_name(mem_ctx, address, &name);
208
209         name.scope = "example";
210         name.type = 0x71;
211         ret &= nbt_test_wins_name(mem_ctx, address, &name);
212
213         name.scope = "foo.example.com";
214         ret &= nbt_test_wins_name(mem_ctx, address, &name);
215
216         name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
217         ret &= nbt_test_wins_name(mem_ctx, address, &name);
218
219         name.name = "";
220         ret &= nbt_test_wins_name(mem_ctx, address, &name);
221
222         name.name = talloc_asprintf(mem_ctx, ".");
223         ret &= nbt_test_wins_name(mem_ctx, address, &name);
224
225         return ret;
226 }
227
228 /*
229   test WINS operations
230 */
231 BOOL torture_nbt_wins(void)
232 {
233         const char *address;
234         struct nbt_name name;
235         TALLOC_CTX *mem_ctx = talloc_new(NULL);
236         NTSTATUS status;
237         BOOL ret = True;
238         
239         name.name = lp_parm_string(-1, "torture", "host");
240         name.type = NBT_NAME_SERVER;
241         name.scope = NULL;
242
243         /* do an initial name resolution to find its IP */
244         status = resolve_name(&name, mem_ctx, &address);
245         if (!NT_STATUS_IS_OK(status)) {
246                 printf("Failed to resolve %s - %s\n",
247                        name.name, nt_errstr(status));
248                 talloc_free(mem_ctx);
249                 return False;
250         }
251
252         ret &= nbt_test_wins(mem_ctx, address);
253
254         talloc_free(mem_ctx);
255
256         return ret;
257 }