r24753: Allow host name in binding string without transport.
[metze/samba/wip.git] / source4 / librpc / tests / binding_string.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of RPC binding string parsing 
5
6    Copyright (C) Jelmer Vernooij 2004
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 "librpc/gen_ndr/epmapper.h"
24 #include "librpc/rpc/dcerpc.h"
25 #include "torture/torture.h"
26
27 static bool test_BindingString(struct torture_context *tctx,
28                                                            const void *test_data)
29 {
30         const char *binding = test_data;
31         struct dcerpc_binding *b, *b2;
32         const char *s, *s2;
33         struct epm_tower tower;
34         TALLOC_CTX *mem_ctx = tctx;
35
36         /* Parse */
37         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(mem_ctx, binding, &b),
38                 "Error parsing binding string");
39
40         s = dcerpc_binding_string(mem_ctx, b);
41         torture_assert(tctx, s != NULL, "Error converting binding back to string");
42
43         torture_assert_casestr_equal(tctx, binding, s, 
44                 "Mismatch while comparing original and regenerated binding strings");
45
46         /* Generate protocol towers */
47         torture_assert_ntstatus_ok(tctx, dcerpc_binding_build_tower(mem_ctx, b, &tower),
48                 "Error generating protocol tower");
49
50         /* Convert back to binding and then back to string and compare */
51
52         torture_assert_ntstatus_ok(tctx, dcerpc_binding_from_tower(mem_ctx, &tower, &b2),
53                             "Error generating binding from tower for original binding");
54
55         /* Compare to a stripped down version of the binding string because 
56          * the protocol tower doesn't contain the extra option data */
57         b->options = NULL;
58
59         b->flags = 0;
60         
61         s = dcerpc_binding_string(mem_ctx, b);
62         torture_assert(tctx, s != NULL, "Error converting binding back to string for (stripped down)"); 
63
64         s2 = dcerpc_binding_string(mem_ctx, b2);
65         torture_assert(tctx, s != NULL, "Error converting binding back to string"); 
66
67         if (is_ipaddress(b->host))
68                 torture_assert_casestr_equal(tctx, s, s2, "Mismatch while comparing original and from protocol tower generated binding strings");
69
70         return true;
71 }
72
73 static const char *test_strings[] = {
74         "ncacn_np:", 
75         "ncalrpc:", 
76         "ncalrpc:[,Security=Sane]", 
77         "ncacn_np:[rpcecho]",
78         "ncacn_np:127.0.0.1[rpcecho]",
79         "ncacn_ip_tcp:127.0.0.1",
80         "ncacn_ip_tcp:127.0.0.1[20]",
81         "ncacn_ip_tcp:127.0.0.1[20,sign]",
82         "ncacn_ip_tcp:127.0.0.1[20,Security=Foobar,sign]",
83         "ncacn_http:127.0.0.1",
84         "ncacn_http:127.0.0.1[78]",
85         "ncacn_http:127.0.0.1[78,ProxyServer=myproxy:3128]",
86         "ncacn_np:localhost[rpcecho]",
87         "ncacn_np:[/pipe/rpcecho]",
88         "ncacn_np:localhost[/pipe/rpcecho,sign,seal]",
89         "ncacn_np:[,sign]",
90         "ncadg_ip_udp:",
91         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:localhost",
92         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:127.0.0.1",
93         "ncacn_unix_stream:[/tmp/epmapper]",
94         "ncalrpc:[IDENTIFIER]",
95         "ncacn_unix_stream:[/tmp/epmapper,sign]",
96 };
97
98 static bool test_no_transport(struct torture_context *tctx)
99 {
100         const char *binding = "somehost";
101         struct dcerpc_binding *b;
102         const char *s;
103
104         /* Parse */
105         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, binding, &b),
106                 "Error parsing binding string");
107
108         torture_assert(tctx, b->transport == NCA_UNKNOWN, "invalid transport");
109
110         s = dcerpc_binding_string(tctx, b);
111         torture_assert(tctx, s != NULL, "Error converting binding back to string");
112
113         torture_assert_casestr_equal(tctx, binding, s, 
114                 "Mismatch while comparing original and regenerated binding strings");
115
116         return true;
117 }
118
119 struct torture_suite *torture_local_binding_string(TALLOC_CTX *mem_ctx)
120 {
121         int i;
122         struct torture_suite *suite = torture_suite_create(mem_ctx, 
123                                                                                                            "BINDING");
124
125         for (i = 0; i < ARRAY_SIZE(test_strings); i++) {
126                 torture_suite_add_simple_tcase(suite, test_strings[i],
127                                                 test_BindingString, test_strings[i]);
128         }
129
130         torture_suite_add_simple_test(suite, "no transport", test_no_transport);
131
132         return suite;
133 }