r25035: Fix some more warnings, use service pointer rather than service number in...
[vlendec/samba-autobuild/.git] / source4 / torture / rpc / svcctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for srvsvc rpc operations
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "librpc/gen_ndr/ndr_svcctl_c.h"
24 #include "torture/rpc/rpc.h"
25
26 static bool test_OpenSCManager(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
27 {
28         struct svcctl_OpenSCManagerW r;
29         
30         r.in.MachineName = NULL;
31         r.in.DatabaseName = NULL;
32         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
33         r.out.handle = h;
34         
35         torture_assert_ntstatus_ok(tctx, 
36                                                            dcerpc_svcctl_OpenSCManagerW(p, tctx, &r), 
37                                                            "OpenSCManager failed!");
38         
39         return true;
40 }
41
42 static bool test_CloseServiceHandle(struct dcerpc_pipe *p, struct torture_context *tctx, struct policy_handle *h)
43 {
44         struct svcctl_CloseServiceHandle r; 
45
46         r.in.handle = h;
47         r.out.handle = h;
48         torture_assert_ntstatus_ok(tctx, 
49                                                            dcerpc_svcctl_CloseServiceHandle(p, tctx, &r), 
50                                                            "CloseServiceHandle failed");
51
52         return true;
53 }
54
55 static bool test_EnumServicesStatus(struct torture_context *tctx, struct dcerpc_pipe *p)
56 {
57         struct svcctl_EnumServicesStatusW r;
58         struct policy_handle h;
59         int i;
60         NTSTATUS status;
61         uint32_t resume_handle = 0;
62         struct ENUM_SERVICE_STATUS *service = NULL; 
63
64         if (!test_OpenSCManager(p, tctx, &h))
65                 return false;
66
67         r.in.handle = &h;
68         r.in.type = SERVICE_TYPE_WIN32;
69         r.in.state = SERVICE_STATE_ALL;
70         r.in.buf_size = 0;
71         r.in.resume_handle = &resume_handle;
72         r.out.service = NULL;
73         r.out.resume_handle = &resume_handle;
74         r.out.services_returned = 0;
75         r.out.bytes_needed = 0;
76
77         status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
78
79         torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
80
81         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
82                 r.in.buf_size = *r.out.bytes_needed;
83                 r.out.service = talloc_array(tctx, uint8_t, *r.out.bytes_needed);
84                 
85                 status = dcerpc_svcctl_EnumServicesStatusW(p, tctx, &r);
86
87                 torture_assert_ntstatus_ok(tctx, status, "EnumServicesStatus failed!");
88                 torture_assert_werr_ok(tctx, r.out.result, "EnumServicesStatus failed");
89
90                 service = (struct ENUM_SERVICE_STATUS *)r.out.service;
91         }
92
93         for(i = 0; i < *r.out.services_returned; i++) {
94                 printf("Type: %d, State: %d\n", service[i].status.type, service[i].status.state);
95         }
96         
97         if (!test_CloseServiceHandle(p, tctx, &h))
98                 return false;
99
100         return true;
101 }
102
103 static bool test_SCManager(struct torture_context *tctx, 
104                                                    struct dcerpc_pipe *p)
105 {
106         struct policy_handle h;
107
108         if (!test_OpenSCManager(p, tctx, &h))
109                 return false;
110
111         if (!test_CloseServiceHandle(p, tctx, &h))
112                 return false;
113
114         return true;
115 }
116
117 struct torture_suite *torture_rpc_svcctl(TALLOC_CTX *mem_ctx)
118 {
119         struct torture_suite *suite = torture_suite_create(mem_ctx, "SVCCTL");
120         struct torture_rpc_tcase *tcase;
121
122         tcase = torture_suite_add_rpc_iface_tcase(suite, "svcctl", 
123                                                                                           &ndr_table_svcctl);
124         
125         torture_rpc_tcase_add_test(tcase, "SCManager", 
126                                                            test_SCManager);
127         torture_rpc_tcase_add_test(tcase, "EnumServicesStatus", 
128                                                            test_EnumServicesStatus);
129
130         return suite;
131 }