Merge commit 'release-4-0-0alpha2' into v4-0-test
[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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_eventlog.h"
25 #include "librpc/gen_ndr/ndr_eventlog_c.h"
26 #include "librpc/gen_ndr/ndr_lsa.h"
27 #include "torture/rpc/rpc.h"
28 #include "param/param.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 get_policy_handle(struct torture_context *tctx, 
38                                                           struct dcerpc_pipe *p,
39                                                           struct policy_handle *handle)
40 {
41         struct eventlog_OpenEventLogW r;
42         struct eventlog_OpenUnknown0 unknown0;
43
44         unknown0.unknown0 = 0x005c;
45         unknown0.unknown1 = 0x0001;
46
47         r.in.unknown0 = &unknown0;
48         init_lsa_String(&r.in.logname, "dns server");
49         init_lsa_String(&r.in.servername, NULL);
50         r.in.unknown2 = 0x00000001;
51         r.in.unknown3 = 0x00000001;
52         r.out.handle = handle;
53
54         torture_assert_ntstatus_ok(tctx, 
55                         dcerpc_eventlog_OpenEventLogW(p, tctx, &r), 
56                         "OpenEventLog failed");
57
58         torture_assert_ntstatus_ok(tctx, r.out.result, "OpenEventLog failed");
59
60         return true;
61 }
62
63
64
65 static bool test_GetNumRecords(struct torture_context *tctx, struct dcerpc_pipe *p)
66 {
67         struct eventlog_GetNumRecords r;
68         struct eventlog_CloseEventLog cr;
69         struct policy_handle handle;
70
71         if (!get_policy_handle(tctx, p, &handle))
72                 return false;
73
74         r.in.handle = &handle;
75
76         torture_assert_ntstatus_ok(tctx, 
77                         dcerpc_eventlog_GetNumRecords(p, tctx, &r), 
78                         "GetNumRecords failed");
79
80         torture_comment(tctx, "%d records\n", *r.out.number);
81
82         cr.in.handle = cr.out.handle = &handle;
83
84         torture_assert_ntstatus_ok(tctx, 
85                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
86                                         "CloseEventLog failed");
87         return true;
88 }
89
90 static bool test_ReadEventLog(struct torture_context *tctx, 
91                                                           struct dcerpc_pipe *p)
92 {
93         NTSTATUS status;
94         struct eventlog_ReadEventLogW r;
95         struct eventlog_CloseEventLog cr;
96         struct policy_handle handle;
97
98         if (!get_policy_handle(tctx, p, &handle))
99                 return false;
100
101         r.in.offset = 0;
102         r.in.handle = &handle;
103         r.in.flags = EVENTLOG_BACKWARDS_READ|EVENTLOG_SEQUENTIAL_READ;
104
105         while (1) {
106                 DATA_BLOB blob;
107                 struct eventlog_Record rec;
108                 struct ndr_pull *ndr;
109                 enum ndr_err_code ndr_err;
110
111                 /* Read first for number of bytes in record */
112
113                 r.in.number_of_bytes = 0;
114                 r.out.data = NULL;
115
116                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
117
118                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
119                         break;
120                 }
121
122                 torture_assert_ntstatus_ok(tctx, status, "ReadEventLog failed");
123
124                 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_BUFFER_TOO_SMALL,
125                         "ReadEventLog failed");
126                 
127                 /* Now read the actual record */
128
129                 r.in.number_of_bytes = *r.out.real_size;
130                 r.out.data = talloc_array(tctx, uint8_t, r.in.number_of_bytes);
131
132                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
133
134                 torture_assert_ntstatus_ok(tctx, status, "ReadEventLog failed");
135                 
136                 /* Decode a user-marshalled record */
137
138                 blob.length = *r.out.sent_size;
139                 blob.data = talloc_steal(tctx, r.out.data);
140
141                 ndr = ndr_pull_init_blob(&blob, tctx, lp_iconv_convenience(tctx->lp_ctx));
142
143                 ndr_err = ndr_pull_eventlog_Record(
144                         ndr, NDR_SCALARS|NDR_BUFFERS, &rec);
145                 status = ndr_map_error2ntstatus(ndr_err);
146
147                 NDR_PRINT_DEBUG(eventlog_Record, &rec);
148
149                 torture_assert_ntstatus_ok(tctx, status, 
150                                 "ReadEventLog failed parsing event log record");
151
152                 r.in.offset++;
153         }
154
155         cr.in.handle = cr.out.handle = &handle;
156
157         torture_assert_ntstatus_ok(tctx, 
158                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
159                                         "CloseEventLog failed");
160
161         return true;
162 }
163
164 static bool test_FlushEventLog(struct torture_context *tctx, 
165                                                            struct dcerpc_pipe *p)
166 {
167         struct eventlog_FlushEventLog r;
168         struct eventlog_CloseEventLog cr;
169         struct policy_handle handle;
170
171         if (!get_policy_handle(tctx, p, &handle))
172                 return false;
173
174         r.in.handle = &handle;
175
176         /* Huh?  Does this RPC always return access denied? */
177         torture_assert_ntstatus_equal(tctx, 
178                         dcerpc_eventlog_FlushEventLog(p, tctx, &r),
179                         NT_STATUS_ACCESS_DENIED, 
180                         "FlushEventLog failed");
181
182         cr.in.handle = cr.out.handle = &handle;
183
184         torture_assert_ntstatus_ok(tctx, 
185                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
186                                         "CloseEventLog failed");
187
188         return true;
189 }
190
191 static bool test_ClearEventLog(struct torture_context *tctx, 
192                                struct dcerpc_pipe *p)
193 {
194         struct eventlog_ClearEventLogW r;
195         struct eventlog_CloseEventLog cr;
196         struct policy_handle handle;
197
198         if (!get_policy_handle(tctx, p, &handle))
199                 return false;
200
201         r.in.handle = &handle;
202         r.in.unknown = NULL;
203
204         torture_assert_ntstatus_ok(tctx, 
205                         dcerpc_eventlog_ClearEventLogW(p, tctx, &r), 
206                         "ClearEventLog failed");
207
208         cr.in.handle = cr.out.handle = &handle;
209
210         torture_assert_ntstatus_ok(tctx, 
211                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
212                                         "CloseEventLog failed");
213
214         return true;
215 }
216
217 static bool test_OpenEventLog(struct torture_context *tctx, 
218                                                           struct dcerpc_pipe *p)
219 {
220         struct policy_handle handle;
221         struct eventlog_CloseEventLog cr;
222
223         if (!get_policy_handle(tctx, p, &handle))
224                 return false;
225
226         cr.in.handle = cr.out.handle = &handle;
227
228         torture_assert_ntstatus_ok(tctx, 
229                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
230                                         "CloseEventLog failed");
231
232         return true;
233 }
234
235 struct torture_suite *torture_rpc_eventlog(TALLOC_CTX *mem_ctx)
236 {
237         struct torture_suite *suite;
238         struct torture_rpc_tcase *tcase;
239         struct torture_test *test;
240
241         suite = torture_suite_create(mem_ctx, "EVENTLOG");
242         tcase = torture_suite_add_rpc_iface_tcase(suite, "eventlog", 
243                                                   &ndr_table_eventlog);
244
245         torture_rpc_tcase_add_test(tcase, "OpenEventLog", test_OpenEventLog);
246         test = torture_rpc_tcase_add_test(tcase, "ClearEventLog", 
247                                           test_ClearEventLog);
248         test->dangerous = true;
249         torture_rpc_tcase_add_test(tcase, "GetNumRecords", test_GetNumRecords);
250         torture_rpc_tcase_add_test(tcase, "ReadEventLog", test_ReadEventLog);
251         torture_rpc_tcase_add_test(tcase, "FlushEventLog", test_FlushEventLog);
252
253         return suite;
254 }