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