torture/eventlog: the NTSTATUS of dcerpc_ functions is the same as r.out.result
[amitay/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         uint32_t number = 0;
71
72         if (!get_policy_handle(tctx, p, &handle))
73                 return false;
74
75         ZERO_STRUCT(r);
76         r.in.handle = &handle;
77         r.out.number = &number;
78
79         torture_assert_ntstatus_ok(tctx, 
80                         dcerpc_eventlog_GetNumRecords(p, tctx, &r), 
81                         "GetNumRecords failed");
82
83         torture_comment(tctx, "%d records\n", *r.out.number);
84
85         cr.in.handle = cr.out.handle = &handle;
86
87         torture_assert_ntstatus_ok(tctx, 
88                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
89                                         "CloseEventLog failed");
90         return true;
91 }
92
93 static bool test_ReadEventLog(struct torture_context *tctx, 
94                                                           struct dcerpc_pipe *p)
95 {
96         NTSTATUS status;
97         struct eventlog_ReadEventLogW r;
98         struct eventlog_CloseEventLog cr;
99         struct policy_handle handle;
100
101         if (!get_policy_handle(tctx, p, &handle))
102                 return false;
103
104         ZERO_STRUCT(r);
105         r.in.offset = 0;
106         r.in.handle = &handle;
107         r.in.flags = EVENTLOG_BACKWARDS_READ|EVENTLOG_SEQUENTIAL_READ;
108
109         while (1) {
110                 DATA_BLOB blob;
111                 struct eventlog_Record rec;
112                 struct ndr_pull *ndr;
113                 enum ndr_err_code ndr_err;
114                 uint32_t sent_size = 0;
115                 uint32_t real_size = 0;
116
117                 /* Read first for number of bytes in record */
118
119                 r.in.number_of_bytes = 0;
120                 r.out.data = NULL;
121                 r.out.sent_size = &sent_size;
122                 r.out.real_size = &real_size;
123
124                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
125
126                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
127                         break;
128                 }
129
130                 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_BUFFER_TOO_SMALL,
131                         "ReadEventLog failed");
132                 
133                 /* Now read the actual record */
134
135                 r.in.number_of_bytes = *r.out.real_size;
136                 r.out.data = talloc_array(tctx, uint8_t, r.in.number_of_bytes);
137
138                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
139
140                 torture_assert_ntstatus_ok(tctx, status, "ReadEventLog failed");
141                 
142                 /* Decode a user-marshalled record */
143
144                 blob.length = *r.out.sent_size;
145                 blob.data = talloc_steal(tctx, r.out.data);
146
147                 ndr = ndr_pull_init_blob(&blob, tctx, lp_iconv_convenience(tctx->lp_ctx));
148
149                 ndr_err = ndr_pull_eventlog_Record(
150                         ndr, NDR_SCALARS|NDR_BUFFERS, &rec);
151                 status = ndr_map_error2ntstatus(ndr_err);
152
153                 NDR_PRINT_DEBUG(eventlog_Record, &rec);
154
155                 torture_assert_ntstatus_ok(tctx, status, 
156                                 "ReadEventLog failed parsing event log record");
157
158                 r.in.offset++;
159         }
160
161         cr.in.handle = cr.out.handle = &handle;
162
163         torture_assert_ntstatus_ok(tctx, 
164                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
165                                         "CloseEventLog failed");
166
167         return true;
168 }
169
170 static bool test_FlushEventLog(struct torture_context *tctx, 
171                                                            struct dcerpc_pipe *p)
172 {
173         struct eventlog_FlushEventLog r;
174         struct eventlog_CloseEventLog cr;
175         struct policy_handle handle;
176
177         if (!get_policy_handle(tctx, p, &handle))
178                 return false;
179
180         r.in.handle = &handle;
181
182         /* Huh?  Does this RPC always return access denied? */
183         torture_assert_ntstatus_equal(tctx, 
184                         dcerpc_eventlog_FlushEventLog(p, tctx, &r),
185                         NT_STATUS_ACCESS_DENIED, 
186                         "FlushEventLog failed");
187
188         cr.in.handle = cr.out.handle = &handle;
189
190         torture_assert_ntstatus_ok(tctx, 
191                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
192                                         "CloseEventLog failed");
193
194         return true;
195 }
196
197 static bool test_ClearEventLog(struct torture_context *tctx, 
198                                struct dcerpc_pipe *p)
199 {
200         struct eventlog_ClearEventLogW r;
201         struct eventlog_CloseEventLog cr;
202         struct policy_handle handle;
203
204         if (!get_policy_handle(tctx, p, &handle))
205                 return false;
206
207         r.in.handle = &handle;
208         r.in.unknown = NULL;
209
210         torture_assert_ntstatus_ok(tctx, 
211                         dcerpc_eventlog_ClearEventLogW(p, tctx, &r), 
212                         "ClearEventLog failed");
213
214         cr.in.handle = cr.out.handle = &handle;
215
216         torture_assert_ntstatus_ok(tctx, 
217                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
218                                         "CloseEventLog failed");
219
220         return true;
221 }
222
223 static bool test_OpenEventLog(struct torture_context *tctx, 
224                                                           struct dcerpc_pipe *p)
225 {
226         struct policy_handle handle;
227         struct eventlog_CloseEventLog cr;
228
229         if (!get_policy_handle(tctx, p, &handle))
230                 return false;
231
232         cr.in.handle = cr.out.handle = &handle;
233
234         torture_assert_ntstatus_ok(tctx, 
235                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
236                                         "CloseEventLog failed");
237
238         return true;
239 }
240
241 struct torture_suite *torture_rpc_eventlog(TALLOC_CTX *mem_ctx)
242 {
243         struct torture_suite *suite;
244         struct torture_rpc_tcase *tcase;
245         struct torture_test *test;
246
247         suite = torture_suite_create(mem_ctx, "EVENTLOG");
248         tcase = torture_suite_add_rpc_iface_tcase(suite, "eventlog", 
249                                                   &ndr_table_eventlog);
250
251         torture_rpc_tcase_add_test(tcase, "OpenEventLog", test_OpenEventLog);
252         test = torture_rpc_tcase_add_test(tcase, "ClearEventLog", 
253                                           test_ClearEventLog);
254         test->dangerous = true;
255         torture_rpc_tcase_add_test(tcase, "GetNumRecords", test_GetNumRecords);
256         torture_rpc_tcase_add_test(tcase, "ReadEventLog", test_ReadEventLog);
257         torture_rpc_tcase_add_test(tcase, "FlushEventLog", test_FlushEventLog);
258
259         return suite;
260 }