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