r14720: Add torture_context argument to all torture tests
[kai/samba.git] / source4 / torture / rpc / eventlog.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for eventlog rpc operations
4
5    Copyright (C) Tim Potter 2003,2005
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 #include "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_eventlog.h"
26 #include "librpc/gen_ndr/ndr_eventlog_c.h"
27 #include "librpc/gen_ndr/ndr_lsa.h"
28 #include "torture/rpc/rpc.h"
29
30 static void init_lsa_String(struct lsa_String *name, const char *s)
31 {
32         name->string = s;
33         name->length = 2*strlen_m(s);
34         name->size = name->length;
35 }
36
37 static BOOL test_GetNumRecords(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
38                                struct policy_handle *handle)
39 {
40         NTSTATUS status;
41         struct eventlog_GetNumRecords r;
42
43         printf("\ntesting GetNumRecords\n");
44
45         r.in.handle = handle;
46
47         status = dcerpc_eventlog_GetNumRecords(p, mem_ctx, &r);
48
49         if (!NT_STATUS_IS_OK(status)) {
50                 printf("GetNumRecords failed - %s\n", nt_errstr(status));
51                 return False;
52         }
53
54         printf("%d records\n", r.out.number);
55
56         return True;
57 }
58
59 static BOOL test_ReadEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
60                               struct policy_handle *handle)
61 {
62         NTSTATUS status;
63         struct eventlog_ReadEventLogW r;
64
65         printf("\ntesting ReadEventLog\n");
66
67         r.in.offset = 0;
68         r.in.handle = handle;
69         r.in.flags = EVENTLOG_BACKWARDS_READ|EVENTLOG_SEQUENTIAL_READ;
70
71         while (1) {
72                 DATA_BLOB blob;
73                 struct eventlog_Record rec;
74                 struct ndr_pull *ndr;
75
76                 /* Read first for number of bytes in record */
77
78                 r.in.number_of_bytes = 0;
79                 r.out.data = NULL;
80
81                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
82
83                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
84                         break;
85                 }
86
87                 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_BUFFER_TOO_SMALL)) {
88                         printf("ReadEventLog failed - %s\n", nt_errstr(r.out.result));
89                         return False;
90                 }
91                 
92                 /* Now read the actual record */
93
94                 r.in.number_of_bytes = r.out.real_size;
95                 r.out.data = talloc_size(mem_ctx, r.in.number_of_bytes);
96
97                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
98
99                 if (!NT_STATUS_IS_OK(status)) {
100                         printf("ReadEventLog failed - %s\n", nt_errstr(status));
101                         return False;
102                 }
103                 
104                 /* Decode a user-marshalled record */
105
106                 blob.length = r.out.sent_size;
107                 blob.data = talloc_steal(mem_ctx, r.out.data);
108
109                 ndr = ndr_pull_init_blob(&blob, mem_ctx);
110
111                 status = ndr_pull_eventlog_Record(
112                         ndr, NDR_SCALARS|NDR_BUFFERS, &rec);
113
114                 NDR_PRINT_DEBUG(eventlog_Record, &rec);
115
116                 if (!NT_STATUS_IS_OK(status)) {
117                         printf("ReadEventLog failed parsing event log record "
118                                "- %s\n", nt_errstr(status));
119                         return False;
120                 }
121
122                 r.in.offset++;
123         }
124
125         return True;
126 }
127
128 static BOOL test_CloseEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
129                                struct policy_handle *handle)
130 {
131         NTSTATUS status;
132         struct eventlog_CloseEventLog r;
133
134         r.in.handle = r.out.handle = handle;
135
136         printf("Testing CloseEventLog\n");
137
138         status = dcerpc_eventlog_CloseEventLog(p, mem_ctx, &r);
139
140         if (!NT_STATUS_IS_OK(status)) {
141                 printf("CloseEventLog failed - %s\n", nt_errstr(status));
142                 return False;
143         }
144
145         return True;
146 }
147
148 static BOOL test_FlushEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
149                                struct policy_handle *handle)
150 {
151         NTSTATUS status;
152         struct eventlog_FlushEventLog r;
153
154         r.in.handle = handle;
155
156         printf("Testing FlushEventLog\n");
157
158         status = dcerpc_eventlog_FlushEventLog(p, mem_ctx, &r);
159
160         /* Huh?  Does this RPC always return access denied? */
161
162         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
163                 printf("FlushEventLog failed - %s\n", nt_errstr(status));
164                 return False;
165         }
166
167         return True;
168 }
169
170 static BOOL test_ClearEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
171                                struct policy_handle *handle)
172 {
173         NTSTATUS status;
174         struct eventlog_ClearEventLogW r;
175
176         r.in.handle = handle;
177         r.in.unknown = NULL;
178
179         printf("Testing ClearEventLog\n");
180
181         status = dcerpc_eventlog_ClearEventLogW(p, mem_ctx, &r);
182
183         if (!NT_STATUS_IS_OK(status)) {
184                 printf("ClearEventLog failed - %s\n", nt_errstr(status));
185                 return False;
186         }
187
188         return True;
189 }
190
191 static BOOL test_OpenEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
192                               struct policy_handle *handle)
193 {
194         NTSTATUS status;
195         struct eventlog_OpenEventLogW r;
196         struct eventlog_OpenUnknown0 unknown0;
197
198         printf("\ntesting OpenEventLog\n");
199
200         unknown0.unknown0 = 0x005c;
201         unknown0.unknown1 = 0x0001;
202
203         r.in.unknown0 = &unknown0;
204         init_lsa_String(&r.in.logname, "dns server");
205         init_lsa_String(&r.in.servername, NULL);
206         r.in.unknown2 = 0x00000001;
207         r.in.unknown3 = 0x00000001;
208         r.out.handle = handle;
209
210         status = dcerpc_eventlog_OpenEventLogW(p, mem_ctx, &r);
211
212         if (!NT_STATUS_IS_OK(status)) {
213                 printf("OpenEventLog failed - %s\n", nt_errstr(status));
214                 return False;
215         }
216
217         if (!NT_STATUS_IS_OK(r.out.result)) {
218                 printf("OpenEventLog failed - %s\n", nt_errstr(r.out.result));
219                 return False;
220         }
221
222         return True;
223 }
224
225 BOOL torture_rpc_eventlog(struct torture_context *torture)
226 {
227         NTSTATUS status;
228         struct dcerpc_pipe *p;
229         struct policy_handle handle;
230         TALLOC_CTX *mem_ctx;
231         BOOL ret = True;
232
233         mem_ctx = talloc_init("torture_rpc_atsvc");
234
235         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_eventlog);
236
237         if (!NT_STATUS_IS_OK(status)) {
238                 talloc_free(mem_ctx);
239                 return False;
240         }
241
242         if (!test_OpenEventLog(p, mem_ctx, &handle)) {
243                 talloc_free(mem_ctx);
244                 return False;
245         }
246
247 #if 0
248         ret &= test_ClearEventLog(p, mem_ctx, &handle); /* Destructive test */
249 #endif
250         
251         ret &= test_GetNumRecords(p, mem_ctx, &handle);
252
253         ret &= test_ReadEventLog(p, mem_ctx, &handle);
254
255         ret &= test_FlushEventLog(p, mem_ctx, &handle);
256
257         ret &= test_CloseEventLog(p, mem_ctx, &handle);
258
259         talloc_free(mem_ctx);
260
261         return ret;
262 }