2 Unix SMB/CIFS implementation.
3 test suite for spoolss rpc operations as performed by various win versions
5 Copyright (C) Kai Blin 2007
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.
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.
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/>.
22 #include "torture/rpc/torture_rpc.h"
23 #include "librpc/gen_ndr/ndr_spoolss_c.h"
24 #include "librpc/gen_ndr/ndr_misc.h"
25 #include "ntvfs/ntvfs.h"
26 #include "param/param.h"
28 struct test_spoolss_win_context {
30 uint32_t printer_count;
31 union spoolss_PrinterInfo *printer_info;
32 union spoolss_PrinterInfo *current_info;
35 const char **printer_keys;
37 bool printer_has_driver;
40 /* This is a convenience function for all OpenPrinterEx calls */
41 static bool test_OpenPrinterEx(struct torture_context *tctx,
42 struct dcerpc_binding_handle *b,
43 struct policy_handle *handle,
44 const char *printer_name,
48 struct spoolss_OpenPrinterEx op;
49 struct spoolss_UserLevel1 ul_1;
51 torture_comment(tctx, "Opening printer '%s'\n", printer_name);
53 op.in.printername = talloc_strdup(tctx, printer_name);
54 op.in.datatype = NULL;
55 op.in.devmode_ctr.devmode = NULL;
56 op.in.access_mask = access_mask;
58 op.in.userlevel.level1 = &ul_1;
59 op.out.handle = handle;
62 ul_1.client = "\\clientname";
63 ul_1.user = "username";
67 ul_1.processor = 4567;
69 status = dcerpc_spoolss_OpenPrinterEx_r(b, tctx, &op);
70 torture_assert_ntstatus_ok(tctx, status, "OpenPrinterEx failed");
71 torture_assert_werr_ok(tctx, op.out.result, "OpenPrinterEx failed");
76 static bool test_OpenPrinterAsAdmin(struct torture_context *tctx,
77 struct dcerpc_binding_handle *b,
78 const char *printername)
81 struct spoolss_OpenPrinterEx op;
82 struct spoolss_ClosePrinter cp;
83 struct spoolss_UserLevel1 ul_1;
84 struct policy_handle handle;
87 ul_1.client = "\\clientname";
88 ul_1.user = "username";
92 ul_1.processor = 4567;
94 op.in.printername = talloc_strdup(tctx, printername);
95 op.in.datatype = NULL;
96 op.in.devmode_ctr.devmode = NULL;
97 op.in.access_mask = SERVER_ALL_ACCESS;
99 op.in.userlevel.level1 = &ul_1;
100 op.out.handle = &handle;
102 cp.in.handle = &handle;
103 cp.out.handle = &handle;
105 torture_comment(tctx, "Testing OpenPrinterEx(%s) with admin rights\n",
108 status = dcerpc_spoolss_OpenPrinterEx_r(b, tctx, &op);
110 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(op.out.result)) {
111 status = dcerpc_spoolss_ClosePrinter_r(b, tctx, &cp);
112 torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
120 /* This replicates the opening sequence of OpenPrinterEx calls XP does */
121 static bool test_OpenPrinterSequence(struct torture_context *tctx,
122 struct dcerpc_pipe *p,
123 struct policy_handle *handle)
126 char *printername = talloc_asprintf(tctx, "\\\\%s",
127 dcerpc_server_name(p));
128 struct dcerpc_binding_handle *b = p->binding_handle;
130 /* First, see if we can open the printer read_only */
131 ret = test_OpenPrinterEx(tctx, b, handle, printername, 0);
132 torture_assert(tctx, ret == true, "OpenPrinterEx failed.");
134 ret = test_ClosePrinter(tctx, b, handle);
135 torture_assert(tctx, ret, "ClosePrinter failed");
137 /* Now let's see if we have admin rights to it. */
138 ret = test_OpenPrinterAsAdmin(tctx, b, printername);
139 torture_assert(tctx, ret == true,
140 "OpenPrinterEx as admin failed unexpectedly.");
142 ret = test_OpenPrinterEx(tctx, b, handle, printername, SERVER_EXECUTE);
143 torture_assert(tctx, ret == true, "OpenPrinterEx failed.");
148 static bool test_GetPrinterData(struct torture_context *tctx,
149 struct dcerpc_binding_handle *b,
150 struct policy_handle *handle,
151 const char *value_name,
152 WERROR expected_werr,
153 uint32_t expected_value)
156 struct spoolss_GetPrinterData gpd;
158 enum winreg_Type type;
159 uint8_t *data = talloc_zero_array(tctx, uint8_t, 4);
161 torture_comment(tctx, "Testing GetPrinterData(%s).\n", value_name);
162 gpd.in.handle = handle;
163 gpd.in.value_name = value_name;
165 gpd.out.needed = &needed;
166 gpd.out.type = &type;
169 status = dcerpc_spoolss_GetPrinterData_r(b, tctx, &gpd);
170 torture_assert_ntstatus_ok(tctx, status, "GetPrinterData failed.");
171 torture_assert_werr_equal(tctx, gpd.out.result, expected_werr,
172 "GetPrinterData did not return expected error value.");
174 if (W_ERROR_IS_OK(expected_werr)) {
175 uint32_t value = IVAL(data, 0);
176 torture_assert_int_equal(tctx, value,
178 talloc_asprintf(tctx, "GetPrinterData for %s did not return expected value.", value_name));
183 static bool test_EnumPrinters(struct torture_context *tctx,
184 struct dcerpc_pipe *p,
185 struct test_spoolss_win_context *ctx,
186 uint32_t initial_blob_size)
189 struct spoolss_EnumPrinters ep;
190 DATA_BLOB blob = data_blob_talloc_zero(ctx, initial_blob_size);
193 union spoolss_PrinterInfo *info;
194 struct dcerpc_binding_handle *b = p->binding_handle;
196 ep.in.flags = PRINTER_ENUM_NAME;
197 ep.in.server = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
199 ep.in.buffer = &blob;
200 ep.in.offered = initial_blob_size;
201 ep.out.needed = &needed;
202 ep.out.count = &count;
205 status = dcerpc_spoolss_EnumPrinters_r(b, ctx, &ep);
206 torture_assert_ntstatus_ok(tctx, status, "EnumPrinters failed.");
208 if (W_ERROR_EQUAL(ep.out.result, WERR_INSUFFICIENT_BUFFER)) {
209 blob = data_blob_talloc_zero(ctx, needed);
210 ep.in.buffer = &blob;
211 ep.in.offered = needed;
212 status = dcerpc_spoolss_EnumPrinters_r(b, ctx, &ep);
213 torture_assert_ntstatus_ok(tctx, status,"EnumPrinters failed.");
216 torture_assert_werr_ok(tctx, ep.out.result, "EnumPrinters failed.");
218 ctx->printer_count = count;
219 ctx->printer_info = info;
221 torture_comment(tctx, "Found %d printer(s).\n", ctx->printer_count);
226 static bool test_GetPrinter(struct torture_context *tctx,
227 struct dcerpc_binding_handle *b,
228 struct policy_handle *handle,
229 struct test_spoolss_win_context *ctx,
231 uint32_t initial_blob_size)
234 struct spoolss_GetPrinter gp;
235 DATA_BLOB blob = data_blob_talloc_zero(ctx, initial_blob_size);
238 torture_comment(tctx, "Test GetPrinter level %d\n", level);
240 gp.in.handle = handle;
242 gp.in.buffer = (initial_blob_size == 0)?NULL:&blob;
243 gp.in.offered = initial_blob_size;
244 gp.out.needed = &needed;
246 status = dcerpc_spoolss_GetPrinter_r(b, tctx, &gp);
247 torture_assert_ntstatus_ok(tctx, status, "GetPrinter failed");
249 if (W_ERROR_EQUAL(gp.out.result, WERR_INSUFFICIENT_BUFFER)) {
250 blob = data_blob_talloc_zero(ctx, needed);
251 gp.in.buffer = &blob;
252 gp.in.offered = needed;
253 status = dcerpc_spoolss_GetPrinter_r(b, tctx, &gp);
254 torture_assert_ntstatus_ok(tctx, status, "GetPrinter failed");
257 torture_assert_werr_ok(tctx, gp.out.result, "GetPrinter failed");
259 ctx->current_info = gp.out.info;
261 if (level == 2 && gp.out.info) {
262 ctx->printer_has_driver = gp.out.info->info2.drivername &&
263 strlen(gp.out.info->info2.drivername);
269 static bool test_EnumJobs(struct torture_context *tctx,
270 struct dcerpc_binding_handle *b,
271 struct policy_handle *handle)
274 struct spoolss_EnumJobs ej;
275 DATA_BLOB blob = data_blob_talloc_zero(tctx, 1024);
278 union spoolss_JobInfo *info;
280 torture_comment(tctx, "Test EnumJobs\n");
282 ej.in.handle = handle;
284 ej.in.buffer = &blob;
285 ej.in.offered = 1024;
286 ej.out.needed = &needed;
287 ej.out.count = &count;
290 status = dcerpc_spoolss_EnumJobs_r(b, tctx, &ej);
291 torture_assert_ntstatus_ok(tctx, status, "EnumJobs failed");
292 if (W_ERROR_EQUAL(ej.out.result, WERR_INSUFFICIENT_BUFFER)) {
293 blob = data_blob_talloc_zero(tctx, needed);
294 ej.in.offered = needed;
295 ej.in.buffer = &blob;
296 status = dcerpc_spoolss_EnumJobs_r(b, tctx, &ej);
297 torture_assert_ntstatus_ok(tctx, status, "EnumJobs failed");
299 torture_assert_werr_ok(tctx, ej.out.result, "EnumJobs failed");
304 static bool test_GetPrinterDriver2(struct torture_context *tctx,
305 struct dcerpc_binding_handle *b,
306 struct test_spoolss_win_context *ctx,
307 struct policy_handle *handle)
310 struct spoolss_GetPrinterDriver2 gpd2;
311 DATA_BLOB blob = data_blob_talloc_zero(tctx, 87424);
313 uint32_t server_major_version;
314 uint32_t server_minor_version;
316 torture_comment(tctx, "Testing GetPrinterDriver2\n");
318 gpd2.in.handle = handle;
319 gpd2.in.architecture = "Windows NT x86";
321 gpd2.in.buffer = &blob;
322 gpd2.in.offered = 87424;
323 gpd2.in.client_major_version = 3;
324 gpd2.in.client_minor_version = 0;
325 gpd2.out.needed = &needed;
326 gpd2.out.server_major_version = &server_major_version;
327 gpd2.out.server_minor_version = &server_minor_version;
329 status = dcerpc_spoolss_GetPrinterDriver2_r(b, tctx, &gpd2);
330 torture_assert_ntstatus_ok(tctx, status, "GetPrinterDriver2 failed");
332 if (ctx->printer_has_driver) {
333 torture_assert_werr_ok(tctx, gpd2.out.result,
334 "GetPrinterDriver2 failed.");
340 static bool test_EnumForms(struct torture_context *tctx,
341 struct dcerpc_binding_handle *b,
342 struct policy_handle *handle,
343 uint32_t initial_blob_size)
346 struct spoolss_EnumForms ef;
347 DATA_BLOB blob = data_blob_talloc_zero(tctx, initial_blob_size);
350 union spoolss_FormInfo *info;
352 torture_comment(tctx, "Testing EnumForms\n");
354 ef.in.handle = handle;
356 ef.in.buffer = (initial_blob_size == 0)?NULL:&blob;
357 ef.in.offered = initial_blob_size;
358 ef.out.needed = &needed;
359 ef.out.count = &count;
362 status = dcerpc_spoolss_EnumForms_r(b, tctx, &ef);
363 torture_assert_ntstatus_ok(tctx, status, "EnumForms failed");
365 if (W_ERROR_EQUAL(ef.out.result, WERR_INSUFFICIENT_BUFFER)) {
366 blob = data_blob_talloc_zero(tctx, needed);
367 ef.in.buffer = &blob;
368 ef.in.offered = needed;
369 status = dcerpc_spoolss_EnumForms_r(b, tctx, &ef);
370 torture_assert_ntstatus_ok(tctx, status, "EnumForms failed");
373 torture_assert_werr_ok(tctx, ef.out.result, "EnumForms failed");
378 static bool test_EnumPrinterKey(struct torture_context *tctx,
379 struct dcerpc_binding_handle *b,
380 struct policy_handle *handle,
382 struct test_spoolss_win_context *ctx)
385 struct spoolss_EnumPrinterKey epk;
387 union spoolss_KeyNames key_buffer;
390 torture_comment(tctx, "Testing EnumPrinterKey(%s)\n", key);
392 epk.in.handle = handle;
393 epk.in.key_name = talloc_strdup(tctx, key);
395 epk.out.needed = &needed;
396 epk.out.key_buffer = &key_buffer;
397 epk.out._ndr_size = &_ndr_size;
399 status = dcerpc_spoolss_EnumPrinterKey_r(b, tctx, &epk);
400 torture_assert_ntstatus_ok(tctx, status, "EnumPrinterKey failed");
402 if (W_ERROR_EQUAL(epk.out.result, WERR_MORE_DATA)) {
403 epk.in.offered = needed;
404 status = dcerpc_spoolss_EnumPrinterKey_r(b, tctx, &epk);
405 torture_assert_ntstatus_ok(tctx, status,
406 "EnumPrinterKey failed");
409 torture_assert_werr_ok(tctx, epk.out.result, "EnumPrinterKey failed");
411 ctx->printer_keys = key_buffer.string_array;
416 static bool test_EnumPrinterDataEx(struct torture_context *tctx,
417 struct dcerpc_binding_handle *b,
418 struct policy_handle *handle,
420 uint32_t initial_blob_size,
421 WERROR expected_error)
424 struct spoolss_EnumPrinterDataEx epde;
425 struct spoolss_PrinterEnumValues *info;
429 torture_comment(tctx, "Testing EnumPrinterDataEx(%s)\n", key);
431 epde.in.handle = handle;
432 epde.in.key_name = talloc_strdup(tctx, key);
434 epde.out.needed = &needed;
435 epde.out.count = &count;
436 epde.out.info = &info;
438 status = dcerpc_spoolss_EnumPrinterDataEx_r(b, tctx, &epde);
439 torture_assert_ntstatus_ok(tctx, status, "EnumPrinterDataEx failed.");
440 if (W_ERROR_EQUAL(epde.out.result, WERR_MORE_DATA)) {
441 epde.in.offered = needed;
442 status = dcerpc_spoolss_EnumPrinterDataEx_r(b, tctx, &epde);
443 torture_assert_ntstatus_ok(tctx, status,
444 "EnumPrinterDataEx failed.");
447 torture_assert_werr_equal(tctx, epde.out.result, expected_error,
448 "EnumPrinterDataEx failed.");
453 static bool test_WinXP(struct torture_context *tctx, struct dcerpc_pipe *p)
456 struct test_spoolss_win_context *ctx, *tmp_ctx;
457 struct policy_handle handle01, handle02, handle03, handle04;
458 /* Sometimes a handle stays unused. In order to make this clear in the
459 * code, the unused_handle structures are used for that. */
460 struct policy_handle unused_handle1, unused_handle2;
463 struct dcerpc_binding_handle *b = p->binding_handle;
465 ntvfs_init(tctx->lp_ctx);
467 ctx = talloc_zero(tctx, struct test_spoolss_win_context);
468 tmp_ctx = talloc_zero(tctx, struct test_spoolss_win_context);
470 ret &= test_OpenPrinterSequence(tctx, p, &handle01);
471 ret &= test_GetPrinterData(tctx, b, &handle01,"UISingleJobStatusString",
472 WERR_INVALID_PARAM, 0);
473 torture_comment(tctx, "Skip RemoteFindNextPrinterChangeNotifyEx test\n");
475 server_name = talloc_asprintf(ctx, "\\\\%s", dcerpc_server_name(p));
476 ret &= test_OpenPrinterEx(tctx, b, &unused_handle1, server_name, 0);
478 ret &= test_EnumPrinters(tctx, p, ctx, 1024);
480 ret &= test_OpenPrinterEx(tctx, b, &handle02, server_name, 0);
481 ret &= test_GetPrinterData(tctx, b, &handle02, "MajorVersion", WERR_OK,
483 ret &= test_ClosePrinter(tctx, b, &handle02);
485 /* If no printers were found, skip all tests that need a printer */
486 if (ctx->printer_count == 0) {
490 ret &= test_OpenPrinterEx(tctx, b, &handle02,
491 ctx->printer_info[0].info2.printername,
493 ret &= test_GetPrinter(tctx, b, &handle02, ctx, 2, 0);
495 torture_assert_str_equal(tctx, ctx->current_info->info2.printername,
496 ctx->printer_info[0].info2.printername,
497 "GetPrinter returned unexpected printername");
498 /*FIXME: Test more components of the PrinterInfo2 struct */
500 ret &= test_OpenPrinterEx(tctx, b, &handle03,
501 ctx->printer_info[0].info2.printername, 0);
502 ret &= test_GetPrinter(tctx, b, &handle03, ctx, 0, 1164);
503 ret &= test_GetPrinter(tctx, b, &handle03, ctx, 2, 0);
505 ret &= test_OpenPrinterEx(tctx, b, &handle04,
506 ctx->printer_info[0].info2.printername, 0);
507 ret &= test_GetPrinter(tctx, b, &handle04, ctx, 2, 0);
508 ret &= test_ClosePrinter(tctx, b, &handle04);
510 ret &= test_OpenPrinterEx(tctx, b, &handle04,
511 ctx->printer_info[0].info2.printername, 0);
512 ret &= test_GetPrinter(tctx, b, &handle04, ctx, 2, 4096);
513 ret &= test_ClosePrinter(tctx, b, &handle04);
515 ret &= test_OpenPrinterAsAdmin(tctx, b,
516 ctx->printer_info[0].info2.printername);
518 ret &= test_OpenPrinterEx(tctx, b, &handle04,
519 ctx->printer_info[0].info2.printername, PRINTER_READ);
520 ret &= test_GetPrinterData(tctx, b, &handle04,"UISingleJobStatusString",
522 torture_comment(tctx, "Skip RemoteFindNextPrinterChangeNotifyEx test\n");
524 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
525 ctx->printer_info[0].info2.printername, 0);
527 ret &= test_EnumJobs(tctx, b, &handle04);
528 ret &= test_GetPrinter(tctx, b, &handle04, ctx, 2, 4096);
530 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
531 ret &= test_ClosePrinter(tctx, b, &handle04);
533 ret &= test_EnumPrinters(tctx, p, ctx, 1556);
534 ret &= test_GetPrinterDriver2(tctx, b, ctx, &handle03);
535 ret &= test_EnumForms(tctx, b, &handle03, 0);
537 ret &= test_EnumPrinterKey(tctx, b, &handle03, "", ctx);
539 for (i=0; ctx->printer_keys && ctx->printer_keys[i] != NULL; i++) {
541 ret &= test_EnumPrinterKey(tctx, b, &handle03,
542 ctx->printer_keys[i],
544 ret &= test_EnumPrinterDataEx(tctx, b, &handle03,
545 ctx->printer_keys[i], 0,
549 ret &= test_EnumPrinterDataEx(tctx, b, &handle03, "", 0,
552 ret &= test_GetPrinter(tctx, b, &handle03, tmp_ctx, 2, 0);
554 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
555 ctx->printer_info[0].info2.printername, 0);
556 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
558 ret &= test_GetPrinter(tctx, b, &handle03, tmp_ctx, 2, 2556);
560 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
561 ctx->printer_info[0].info2.printername, 0);
562 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
564 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
565 ctx->printer_info[0].info2.printername, 0);
566 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
568 ret &= test_GetPrinter(tctx, b, &handle03, tmp_ctx, 7, 0);
570 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
571 ctx->printer_info[0].info2.printername, 0);
572 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
574 ret &= test_ClosePrinter(tctx, b, &handle03);
576 ret &= test_OpenPrinterEx(tctx, b, &unused_handle2,
577 ctx->printer_info[0].info2.printername, 0);
578 ret &= test_ClosePrinter(tctx, b, &unused_handle2);
580 ret &= test_OpenPrinterEx(tctx, b, &handle03, server_name, 0);
581 ret &= test_GetPrinterData(tctx, b, &handle03, "W3SvcInstalled",
583 ret &= test_ClosePrinter(tctx, b, &handle03);
585 ret &= test_ClosePrinter(tctx, b, &unused_handle1);
586 ret &= test_ClosePrinter(tctx, b, &handle02);
588 ret &= test_OpenPrinterEx(tctx, b, &handle02,
589 ctx->printer_info[0].info2.sharename, 0);
590 ret &= test_GetPrinter(tctx, b, &handle02, tmp_ctx, 2, 0);
591 ret &= test_ClosePrinter(tctx, b, &handle02);
594 ret &= test_ClosePrinter(tctx, b, &handle01);
596 talloc_free(tmp_ctx);
601 struct torture_suite *torture_rpc_spoolss_win(TALLOC_CTX *mem_ctx)
603 struct torture_suite *suite = torture_suite_create(mem_ctx, "spoolss.win");
605 struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
606 "win", &ndr_table_spoolss);
608 torture_rpc_tcase_add_test(tcase, "testWinXP", test_WinXP);