66e8a5350f739619fe7f60a25cb4d3f482e1da54
[kai/samba-autobuild/.git] / source4 / torture / local / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_epmapper.h"
25
26 static BOOL test_BindingString(TALLOC_CTX *mem_ctx, const char *binding)
27 {
28         struct dcerpc_binding b, b2;
29         const char *s, *s2;
30         struct epm_tower tower;
31         NTSTATUS status;
32
33         /* Parse */
34         status = dcerpc_parse_binding(mem_ctx, binding, &b);
35         if (NT_STATUS_IS_ERR(status)) {
36                 DEBUG(0, ("Error parsing binding string '%s': %s\n", binding, nt_errstr(status)));
37                 return False;
38         }
39
40         s = dcerpc_binding_string(mem_ctx, &b);
41         if (!s) {
42                 DEBUG(0, ("Error converting binding back to string for '%s'\n", binding)); 
43                 return False;
44         }
45
46         if (strcasecmp(binding, s) != 0) {
47                 DEBUG(0, ("Mismatch while comparing original and regenerated binding strings: '%s' <> '%s'\n", binding, s));
48                 return False;
49         }
50
51         /* Generate protocol towers */
52         status = dcerpc_binding_build_tower(mem_ctx, &b, &tower);
53         if (NT_STATUS_IS_ERR(status)) {
54                 DEBUG(0, ("Error generating protocol tower from '%s': %s\n", binding, nt_errstr(status)));
55                 return False;
56         }
57
58         /* Convert back to binding and then back to string and compare */
59
60         status = dcerpc_binding_from_tower(mem_ctx, &tower, &b2);
61         if (NT_STATUS_IS_ERR(status)) {
62                 DEBUG(0, ("Error generating binding from tower for original binding '%s': %s\n", binding, nt_errstr(status)));
63                 return False;
64         }
65
66         /* Compare to a stripped down version of the binding string because 
67          * the protocol tower doesn't contain the extra option data */
68         b.options = NULL;
69
70         b.flags = 0;
71         
72         s = dcerpc_binding_string(mem_ctx, &b);
73         if (!s) {
74                 DEBUG(0, ("Error converting binding back to string for (stripped down) '%s'\n", binding)); 
75                 return False;
76         }
77
78
79         s2 = dcerpc_binding_string(mem_ctx, &b2);
80         if (!s) {
81                 DEBUG(0, ("Error converting binding back to string for '%s'\n", binding)); 
82                 return False;
83         }
84
85         if (strcasecmp(s, s2) != 0) {
86                 DEBUG(0, ("Mismatch while comparing original and from protocol tower generated binding strings: '%s' <> '%s'\n", s, s2));
87                 return False;
88         }
89
90         return True;
91 }
92
93 static const char *test_strings[] = {
94         "ncacn_np:", 
95         "ncalrpc:", 
96         "ncalrpc:[,Security=Sane]", 
97         "ncacn_np:[rpcecho]",
98         "ncacn_np:127.0.0.1[rpcecho]",
99         "ncacn_ip_tcp:127.0.0.1",
100         "ncacn_ip_tcp:127.0.0.1[20]",
101         "ncacn_ip_tcp:127.0.0.1[20,sign]",
102         "ncacn_ip_tcp:127.0.0.1[20,Security=Foobar,sign]",
103         "ncacn_http:127.0.0.1",
104         "ncacn_http:127.0.0.1[78]",
105         "ncacn_http:127.0.0.1[78,ProxyServer=myproxy:3128]",
106         "ncacn_np:localhost[rpcecho]",
107         "ncacn_np:[/pipe/rpcecho]",
108         "ncacn_np:localhost[/pipe/rpcecho,sign,seal]",
109         "ncacn_np:[,sign]",
110         "ncadg_ip_udp:",
111         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_np:localhost",
112         "308FB580-1EB2-11CA-923B-08002B1075A7@ncacn_ip_tcp:127.0.0.1",
113         "ncacn_unix_stream:[/tmp/epmapper]",
114         "ncalrpc:[IDENTIFIER]",
115         "ncacn_unix_stream:[/tmp/epmapper,sign]",
116 };
117
118 BOOL torture_local_binding_string(void) 
119 {
120         BOOL ret = True;
121         TALLOC_CTX *mem_ctx = talloc_init("test_BindingString");
122         int i;
123
124         for (i = 0; i < ARRAY_SIZE(test_strings); i++) {
125                 ret &= test_BindingString(mem_ctx, test_strings[i]);
126         }
127
128         talloc_free(mem_ctx);
129
130         return ret;
131 }