4e1672e67c52ababa0dc06c7b3af3f3f8b85d419
[ira/wip.git] / testprogs / win32 / spoolss / spoolss.c
1 /*
2    Unix SMB/CIFS implementation.
3    test suite for spoolss rpc operations
4
5    Copyright (C) Guenther Deschner 2009-2010
6
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.
11
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.
16
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/>.
19 */
20
21 /****************************************************************************
22 ****************************************************************************/
23
24 #include "spoolss.h"
25 #include "string.h"
26 #include "torture.h"
27
28 /****************************************************************************
29 ****************************************************************************/
30
31 static BOOL test_OpenPrinter(struct torture_context *tctx,
32                              LPSTR printername,
33                              LPPRINTER_DEFAULTS defaults,
34                              LPHANDLE handle)
35 {
36         torture_comment(tctx, "Testing OpenPrinter(%s)", printername);
37
38         if (!OpenPrinter(printername, handle, defaults)) {
39                 char tmp[1024];
40                 sprintf(tmp, "failed to open printer %s, error was: 0x%08x\n",
41                         printername, GetLastError());
42                 torture_fail(tctx, tmp);
43         }
44
45         return TRUE;
46 }
47
48 /****************************************************************************
49 ****************************************************************************/
50
51 static BOOL test_ClosePrinter(struct torture_context *tctx,
52                               HANDLE handle)
53 {
54         torture_comment(tctx, "Testing ClosePrinter");
55
56         if (!ClosePrinter(handle)) {
57                 char tmp[1024];
58                 sprintf(tmp, "failed to close printer, error was: %s\n",
59                         errstr(GetLastError()));
60                 torture_fail(tctx, tmp);
61         }
62
63         return TRUE;
64 }
65
66
67 /****************************************************************************
68 ****************************************************************************/
69
70 static BOOL test_EnumPrinters(struct torture_context *tctx,
71                               LPSTR servername)
72 {
73         DWORD levels[]  = { 1, 2, 5 };
74         DWORD success[] = { 1, 1, 1 };
75         DWORD i;
76         DWORD flags = PRINTER_ENUM_NAME;
77         LPBYTE buffer = NULL;
78
79         for (i=0; i < ARRAY_SIZE(levels); i++) {
80
81                 DWORD needed = 0;
82                 DWORD returned = 0;
83                 DWORD err = 0;
84                 char tmp[1024];
85
86                 torture_comment(tctx, "Testing EnumPrinters level %d", levels[i]);
87
88                 EnumPrinters(flags, servername, levels[i], NULL, 0, &needed, &returned);
89                 err = GetLastError();
90                 if (err == ERROR_INSUFFICIENT_BUFFER) {
91                         err = 0;
92                         buffer = malloc(needed);
93                         torture_assert(tctx, buffer, "malloc failed");
94                         if (!EnumPrinters(flags, servername, levels[i], buffer, needed, &needed, &returned)) {
95                                 err = GetLastError();
96                         }
97                 }
98                 if (err) {
99                         sprintf(tmp, "EnumPrinters failed level %d on [%s] (buffer size = %d), error: %s\n",
100                                 levels[i], servername, needed, errstr(err));
101                         if (success[i]) {
102                                 torture_fail(tctx, tmp);
103                         } else {
104                                 torture_warning(tctx, tmp);
105                         }
106                 }
107
108                 if (tctx->print) {
109                         print_printer_info_bylevel(levels[i], buffer, returned);
110                 }
111
112                 free(buffer);
113                 buffer = NULL;
114         }
115
116         return TRUE;
117 }
118
119 /****************************************************************************
120 ****************************************************************************/
121
122 static BOOL test_EnumDrivers(struct torture_context *tctx,
123                              LPSTR servername,
124                              LPSTR architecture)
125 {
126         DWORD levels[]  = { 1, 2, 3, 4, 5, 6 };
127         DWORD success[] = { 1, 1, 1, 1, 1, 1 };
128         DWORD i;
129         LPBYTE buffer = NULL;
130
131         for (i=0; i < ARRAY_SIZE(levels); i++) {
132
133                 DWORD needed = 0;
134                 DWORD returned = 0;
135                 DWORD err = 0;
136                 char tmp[1024];
137
138                 torture_comment(tctx, "Testing EnumPrinterDrivers level %d", levels[i]);
139
140                 EnumPrinterDrivers(servername, architecture, levels[i], NULL, 0, &needed, &returned);
141                 err = GetLastError();
142                 if (err == ERROR_INSUFFICIENT_BUFFER) {
143                         err = 0;
144                         buffer = malloc(needed);
145                         torture_assert(tctx, buffer, "malloc failed");
146                         if (!EnumPrinterDrivers(servername, architecture, levels[i], buffer, needed, &needed, &returned)) {
147                                 err = GetLastError();
148                         }
149                 }
150                 if (err) {
151                         sprintf(tmp, "EnumPrinterDrivers failed level %d on [%s] (buffer size = %d), error: %s\n",
152                                 levels[i], servername, needed, errstr(err));
153                         if (success[i]) {
154                                 torture_fail(tctx, tmp);
155                         } else {
156                                 torture_warning(tctx, tmp);
157                         }
158                 }
159
160                 if (tctx->print) {
161                         print_driver_info_bylevel(levels[i], buffer, returned);
162                 }
163
164                 free(buffer);
165                 buffer = NULL;
166         }
167
168         return TRUE;
169 }
170
171 /****************************************************************************
172 ****************************************************************************/
173
174 static BOOL test_GetForm(struct torture_context *tctx,
175                          LPSTR servername,
176                          HANDLE handle,
177                          LPSTR formname)
178 {
179         DWORD levels[]  = { 1, 2 };
180         DWORD success[] = { 1, 0 };
181         DWORD i;
182         LPBYTE buffer = NULL;
183
184         for (i=0; i < ARRAY_SIZE(levels); i++) {
185
186                 DWORD needed = 0;
187                 DWORD err = 0;
188                 char tmp[1024];
189
190                 torture_comment(tctx, "Testing GetForm(%s) level %d", formname, levels[i]);
191
192                 GetForm(handle, formname, levels[i], NULL, 0, &needed);
193                 err = GetLastError();
194                 if (err == ERROR_INSUFFICIENT_BUFFER) {
195                         err = 0;
196                         buffer = malloc(needed);
197                         torture_assert(tctx, buffer, "malloc failed");
198                         if (!GetForm(handle, formname, levels[i], buffer, needed, &needed)) {
199                                 err = GetLastError();
200                         }
201                 }
202                 if (err) {
203                         sprintf(tmp, "GetForm failed level %d on [%s] (buffer size = %d), error: %s\n",
204                                 levels[i], servername, needed, errstr(err));
205                         if (success[i]) {
206                                 torture_fail(tctx, tmp);
207                         } else {
208                                 torture_warning(tctx, tmp);
209                         }
210                 }
211
212                 if (tctx->print) {
213                         print_form_info_bylevel(levels[i], buffer, 1);
214                 }
215
216                 free(buffer);
217                 buffer = NULL;
218         }
219
220         return TRUE;
221 }
222
223 /****************************************************************************
224 ****************************************************************************/
225
226 static BOOL test_EnumForms(struct torture_context *tctx,
227                            LPSTR servername,
228                            HANDLE handle)
229 {
230         DWORD levels[]  = { 1, 2 };
231         DWORD success[] = { 1, 0 };
232         DWORD i;
233         LPBYTE buffer = NULL;
234
235         for (i=0; i < ARRAY_SIZE(levels); i++) {
236
237                 DWORD needed = 0;
238                 DWORD returned = 0;
239                 DWORD err = 0;
240                 char tmp[1024];
241
242                 torture_comment(tctx, "Testing EnumForms level %d", levels[i]);
243
244                 EnumForms(handle, levels[i], NULL, 0, &needed, &returned);
245                 err = GetLastError();
246                 if (err == ERROR_INSUFFICIENT_BUFFER) {
247                         err = 0;
248                         buffer = malloc(needed);
249                         torture_assert(tctx, buffer, "malloc failed");
250                         if (!EnumForms(handle, levels[i], buffer, needed, &needed, &returned)) {
251                                 err = GetLastError();
252                         }
253                 }
254                 if (err) {
255                         sprintf(tmp, "EnumForms failed level %d on [%s] (buffer size = %d), error: %s\n",
256                                 levels[i], servername, needed, errstr(err));
257                         if (success[i]) {
258                                 torture_fail(tctx, tmp);
259                         } else {
260                                 torture_warning(tctx, tmp);
261                         }
262                 }
263
264                 if (tctx->print) {
265                         print_form_info_bylevel(levels[i], buffer, returned);
266                 }
267
268                 free(buffer);
269                 buffer = NULL;
270         }
271
272         return TRUE;
273 }
274
275 /****************************************************************************
276 ****************************************************************************/
277
278 static BOOL test_EnumPorts(struct torture_context *tctx,
279                            LPSTR servername)
280 {
281         DWORD levels[]  = { 1, 2 };
282         DWORD success[] = { 1, 1 };
283         DWORD i;
284         LPBYTE buffer = NULL;
285
286         for (i=0; i < ARRAY_SIZE(levels); i++) {
287
288                 DWORD needed = 0;
289                 DWORD returned = 0;
290                 DWORD err = 0;
291                 char tmp[1024];
292
293                 torture_comment(tctx, "Testing EnumPorts level %d", levels[i]);
294
295                 EnumPorts(servername, levels[i], NULL, 0, &needed, &returned);
296                 err = GetLastError();
297                 if (err == ERROR_INSUFFICIENT_BUFFER) {
298                         err = 0;
299                         buffer = malloc(needed);
300                         torture_assert(tctx, buffer, "malloc failed");
301                         if (!EnumPorts(servername, levels[i], buffer, needed, &needed, &returned)) {
302                                 err = GetLastError();
303                         }
304                 }
305                 if (err) {
306                         sprintf(tmp, "EnumPorts failed level %d on [%s] (buffer size = %d), error: %s\n",
307                                 levels[i], servername, needed, errstr(err));
308                         if (success[i]) {
309                                 torture_fail(tctx, tmp);
310                         } else {
311                                 torture_warning(tctx, tmp);
312                         }
313                 }
314
315                 if (tctx->print) {
316                         print_port_info_bylevel(levels[i], buffer, returned);
317                 }
318
319                 free(buffer);
320                 buffer = NULL;
321         }
322
323         return TRUE;
324 }
325
326 /****************************************************************************
327 ****************************************************************************/
328
329 static BOOL test_EnumMonitors(struct torture_context *tctx,
330                               LPSTR servername)
331 {
332         DWORD levels[]  = { 1, 2 };
333         DWORD success[] = { 1, 1 };
334         DWORD i;
335         LPBYTE buffer = NULL;
336
337         for (i=0; i < ARRAY_SIZE(levels); i++) {
338
339                 DWORD needed = 0;
340                 DWORD returned = 0;
341                 DWORD err = 0;
342                 char tmp[1024];
343
344                 torture_comment(tctx, "Testing EnumMonitors level %d", levels[i]);
345
346                 EnumMonitors(servername, levels[i], NULL, 0, &needed, &returned);
347                 err = GetLastError();
348                 if (err == ERROR_INSUFFICIENT_BUFFER) {
349                         err = 0;
350                         buffer = malloc(needed);
351                         torture_assert(tctx, buffer, "malloc failed");
352                         if (!EnumMonitors(servername, levels[i], buffer, needed, &needed, &returned)) {
353                                 err = GetLastError();
354                         }
355                 }
356                 if (err) {
357                         sprintf(tmp, "EnumMonitors failed level %d on [%s] (buffer size = %d), error: %s\n",
358                                 levels[i], servername, needed, errstr(err));
359                         if (success[i]) {
360                                 torture_fail(tctx, tmp);
361                         } else {
362                                 torture_warning(tctx, tmp);
363                         }
364                 }
365
366                 if (tctx->print) {
367                         print_monitor_info_bylevel(levels[i], buffer, returned);
368                 }
369
370                 free(buffer);
371                 buffer = NULL;
372         }
373
374         return TRUE;
375 }
376
377 /****************************************************************************
378 ****************************************************************************/
379
380 static BOOL test_EnumPrintProcessors(struct torture_context *tctx,
381                                      LPSTR servername,
382                                      LPSTR architecture)
383 {
384         DWORD levels[]  = { 1 };
385         DWORD success[] = { 1 };
386         DWORD i;
387         LPBYTE buffer = NULL;
388
389         for (i=0; i < ARRAY_SIZE(levels); i++) {
390
391                 DWORD needed = 0;
392                 DWORD returned = 0;
393                 DWORD err = 0;
394                 char tmp[1024];
395
396                 torture_comment(tctx, "Testing EnumPrintProcessors level %d", levels[i]);
397
398                 EnumPrintProcessors(servername, architecture, levels[i], NULL, 0, &needed, &returned);
399                 err = GetLastError();
400                 if (err == ERROR_INSUFFICIENT_BUFFER) {
401                         err = 0;
402                         buffer = malloc(needed);
403                         torture_assert(tctx, buffer, "malloc failed");
404                         if (!EnumPrintProcessors(servername, architecture, levels[i], buffer, needed, &needed, &returned)) {
405                                 err = GetLastError();
406                         }
407                 }
408                 if (err) {
409                         sprintf(tmp, "EnumPrintProcessors failed level %d on [%s] (buffer size = %d), error: %s\n",
410                                 levels[i], servername, needed, errstr(err));
411                         if (success[i]) {
412                                 torture_fail(tctx, tmp);
413                         } else {
414                                 torture_warning(tctx, tmp);
415                         }
416                 }
417
418                 if (tctx->print) {
419                         print_printprocessor_info_bylevel(levels[i], buffer, returned);
420                 }
421
422                 free(buffer);
423                 buffer = NULL;
424         }
425
426         return TRUE;
427 }
428
429 /****************************************************************************
430 ****************************************************************************/
431
432 static BOOL test_EnumPrintProcessorDatatypes(struct torture_context *tctx,
433                                              LPSTR servername)
434 {
435         DWORD levels[]  = { 1 };
436         DWORD success[] = { 1 };
437         DWORD i;
438         LPBYTE buffer = NULL;
439
440         for (i=0; i < ARRAY_SIZE(levels); i++) {
441
442                 DWORD needed = 0;
443                 DWORD returned = 0;
444                 DWORD err = 0;
445                 char tmp[1024];
446
447                 torture_comment(tctx, "Testing EnumPrintProcessorDatatypes level %d", levels[i]);
448
449                 EnumPrintProcessorDatatypes(servername, "winprint", levels[i], NULL, 0, &needed, &returned);
450                 err = GetLastError();
451                 if (err == ERROR_INSUFFICIENT_BUFFER) {
452                         err = 0;
453                         buffer = malloc(needed);
454                         torture_assert(tctx, buffer, "malloc failed");
455                         if (!EnumPrintProcessorDatatypes(servername, "winprint", levels[i], buffer, needed, &needed, &returned)) {
456                                 err = GetLastError();
457                         }
458                 }
459                 if (err) {
460                         sprintf(tmp, "EnumPrintProcessorDatatypes failed level %d on [%s] (buffer size = %d), error: %s\n",
461                                 levels[i], servername, needed, errstr(err));
462                         if (success[i]) {
463                                 torture_fail(tctx, tmp);
464                         } else {
465                                 torture_warning(tctx, tmp);
466                         }
467                 }
468
469                 free(buffer);
470                 buffer = NULL;
471         }
472
473         return TRUE;
474 }
475
476 /****************************************************************************
477 ****************************************************************************/
478
479 static BOOL test_EnumPrinterKey(struct torture_context *tctx,
480                                 LPSTR servername,
481                                 HANDLE handle,
482                                 LPCSTR key)
483 {
484         LPSTR buffer = NULL;
485         DWORD needed = 0;
486         DWORD err = 0;
487         char tmp[1024];
488
489         torture_comment(tctx, "Testing EnumPrinterKey(%s)", key);
490
491         err = EnumPrinterKey(handle, key, NULL, 0, &needed);
492         if (err == ERROR_MORE_DATA) {
493                 buffer = (LPTSTR)malloc(needed);
494                 torture_assert(tctx, buffer, "malloc failed");
495                 err = EnumPrinterKey(handle, key, buffer, needed, &needed);
496         }
497         if (err) {
498                 sprintf(tmp, "EnumPrinterKey(%s) failed on [%s] (buffer size = %d), error: %s\n",
499                         key, servername, needed, errstr(err));
500                 torture_fail(tctx, tmp);
501         }
502
503         if (tctx->print) {
504                 print_printer_keys(buffer);
505         }
506
507         free(buffer);
508
509         return TRUE;
510 }
511
512 /****************************************************************************
513 ****************************************************************************/
514
515 static BOOL test_GetPrinter(struct torture_context *tctx,
516                             LPSTR printername,
517                             HANDLE handle)
518 {
519         DWORD levels[]  = { 1, 2, 3, 4, 5, 6, 7, 8 };
520         DWORD success[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
521         DWORD i;
522         LPBYTE buffer = NULL;
523
524         for (i=0; i < ARRAY_SIZE(levels); i++) {
525
526                 DWORD needed = 0;
527                 DWORD err = 0;
528                 char tmp[1024];
529
530                 torture_comment(tctx, "Testing GetPrinter level %d", levels[i]);
531
532                 GetPrinter(handle, levels[i], NULL, 0, &needed);
533                 err = GetLastError();
534                 if (err == ERROR_INSUFFICIENT_BUFFER) {
535                         err = 0;
536                         buffer = malloc(needed);
537                         torture_assert(tctx, buffer, "malloc failed");
538                         if (!GetPrinter(handle, levels[i], buffer, needed, &needed)) {
539                                 err = GetLastError();
540                         }
541                 }
542                 if (err) {
543                         sprintf(tmp, "GetPrinter failed level %d on [%s] (buffer size = %d), error: %s\n",
544                                 levels[i], printername, needed, errstr(err));
545                         if (success[i]) {
546                                 torture_fail(tctx, tmp);
547                         } else {
548                                 torture_warning(tctx, tmp);
549                         }
550                 }
551
552                 if (tctx->print) {
553                         print_printer_info_bylevel(levels[i], buffer, 1);
554                 }
555
556                 free(buffer);
557                 buffer = NULL;
558         }
559
560         return TRUE;
561 }
562
563 /****************************************************************************
564 ****************************************************************************/
565
566 static BOOL test_GetPrinterDriver(struct torture_context *tctx,
567                                   LPSTR printername,
568                                   LPSTR architecture,
569                                   HANDLE handle)
570 {
571         DWORD levels[]  = { 1, 2, 3, 4, 5, 6, 8 };
572         DWORD success[] = { 1, 1, 1, 1, 1, 1, 1 };
573         DWORD i;
574         LPBYTE buffer = NULL;
575
576         for (i=0; i < ARRAY_SIZE(levels); i++) {
577
578                 DWORD needed = 0;
579                 DWORD err = 0;
580                 char tmp[1024];
581
582                 torture_comment(tctx, "Testing GetPrinterDriver level %d", levels[i]);
583
584                 GetPrinterDriver(handle, architecture, levels[i], NULL, 0, &needed);
585                 err = GetLastError();
586                 if (err == ERROR_INSUFFICIENT_BUFFER) {
587                         err = 0;
588                         buffer = malloc(needed);
589                         torture_assert(tctx, buffer, "malloc failed");
590                         if (!GetPrinterDriver(handle, architecture, levels[i], buffer, needed, &needed)) {
591                                 err = GetLastError();
592                         }
593                 }
594                 if (err) {
595                         sprintf(tmp, "GetPrinterDriver failed level %d on [%s] (buffer size = %d), error: %s\n",
596                                 levels[i], printername, needed, errstr(err));
597                         if (success[i]) {
598                                 torture_fail(tctx, tmp);
599                         } else {
600                                 torture_warning(tctx, tmp);
601                         }
602                 }
603
604                 if (tctx->print) {
605                         print_driver_info_bylevel(levels[i], buffer, 1);
606                 }
607
608                 free(buffer);
609                 buffer = NULL;
610         }
611
612         return TRUE;
613 }
614
615
616 /****************************************************************************
617 ****************************************************************************/
618
619 static BOOL test_EnumJobs(struct torture_context *tctx,
620                           LPSTR printername,
621                           HANDLE handle)
622 {
623         DWORD levels[]  = { 1, 2, 3, 4 };
624         DWORD success[] = { 1, 1, 1, 1 };
625         DWORD i;
626         LPBYTE buffer = NULL;
627
628         for (i=0; i < ARRAY_SIZE(levels); i++) {
629
630                 DWORD needed = 0;
631                 DWORD returned = 0;
632                 DWORD err = 0;
633                 char tmp[1024];
634
635                 torture_comment(tctx, "Testing EnumJobs level %d", levels[i]);
636
637                 EnumJobs(handle, 0, 100, levels[i], NULL, 0, &needed, &returned);
638                 err = GetLastError();
639                 if (err == ERROR_INSUFFICIENT_BUFFER) {
640                         err = 0;
641                         buffer = malloc(needed);
642                         torture_assert(tctx, buffer, "malloc failed");
643                         if (!EnumJobs(handle, 0, 100, levels[i], buffer, needed, &needed, &returned)) {
644                                 err = GetLastError();
645                         }
646                 }
647                 if (err) {
648                         sprintf(tmp, "EnumJobs failed level %d on [%s] (buffer size = %d), error: %s\n",
649                                 levels[i], printername, needed, errstr(err));
650                         if (success[i]) {
651                                 torture_fail(tctx, tmp);
652                         } else {
653                                 torture_warning(tctx, tmp);
654                         }
655                 }
656
657                 free(buffer);
658                 buffer = NULL;
659         }
660
661         return TRUE;
662 }
663
664 /****************************************************************************
665 ****************************************************************************/
666
667 static BOOL test_EnumPrinterDataEx(struct torture_context *tctx,
668                                    LPSTR servername,
669                                    LPSTR keyname,
670                                    HANDLE handle,
671                                    LPBYTE *buffer_p,
672                                    DWORD *returned_p)
673 {
674         LPBYTE buffer = NULL;
675         DWORD needed = 0;
676         DWORD returned = 0;
677         DWORD err = 0;
678         char tmp[1024];
679
680         torture_comment(tctx, "Testing EnumPrinterDataEx(%s)", keyname);
681
682         err = EnumPrinterDataEx(handle, keyname, NULL, 0, &needed, &returned);
683         if (err == ERROR_MORE_DATA) {
684                 buffer = malloc(needed);
685                 torture_assert(tctx, buffer, "malloc failed");
686                 err = EnumPrinterDataEx(handle, keyname, buffer, needed, &needed, &returned);
687         }
688         if (err) {
689                 sprintf(tmp, "EnumPrinterDataEx(%s) failed on [%s] (buffer size = %d), error: %s\n",
690                         keyname, servername, needed, errstr(err));
691                 torture_fail(tctx, tmp);
692         }
693
694         if (tctx->print) {
695                 DWORD i;
696                 LPPRINTER_ENUM_VALUES v = (LPPRINTER_ENUM_VALUES)buffer;
697                 for (i=0; i < returned; i++) {
698                         print_printer_enum_values(&v[i]);
699                 }
700         }
701
702         if (returned_p) {
703                 *returned_p = returned;
704         }
705
706         if (buffer_p) {
707                 *buffer_p = buffer;
708         } else {
709                 free(buffer);
710         }
711
712         return TRUE;
713 }
714
715
716 /****************************************************************************
717 ****************************************************************************/
718
719 static BOOL test_OnePrinter(struct torture_context *tctx,
720                             LPSTR printername,
721                             LPSTR architecture,
722                             LPPRINTER_DEFAULTS defaults)
723 {
724         HANDLE handle;
725         BOOL ret = TRUE;
726
727         torture_comment(tctx, "Testing Printer %s", printername);
728
729         ret &= test_OpenPrinter(tctx, printername, defaults, &handle);
730         ret &= test_GetPrinter(tctx, printername, handle);
731         ret &= test_GetPrinterDriver(tctx, printername, architecture, handle);
732         ret &= test_EnumForms(tctx, printername, handle);
733         ret &= test_EnumJobs(tctx, printername, handle);
734         ret &= test_EnumPrinterKey(tctx, printername, handle, "");
735         ret &= test_EnumPrinterKey(tctx, printername, handle, "PrinterDriverData");
736         ret &= test_EnumPrinterDataEx(tctx, printername, "PrinterDriverData", handle, NULL, NULL);
737         ret &= test_ClosePrinter(tctx, handle);
738
739         return ret;
740 }
741
742 /****************************************************************************
743 ****************************************************************************/
744
745 static BOOL test_EachPrinter(struct torture_context *tctx,
746                              LPSTR servername,
747                              LPSTR architecture,
748                              LPPRINTER_DEFAULTS defaults)
749 {
750         DWORD needed = 0;
751         DWORD returned = 0;
752         DWORD err = 0;
753         char tmp[1024];
754         DWORD i;
755         DWORD flags = PRINTER_ENUM_NAME;
756         PPRINTER_INFO_1 buffer = NULL;
757         BOOL ret = TRUE;
758
759         torture_comment(tctx, "Testing EnumPrinters level %d", 1);
760
761         EnumPrinters(flags, servername, 1, NULL, 0, &needed, &returned);
762         err = GetLastError();
763         if (err == ERROR_INSUFFICIENT_BUFFER) {
764                 err = 0;
765                 buffer = (PPRINTER_INFO_1)malloc(needed);
766                 torture_assert(tctx, buffer, "malloc failed");
767                 if (!EnumPrinters(flags, servername, 1, (LPBYTE)buffer, needed, &needed, &returned)) {
768                         err = GetLastError();
769                 }
770         }
771         if (err) {
772                 sprintf(tmp, "EnumPrinters failed level %d on [%s] (buffer size = %d), error: %s\n",
773                         1, servername, needed, errstr(err));
774                 torture_fail(tctx, tmp);
775         }
776
777         for (i=0; i < returned; i++) {
778                 ret &= test_OnePrinter(tctx, buffer[i].pName, architecture, defaults);
779         }
780
781         free(buffer);
782
783         return ret;
784 }
785
786 /****************************************************************************
787 ****************************************************************************/
788
789 static BOOL test_GetPrintProcessorDirectory(struct torture_context *tctx,
790                                             LPSTR servername,
791                                             LPSTR architecture)
792 {
793         DWORD levels[]  = { 1 };
794         DWORD success[] = { 1 };
795         DWORD i;
796         LPBYTE buffer = NULL;
797
798         for (i=0; i < ARRAY_SIZE(levels); i++) {
799
800                 DWORD needed = 0;
801                 DWORD err = 0;
802                 char tmp[1024];
803
804                 torture_comment(tctx, "Testing GetPrintProcessorDirectory level %d", levels[i]);
805
806                 GetPrintProcessorDirectory(servername, architecture, levels[i], NULL, 0, &needed);
807                 err = GetLastError();
808                 if (err == ERROR_INSUFFICIENT_BUFFER) {
809                         err = 0;
810                         buffer = malloc(needed);
811                         torture_assert(tctx, buffer, "malloc failed");
812                         if (!GetPrintProcessorDirectory(servername, architecture, levels[i], buffer, needed, &needed)) {
813                                 err = GetLastError();
814                         }
815                 }
816                 if (err) {
817                         sprintf(tmp, "GetPrintProcessorDirectory failed level %d on [%s] (buffer size = %d), error: %s\n",
818                                 levels[i], servername, needed, errstr(err));
819                         if (success[i]) {
820                                 torture_fail(tctx, tmp);
821                         } else {
822                                 torture_warning(tctx, tmp);
823                         }
824                 }
825
826                 free(buffer);
827                 buffer = NULL;
828         }
829
830         return TRUE;
831 }
832
833 /****************************************************************************
834 ****************************************************************************/
835
836 static BOOL test_GetPrinterDriverDirectory(struct torture_context *tctx,
837                                            LPSTR servername,
838                                            LPSTR architecture)
839 {
840         DWORD levels[]  = { 1 };
841         DWORD success[] = { 1 };
842         DWORD i;
843         LPBYTE buffer = NULL;
844
845         for (i=0; i < ARRAY_SIZE(levels); i++) {
846
847                 DWORD needed = 0;
848                 DWORD err = 0;
849                 char tmp[1024];
850
851                 torture_comment(tctx, "Testing GetPrinterDriverDirectory level %d", levels[i]);
852
853                 GetPrinterDriverDirectory(servername, architecture, levels[i], NULL, 0, &needed);
854                 err = GetLastError();
855                 if (err == ERROR_INSUFFICIENT_BUFFER) {
856                         err = 0;
857                         buffer = malloc(needed);
858                         torture_assert(tctx, buffer, "malloc failed");
859                         if (!GetPrinterDriverDirectory(servername, architecture, levels[i], buffer, needed, &needed)) {
860                                 err = GetLastError();
861                         }
862                 }
863                 if (err) {
864                         sprintf(tmp, "GetPrinterDriverDirectory failed level %d on [%s] (buffer size = %d), error: %s\n",
865                                 levels[i], servername, needed, errstr(err));
866                         if (success[i]) {
867                                 torture_fail(tctx, tmp);
868                         } else {
869                                 torture_warning(tctx, tmp);
870                         }
871                 }
872
873                 free(buffer);
874                 buffer = NULL;
875         }
876
877         return TRUE;
878 }
879
880 /****************************************************************************
881 ****************************************************************************/
882
883 static BOOL test_GetPrinterData(struct torture_context *tctx,
884                                 LPSTR servername,
885                                 LPSTR valuename,
886                                 HANDLE handle,
887                                 DWORD *type_p,
888                                 LPBYTE *buffer_p,
889                                 DWORD *size_p)
890 {
891         LPBYTE buffer = NULL;
892         DWORD needed = 0;
893         DWORD type;
894         DWORD err = 0;
895         char tmp[1024];
896
897         torture_comment(tctx, "Testing GetPrinterData(%s)", valuename);
898
899         err = GetPrinterData(handle, valuename, &type, NULL, 0, &needed);
900         if (err == ERROR_MORE_DATA) {
901                 buffer = (LPBYTE)malloc(needed);
902                 torture_assert(tctx, buffer, "malloc failed");
903                 err = GetPrinterData(handle, valuename, &type, buffer, needed, &needed);
904         }
905         if (err) {
906                 sprintf(tmp, "GetPrinterData(%s) failed on [%s] (buffer size = %d), error: %s\n",
907                         valuename, servername, needed, errstr(err));
908                 torture_fail(tctx, tmp);
909         }
910
911         if (tctx->print) {
912                 print_printer_data("PrinterDriverData", valuename, needed, buffer, type);
913         }
914
915         if (type_p) {
916                 *type_p = type;
917         }
918
919         if (size_p) {
920                 *size_p = needed;
921         }
922
923         if (buffer_p) {
924                 *buffer_p = buffer;
925         } else {
926                 free(buffer);
927         }
928
929         return TRUE;
930 }
931
932 /****************************************************************************
933 ****************************************************************************/
934
935 static BOOL test_GetPrinterDataEx(struct torture_context *tctx,
936                                   LPSTR servername,
937                                   LPSTR keyname,
938                                   LPSTR valuename,
939                                   HANDLE handle,
940                                   DWORD *type_p,
941                                   LPBYTE *buffer_p,
942                                   DWORD *size_p)
943 {
944         LPBYTE buffer = NULL;
945         DWORD needed = 0;
946         DWORD type;
947         DWORD err = 0;
948         char tmp[1024];
949
950         torture_comment(tctx, "Testing GetPrinterDataEx(%s - %s)", keyname, valuename);
951
952         err = GetPrinterDataEx(handle, keyname, valuename, &type, NULL, 0, &needed);
953         if (err == ERROR_MORE_DATA) {
954                 buffer = (LPBYTE)malloc(needed);
955                 torture_assert(tctx, buffer, "malloc failed");
956                 err = GetPrinterDataEx(handle, keyname, valuename, &type, buffer, needed, &needed);
957         }
958         if (err) {
959                 sprintf(tmp, "GetPrinterDataEx(%s) failed on [%s] (buffer size = %d), error: %s\n",
960                         valuename, servername, needed, errstr(err));
961                 torture_fail(tctx, tmp);
962         }
963
964         if (tctx->print) {
965                 print_printer_data(keyname, valuename, needed, buffer, type);
966         }
967
968         if (type_p) {
969                 *type_p = type;
970         }
971
972         if (size_p) {
973                 *size_p = needed;
974         }
975
976         if (buffer_p) {
977                 *buffer_p = buffer;
978         } else {
979                 free(buffer);
980         }
981
982         return TRUE;
983 }
984
985 /****************************************************************************
986 ****************************************************************************/
987
988 static BOOL test_PrinterData(struct torture_context *tctx,
989                              LPSTR servername,
990                              HANDLE handle)
991 {
992         BOOL ret = TRUE;
993         DWORD i;
994         DWORD type, type_ex;
995         LPBYTE buffer, buffer_ex;
996         DWORD size, size_ex;
997         LPSTR valuenames[] = {
998                 SPLREG_DEFAULT_SPOOL_DIRECTORY,
999                 SPLREG_MAJOR_VERSION,
1000                 SPLREG_MINOR_VERSION,
1001                 SPLREG_DS_PRESENT,
1002                 SPLREG_DNS_MACHINE_NAME,
1003                 SPLREG_ARCHITECTURE,
1004                 SPLREG_OS_VERSION
1005         };
1006
1007         for (i=0; i < ARRAY_SIZE(valuenames); i++) {
1008                 ret &= test_GetPrinterData(tctx, servername, valuenames[i], handle, &type, &buffer, &size);
1009                 ret &= test_GetPrinterDataEx(tctx, servername, "random", valuenames[i], handle, &type_ex, &buffer_ex, &size_ex);
1010                 torture_assert_int_equal(tctx, type, type_ex, "type mismatch");
1011                 torture_assert_int_equal(tctx, size, size_ex, "size mismatch");
1012                 torture_assert_mem_equal(tctx, buffer, buffer_ex, size, "buffer mismatch");
1013                 free(buffer);
1014                 free(buffer_ex);
1015         }
1016
1017         return ret;
1018 }
1019
1020 /****************************************************************************
1021 ****************************************************************************/
1022
1023 int main(int argc, char *argv[])
1024 {
1025         BOOL ret = FALSE;
1026         LPSTR servername;
1027         LPSTR architecture = "Windows NT x86";
1028         HANDLE server_handle;
1029         PRINTER_DEFAULTS defaults_admin, defaults_use;
1030         struct torture_context *tctx;
1031
1032         if (argc < 2) {
1033                 fprintf(stderr, "usage: %s <servername> [print]\n", argv[0]);
1034                 exit(-1);
1035         }
1036
1037         tctx = malloc(sizeof(struct torture_context));
1038         if (!tctx) {
1039                 fprintf(stderr, "out of memory\n");
1040                 exit(-1);
1041         }
1042         memset(tctx, '\0', sizeof(*tctx));
1043
1044         servername = argv[1];
1045
1046         if (argc >= 3) {
1047                 if (strcmp(argv[2], "print") == 0) {
1048                         tctx->print = TRUE;
1049                 }
1050         }
1051
1052         defaults_admin.pDatatype = NULL;
1053         defaults_admin.pDevMode = NULL;
1054         defaults_admin.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
1055
1056         defaults_use.pDatatype = NULL;
1057         defaults_use.pDevMode = NULL;
1058         defaults_use.DesiredAccess = PRINTER_ACCESS_USE;
1059
1060         ret &= test_EnumPrinters(tctx, servername);
1061         ret &= test_EnumDrivers(tctx, servername, architecture);
1062         ret &= test_OpenPrinter(tctx, servername, NULL, &server_handle);
1063 /*      ret &= test_EnumPrinterKey(tctx, servername, server_handle, ""); */
1064         ret &= test_PrinterData(tctx, servername, server_handle);
1065         ret &= test_EnumForms(tctx, servername, server_handle);
1066         ret &= test_ClosePrinter(tctx, server_handle);
1067         ret &= test_EnumPorts(tctx, servername);
1068         ret &= test_EnumMonitors(tctx, servername);
1069         ret &= test_EnumPrintProcessors(tctx, servername, architecture);
1070         ret &= test_EnumPrintProcessorDatatypes(tctx, servername);
1071         ret &= test_GetPrintProcessorDirectory(tctx, servername, architecture);
1072         ret &= test_GetPrinterDriverDirectory(tctx, servername, architecture);
1073         ret &= test_EachPrinter(tctx, servername, architecture, NULL);
1074
1075         if (!ret) {
1076                 if (tctx->last_reason) {
1077                         fprintf(stderr, "failed: %s\n", tctx->last_reason);
1078                 }
1079                 free(tctx);
1080                 return -1;
1081         }
1082
1083         printf("%s run successfully\n", argv[0]);
1084
1085         free(tctx);
1086         return 0;
1087 }