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/torture.h"
23 #include "torture/ui.h"
24 #include "torture/rpc/rpc.h"
25 #include "librpc/gen_ndr/ndr_spoolss_c.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "ntvfs/ntvfs.h"
28 #include "param/param.h"
30 struct test_spoolss_win_context
{
32 uint32_t printer_count
;
33 union spoolss_PrinterInfo
*printer_info
;
34 union spoolss_PrinterInfo
*current_info
;
40 /* This is a convenience function for all OpenPrinterEx calls */
41 static bool test_OpenPrinterEx(struct torture_context
*tctx
,
42 struct dcerpc_pipe
*p
,
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(p
, 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_pipe
*p
,
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(p
, tctx
, &op
);
110 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(op
.out
.result
)) {
111 status
= dcerpc_spoolss_ClosePrinter(p
, tctx
, &cp
);
112 torture_assert_ntstatus_ok(tctx
, status
, "ClosePrinter failed");
119 static bool test_ClosePrinter(struct torture_context
*tctx
,
120 struct dcerpc_pipe
*p
,
121 struct policy_handle
*handle
);
123 /* This replicates the opening sequence of OpenPrinterEx calls XP does */
124 static bool test_OpenPrinterSequence(struct torture_context
*tctx
,
125 struct dcerpc_pipe
*p
,
126 struct policy_handle
*handle
)
129 char *printername
= talloc_asprintf(tctx
, "\\\\%s",
130 dcerpc_server_name(p
));
132 /* First, see if we can open the printer read_only */
133 ret
= test_OpenPrinterEx(tctx
, p
, handle
, printername
, 0);
134 torture_assert(tctx
, ret
== true, "OpenPrinterEx failed.");
136 ret
= test_ClosePrinter(tctx
, p
, handle
);
137 torture_assert(tctx
, ret
, "ClosePrinter failed");
139 /* Now let's see if we have admin rights to it. */
140 ret
= test_OpenPrinterAsAdmin(tctx
, p
, printername
);
141 torture_assert(tctx
, ret
== true,
142 "OpenPrinterEx as admin failed unexpectedly.");
144 ret
= test_OpenPrinterEx(tctx
, p
, handle
, printername
, SERVER_EXECUTE
);
145 torture_assert(tctx
, ret
== true, "OpenPrinterEx failed.");
150 static bool test_GetPrinterData(struct torture_context
*tctx
,
151 struct dcerpc_pipe
*p
,
152 struct policy_handle
*handle
,
153 const char *value_name
,
154 WERROR expected_werr
,
155 uint32_t expected_value
)
158 struct spoolss_GetPrinterData gpd
;
160 torture_comment(tctx
, "Testing GetPrinterData(%s).\n", value_name
);
161 gpd
.in
.handle
= handle
;
162 gpd
.in
.value_name
= value_name
;
165 status
= dcerpc_spoolss_GetPrinterData(p
, tctx
, &gpd
);
166 torture_assert_ntstatus_ok(tctx
, status
, "GetPrinterData failed.");
167 torture_assert_werr_equal(tctx
, gpd
.out
.result
, expected_werr
,
168 "GetPrinterData did not return expected error value.");
170 if (W_ERROR_IS_OK(expected_werr
)) {
171 torture_assert_int_equal(tctx
, gpd
.out
.data
.value
,
173 "GetPrinterData did not return expected value.");
178 static bool test_EnumPrinters(struct torture_context
*tctx
,
179 struct dcerpc_pipe
*p
,
180 struct test_spoolss_win_context
*ctx
,
181 uint32_t initial_blob_size
)
184 struct spoolss_EnumPrinters ep
;
185 DATA_BLOB blob
= data_blob_talloc_zero(ctx
, initial_blob_size
);
187 ep
.in
.flags
= PRINTER_ENUM_NAME
;
188 ep
.in
.server
= talloc_asprintf(tctx
, "\\\\%s", dcerpc_server_name(p
));
190 ep
.in
.buffer
= &blob
;
191 ep
.in
.offered
= initial_blob_size
;
193 status
= dcerpc_spoolss_EnumPrinters(p
, ctx
, &ep
);
194 torture_assert_ntstatus_ok(tctx
, status
, "EnumPrinters failed.");
196 if (W_ERROR_EQUAL(ep
.out
.result
, WERR_INSUFFICIENT_BUFFER
)) {
197 blob
= data_blob_talloc_zero(ctx
, ep
.out
.needed
);
198 ep
.in
.buffer
= &blob
;
199 ep
.in
.offered
= ep
.out
.needed
;
200 status
= dcerpc_spoolss_EnumPrinters(p
, ctx
, &ep
);
201 torture_assert_ntstatus_ok(tctx
, status
,"EnumPrinters failed.");
204 torture_assert_werr_ok(tctx
, ep
.out
.result
, "EnumPrinters failed.");
206 ctx
->printer_count
= ep
.out
.count
;
207 ctx
->printer_info
= ep
.out
.info
;
209 torture_comment(tctx
, "Found %d printer(s).\n", ctx
->printer_count
);
214 static bool test_GetPrinter(struct torture_context
*tctx
,
215 struct dcerpc_pipe
*p
,
216 struct policy_handle
*handle
,
217 struct test_spoolss_win_context
*ctx
,
219 uint32_t initial_blob_size
)
222 struct spoolss_GetPrinter gp
;
223 DATA_BLOB blob
= data_blob_talloc_zero(ctx
, initial_blob_size
);
225 torture_comment(tctx
, "Test GetPrinter level %d\n", level
);
227 gp
.in
.handle
= handle
;
229 gp
.in
.buffer
= (initial_blob_size
== 0)?NULL
:&blob
;
230 gp
.in
.offered
= initial_blob_size
;
232 status
= dcerpc_spoolss_GetPrinter(p
, tctx
, &gp
);
233 torture_assert_ntstatus_ok(tctx
, status
, "GetPrinter failed");
235 if (W_ERROR_EQUAL(gp
.out
.result
, WERR_INSUFFICIENT_BUFFER
)) {
236 blob
= data_blob_talloc_zero(ctx
, gp
.out
.needed
);
237 gp
.in
.buffer
= &blob
;
238 gp
.in
.offered
= gp
.out
.needed
;
239 status
= dcerpc_spoolss_GetPrinter(p
, tctx
, &gp
);
240 torture_assert_ntstatus_ok(tctx
, status
, "GetPrinter failed");
243 torture_assert_werr_ok(tctx
, gp
.out
.result
, "GetPrinter failed");
245 ctx
->current_info
= gp
.out
.info
;
249 static bool test_EnumJobs(struct torture_context
*tctx
,
250 struct dcerpc_pipe
*p
,
251 struct policy_handle
*handle
)
254 struct spoolss_EnumJobs ej
;
255 DATA_BLOB blob
= data_blob_talloc_zero(tctx
, 1024);
257 torture_comment(tctx
, "Test EnumJobs\n");
259 ej
.in
.handle
= handle
;
261 ej
.in
.buffer
= &blob
;
262 ej
.in
.offered
= 1024;
264 status
= dcerpc_spoolss_EnumJobs(p
, tctx
, &ej
);
265 torture_assert_ntstatus_ok(tctx
, status
, "EnumJobs failed");
266 torture_assert_werr_ok(tctx
, ej
.out
.result
, "EnumJobs failed");
271 static bool test_GetPrinterDriver2(struct torture_context
*tctx
,
272 struct dcerpc_pipe
*p
,
273 struct policy_handle
*handle
)
276 struct spoolss_GetPrinterDriver2 gpd2
;
277 DATA_BLOB blob
= data_blob_talloc_zero(tctx
, 87424);
279 torture_comment(tctx
, "Testing GetPrinterDriver2\n");
281 gpd2
.in
.handle
= handle
;
282 gpd2
.in
.architecture
= "Windows NT x86";
284 gpd2
.in
.buffer
= &blob
;
285 gpd2
.in
.offered
= 87424;
286 gpd2
.in
.client_major_version
= 3;
287 gpd2
.in
.client_minor_version
= 0;
289 status
= dcerpc_spoolss_GetPrinterDriver2(p
, tctx
, &gpd2
);
290 torture_assert_ntstatus_ok(tctx
, status
, "GetPrinterDriver2 failed");
291 torture_assert_werr_ok(tctx
, gpd2
.out
.result
,
292 "GetPrinterDriver2 failed.");
297 static bool test_EnumForms(struct torture_context
*tctx
,
298 struct dcerpc_pipe
*p
,
299 struct policy_handle
*handle
,
300 uint32_t initial_blob_size
)
303 struct spoolss_EnumForms ef
;
304 DATA_BLOB blob
= data_blob_talloc_zero(tctx
, initial_blob_size
);
306 torture_comment(tctx
, "Testing EnumForms\n");
308 ef
.in
.handle
= handle
;
310 ef
.in
.buffer
= (initial_blob_size
== 0)?NULL
:&blob
;
311 ef
.in
.offered
= initial_blob_size
;
313 status
= dcerpc_spoolss_EnumForms(p
, tctx
, &ef
);
314 torture_assert_ntstatus_ok(tctx
, status
, "EnumForms failed");
316 if (W_ERROR_EQUAL(ef
.out
.result
, WERR_INSUFFICIENT_BUFFER
)) {
317 blob
= data_blob_talloc_zero(tctx
, ef
.out
.needed
);
318 ef
.in
.buffer
= &blob
;
319 ef
.in
.offered
= ef
.out
.needed
;
320 status
= dcerpc_spoolss_EnumForms(p
, tctx
, &ef
);
321 torture_assert_ntstatus_ok(tctx
, status
, "EnumForms failed");
324 torture_assert_werr_ok(tctx
, ef
.out
.result
, "EnumForms failed");
329 static bool test_EnumPrinterKey(struct torture_context
*tctx
,
330 struct dcerpc_pipe
*p
,
331 struct policy_handle
*handle
,
333 struct test_spoolss_win_context
*ctx
)
336 struct spoolss_EnumPrinterKey epk
;
339 torture_comment(tctx
, "Testing EnumPrinterKey(%s)\n", key
);
341 epk
.in
.handle
= handle
;
342 epk
.in
.key_name
= talloc_strdup(tctx
, key
);
343 epk
.in
.needed
= needed
;
345 status
= dcerpc_spoolss_EnumPrinterKey(p
, tctx
, &epk
);
346 torture_assert_ntstatus_ok(tctx
, status
, "EnumPrinterKey failed");
349 if (W_ERROR_EQUAL(epk
.out
.result
, WERR_MORE_DATA
)) {
350 epk
.in
.needed
= epk
.out
.needed
;
351 status
= dcerpc_spoolss_EnumPrinterKey(p
, tctx
, &epk
);
352 torture_assert_ntstatus_ok(tctx
, status
,
353 "EnumPrinterKey failed");
356 torture_assert_werr_ok(tctx
, epk
.out
.result
, "EnumPrinterKey failed");
358 convert_string_talloc(ctx
, lp_iconv_convenience(tctx
->lp_ctx
), CH_UTF16
,
359 CH_UNIX
, epk
.out
.key_buffer
, epk
.out
.needed
,
360 (void**)&ctx
->printer_keys
);
365 static bool test_EnumPrinterDataEx(struct torture_context
*tctx
,
366 struct dcerpc_pipe
*p
,
367 struct policy_handle
*handle
,
369 uint32_t initial_blob_size
,
370 WERROR expected_error
)
373 struct spoolss_EnumPrinterDataEx epde
;
375 torture_comment(tctx
, "Testing EnumPrinterDataEx(%s)\n", key
);
377 epde
.in
.handle
= handle
;
378 epde
.in
.key_name
= talloc_strdup(tctx
, key
);
381 status
= dcerpc_spoolss_EnumPrinterDataEx(p
, tctx
, &epde
);
382 torture_assert_ntstatus_ok(tctx
, status
, "EnumPrinterDataEx failed.");
383 if (W_ERROR_EQUAL(epde
.out
.result
, WERR_MORE_DATA
)) {
384 epde
.in
.offered
= epde
.out
.needed
;
385 status
= dcerpc_spoolss_EnumPrinterDataEx(p
, tctx
, &epde
);
386 torture_assert_ntstatus_ok(tctx
, status
,
387 "EnumPrinterDataEx failed.");
390 torture_assert_werr_equal(tctx
, epde
.out
.result
, expected_error
,
391 "EnumPrinterDataEx failed.");
396 static bool test_ClosePrinter(struct torture_context
*tctx
,
397 struct dcerpc_pipe
*p
,
398 struct policy_handle
*handle
)
401 struct spoolss_ClosePrinter cp
;
403 cp
.in
.handle
= handle
;
404 cp
.out
.handle
= handle
;
406 status
= dcerpc_spoolss_ClosePrinter(p
, tctx
, &cp
);
407 torture_assert_ntstatus_ok(tctx
, status
, "ClosePrinter failed");
412 static bool test_WinXP(struct torture_context
*tctx
, struct dcerpc_pipe
*p
)
415 struct test_spoolss_win_context
*ctx
, *tmp_ctx
;
416 struct policy_handle handle01
, handle02
, handle03
, handle04
;
417 /* Sometimes a handle stays unused. In order to make this clear in the
418 * code, the unused_handle structures are used for that. */
419 struct policy_handle unused_handle1
, unused_handle2
;
423 ntvfs_init(tctx
->lp_ctx
);
425 ctx
= talloc_zero(tctx
, struct test_spoolss_win_context
);
426 tmp_ctx
= talloc_zero(tctx
, struct test_spoolss_win_context
);
428 ret
&= test_OpenPrinterSequence(tctx
, p
, &handle01
);
429 ret
&= test_GetPrinterData(tctx
, p
, &handle01
,"UISingleJobStatusString",
430 WERR_INVALID_PARAM
, 0);
431 torture_comment(tctx
, "Skip RemoteFindNextPrinterChangeNotifyEx test\n");
433 server_name
= talloc_asprintf(ctx
, "\\\\%s", dcerpc_server_name(p
));
434 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle1
, server_name
, 0);
436 ret
&= test_EnumPrinters(tctx
, p
, ctx
, 1024);
438 ret
&= test_OpenPrinterEx(tctx
, p
, &handle02
, server_name
, 0);
439 ret
&= test_GetPrinterData(tctx
, p
, &handle02
, "MajorVersion", WERR_OK
,
441 ret
&= test_ClosePrinter(tctx
, p
, &handle02
);
443 /* If no printers were found, skip all tests that need a printer */
444 if (ctx
->printer_count
== 0) {
448 ret
&= test_OpenPrinterEx(tctx
, p
, &handle02
,
449 ctx
->printer_info
[0].info2
.printername
,
451 ret
&= test_GetPrinter(tctx
, p
, &handle02
, ctx
, 2, 0);
453 torture_assert_str_equal(tctx
, ctx
->current_info
->info2
.printername
,
454 ctx
->printer_info
[0].info2
.printername
,
455 "GetPrinter returned unexpected printername");
456 /*FIXME: Test more components of the PrinterInfo2 struct */
458 ret
&= test_OpenPrinterEx(tctx
, p
, &handle03
,
459 ctx
->printer_info
[0].info2
.printername
, 0);
460 ret
&= test_GetPrinter(tctx
, p
, &handle03
, ctx
, 0, 1164);
461 ret
&= test_GetPrinter(tctx
, p
, &handle03
, ctx
, 2, 0);
463 ret
&= test_OpenPrinterEx(tctx
, p
, &handle04
,
464 ctx
->printer_info
[0].info2
.printername
, 0);
465 ret
&= test_GetPrinter(tctx
, p
, &handle04
, ctx
, 2, 0);
466 ret
&= test_ClosePrinter(tctx
, p
, &handle04
);
468 ret
&= test_OpenPrinterEx(tctx
, p
, &handle04
,
469 ctx
->printer_info
[0].info2
.printername
, 0);
470 ret
&= test_GetPrinter(tctx
, p
, &handle04
, ctx
, 2, 4096);
471 ret
&= test_ClosePrinter(tctx
, p
, &handle04
);
473 ret
&= test_OpenPrinterAsAdmin(tctx
, p
,
474 ctx
->printer_info
[0].info2
.printername
);
476 ret
&= test_OpenPrinterEx(tctx
, p
, &handle04
,
477 ctx
->printer_info
[0].info2
.printername
, PRINTER_READ
);
478 ret
&= test_GetPrinterData(tctx
, p
, &handle04
,"UISingleJobStatusString",
480 torture_comment(tctx
, "Skip RemoteFindNextPrinterChangeNotifyEx test\n");
482 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
483 ctx
->printer_info
[0].info2
.printername
, 0);
485 ret
&= test_EnumJobs(tctx
, p
, &handle04
);
486 ret
&= test_GetPrinter(tctx
, p
, &handle04
, ctx
, 2, 4096);
488 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
489 ret
&= test_ClosePrinter(tctx
, p
, &handle04
);
491 ret
&= test_EnumPrinters(tctx
, p
, ctx
, 1556);
492 ret
&= test_GetPrinterDriver2(tctx
, p
, &handle03
);
493 ret
&= test_EnumForms(tctx
, p
, &handle03
, 0);
495 ret
&= test_EnumPrinterKey(tctx
, p
, &handle03
, "", ctx
);
496 key_pointer
= ctx
->printer_keys
;
497 while(*key_pointer
!= '\0') {
501 for(end_pointer
= key_pointer
; *end_pointer
!= '\0';
503 /* Do nothing, just move the pointer */
505 key_name
= talloc_strndup(tctx
, key_pointer
,
506 end_pointer
- key_pointer
);
508 ret
&= test_EnumPrinterKey(tctx
, p
, &handle03
, key_name
,
510 ret
&= test_EnumPrinterDataEx(tctx
, p
, &handle03
, key_name
, 0,
513 key_pointer
= ++end_pointer
;
516 ret
&= test_EnumPrinterDataEx(tctx
, p
, &handle03
, "", 0,
519 ret
&= test_GetPrinter(tctx
, p
, &handle03
, tmp_ctx
, 2, 0);
521 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
522 ctx
->printer_info
[0].info2
.printername
, 0);
523 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
525 ret
&= test_GetPrinter(tctx
, p
, &handle03
, tmp_ctx
, 2, 2556);
527 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
528 ctx
->printer_info
[0].info2
.printername
, 0);
529 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
531 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
532 ctx
->printer_info
[0].info2
.printername
, 0);
533 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
535 ret
&= test_GetPrinter(tctx
, p
, &handle03
, tmp_ctx
, 7, 0);
537 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
538 ctx
->printer_info
[0].info2
.printername
, 0);
539 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
541 ret
&= test_ClosePrinter(tctx
, p
, &handle03
);
543 ret
&= test_OpenPrinterEx(tctx
, p
, &unused_handle2
,
544 ctx
->printer_info
[0].info2
.printername
, 0);
545 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle2
);
547 ret
&= test_OpenPrinterEx(tctx
, p
, &handle03
, server_name
, 0);
548 ret
&= test_GetPrinterData(tctx
, p
, &handle03
, "W3SvcInstalled",
550 ret
&= test_ClosePrinter(tctx
, p
, &handle03
);
552 ret
&= test_ClosePrinter(tctx
, p
, &unused_handle1
);
553 ret
&= test_ClosePrinter(tctx
, p
, &handle02
);
555 ret
&= test_OpenPrinterEx(tctx
, p
, &handle02
,
556 ctx
->printer_info
[0].info2
.sharename
, 0);
557 ret
&= test_GetPrinter(tctx
, p
, &handle02
, tmp_ctx
, 2, 0);
558 ret
&= test_ClosePrinter(tctx
, p
, &handle02
);
561 ret
&= test_ClosePrinter(tctx
, p
, &handle01
);
563 talloc_free(tmp_ctx
);
568 struct torture_suite
*torture_rpc_spoolss_win(TALLOC_CTX
*mem_ctx
)
570 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "SPOOLSS-WIN");
572 struct torture_rpc_tcase
*tcase
= torture_suite_add_rpc_iface_tcase(suite
,
573 "win", &ndr_table_spoolss
);
575 torture_rpc_tcase_add_test(tcase
, "testWinXP", test_WinXP
);