102a3f0dab360028a9f805d3b8e845076c552d79
[abartlet/samba.git/.git] / source4 / torture / dcom / simple.c
1 /* 
2    Unix SMB/CIFS implementation.
3    run the "simple" example DCOM program 
4
5    Copyright (C) Jelmer Vernooij 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_dcom.h"
24 #include "librpc/gen_ndr/ndr_oxidresolver.h"
25
26 #define CLSID_SIMPLE "5e9ddec7-5767-11cf-beab-00aa006c3606"
27 #define DEFAULT_TRANS 4096
28
29 static BOOL test_readwrite(TALLOC_CTX *mem_ctx, const char *host)
30 {
31         NTSTATUS status;
32         struct dcerpc_pipe *p = NULL;
33         BOOL ret = True;
34         struct GUID IID[2];
35         struct GUID clsid;
36         WERROR error;
37         struct dcom_interface_p **interfaces;
38         struct Read r_read;
39         struct Write r_write;
40         WERROR results[2];
41         struct dcom_context *ctx;
42         char test_data[5];
43         int i;
44         extern NTSTATUS dcom_IUnknown_init(void);
45         extern NTSTATUS dcom_IStream_init(void);
46
47         torture_dcom_init(&ctx);
48
49         GUID_from_string(DCERPC_ISTREAM_UUID, &IID[0]);
50         GUID_from_string(DCERPC_IUNKNOWN_UUID, &IID[1]);
51         GUID_from_string(CLSID_SIMPLE, &clsid);
52         error = dcom_create_object(ctx, &clsid, 
53                                                           host, 2, IID,
54                                                           &interfaces, 
55                                                           results);
56
57         if (!W_ERROR_IS_OK(error)) {
58                 printf("dcom_create_object failed - %s\n", win_errstr(error));
59                 return False;
60         }
61
62         if (!W_ERROR_IS_OK(results[0])) {
63                 printf("dcom_create_object didn't return IStream interface - %s\n", win_errstr(results[0]));
64                 return False;
65         }
66         
67         ZERO_STRUCT(r_read);
68         r_read.in.num_requested = 20; /* Give me 20 0xFF bytes... */
69         status = dcom_IStream_Read(interfaces[0], mem_ctx, &r_read);
70         if (NT_STATUS_IS_ERR(status)) {
71                 printf("IStream::Read() failed - %s\n", nt_errstr(status));
72                 ret = False;
73         } else if (!W_ERROR_IS_OK(r_read.out.result)) {
74                 printf("IStream::Read() failed - %s\n", win_errstr(r_read.out.result));
75                 ret = False;
76         }
77
78         for (i = 0; i < 5; i++) {
79                 test_data[i] = i+1;
80         }
81         r_write.in.num_requested = 5;
82         r_write.in.data = (uint8_t *)&test_data;
83         status = dcom_IStream_Write(interfaces[0], mem_ctx, &r_write);
84         if (NT_STATUS_IS_ERR(status)) {
85                 printf("IStream::Write() failed - %s\n", nt_errstr(status));
86                 ret = False;
87         } else if (!W_ERROR_IS_OK(r_write.out.result)) {
88                 printf("IStream::Write() failed - %s\n", win_errstr(r_write.out.result));
89                 ret = False;
90         }
91
92         status = dcom_IUnknown_Release(interfaces[1], mem_ctx, NULL);
93         if (NT_STATUS_IS_ERR(status)) {
94                 printf("IUnknown::Release() failed - %s\n", nt_errstr(status));
95                 return False;
96         }
97
98         talloc_destroy(mem_ctx);
99
100         torture_rpc_close(p);
101
102         return True;
103 }
104
105 BOOL torture_dcom_simple(void)
106 {
107         BOOL ret = True;
108         TALLOC_CTX *mem_ctx = talloc_init("torture_dcom_simple");
109
110         ret &= test_readwrite(mem_ctx, NULL);
111         ret &= test_readwrite(mem_ctx, lp_parm_string(-1, "torture", "dcomhost"));
112
113         return ret;
114 }