Remove trailing whitespace.
[Samba.git] / source4 / librpc / tests / binding_string.c
blobb2866a4d89d066a8164147f2b877b11a26f755dd
1 /*
2 Unix SMB/CIFS implementation.
4 local testing of RPC binding string parsing
6 Copyright (C) Jelmer Vernooij 2004
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 "librpc/gen_ndr/epmapper.h"
24 #include "librpc/rpc/dcerpc.h"
25 #include "librpc/rpc/dcerpc_proto.h"
26 #include "torture/torture.h"
27 #include "torture/local/proto.h"
28 #include "lib/util/util_net.h"
30 static bool test_BindingString(struct torture_context *tctx,
31 const void *test_data)
33 const char *binding = test_data;
34 struct dcerpc_binding *b, *b2;
35 char *s, *s2, *p;
36 struct epm_tower tower;
37 TALLOC_CTX *mem_ctx = tctx;
38 const char *host;
39 struct GUID object;
41 /* Parse */
42 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(mem_ctx, binding, &b),
43 "Error parsing binding string");
45 object = dcerpc_binding_get_object(b);
47 s = dcerpc_binding_string(mem_ctx, b);
48 torture_assert(tctx, s != NULL, "Error converting binding back to string");
50 torture_assert_casestr_equal(tctx, binding, s,
51 "Mismatch while comparing original and regenerated binding strings");
53 /* Generate protocol towers */
54 torture_assert_ntstatus_ok(tctx, dcerpc_binding_build_tower(mem_ctx, b, &tower),
55 "Error generating protocol tower");
57 /* Convert back to binding and then back to string and compare */
59 torture_assert_ntstatus_ok(tctx, dcerpc_binding_from_tower(mem_ctx, &tower, &b2),
60 "Error generating binding from tower for original binding");
62 /* The tower doesn't contain the object */
63 torture_assert_ntstatus_ok(tctx, dcerpc_binding_set_object(b2, object),
64 "set object on tower binding");
66 s = dcerpc_binding_string(mem_ctx, b);
67 torture_assert(tctx, s != NULL, "Error converting binding back to string for (stripped down)");
70 * Compare to a stripped down version of the binding string because
71 * the protocol tower doesn't contain the extra option data
73 * We remove all options except of the endpoint.
75 p = strchr(s, '[');
76 if (p != NULL) {
77 char *p2;
79 p2 = strchr(p + 1, ',');
80 if (p2 != NULL) {
82 * We only look at the first option,
83 * which might be the endpoint.
85 p2[0] = ']';
86 p2[1] = '\0';
89 p2 = strchr(p + 1, '=');
90 if (p2 != NULL) {
92 * It's not the endpoint, so remove the
93 * whole option section.
95 *p = '\0';
99 s2 = dcerpc_binding_string(mem_ctx, b2);
100 torture_assert(tctx, s != NULL, "Error converting binding back to string");
102 host = dcerpc_binding_get_string_option(b, "host");
103 if (host && is_ipaddress_v4(host)) {
104 torture_assert_casestr_equal(tctx, s, s2, "Mismatch while comparing original and from protocol tower generated binding strings");
107 return true;
110 static const char *test_strings[] = {
111 "ncacn_np:",
112 "ncalrpc:",
113 "ncalrpc:[,Security=Sane]",
114 "ncacn_np:[rpcecho]",
115 "ncacn_np:127.0.0.1[rpcecho]",
116 "ncacn_ip_tcp:127.0.0.1",
117 "ncacn_ip_tcp:127.0.0.1[20]",
118 "ncacn_ip_tcp:127.0.0.1[20,sign]",
119 "ncacn_ip_tcp:127.0.0.1[20,sign,Security=Foobar]",
120 "ncacn_http:127.0.0.1",
121 "ncacn_http:127.0.0.1[78]",
122 "ncacn_http:127.0.0.1[78,ProxyServer=myproxy:3128]",
123 "ncacn_np:localhost[rpcecho]",
124 "ncacn_np:[/pipe/rpcecho]",
125 "ncacn_np:localhost[/pipe/rpcecho,sign,seal]",
126 "ncacn_np:[,sign]",
127 "ncadg_ip_udp:",
128 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:localhost",
129 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:127.0.0.1",
130 "ncacn_unix_stream:[/tmp/epmapper]",
131 "ncalrpc:[IDENTIFIER]",
132 "ncacn_unix_stream:[/tmp/epmapper,sign]",
133 "ncacn_ip_tcp:127.0.0.1[75,target_hostname=port75.example.com,target_principal=host/port75.example.com]",
134 "ncacn_ip_tcp:127.0.0.1[75,connect,target_hostname=port75.example.com,target_principal=host/port75.example.com,assoc_group_id=0x01234567]",
135 "ncacn_ip_tcp:::",
136 "ncacn_ip_tcp:::[75]",
137 "ncacn_ip_tcp:FD00::5357:5F00",
138 "ncacn_ip_tcp:FD00::5357:5F00[75]",
139 "ncacn_ip_tcp:FD00::5357:5F00[,target_hostname=port75.example.com]",
140 "ncacn_ip_tcp:FD00::5357:5F00[75,target_hostname=port75.example.com]",
141 "ncacn_ip_tcp:fe80::5357:5F00%75",
142 "ncacn_ip_tcp:fe80::5357:5F00%75[75]",
143 "ncacn_ip_tcp:fe80::5357:5F00%75[,target_hostname=port75.example.com]",
144 "ncacn_ip_tcp:fe80::5357:5F00%75[75,target_hostname=port75.example.com]",
147 static bool test_parse_check_results(struct torture_context *tctx)
149 struct dcerpc_binding *b;
150 struct GUID uuid;
151 struct GUID object;
152 struct ndr_syntax_id abstract;
153 enum dcerpc_transport_t transport;
154 const char *endpoint;
155 uint32_t flags;
157 torture_assert_ntstatus_ok(tctx,
158 GUID_from_string("308FB580-1EB2-11CA-923B-08002B1075A7", &uuid),
159 "parsing uuid");
161 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER", &b), "parse");
162 transport = dcerpc_binding_get_transport(b);
163 torture_assert(tctx, transport == NCACN_NP, "ncacn_np expected");
164 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER", &b), "parse");
165 transport = dcerpc_binding_get_transport(b);
166 torture_assert(tctx, transport == NCACN_IP_TCP, "ncacn_ip_tcp expected");
167 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[rpcecho]", &b), "parse");
168 endpoint = dcerpc_binding_get_string_option(b, "endpoint");
169 torture_assert_str_equal(tctx, endpoint, "rpcecho", "endpoint");
170 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho]", &b), "parse");
171 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho,sign,seal]", &b), "parse");
172 flags = dcerpc_binding_get_flags(b);
173 torture_assert(tctx, flags == DCERPC_SIGN+DCERPC_SEAL, "sign+seal flags");
174 endpoint = dcerpc_binding_get_string_option(b, "endpoint");
175 torture_assert_str_equal(tctx, endpoint, "/pipe/rpcecho", "endpoint");
176 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[,sign]", &b), "parse");
177 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER[,sign]", &b), "parse");
178 endpoint = dcerpc_binding_get_string_option(b, "endpoint");
179 torture_assert(tctx, endpoint == NULL, "endpoint");
180 flags = dcerpc_binding_get_flags(b);
181 torture_assert(tctx, flags == DCERPC_SIGN, "sign flag");
182 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncalrpc:", &b), "parse");
183 transport = dcerpc_binding_get_transport(b);
184 torture_assert(tctx, transport == NCALRPC, "ncalrpc expected");
185 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
186 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:$SERVER", &b), "parse");
187 object = dcerpc_binding_get_object(b);
188 abstract = dcerpc_binding_get_abstract_syntax(b);
189 torture_assert(tctx, GUID_equal(&object, &uuid), "object uuid");
190 torture_assert(tctx, ndr_syntax_id_equal(&abstract, &ndr_syntax_id_null),
191 "null abstract syntax");
192 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
193 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:$SERVER", &b), "parse");
194 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER[,sign,localaddress=192.168.1.1]", &b), "parse");
195 transport = dcerpc_binding_get_transport(b);
196 torture_assert(tctx, transport == NCACN_IP_TCP, "ncacn_ip_tcp expected");
197 flags = dcerpc_binding_get_flags(b);
198 torture_assert(tctx, flags == DCERPC_SIGN, "sign flag");
199 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "localaddress"),
200 "192.168.1.1", "localaddress");
201 torture_assert_str_equal(tctx, "ncacn_ip_tcp:$SERVER[,sign,localaddress=192.168.1.1]",
202 dcerpc_binding_string(tctx, b), "back to string");
203 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
204 "$SERVER", "host");
205 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
206 "$SERVER", "target_hostname");
208 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
209 "ncacn_ip_tcp:$HOST[,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL]",
210 &b), "parse");
211 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
212 "$HOST", "host");
213 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
214 "$HOSTNAME", "target_hostname");
215 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_principal"),
216 "$PRINCIPAL", "target_principal");
217 torture_assert_str_equal(tctx,
218 dcerpc_binding_string(tctx, b),
219 "ncacn_ip_tcp:$HOST[,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL]",
220 "back to string");
222 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx,
223 "ncacn_ip_tcp:$HOST[,connect,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
224 &b), "parse");
225 flags = dcerpc_binding_get_flags(b);
226 torture_assert(tctx, flags == DCERPC_CONNECT, "connect flag");
227 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "host"),
228 "$HOST", "host");
229 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_hostname"),
230 "$HOSTNAME", "target_hostname");
231 torture_assert_str_equal(tctx, dcerpc_binding_get_string_option(b, "target_principal"),
232 "$PRINCIPAL", "target_principal");
233 torture_assert_int_equal(tctx, dcerpc_binding_get_assoc_group_id(b), 0x01234567,
234 "assoc_group_id");
235 torture_assert_str_equal(tctx,
236 dcerpc_binding_string(tctx, b),
237 "ncacn_ip_tcp:$HOST[,connect,target_hostname=$HOSTNAME,target_principal=$PRINCIPAL,assoc_group_id=0x01234567]",
238 "back to string");
240 return true;
243 static bool test_no_transport(struct torture_context *tctx, const void *test_data)
245 const char *binding = test_data;
246 struct dcerpc_binding *b;
247 enum dcerpc_transport_t transport;
248 const char *s;
250 /* Parse */
251 torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, binding, &b),
252 "Error parsing binding string");
254 transport = dcerpc_binding_get_transport(b);
255 torture_assert(tctx, transport == NCA_UNKNOWN, "invalid transport");
257 s = dcerpc_binding_string(tctx, b);
258 torture_assert(tctx, s != NULL, "Error converting binding back to string");
260 torture_assert_casestr_equal(tctx, binding, s,
261 "Mismatch while comparing original and regenerated binding strings");
263 return true;
266 static const char *test_no_strings[] = {
267 "port75.example.com",
268 "port75.example.com[75]",
269 "127.0.0.1",
270 "127.0.0.1[75]",
271 "127.0.0.1[,target_hostname=port75.example.com]",
272 "127.0.0.1[75,target_hostname=port75.example.com]",
273 "::",
274 "::[75]",
275 "::[,target_hostname=port75.example.com]",
276 "::[75,target_hostname=port75.example.com]",
277 "FD00::5357:5F00",
278 "FD00::5357:5F00[75]",
279 "FD00::5357:5F00[,target_hostname=port75.example.com]",
280 "FD00::5357:5F00[75,target_hostname=port75.example.com]",
281 "fe80::5357:5F00%75",
282 "fe80::5357:5F00%75[75]",
283 "fe80::5357:5F00%75[,target_hostname=port75.example.com]",
284 "fe80::5357:5F00%75[75,target_hostname=port75.example.com]",
287 struct torture_suite *torture_local_binding_string(TALLOC_CTX *mem_ctx)
289 int i;
290 struct torture_suite *suite = torture_suite_create(mem_ctx, "binding");
292 for (i = 0; i < ARRAY_SIZE(test_strings); i++) {
293 torture_suite_add_simple_tcase_const(suite, test_strings[i],
294 test_BindingString,
295 test_strings[i]);
298 for (i = 0; i < ARRAY_SIZE(test_no_strings); i++) {
299 torture_suite_add_simple_tcase_const(suite, test_no_strings[i],
300 test_no_transport,
301 test_no_strings[i]);
304 torture_suite_add_simple_test(suite, "parsing results",
305 test_parse_check_results);
307 return suite;