ae8fc0fb129f4dd05fcba79d53bb2cd6c2fbf302
[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                 if (tctx->print) {
470                         print_datatypes_info_bylevel(levels[i], buffer, returned);
471                 }
472
473                 free(buffer);
474                 buffer = NULL;
475         }
476
477         return TRUE;
478 }
479
480 /****************************************************************************
481 ****************************************************************************/
482
483 static BOOL test_EnumPrinterKey(struct torture_context *tctx,
484                                 LPSTR servername,
485                                 HANDLE handle,
486                                 LPCSTR key)
487 {
488         LPSTR buffer = NULL;
489         DWORD needed = 0;
490         DWORD err = 0;
491         char tmp[1024];
492
493         torture_comment(tctx, "Testing EnumPrinterKey(%s)", key);
494
495         err = EnumPrinterKey(handle, key, NULL, 0, &needed);
496         if (err == ERROR_MORE_DATA) {
497                 buffer = (LPTSTR)malloc(needed);
498                 torture_assert(tctx, buffer, "malloc failed");
499                 err = EnumPrinterKey(handle, key, buffer, needed, &needed);
500         }
501         if (err) {
502                 sprintf(tmp, "EnumPrinterKey(%s) failed on [%s] (buffer size = %d), error: %s\n",
503                         key, servername, needed, errstr(err));
504                 torture_fail(tctx, tmp);
505         }
506
507         if (tctx->print) {
508                 print_printer_keys(buffer);
509         }
510
511         free(buffer);
512
513         return TRUE;
514 }
515
516 /****************************************************************************
517 ****************************************************************************/
518
519 static BOOL test_GetPrinter(struct torture_context *tctx,
520                             LPSTR printername,
521                             HANDLE handle)
522 {
523         DWORD levels[]  = { 1, 2, 3, 4, 5, 6, 7, 8 };
524         DWORD success[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
525         DWORD i;
526         LPBYTE buffer = NULL;
527
528         for (i=0; i < ARRAY_SIZE(levels); i++) {
529
530                 DWORD needed = 0;
531                 DWORD err = 0;
532                 char tmp[1024];
533
534                 torture_comment(tctx, "Testing GetPrinter level %d", levels[i]);
535
536                 GetPrinter(handle, levels[i], NULL, 0, &needed);
537                 err = GetLastError();
538                 if (err == ERROR_INSUFFICIENT_BUFFER) {
539                         err = 0;
540                         buffer = malloc(needed);
541                         torture_assert(tctx, buffer, "malloc failed");
542                         if (!GetPrinter(handle, levels[i], buffer, needed, &needed)) {
543                                 err = GetLastError();
544                         }
545                 }
546                 if (err) {
547                         sprintf(tmp, "GetPrinter failed level %d on [%s] (buffer size = %d), error: %s\n",
548                                 levels[i], printername, needed, errstr(err));
549                         if (success[i]) {
550                                 torture_fail(tctx, tmp);
551                         } else {
552                                 torture_warning(tctx, tmp);
553                         }
554                 }
555
556                 if (tctx->print) {
557                         print_printer_info_bylevel(levels[i], buffer, 1);
558                 }
559
560                 free(buffer);
561                 buffer = NULL;
562         }
563
564         return TRUE;
565 }
566
567 /****************************************************************************
568 ****************************************************************************/
569
570 static BOOL test_GetPrinterDriver(struct torture_context *tctx,
571                                   LPSTR printername,
572                                   LPSTR architecture,
573                                   HANDLE handle)
574 {
575         DWORD levels[]  = { 1, 2, 3, 4, 5, 6, 8 };
576         DWORD success[] = { 1, 1, 1, 1, 1, 1, 1 };
577         DWORD i;
578         LPBYTE buffer = NULL;
579
580         for (i=0; i < ARRAY_SIZE(levels); i++) {
581
582                 DWORD needed = 0;
583                 DWORD err = 0;
584                 char tmp[1024];
585
586                 torture_comment(tctx, "Testing GetPrinterDriver level %d", levels[i]);
587
588                 GetPrinterDriver(handle, architecture, levels[i], NULL, 0, &needed);
589                 err = GetLastError();
590                 if (err == ERROR_INSUFFICIENT_BUFFER) {
591                         err = 0;
592                         buffer = malloc(needed);
593                         torture_assert(tctx, buffer, "malloc failed");
594                         if (!GetPrinterDriver(handle, architecture, levels[i], buffer, needed, &needed)) {
595                                 err = GetLastError();
596                         }
597                 }
598                 if (err) {
599                         sprintf(tmp, "GetPrinterDriver failed level %d on [%s] (buffer size = %d), error: %s\n",
600                                 levels[i], printername, needed, errstr(err));
601                         if (success[i]) {
602                                 torture_fail(tctx, tmp);
603                         } else {
604                                 torture_warning(tctx, tmp);
605                         }
606                 }
607
608                 if (tctx->print) {
609                         print_driver_info_bylevel(levels[i], buffer, 1);
610                 }
611
612                 free(buffer);
613                 buffer = NULL;
614         }
615
616         return TRUE;
617 }
618
619
620 /****************************************************************************
621 ****************************************************************************/
622
623 static BOOL test_EnumJobs(struct torture_context *tctx,
624                           LPSTR printername,
625                           HANDLE handle)
626 {
627         DWORD levels[]  = { 1, 2, 3, 4 };
628         DWORD success[] = { 1, 1, 1, 1 };
629         DWORD i;
630         LPBYTE buffer = NULL;
631
632         for (i=0; i < ARRAY_SIZE(levels); i++) {
633
634                 DWORD needed = 0;
635                 DWORD returned = 0;
636                 DWORD err = 0;
637                 char tmp[1024];
638
639                 torture_comment(tctx, "Testing EnumJobs level %d", levels[i]);
640
641                 EnumJobs(handle, 0, 100, levels[i], NULL, 0, &needed, &returned);
642                 err = GetLastError();
643                 if (err == ERROR_INSUFFICIENT_BUFFER) {
644                         err = 0;
645                         buffer = malloc(needed);
646                         torture_assert(tctx, buffer, "malloc failed");
647                         if (!EnumJobs(handle, 0, 100, levels[i], buffer, needed, &needed, &returned)) {
648                                 err = GetLastError();
649                         }
650                 }
651                 if (err) {
652                         sprintf(tmp, "EnumJobs failed level %d on [%s] (buffer size = %d), error: %s\n",
653                                 levels[i], printername, needed, errstr(err));
654                         if (success[i]) {
655                                 torture_fail(tctx, tmp);
656                         } else {
657                                 torture_warning(tctx, tmp);
658                         }
659                 }
660
661                 free(buffer);
662                 buffer = NULL;
663         }
664
665         return TRUE;
666 }
667
668 /****************************************************************************
669 ****************************************************************************/
670
671 static BOOL test_EnumPrinterDataEx(struct torture_context *tctx,
672                                    LPSTR servername,
673                                    LPSTR keyname,
674                                    HANDLE handle,
675                                    LPBYTE *buffer_p,
676                                    DWORD *returned_p)
677 {
678         LPBYTE buffer = NULL;
679         DWORD needed = 0;
680         DWORD returned = 0;
681         DWORD err = 0;
682         char tmp[1024];
683
684         torture_comment(tctx, "Testing EnumPrinterDataEx(%s)", keyname);
685
686         err = EnumPrinterDataEx(handle, keyname, NULL, 0, &needed, &returned);
687         if (err == ERROR_MORE_DATA) {
688                 buffer = malloc(needed);
689                 torture_assert(tctx, buffer, "malloc failed");
690                 err = EnumPrinterDataEx(handle, keyname, buffer, needed, &needed, &returned);
691         }
692         if (err) {
693                 sprintf(tmp, "EnumPrinterDataEx(%s) failed on [%s] (buffer size = %d), error: %s\n",
694                         keyname, servername, needed, errstr(err));
695                 torture_fail(tctx, tmp);
696         }
697
698         if (tctx->print) {
699                 DWORD i;
700                 LPPRINTER_ENUM_VALUES v = (LPPRINTER_ENUM_VALUES)buffer;
701                 for (i=0; i < returned; i++) {
702                         print_printer_enum_values(&v[i]);
703                 }
704         }
705
706         if (returned_p) {
707                 *returned_p = returned;
708         }
709
710         if (buffer_p) {
711                 *buffer_p = buffer;
712         } else {
713                 free(buffer);
714         }
715
716         return TRUE;
717 }
718
719
720 /****************************************************************************
721 ****************************************************************************/
722
723 static BOOL test_OnePrinter(struct torture_context *tctx,
724                             LPSTR printername,
725                             LPSTR architecture,
726                             LPPRINTER_DEFAULTS defaults)
727 {
728         HANDLE handle;
729         BOOL ret = TRUE;
730
731         torture_comment(tctx, "Testing Printer %s", printername);
732
733         ret &= test_OpenPrinter(tctx, printername, defaults, &handle);
734         ret &= test_GetPrinter(tctx, printername, handle);
735         ret &= test_GetPrinterDriver(tctx, printername, architecture, handle);
736         ret &= test_EnumForms(tctx, printername, handle);
737         ret &= test_EnumJobs(tctx, printername, handle);
738         ret &= test_EnumPrinterKey(tctx, printername, handle, "");
739         ret &= test_EnumPrinterKey(tctx, printername, handle, "PrinterDriverData");
740         ret &= test_EnumPrinterDataEx(tctx, printername, "PrinterDriverData", handle, NULL, NULL);
741         ret &= test_ClosePrinter(tctx, handle);
742
743         return ret;
744 }
745
746 /****************************************************************************
747 ****************************************************************************/
748
749 static BOOL test_EachPrinter(struct torture_context *tctx,
750                              LPSTR servername,
751                              LPSTR architecture,
752                              LPPRINTER_DEFAULTS defaults)
753 {
754         DWORD needed = 0;
755         DWORD returned = 0;
756         DWORD err = 0;
757         char tmp[1024];
758         DWORD i;
759         DWORD flags = PRINTER_ENUM_NAME;
760         PPRINTER_INFO_1 buffer = NULL;
761         BOOL ret = TRUE;
762
763         torture_comment(tctx, "Testing EnumPrinters level %d", 1);
764
765         EnumPrinters(flags, servername, 1, NULL, 0, &needed, &returned);
766         err = GetLastError();
767         if (err == ERROR_INSUFFICIENT_BUFFER) {
768                 err = 0;
769                 buffer = (PPRINTER_INFO_1)malloc(needed);
770                 torture_assert(tctx, buffer, "malloc failed");
771                 if (!EnumPrinters(flags, servername, 1, (LPBYTE)buffer, needed, &needed, &returned)) {
772                         err = GetLastError();
773                 }
774         }
775         if (err) {
776                 sprintf(tmp, "EnumPrinters failed level %d on [%s] (buffer size = %d), error: %s\n",
777                         1, servername, needed, errstr(err));
778                 torture_fail(tctx, tmp);
779         }
780
781         for (i=0; i < returned; i++) {
782                 ret &= test_OnePrinter(tctx, buffer[i].pName, architecture, defaults);
783         }
784
785         free(buffer);
786
787         return ret;
788 }
789
790 /****************************************************************************
791 ****************************************************************************/
792
793 static BOOL test_GetPrintProcessorDirectory(struct torture_context *tctx,
794                                             LPSTR servername,
795                                             LPSTR architecture)
796 {
797         DWORD levels[]  = { 1 };
798         DWORD success[] = { 1 };
799         DWORD i;
800         LPBYTE buffer = NULL;
801
802         for (i=0; i < ARRAY_SIZE(levels); i++) {
803
804                 DWORD needed = 0;
805                 DWORD err = 0;
806                 char tmp[1024];
807
808                 torture_comment(tctx, "Testing GetPrintProcessorDirectory level %d", levels[i]);
809
810                 GetPrintProcessorDirectory(servername, architecture, levels[i], NULL, 0, &needed);
811                 err = GetLastError();
812                 if (err == ERROR_INSUFFICIENT_BUFFER) {
813                         err = 0;
814                         buffer = malloc(needed);
815                         torture_assert(tctx, buffer, "malloc failed");
816                         if (!GetPrintProcessorDirectory(servername, architecture, levels[i], buffer, needed, &needed)) {
817                                 err = GetLastError();
818                         }
819                 }
820                 if (err) {
821                         sprintf(tmp, "GetPrintProcessorDirectory failed level %d on [%s] (buffer size = %d), error: %s\n",
822                                 levels[i], servername, needed, errstr(err));
823                         if (success[i]) {
824                                 torture_fail(tctx, tmp);
825                         } else {
826                                 torture_warning(tctx, tmp);
827                         }
828                 }
829
830                 free(buffer);
831                 buffer = NULL;
832         }
833
834         return TRUE;
835 }
836
837 /****************************************************************************
838 ****************************************************************************/
839
840 static BOOL test_GetPrinterDriverDirectory(struct torture_context *tctx,
841                                            LPSTR servername,
842                                            LPSTR architecture)
843 {
844         DWORD levels[]  = { 1 };
845         DWORD success[] = { 1 };
846         DWORD i;
847         LPBYTE buffer = NULL;
848
849         for (i=0; i < ARRAY_SIZE(levels); i++) {
850
851                 DWORD needed = 0;
852                 DWORD err = 0;
853                 char tmp[1024];
854
855                 torture_comment(tctx, "Testing GetPrinterDriverDirectory level %d", levels[i]);
856
857                 GetPrinterDriverDirectory(servername, architecture, levels[i], NULL, 0, &needed);
858                 err = GetLastError();
859                 if (err == ERROR_INSUFFICIENT_BUFFER) {
860                         err = 0;
861                         buffer = malloc(needed);
862                         torture_assert(tctx, buffer, "malloc failed");
863                         if (!GetPrinterDriverDirectory(servername, architecture, levels[i], buffer, needed, &needed)) {
864                                 err = GetLastError();
865                         }
866                 }
867                 if (err) {
868                         sprintf(tmp, "GetPrinterDriverDirectory failed level %d on [%s] (buffer size = %d), error: %s\n",
869                                 levels[i], servername, needed, errstr(err));
870                         if (success[i]) {
871                                 torture_fail(tctx, tmp);
872                         } else {
873                                 torture_warning(tctx, tmp);
874                         }
875                 }
876
877                 free(buffer);
878                 buffer = NULL;
879         }
880
881         return TRUE;
882 }
883
884 /****************************************************************************
885 ****************************************************************************/
886
887 static BOOL test_GetPrinterData(struct torture_context *tctx,
888                                 LPSTR servername,
889                                 LPSTR valuename,
890                                 HANDLE handle,
891                                 DWORD *type_p,
892                                 LPBYTE *buffer_p,
893                                 DWORD *size_p)
894 {
895         LPBYTE buffer = NULL;
896         DWORD needed = 0;
897         DWORD type;
898         DWORD err = 0;
899         char tmp[1024];
900
901         torture_comment(tctx, "Testing GetPrinterData(%s)", valuename);
902
903         err = GetPrinterData(handle, valuename, &type, NULL, 0, &needed);
904         if (err == ERROR_MORE_DATA) {
905                 buffer = (LPBYTE)malloc(needed);
906                 torture_assert(tctx, buffer, "malloc failed");
907                 err = GetPrinterData(handle, valuename, &type, buffer, needed, &needed);
908         }
909         if (err) {
910                 sprintf(tmp, "GetPrinterData(%s) failed on [%s] (buffer size = %d), error: %s\n",
911                         valuename, servername, needed, errstr(err));
912                 torture_fail(tctx, tmp);
913         }
914
915         if (tctx->print) {
916                 print_printer_data("PrinterDriverData", valuename, needed, buffer, type);
917         }
918
919         if (type_p) {
920                 *type_p = type;
921         }
922
923         if (size_p) {
924                 *size_p = needed;
925         }
926
927         if (buffer_p) {
928                 *buffer_p = buffer;
929         } else {
930                 free(buffer);
931         }
932
933         return TRUE;
934 }
935
936 /****************************************************************************
937 ****************************************************************************/
938
939 static BOOL test_GetPrinterDataEx(struct torture_context *tctx,
940                                   LPSTR servername,
941                                   LPSTR keyname,
942                                   LPSTR valuename,
943                                   HANDLE handle,
944                                   DWORD *type_p,
945                                   LPBYTE *buffer_p,
946                                   DWORD *size_p)
947 {
948         LPBYTE buffer = NULL;
949         DWORD needed = 0;
950         DWORD type;
951         DWORD err = 0;
952         char tmp[1024];
953
954         torture_comment(tctx, "Testing GetPrinterDataEx(%s - %s)", keyname, valuename);
955
956         err = GetPrinterDataEx(handle, keyname, valuename, &type, NULL, 0, &needed);
957         if (err == ERROR_MORE_DATA) {
958                 buffer = (LPBYTE)malloc(needed);
959                 torture_assert(tctx, buffer, "malloc failed");
960                 err = GetPrinterDataEx(handle, keyname, valuename, &type, buffer, needed, &needed);
961         }
962         if (err) {
963                 sprintf(tmp, "GetPrinterDataEx(%s) failed on [%s] (buffer size = %d), error: %s\n",
964                         valuename, servername, needed, errstr(err));
965                 torture_fail(tctx, tmp);
966         }
967
968         if (tctx->print) {
969                 print_printer_data(keyname, valuename, needed, buffer, type);
970         }
971
972         if (type_p) {
973                 *type_p = type;
974         }
975
976         if (size_p) {
977                 *size_p = needed;
978         }
979
980         if (buffer_p) {
981                 *buffer_p = buffer;
982         } else {
983                 free(buffer);
984         }
985
986         return TRUE;
987 }
988
989 /****************************************************************************
990 ****************************************************************************/
991
992 static BOOL test_PrinterData(struct torture_context *tctx,
993                              LPSTR servername,
994                              HANDLE handle)
995 {
996         BOOL ret = TRUE;
997         DWORD i;
998         DWORD type, type_ex;
999         LPBYTE buffer, buffer_ex;
1000         DWORD size, size_ex;
1001         LPSTR valuenames[] = {
1002                 SPLREG_DEFAULT_SPOOL_DIRECTORY,
1003                 SPLREG_MAJOR_VERSION,
1004                 SPLREG_MINOR_VERSION,
1005                 SPLREG_DS_PRESENT,
1006                 SPLREG_DNS_MACHINE_NAME,
1007                 SPLREG_ARCHITECTURE,
1008                 SPLREG_OS_VERSION
1009         };
1010
1011         for (i=0; i < ARRAY_SIZE(valuenames); i++) {
1012                 ret &= test_GetPrinterData(tctx, servername, valuenames[i], handle, &type, &buffer, &size);
1013                 ret &= test_GetPrinterDataEx(tctx, servername, "random", valuenames[i], handle, &type_ex, &buffer_ex, &size_ex);
1014                 torture_assert_int_equal(tctx, type, type_ex, "type mismatch");
1015                 torture_assert_int_equal(tctx, size, size_ex, "size mismatch");
1016                 torture_assert_mem_equal(tctx, buffer, buffer_ex, size, "buffer mismatch");
1017                 free(buffer);
1018                 free(buffer_ex);
1019         }
1020
1021         return ret;
1022 }
1023
1024 /****************************************************************************
1025 ****************************************************************************/
1026
1027 int main(int argc, char *argv[])
1028 {
1029         BOOL ret = FALSE;
1030         LPSTR servername;
1031         LPSTR architecture = "Windows NT x86";
1032         HANDLE server_handle;
1033         PRINTER_DEFAULTS defaults_admin, defaults_use;
1034         struct torture_context *tctx;
1035
1036         if (argc < 2) {
1037                 fprintf(stderr, "usage: %s <servername> [print]\n", argv[0]);
1038                 exit(-1);
1039         }
1040
1041         tctx = malloc(sizeof(struct torture_context));
1042         if (!tctx) {
1043                 fprintf(stderr, "out of memory\n");
1044                 exit(-1);
1045         }
1046         memset(tctx, '\0', sizeof(*tctx));
1047
1048         servername = argv[1];
1049
1050         if (argc >= 3) {
1051                 if (strcmp(argv[2], "print") == 0) {
1052                         tctx->print = TRUE;
1053                 }
1054         }
1055
1056         defaults_admin.pDatatype = NULL;
1057         defaults_admin.pDevMode = NULL;
1058         defaults_admin.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
1059
1060         defaults_use.pDatatype = NULL;
1061         defaults_use.pDevMode = NULL;
1062         defaults_use.DesiredAccess = PRINTER_ACCESS_USE;
1063
1064         ret &= test_EnumPrinters(tctx, servername);
1065         ret &= test_EnumDrivers(tctx, servername, architecture);
1066         ret &= test_OpenPrinter(tctx, servername, NULL, &server_handle);
1067 /*      ret &= test_EnumPrinterKey(tctx, servername, server_handle, ""); */
1068         ret &= test_PrinterData(tctx, servername, server_handle);
1069         ret &= test_EnumForms(tctx, servername, server_handle);
1070         ret &= test_ClosePrinter(tctx, server_handle);
1071         ret &= test_EnumPorts(tctx, servername);
1072         ret &= test_EnumMonitors(tctx, servername);
1073         ret &= test_EnumPrintProcessors(tctx, servername, architecture);
1074         ret &= test_EnumPrintProcessorDatatypes(tctx, servername);
1075         ret &= test_GetPrintProcessorDirectory(tctx, servername, architecture);
1076         ret &= test_GetPrinterDriverDirectory(tctx, servername, architecture);
1077         ret &= test_EachPrinter(tctx, servername, architecture, NULL);
1078
1079         if (!ret) {
1080                 if (tctx->last_reason) {
1081                         fprintf(stderr, "failed: %s\n", tctx->last_reason);
1082                 }
1083                 free(tctx);
1084                 return -1;
1085         }
1086
1087         printf("%s run successfully\n", argv[0]);
1088
1089         free(tctx);
1090         return 0;
1091 }