r5928: Use cli_credentials in:
[samba.git] / source4 / torture / com / simple.c
1 /* 
2    Unix SMB/CIFS implementation.
3    run the "simple" example (D)COM 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 "lib/com/com.h"
24 #include "librpc/gen_ndr/com_dcom.h"
25 #include "lib/cmdline/popt_common.h"
26
27 #define DEFAULT_TRANS 4096
28
29 static BOOL test_readwrite(TALLOC_CTX *mem_ctx, const char *host)
30 {
31         struct dcerpc_pipe *p = NULL;
32         BOOL ret = True;
33         struct GUID IID[2];
34         struct GUID clsid;
35         WERROR error;
36         struct IUnknown *interfaces[3];
37         WERROR results[2];
38         struct com_context *ctx;
39         char test_data[5];
40         int i;
41
42         com_init(&ctx);
43         dcom_client_init(ctx, cmdline_credentials);
44
45         GUID_from_string(COM_ISTREAM_UUID, &IID[0]);
46         GUID_from_string(COM_IUNKNOWN_UUID, &IID[1]);
47         GUID_from_string(CLSID_SIMPLE, &clsid);
48
49         if (host) {
50                 error = dcom_create_object(ctx, &clsid, 
51                                                           host, 2, IID,
52                                                           &interfaces, 
53                                                           results);
54         } else {
55                 error = com_create_object(ctx, &clsid, 2, IID, interfaces, results);
56         }
57
58         if (!W_ERROR_IS_OK(error)) {
59                 printf("(d)com_create_object failed - %s\n", win_errstr(error));
60                 return False;
61         }
62         
63         error = IStream_Read((struct IStream *)interfaces[0], mem_ctx, NULL, 20, 20, 30);
64         if (!W_ERROR_IS_OK(error)) {
65                 printf("IStream::Read() failed - %s\n", win_errstr(error));
66                 ret = False;
67         }
68
69         for (i = 0; i < 5; i++) {
70                 test_data[i] = i+1;
71         }
72
73         error = IStream_Write((struct IStream *)interfaces[0], mem_ctx, &test_data, 5, NULL);
74         if (!W_ERROR_IS_OK(error)) {
75                 printf("IStream::Write() failed - %s\n", win_errstr(error));
76                 ret = False;
77         }
78
79         IUnknown_Release((struct IUnknown *)interfaces[1], mem_ctx);
80
81         torture_rpc_close(p);
82
83         return True;
84 }
85
86 BOOL torture_com_simple(void)
87 {
88         BOOL ret = True;
89         TALLOC_CTX *mem_ctx = talloc_init("torture_dcom_simple");
90         const char *host = lp_parm_string(-1, "dcom", "host");
91
92         ret &= test_readwrite(mem_ctx, host);
93
94         talloc_free(mem_ctx);
95
96         return ret;
97 }