r24849: Add helper function for running tests as a member server.
[kai/samba.git] / source4 / torture / rpc / countcalls.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    count number of calls on an interface
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/ndr/ndr_table.h"
27 #include "torture/rpc/rpc.h"
28
29
30         
31 BOOL count_calls(TALLOC_CTX *mem_ctx,
32                  const struct ndr_interface_table *iface,
33         BOOL all) 
34 {
35         struct dcerpc_pipe *p;
36         DATA_BLOB stub_in, stub_out;
37         int i;
38         NTSTATUS status = torture_rpc_connection(mem_ctx, &p, iface);
39         if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)
40             || NT_STATUS_EQUAL(NT_STATUS_NET_WRITE_FAULT, status)
41             || NT_STATUS_EQUAL(NT_STATUS_PORT_UNREACHABLE, status)
42             || NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
43                 if (all) {
44                         /* Not fatal if looking for all pipes */
45                         return True;
46                 } else {
47                         printf("Failed to open '%s' to count calls - %s\n", iface->name, nt_errstr(status));
48                         return False;
49                 }
50         } else if (!NT_STATUS_IS_OK(status)) {
51                 printf("Failed to open '%s' to count calls - %s\n", iface->name, nt_errstr(status));
52                 return False;
53         }
54
55         stub_in = data_blob_talloc(p, mem_ctx, 0);
56
57         printf("\nScanning pipe '%s'\n", iface->name);
58
59         for (i=0;i<500;i++) {
60                 status = dcerpc_request(p, NULL, i, False, p, &stub_in, &stub_out);
61                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT) &&
62                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
63                         i--;
64                         break;
65                 }
66                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT) &&
67                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
68                         i--;
69                         break;
70                 }
71                 if (NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_DISCONNECTED)) {
72                         i--;
73                         break;
74                 }
75                 if (NT_STATUS_EQUAL(status, NT_STATUS_PIPE_DISCONNECTED)) {
76                         i--;
77                         break;
78                 }
79                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
80                         i--;
81                         break;
82                 }
83                 if (NT_STATUS_EQUAL(status, NT_STATUS_LOGON_FAILURE)) {
84                         i--;
85                         break;
86                 }
87         }
88         
89         if (i==500) {
90                 talloc_free(p);
91                 printf("no limit on calls: %s!?\n", nt_errstr(status));
92                 return False;
93         }
94
95         printf("Found %d calls\n", i);
96
97         talloc_free(p);
98         
99         return True;
100
101 }
102
103 BOOL torture_rpc_countcalls(struct torture_context *torture)
104 {
105         const struct ndr_interface_table *iface;
106         const char *iface_name;
107         BOOL ret = True;
108         const struct ndr_interface_list *l;
109         TALLOC_CTX *mem_ctx = talloc_named(torture, 0, "torture_rpc_countcalls context");
110         if (!mem_ctx) {
111                 return False;
112         }
113         iface_name = lp_parm_string(-1, "countcalls", "interface");
114         if (iface_name != NULL) {
115                 iface = ndr_table_by_name(iface_name);
116                 if (!iface) {
117                         printf("Unknown interface '%s'\n", iface_name);
118                         return False;
119                 }
120                 return count_calls(mem_ctx, iface, False);
121         }
122
123         for (l=ndr_table_list();l;l=l->next) {          
124                 TALLOC_CTX *loop_ctx;
125                 loop_ctx = talloc_named(mem_ctx, 0, "torture_rpc_councalls loop context");
126                 ret &= count_calls(loop_ctx, l->table, True);
127                 talloc_free(loop_ctx);
128         }
129         return ret;
130 }