b9f06c479d0d7b6a2840f34623a1313f85b8eaed
[bbaumbach/samba-autobuild/.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 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 "torture/torture.h"
28 #include "torture/nbt/proto.h"
29 #include "param/param.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
38
39
40 /*
41   test that a server responds correctly to attempted registrations of its name
42 */
43 static bool nbt_register_own(struct torture_context *tctx)
44 {
45         struct nbt_name_register io;
46         NTSTATUS status;
47         struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL,
48                                                                lp_iconv_convenience(tctx->lp_ctx));
49         struct socket_address *socket_address;
50         struct nbt_name name;
51         const char *address;
52         const char *myaddress;
53         struct interface *ifaces;
54
55         if (!torture_nbt_get_name(tctx, &name, &address))
56                 return false;
57
58         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
59
60         myaddress = iface_best_ip(ifaces, address);
61
62         socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
63                                                      myaddress, 0);
64         torture_assert(tctx, socket_address != NULL, "Unable to get address");
65
66         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
67         torture_assert_ntstatus_ok(tctx, status, 
68                                 "socket_listen for nbt_register_own failed");
69
70         torture_comment(tctx, "Testing name defense to name registration\n");
71
72         io.in.name = name;
73         io.in.dest_addr = address;
74         io.in.dest_port = lp_nbt_port(tctx->lp_ctx);
75         io.in.address = myaddress;
76         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
77         io.in.register_demand = false;
78         io.in.broadcast = true;
79         io.in.multi_homed = false;
80         io.in.ttl = 1234;
81         io.in.timeout = 3;
82         io.in.retries = 0;
83         
84         status = nbt_name_register(nbtsock, tctx, &io);
85         torture_assert_ntstatus_ok(tctx, status, 
86                                 talloc_asprintf(tctx, "Bad response from %s for name register",
87                        address));
88         
89         CHECK_STRING(tctx, io.out.name.name, name.name);
90         CHECK_VALUE(tctx, io.out.name.type, name.type);
91         CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
92
93         /* check a register demand */
94         io.in.address = myaddress;
95         io.in.register_demand = true;
96
97         status = nbt_name_register(nbtsock, tctx, &io);
98
99         torture_assert_ntstatus_ok(tctx, status, 
100                                 talloc_asprintf(tctx, "Bad response from %s for name register demand", address));
101         
102         CHECK_STRING(tctx, io.out.name.name, name.name);
103         CHECK_VALUE(tctx, io.out.name.type, name.type);
104         CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
105
106         return true;
107 }
108
109
110 /*
111   test that a server responds correctly to attempted name refresh requests
112 */
113 static bool nbt_refresh_own(struct torture_context *tctx)
114 {
115         struct nbt_name_refresh io;
116         NTSTATUS status;
117         struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL,
118                                                                lp_iconv_convenience(tctx->lp_ctx));
119         const char *myaddress;
120         struct socket_address *socket_address;
121         struct nbt_name name;
122         const char *address;
123         struct interface *ifaces;
124
125         if (!torture_nbt_get_name(tctx, &name, &address))
126                 return false;
127         
128         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
129
130         myaddress = iface_best_ip(ifaces, address);
131
132         socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
133                                                      myaddress, 0);
134         torture_assert(tctx, socket_address != NULL, 
135                                    "Can't parse socket address");
136
137         status = socket_listen(nbtsock->sock, socket_address, 0, 0);
138         torture_assert_ntstatus_ok(tctx, status, 
139                                                            "socket_listen for nbt_referesh_own failed");
140
141         torture_comment(tctx, "Testing name defense to name refresh\n");
142
143         io.in.name = name;
144         io.in.dest_addr = address;
145         io.in.dest_port = lp_nbt_port(tctx->lp_ctx);
146         io.in.address = myaddress;
147         io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
148         io.in.broadcast = false;
149         io.in.ttl = 1234;
150         io.in.timeout = 3;
151         io.in.retries = 0;
152         
153         status = nbt_name_refresh(nbtsock, tctx, &io);
154
155         torture_assert_ntstatus_ok(tctx, status, 
156                                 talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
157         
158         CHECK_STRING(tctx, io.out.name.name, name.name);
159         CHECK_VALUE(tctx, io.out.name.type, name.type);
160         CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
161
162         return true;
163 }
164
165
166 /*
167   test name registration to a server
168 */
169 struct torture_suite *torture_nbt_register(TALLOC_CTX *mem_ctx)
170 {
171         struct torture_suite *suite;
172
173         suite = torture_suite_create(mem_ctx, "REGISTER");
174         torture_suite_add_simple_test(suite, "register_own", nbt_register_own);
175         torture_suite_add_simple_test(suite, "refresh_own", nbt_refresh_own);
176
177         return suite;
178 }