r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[Samba.git] / source / torture / nbt / register.c
blobf8ae0b261905c966cf7a5da6b27ef87c40a0d1a9
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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"
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 = 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;
53 if (!torture_nbt_get_name(tctx, &name, &address))
54 return false;
56 myaddress = iface_best_ip(address);
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");
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");
66 torture_comment(tctx, "Testing name defense to name registration\n");
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;
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));
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);
88 /* check a register demand */
89 io.in.address = myaddress;
90 io.in.register_demand = True;
92 status = nbt_name_register(nbtsock, tctx, &io);
94 torture_assert_ntstatus_ok(tctx, status,
95 talloc_asprintf(tctx, "Bad response from %s for name register demand", address));
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);
101 return true;
106 test that a server responds correctly to attempted name refresh requests
108 static bool nbt_refresh_own(struct torture_context *tctx)
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;
118 if (!torture_nbt_get_name(tctx, &name, &address))
119 return false;
121 myaddress = iface_best_ip(address);
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");
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");
132 torture_comment(tctx, "Testing name defense to name refresh\n");
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;
143 status = nbt_name_refresh(nbtsock, tctx, &io);
145 torture_assert_ntstatus_ok(tctx, status,
146 talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
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);
152 return true;
157 test name registration to a server
159 struct torture_suite *torture_nbt_register(void)
161 struct torture_suite *suite;
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);
167 return suite;