r14720: Add torture_context argument to all torture tests
[kamenim/samba.git] / source4 / torture / nbt / register.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT name registration 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 "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "netif/netif.h"
28 #include "torture/torture.h"
29
30 #define CHECK_VALUE(v, correct) do { \
31         if ((v) != (correct)) { \
32                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
33                        __location__, #v, (int)v, (int)correct); \
34                 ret = False; \
35         }} while (0)
36
37 #define CHECK_STRING(v, correct) do { \
38         if (strcasecmp_m(v, correct) != 0) { \
39                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
40                        __location__, #v, v, correct); \
41                 ret = False; \
42         }} while (0)
43
44 /*
45   test that a server responds correctly to attempted registrations of its name
46 */
47 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
48                              const char *address)
49 {
50         struct nbt_name_register io;
51         NTSTATUS status;
52         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
53         BOOL ret = True;
54         const char *myaddress = iface_best_ip(address);
55         struct socket_address *socket_address;
56
57         socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
58                                                      myaddress, 0);
59         if (!socket_address) {
60                 return False;
61         }
62
63         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
64         if (!NT_STATUS_IS_OK(status)) {
65                 printf("socket_listen for nbt_register_own failed: %s\n", nt_errstr(status));
66                 return False;
67         }
68
69         printf("Testing name defense to name registration\n");
70
71         io.in.name = *name;
72         io.in.dest_addr = address;
73         io.in.address = myaddress;
74         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
75         io.in.register_demand = False;
76         io.in.broadcast = True;
77         io.in.multi_homed = False;
78         io.in.ttl = 1234;
79         io.in.timeout = 3;
80         io.in.retries = 0;
81         
82         status = nbt_name_register(nbtsock, mem_ctx, &io);
83         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
84                 printf("No response from %s for name register\n", address);
85                 return False;
86         }
87         if (!NT_STATUS_IS_OK(status)) {
88                 printf("Bad response from %s for name register - %s\n",
89                        address, nt_errstr(status));
90                 return False;
91         }
92         
93         CHECK_STRING(io.out.name.name, name->name);
94         CHECK_VALUE(io.out.name.type, name->type);
95         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
96
97         /* check a register demand */
98         io.in.address = myaddress;
99         io.in.register_demand = True;
100
101         status = nbt_name_register(nbtsock, mem_ctx, &io);
102         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
103                 printf("No response from %s for name register demand\n", address);
104                 return False;
105         }
106         if (!NT_STATUS_IS_OK(status)) {
107                 printf("Bad response from %s for name register demand - %s\n",
108                        address, nt_errstr(status));
109                 return False;
110         }
111         
112         CHECK_STRING(io.out.name.name, name->name);
113         CHECK_VALUE(io.out.name.type, name->type);
114         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
115
116         return ret;
117 }
118
119
120 /*
121   test that a server responds correctly to attempted name refresh requests
122 */
123 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
124                             const char *address)
125 {
126         struct nbt_name_refresh io;
127         NTSTATUS status;
128         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
129         BOOL ret = True;
130         const char *myaddress = iface_best_ip(address);
131         struct socket_address *socket_address;
132
133         socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
134                                                      myaddress, 0);
135         if (!socket_address) {
136                 return False;
137         }
138
139         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
140         if (!NT_STATUS_IS_OK(status)) {
141                 printf("socket_listen for nbt_referesh_own failed: %s\n", nt_errstr(status));
142                 return False;
143         }
144
145         printf("Testing name defense to name refresh\n");
146
147         io.in.name = *name;
148         io.in.dest_addr = address;
149         io.in.address = myaddress;
150         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
151         io.in.broadcast = False;
152         io.in.ttl = 1234;
153         io.in.timeout = 3;
154         io.in.retries = 0;
155         
156         status = nbt_name_refresh(nbtsock, mem_ctx, &io);
157         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
158                 printf("No response from %s for name refresh\n", address);
159                 return False;
160         }
161         if (!NT_STATUS_IS_OK(status)) {
162                 printf("Bad response from %s for name refresh - %s\n",
163                        address, nt_errstr(status));
164                 return False;
165         }
166         
167         CHECK_STRING(io.out.name.name, name->name);
168         CHECK_VALUE(io.out.name.type, name->type);
169         CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
170
171         return ret;
172 }
173
174
175
176 /*
177   test name registration to a server
178 */
179 BOOL torture_nbt_register(struct torture_context *torture)
180 {
181         const char *address;
182         struct nbt_name name;
183         TALLOC_CTX *mem_ctx = talloc_new(NULL);
184         NTSTATUS status;
185         BOOL ret = True;
186         
187         make_nbt_name_server(&name, strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host")));
188
189         /* do an initial name resolution to find its IP */
190         status = resolve_name(&name, mem_ctx, &address, NULL);
191         if (!NT_STATUS_IS_OK(status)) {
192                 printf("Failed to resolve %s - %s\n",
193                        name.name, nt_errstr(status));
194                 talloc_free(mem_ctx);
195                 return False;
196         }
197
198         ret &= nbt_register_own(mem_ctx, &name, address);
199         ret &= nbt_refresh_own(mem_ctx, &name, address);
200
201         talloc_free(mem_ctx);
202
203         return ret;
204 }