s3:winbindd: s/struct event_context/struct tevent_context
[Samba/gebeck_regimport.git] / source4 / torture / nbt / register.c
blob24ca328b30fe09e0482a603a75af6f3abbea0f10
1 /*
2 Unix SMB/CIFS implementation.
4 NBT name registration testing
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
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"
31 #define CHECK_VALUE(tctx, v, correct) \
32 torture_assert_int_equal(tctx, v, correct, "Incorrect value")
34 #define CHECK_STRING(tctx, v, correct) \
35 torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
41 test that a server responds correctly to attempted registrations of its name
43 static bool nbt_register_own(struct torture_context *tctx)
45 struct nbt_name_register io;
46 NTSTATUS status;
47 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
48 struct socket_address *socket_address;
49 struct nbt_name name;
50 const char *address;
51 const char *myaddress;
52 struct interface *ifaces;
54 if (!torture_nbt_get_name(tctx, &name, &address))
55 return false;
57 load_interface_list(tctx, tctx->lp_ctx, &ifaces);
59 myaddress = iface_list_best_ip(ifaces, address);
61 socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
62 myaddress, 0);
63 torture_assert(tctx, socket_address != NULL, "Unable to get address");
65 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
66 torture_assert_ntstatus_ok(tctx, status,
67 "socket_listen for nbt_register_own failed");
69 torture_comment(tctx, "Testing name defense to name registration\n");
71 io.in.name = name;
72 io.in.dest_addr = address;
73 io.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
74 io.in.address = myaddress;
75 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
76 io.in.register_demand = false;
77 io.in.broadcast = true;
78 io.in.multi_homed = false;
79 io.in.ttl = 1234;
80 io.in.timeout = 3;
81 io.in.retries = 0;
83 status = nbt_name_register(nbtsock, tctx, &io);
84 torture_assert_ntstatus_ok(tctx, status,
85 talloc_asprintf(tctx, "Bad response from %s for name register",
86 address));
88 CHECK_STRING(tctx, io.out.name.name, name.name);
89 CHECK_VALUE(tctx, io.out.name.type, name.type);
90 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
92 /* check a register demand */
93 io.in.address = myaddress;
94 io.in.register_demand = true;
96 status = nbt_name_register(nbtsock, tctx, &io);
98 torture_assert_ntstatus_ok(tctx, status,
99 talloc_asprintf(tctx, "Bad response from %s for name register demand", address));
101 CHECK_STRING(tctx, io.out.name.name, name.name);
102 CHECK_VALUE(tctx, io.out.name.type, name.type);
103 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
105 return true;
110 test that a server responds correctly to attempted name refresh requests
112 static bool nbt_refresh_own(struct torture_context *tctx)
114 struct nbt_name_refresh io;
115 NTSTATUS status;
116 struct nbt_name_socket *nbtsock = torture_init_nbt_socket(tctx);
117 const char *myaddress;
118 struct socket_address *socket_address;
119 struct nbt_name name;
120 const char *address;
121 struct interface *ifaces;
123 if (!torture_nbt_get_name(tctx, &name, &address))
124 return false;
126 load_interface_list(tctx, tctx->lp_ctx, &ifaces);
128 myaddress = iface_list_best_ip(ifaces, address);
130 socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
131 myaddress, 0);
132 torture_assert(tctx, socket_address != NULL,
133 "Can't parse socket address");
135 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
136 torture_assert_ntstatus_ok(tctx, status,
137 "socket_listen for nbt_referesh_own failed");
139 torture_comment(tctx, "Testing name defense to name refresh\n");
141 io.in.name = name;
142 io.in.dest_addr = address;
143 io.in.dest_port = lpcfg_nbt_port(tctx->lp_ctx);
144 io.in.address = myaddress;
145 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
146 io.in.broadcast = false;
147 io.in.ttl = 1234;
148 io.in.timeout = 3;
149 io.in.retries = 0;
151 status = nbt_name_refresh(nbtsock, tctx, &io);
153 torture_assert_ntstatus_ok(tctx, status,
154 talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
156 CHECK_STRING(tctx, io.out.name.name, name.name);
157 CHECK_VALUE(tctx, io.out.name.type, name.type);
158 CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
160 return true;
165 test name registration to a server
167 struct torture_suite *torture_nbt_register(TALLOC_CTX *mem_ctx)
169 struct torture_suite *suite;
171 suite = torture_suite_create(mem_ctx, "register");
172 torture_suite_add_simple_test(suite, "register_own", nbt_register_own);
173 torture_suite_add_simple_test(suite, "refresh_own", nbt_refresh_own);
175 return suite;