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