01cdfae80d0e6ecd7b170212b1f20bb4cb4315bc
[samba.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 "librpc/rpc/dcerpc_proto.h"
26 #include "torture/torture.h"
27
28 static bool test_BindingString(struct torture_context *tctx,
29                                                            const void *test_data)
30 {
31         const char *binding = test_data;
32         struct dcerpc_binding *b, *b2;
33         const char *s, *s2;
34         struct epm_tower tower;
35         TALLOC_CTX *mem_ctx = tctx;
36
37         /* Parse */
38         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(mem_ctx, binding, &b),
39                 "Error parsing binding string");
40
41         s = dcerpc_binding_string(mem_ctx, b);
42         torture_assert(tctx, s != NULL, "Error converting binding back to string");
43
44         torture_assert_casestr_equal(tctx, binding, s, 
45                 "Mismatch while comparing original and regenerated binding strings");
46
47         /* Generate protocol towers */
48         torture_assert_ntstatus_ok(tctx, dcerpc_binding_build_tower(mem_ctx, b, &tower),
49                 "Error generating protocol tower");
50
51         /* Convert back to binding and then back to string and compare */
52
53         torture_assert_ntstatus_ok(tctx, dcerpc_binding_from_tower(mem_ctx, &tower, &b2),
54                             "Error generating binding from tower for original binding");
55
56         /* Compare to a stripped down version of the binding string because 
57          * the protocol tower doesn't contain the extra option data */
58         b->options = NULL;
59
60         b->flags = 0;
61         
62         s = dcerpc_binding_string(mem_ctx, b);
63         torture_assert(tctx, s != NULL, "Error converting binding back to string for (stripped down)"); 
64
65         s2 = dcerpc_binding_string(mem_ctx, b2);
66         torture_assert(tctx, s != NULL, "Error converting binding back to string"); 
67
68         if (is_ipaddress(b->host))
69                 torture_assert_casestr_equal(tctx, s, s2, "Mismatch while comparing original and from protocol tower generated binding strings");
70
71         return true;
72 }
73
74 static const char *test_strings[] = {
75         "ncacn_np:", 
76         "ncalrpc:", 
77         "ncalrpc:[,Security=Sane]", 
78         "ncacn_np:[rpcecho]",
79         "ncacn_np:127.0.0.1[rpcecho]",
80         "ncacn_ip_tcp:127.0.0.1",
81         "ncacn_ip_tcp:127.0.0.1[20]",
82         "ncacn_ip_tcp:127.0.0.1[20,sign]",
83         "ncacn_ip_tcp:127.0.0.1[20,Security=Foobar,sign]",
84         "ncacn_http:127.0.0.1",
85         "ncacn_http:127.0.0.1[78]",
86         "ncacn_http:127.0.0.1[78,ProxyServer=myproxy:3128]",
87         "ncacn_np:localhost[rpcecho]",
88         "ncacn_np:[/pipe/rpcecho]",
89         "ncacn_np:localhost[/pipe/rpcecho,sign,seal]",
90         "ncacn_np:[,sign]",
91         "ncadg_ip_udp:",
92         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:localhost",
93         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:127.0.0.1",
94         "ncacn_unix_stream:[/tmp/epmapper]",
95         "ncalrpc:[IDENTIFIER]",
96         "ncacn_unix_stream:[/tmp/epmapper,sign]",
97 };
98
99 static bool test_parse_check_results(struct torture_context *tctx)
100 {
101         struct dcerpc_binding *b;
102         struct GUID uuid;
103
104         torture_assert_ntstatus_ok(tctx, 
105                                    GUID_from_string("308FB580-1EB2-11CA-923B-08002B1075A7", &uuid),
106                                    "parsing uuid");
107
108         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER", &b), "parse");
109         torture_assert(tctx, b->transport == NCACN_NP, "ncacn_np expected");
110         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER", &b), "parse");
111         torture_assert(tctx, b->transport == NCACN_IP_TCP, "ncacn_ip_tcp expected");
112         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[rpcecho]", &b), "parse");
113         torture_assert_str_equal(tctx, b->endpoint, "rpcecho", "endpoint");
114         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho]", &b), "parse");
115         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[/pipe/rpcecho,sign,seal]", &b), "parse");
116         torture_assert(tctx, b->flags == DCERPC_SIGN+DCERPC_SEAL, "sign+seal flags");
117         torture_assert_str_equal(tctx, b->endpoint, "/pipe/rpcecho", "endpoint");
118         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_np:$SERVER[,sign]", &b), "parse");
119         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncacn_ip_tcp:$SERVER[,sign]", &b), "parse");
120         torture_assert(tctx, b->endpoint == NULL, "endpoint");
121         torture_assert(tctx, b->flags == DCERPC_SIGN, "sign flag");
122         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, "ncalrpc:", &b), "parse");
123         torture_assert(tctx, b->transport == NCALRPC, "ncalrpc expected");
124         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, 
125                 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:$SERVER", &b), "parse");
126         torture_assert(tctx, GUID_equal(&b->object.uuid, &uuid), "object uuid");
127         torture_assert_int_equal(tctx, b->object.if_version, 0, "object version");
128         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, 
129                 "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:$SERVER", &b), "parse");
130
131         return true;
132 }
133
134 static bool test_no_transport(struct torture_context *tctx)
135 {
136         const char *binding = "somehost";
137         struct dcerpc_binding *b;
138         const char *s;
139
140         /* Parse */
141         torture_assert_ntstatus_ok(tctx, dcerpc_parse_binding(tctx, binding, &b),
142                 "Error parsing binding string");
143
144         torture_assert(tctx, b->transport == NCA_UNKNOWN, "invalid transport");
145
146         s = dcerpc_binding_string(tctx, b);
147         torture_assert(tctx, s != NULL, "Error converting binding back to string");
148
149         torture_assert_casestr_equal(tctx, binding, s, 
150                 "Mismatch while comparing original and regenerated binding strings");
151
152         return true;
153 }
154
155 struct torture_suite *torture_local_binding_string(TALLOC_CTX *mem_ctx)
156 {
157         int i;
158         struct torture_suite *suite = torture_suite_create(mem_ctx, "BINDING");
159
160         for (i = 0; i < ARRAY_SIZE(test_strings); i++) {
161                 torture_suite_add_simple_tcase_const(suite, test_strings[i],
162                                                 test_BindingString,
163                                                 test_strings[i]);
164         }
165
166         torture_suite_add_simple_test(suite, "no transport",test_no_transport);
167
168         torture_suite_add_simple_test(suite, "parsing results",
169                         test_parse_check_results);
170
171         return suite;
172 }