s4-smbtorture: simplify test_PrinterInfo_DevMode a bit.
[ira/wip.git] / source4 / torture / rpc / spoolss.c
index 91364541d0c88a179a46484f4a7d8e9165d204b0..f57ffe527d4a47b72fa76bbfbce3f7f4c1eb00e8 100644 (file)
@@ -5,7 +5,7 @@
    Copyright (C) Tim Potter 2003
    Copyright (C) Stefan Metzmacher 2005
    Copyright (C) Jelmer Vernooij 2007
-   Copyright (C) Guenther Deschner 2009
+   Copyright (C) Guenther Deschner 2009-2010
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "includes.h"
 #include "torture/torture.h"
-#include "torture/rpc/rpc.h"
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "librpc/gen_ndr/ndr_spoolss.h"
 #include "librpc/gen_ndr/ndr_spoolss_c.h"
+#include "librpc/gen_ndr/ndr_security.h"
+#include "libcli/security/security.h"
+#include "torture/rpc/rpc.h"
 #include "param/param.h"
 
 #define TORTURE_WELLKNOWN_PRINTER      "torture_wkn_printer"
@@ -865,45 +867,67 @@ static bool test_GetPrinterDriver2(struct torture_context *tctx,
                                   struct policy_handle *handle,
                                   const char *driver_name);
 
-static bool test_GetPrinter(struct torture_context *tctx,
-                           struct dcerpc_pipe *p,
-                    struct policy_handle *handle)
+bool test_GetPrinter_level(struct torture_context *tctx,
+                          struct dcerpc_pipe *p,
+                          struct policy_handle *handle,
+                          uint32_t level,
+                          union spoolss_PrinterInfo *info)
 {
-       NTSTATUS status;
        struct spoolss_GetPrinter r;
-       uint16_t levels[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
-       int i;
        uint32_t needed;
 
-       for (i=0;i<ARRAY_SIZE(levels);i++) {
-               r.in.handle = handle;
-               r.in.level = levels[i];
-               r.in.buffer = NULL;
-               r.in.offered = 0;
-               r.out.needed = &needed;
+       r.in.handle = handle;
+       r.in.level = level;
+       r.in.buffer = NULL;
+       r.in.offered = 0;
+       r.out.needed = &needed;
 
-               torture_comment(tctx, "Testing GetPrinter level %u\n", r.in.level);
+       torture_comment(tctx, "Testing GetPrinter level %u\n", r.in.level);
 
-               status = dcerpc_spoolss_GetPrinter(p, tctx, &r);
-               torture_assert_ntstatus_ok(tctx, status, "GetPrinter failed");
+       torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_GetPrinter(p, tctx, &r),
+               "GetPrinter failed");
 
-               if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
-                       DATA_BLOB blob = data_blob_talloc(tctx, NULL, needed);
-                       data_blob_clear(&blob);
-                       r.in.buffer = &blob;
-                       r.in.offered = needed;
-                       status = dcerpc_spoolss_GetPrinter(p, tctx, &r);
-               }
+       if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
+               DATA_BLOB blob = data_blob_talloc(tctx, NULL, needed);
+               data_blob_clear(&blob);
+               r.in.buffer = &blob;
+               r.in.offered = needed;
 
-               torture_assert_ntstatus_ok(tctx, status, "GetPrinter failed");
+               torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_GetPrinter(p, tctx, &r),
+                       "GetPrinter failed");
+       }
+
+       torture_assert_werr_ok(tctx, r.out.result, "GetPrinter failed");
 
-               torture_assert_werr_ok(tctx, r.out.result, "GetPrinter failed");
+       CHECK_NEEDED_SIZE_LEVEL(spoolss_PrinterInfo, r.out.info, r.in.level, lp_iconv_convenience(tctx->lp_ctx), needed, 4);
 
-               CHECK_NEEDED_SIZE_LEVEL(spoolss_PrinterInfo, r.out.info, r.in.level, lp_iconv_convenience(tctx->lp_ctx), needed, 4);
+       if (info && r.out.info) {
+               *info = *r.out.info;
+       }
 
-               if ((r.in.level == 2) && r.out.info->info2.drivername && strlen(r.out.info->info2.drivername)) {
+       return true;
+}
+
+
+static bool test_GetPrinter(struct torture_context *tctx,
+                           struct dcerpc_pipe *p,
+                           struct policy_handle *handle)
+{
+       uint32_t levels[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+       int i;
+
+       for (i=0;i<ARRAY_SIZE(levels);i++) {
+
+               union spoolss_PrinterInfo info;
+
+               ZERO_STRUCT(info);
+
+               torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, levels[i], &info),
+                       "failed to call GetPrinter");
+
+               if ((levels[i] == 2) && info.info2.drivername && strlen(info.info2.drivername)) {
                        torture_assert(tctx,
-                               test_GetPrinterDriver2(tctx, p, handle, r.out.info->info2.drivername),
+                               test_GetPrinterDriver2(tctx, p, handle, info.info2.drivername),
                                "failed to call test_GetPrinterDriver2");
                }
        }
@@ -911,6 +935,32 @@ static bool test_GetPrinter(struct torture_context *tctx,
        return true;
 }
 
+static bool test_SetPrinter(struct torture_context *tctx,
+                           struct dcerpc_pipe *p,
+                           struct policy_handle *handle,
+                           struct spoolss_SetPrinterInfoCtr *info_ctr,
+                           struct spoolss_DevmodeContainer *devmode_ctr,
+                           struct sec_desc_buf *secdesc_ctr,
+                           enum spoolss_PrinterControl command)
+{
+       struct spoolss_SetPrinter r;
+
+       r.in.handle = handle;
+       r.in.info_ctr = info_ctr;
+       r.in.devmode_ctr = devmode_ctr;
+       r.in.secdesc_ctr = secdesc_ctr;
+       r.in.command = command;
+
+       torture_comment(tctx, "Testing SetPrinter level %d\n", r.in.info_ctr->level);
+
+       torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_SetPrinter(p, tctx, &r),
+               "failed to call SetPrinter");
+       torture_assert_werr_ok(tctx, r.out.result,
+               "failed to call SetPrinter");
+
+       return true;
+}
+
 static bool test_SetPrinter_errors(struct torture_context *tctx,
                                   struct dcerpc_pipe *p,
                                   struct policy_handle *handle)
@@ -953,8 +1003,8 @@ static bool test_SetPrinter_errors(struct torture_context *tctx,
                struct spoolss_SetPrinterInfo5 info5;
                struct spoolss_SetPrinterInfo6 info6;
                struct spoolss_SetPrinterInfo7 info7;
-               struct spoolss_DeviceModeInfo info8;
-               struct spoolss_DeviceModeInfo info9;
+               struct spoolss_SetPrinterInfo8 info8;
+               struct spoolss_SetPrinterInfo9 info9;
 
 
                info_ctr.level = levels[i];
@@ -1075,8 +1125,8 @@ static bool test_SetPrinter_errors(struct torture_context *tctx,
 static void clear_info2(struct spoolss_SetPrinterInfoCtr *r)
 {
        if ((r->level == 2) && (r->info.info2)) {
-               r->info.info2->secdesc = NULL;
-               r->info.info2->devmode = NULL;
+               r->info.info2->secdesc_ptr = 0;
+               r->info.info2->devmode_ptr = 0;
        }
 }
 
@@ -1270,6 +1320,14 @@ static bool test_PrinterInfo(struct torture_context *tctx,
                        break; \
                }
 
+#define SD_EQUAL(sd1, sd2, field) \
+               if (!security_descriptor_equal(sd1, sd2)) { \
+                       torture_comment(tctx, "Failed to set %s (%s)\n", \
+                              #field, __location__); \
+                       ret = false; \
+                       break; \
+               }
+
 #define TEST_PRINTERINFO_STRING_EXP_ERR(lvl1, field1, lvl2, field2, value, err) do { \
                torture_comment(tctx, "field test %d/%s vs %d/%s\n", lvl1, #field1, lvl2, #field2); \
                q.in.level = lvl1; \
@@ -1469,6 +1527,507 @@ static bool test_PrinterInfo(struct torture_context *tctx,
        return ret;
 }
 
+#define torture_assert_sid_equal(torture_ctx,got,expected,cmt)\
+       do { struct dom_sid *__got = (got), *__expected = (expected); \
+       if (!dom_sid_equal(__got, __expected)) { \
+               torture_result(torture_ctx, TORTURE_FAIL, \
+                                          __location__": "#got" was %s, expected %s: %s", \
+                                          dom_sid_string(torture_ctx, __got), dom_sid_string(torture_ctx, __expected), cmt); \
+               return false; \
+       } \
+       } while(0)
+
+static bool test_security_descriptor_equal(struct torture_context *tctx,
+                                          const struct security_descriptor *sd1,
+                                          const struct security_descriptor *sd2)
+{
+       if (sd1 == sd2) {
+               return true;
+       }
+
+       if (!sd1 || !sd2) {
+               torture_comment(tctx, "%s\n", __location__);
+               return false;
+       }
+
+       torture_assert_int_equal(tctx, sd1->revision, sd2->revision, "revision mismatch");
+       torture_assert_int_equal(tctx, sd1->type, sd2->type, "type mismatch");
+
+       torture_assert_sid_equal(tctx, sd1->owner_sid, sd2->owner_sid, "owner mismatch");
+       torture_assert_sid_equal(tctx, sd1->group_sid, sd2->group_sid, "group mismatch");
+
+       if (!security_acl_equal(sd1->sacl, sd2->sacl)) {
+               torture_comment(tctx, "%s: sacl mismatch\n", __location__);
+               NDR_PRINT_DEBUG(security_acl, sd1->sacl);
+               NDR_PRINT_DEBUG(security_acl, sd2->sacl);
+               return false;
+       }
+       if (!security_acl_equal(sd1->dacl, sd2->dacl)) {
+               torture_comment(tctx, "%s: dacl mismatch\n", __location__);
+               NDR_PRINT_DEBUG(security_acl, sd1->dacl);
+               NDR_PRINT_DEBUG(security_acl, sd2->dacl);
+               return false;
+       }
+
+       return true;
+}
+
+static bool test_sd_set_level(struct torture_context *tctx,
+                             struct dcerpc_pipe *p,
+                             struct policy_handle *handle,
+                             uint32_t level,
+                             struct security_descriptor *sd)
+{
+       struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       struct sec_desc_buf secdesc_ctr;
+
+       ZERO_STRUCT(devmode_ctr);
+       ZERO_STRUCT(secdesc_ctr);
+
+       switch (level) {
+       case 2: {
+               union spoolss_PrinterInfo info;
+               struct spoolss_SetPrinterInfo2 info2;
+               torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+               info2.servername        = info.info2.servername;
+               info2.printername       = info.info2.printername;
+               info2.sharename         = info.info2.sharename;
+               info2.portname          = info.info2.portname;
+               info2.drivername        = info.info2.drivername;
+               info2.comment           = info.info2.comment;
+               info2.location          = info.info2.location;
+               info2.devmode_ptr       = 0;
+               info2.sepfile           = info.info2.sepfile;
+               info2.printprocessor    = info.info2.printprocessor;
+               info2.datatype          = info.info2.datatype;
+               info2.parameters        = info.info2.parameters;
+               info2.secdesc_ptr       = 0;
+               info2.attributes        = info.info2.attributes;
+               info2.priority          = info.info2.priority;
+               info2.defaultpriority   = info.info2.defaultpriority;
+               info2.starttime         = info.info2.starttime;
+               info2.untiltime         = info.info2.untiltime;
+               info2.status            = info.info2.status;
+               info2.cjobs             = info.info2.cjobs;
+               info2.averageppm        = info.info2.averageppm;
+
+               info_ctr.level = 2;
+               info_ctr.info.info2 = &info2;
+
+               break;
+       }
+       case 3: {
+               struct spoolss_SetPrinterInfo3 info3;
+
+               info3.sec_desc_ptr = 0;
+
+               info_ctr.level = 3;
+               info_ctr.info.info3 = &info3;
+
+               break;
+       }
+       default:
+               return false;
+       }
+
+       secdesc_ctr.sd = sd;
+
+       torture_assert(tctx,
+               test_SetPrinter(tctx, p, handle, &info_ctr, &devmode_ctr, &secdesc_ctr, 0), "");
+
+       return true;
+}
+
+static bool test_PrinterInfo_SDs(struct torture_context *tctx,
+                                struct dcerpc_pipe *p,
+                                struct policy_handle *handle)
+{
+       union spoolss_PrinterInfo info;
+       struct security_descriptor *sd1, *sd2;
+       int i;
+
+       /* just compare level 2 and level 3 */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd1 = info.info2.secdesc;
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 3, &info), "");
+
+       sd2 = info.info3.secdesc;
+
+       torture_assert(tctx, test_security_descriptor_equal(tctx, sd1, sd2), "");
+
+
+       /* query level 2, set level 2, query level 2 */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd1 = info.info2.secdesc;
+
+       torture_assert(tctx, test_sd_set_level(tctx, p, handle, 2, sd1), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd2 = info.info2.secdesc;
+       if (sd1->type & SEC_DESC_DACL_DEFAULTED) {
+               torture_comment(tctx, "removing SEC_DESC_DACL_DEFAULTED\n");
+               sd1->type &= ~SEC_DESC_DACL_DEFAULTED;
+       }
+
+       torture_assert(tctx, test_security_descriptor_equal(tctx, sd1, sd2), "");
+
+
+       /* query level 2, set level 3, query level 2 */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd1 = info.info2.secdesc;
+
+       torture_assert(tctx, test_sd_set_level(tctx, p, handle, 3, sd1), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd2 = info.info2.secdesc;
+
+       torture_assert(tctx, test_security_descriptor_equal(tctx, sd1, sd2), "");
+
+
+       /* set modified sd level 3, query level 2 */
+
+       for (i=0; i < 93; i++) {
+               struct security_ace a;
+               const char *sid_string = talloc_asprintf(tctx, "S-1-5-32-9999%i", i);
+               a.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
+               a.flags = 0;
+               a.size = 0; /* autogenerated */
+               a.access_mask = 0;
+               a.trustee = *dom_sid_parse_talloc(tctx, sid_string);
+               torture_assert_ntstatus_ok(tctx, security_descriptor_dacl_add(sd1, &a), "");
+       }
+
+       torture_assert(tctx, test_sd_set_level(tctx, p, handle, 3, sd1), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+       sd2 = info.info2.secdesc;
+
+       if (sd1->type & SEC_DESC_DACL_DEFAULTED) {
+               torture_comment(tctx, "removing SEC_DESC_DACL_DEFAULTED\n");
+               sd1->type &= ~SEC_DESC_DACL_DEFAULTED;
+       }
+
+       torture_assert(tctx, test_security_descriptor_equal(tctx, sd1, sd2), "");
+
+       return true;
+}
+
+/*
+ * wrapper call that saves original sd, runs tests, and restores sd
+ */
+
+static bool test_PrinterInfo_SD(struct torture_context *tctx,
+                               struct dcerpc_pipe *p,
+                               struct policy_handle *handle)
+{
+       union spoolss_PrinterInfo info;
+       struct spoolss_SetPrinterInfo3 info3;
+       struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       struct sec_desc_buf secdesc_ctr;
+       struct security_descriptor *sd;
+       bool ret = true;
+
+       /* save original sd */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       sd = security_descriptor_copy(tctx, info.info2.secdesc);
+
+       /* run tests */
+
+       ret = test_PrinterInfo_SDs(tctx, p, handle);
+
+       /* restore original sd */
+
+       ZERO_STRUCT(devmode_ctr);
+       ZERO_STRUCT(secdesc_ctr);
+
+       info3.sec_desc_ptr = 0;
+
+       info_ctr.level = 3;
+       info_ctr.info.info3 = &info3;
+
+       secdesc_ctr.sd = sd;
+
+       torture_assert(tctx,
+               test_SetPrinter(tctx, p, handle, &info_ctr, &devmode_ctr, &secdesc_ctr, 0), "");
+
+       return ret;
+}
+
+static bool test_devmode_set_level(struct torture_context *tctx,
+                                  struct dcerpc_pipe *p,
+                                  struct policy_handle *handle,
+                                  uint32_t level,
+                                  struct spoolss_DeviceMode *devmode)
+{
+       struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       struct sec_desc_buf secdesc_ctr;
+
+       ZERO_STRUCT(devmode_ctr);
+       ZERO_STRUCT(secdesc_ctr);
+
+       switch (level) {
+       case 2: {
+               union spoolss_PrinterInfo info;
+               struct spoolss_SetPrinterInfo2 info2;
+               torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+               info2.servername        = info.info2.servername;
+               info2.printername       = info.info2.printername;
+               info2.sharename         = info.info2.sharename;
+               info2.portname          = info.info2.portname;
+               info2.drivername        = info.info2.drivername;
+               info2.comment           = info.info2.comment;
+               info2.location          = info.info2.location;
+               info2.devmode_ptr       = 0;
+               info2.sepfile           = info.info2.sepfile;
+               info2.printprocessor    = info.info2.printprocessor;
+               info2.datatype          = info.info2.datatype;
+               info2.parameters        = info.info2.parameters;
+               info2.secdesc_ptr       = 0;
+               info2.attributes        = info.info2.attributes;
+               info2.priority          = info.info2.priority;
+               info2.defaultpriority   = info.info2.defaultpriority;
+               info2.starttime         = info.info2.starttime;
+               info2.untiltime         = info.info2.untiltime;
+               info2.status            = info.info2.status;
+               info2.cjobs             = info.info2.cjobs;
+               info2.averageppm        = info.info2.averageppm;
+
+               info_ctr.level = 2;
+               info_ctr.info.info2 = &info2;
+
+               break;
+       }
+       case 8: {
+               struct spoolss_SetPrinterInfo8 info8;
+
+               info8.devmode_ptr = 0;
+
+               info_ctr.level = 8;
+               info_ctr.info.info8 = &info8;
+
+               break;
+       }
+       default:
+               return false;
+       }
+
+       devmode_ctr.devmode = devmode;
+
+       torture_assert(tctx,
+               test_SetPrinter(tctx, p, handle, &info_ctr, &devmode_ctr, &secdesc_ctr, 0), "");
+
+       return true;
+}
+
+
+static bool test_devicemode_equal(struct torture_context *tctx,
+                                 const struct spoolss_DeviceMode *d1,
+                                 const struct spoolss_DeviceMode *d2)
+{
+       if (d1 == d2) {
+               return true;
+       }
+
+       if (!d1 || !d2) {
+               torture_comment(tctx, "%s\n", __location__);
+               return false;
+       }
+       torture_assert_str_equal(tctx, d1->devicename, d2->devicename, "devicename mismatch");
+       torture_assert_int_equal(tctx, d1->specversion, d2->specversion, "specversion mismatch");
+       torture_assert_int_equal(tctx, d1->driverversion, d2->driverversion, "driverversion mismatch");
+       torture_assert_int_equal(tctx, d1->size, d2->size, "size mismatch");
+       torture_assert_int_equal(tctx, d1->__driverextra_length, d2->__driverextra_length, "__driverextra_length mismatch");
+       torture_assert_int_equal(tctx, d1->fields, d2->fields, "fields mismatch");
+       torture_assert_int_equal(tctx, d1->orientation, d2->orientation, "orientation mismatch");
+       torture_assert_int_equal(tctx, d1->papersize, d2->papersize, "papersize mismatch");
+       torture_assert_int_equal(tctx, d1->paperlength, d2->paperlength, "paperlength mismatch");
+       torture_assert_int_equal(tctx, d1->paperwidth, d2->paperwidth, "paperwidth mismatch");
+       torture_assert_int_equal(tctx, d1->scale, d2->scale, "scale mismatch");
+       torture_assert_int_equal(tctx, d1->copies, d2->copies, "copies mismatch");
+       torture_assert_int_equal(tctx, d1->defaultsource, d2->defaultsource, "defaultsource mismatch");
+       torture_assert_int_equal(tctx, d1->printquality, d2->printquality, "printquality mismatch");
+       torture_assert_int_equal(tctx, d1->color, d2->color, "color mismatch");
+       torture_assert_int_equal(tctx, d1->duplex, d2->duplex, "duplex mismatch");
+       torture_assert_int_equal(tctx, d1->yresolution, d2->yresolution, "yresolution mismatch");
+       torture_assert_int_equal(tctx, d1->ttoption, d2->ttoption, "ttoption mismatch");
+       torture_assert_int_equal(tctx, d1->collate, d2->collate, "collate mismatch");
+       torture_assert_str_equal(tctx, d1->formname, d2->formname, "formname mismatch");
+       torture_assert_int_equal(tctx, d1->logpixels, d2->logpixels, "logpixels mismatch");
+       torture_assert_int_equal(tctx, d1->bitsperpel, d2->bitsperpel, "bitsperpel mismatch");
+       torture_assert_int_equal(tctx, d1->pelswidth, d2->pelswidth, "pelswidth mismatch");
+       torture_assert_int_equal(tctx, d1->pelsheight, d2->pelsheight, "pelsheight mismatch");
+       torture_assert_int_equal(tctx, d1->displayflags, d2->displayflags, "displayflags mismatch");
+       torture_assert_int_equal(tctx, d1->displayfrequency, d2->displayfrequency, "displayfrequency mismatch");
+       torture_assert_int_equal(tctx, d1->icmmethod, d2->icmmethod, "icmmethod mismatch");
+       torture_assert_int_equal(tctx, d1->icmintent, d2->icmintent, "icmintent mismatch");
+       torture_assert_int_equal(tctx, d1->mediatype, d2->mediatype, "mediatype mismatch");
+       torture_assert_int_equal(tctx, d1->dithertype, d2->dithertype, "dithertype mismatch");
+       torture_assert_int_equal(tctx, d1->reserved1, d2->reserved1, "reserved1 mismatch");
+       torture_assert_int_equal(tctx, d1->reserved2, d2->reserved2, "reserved2 mismatch");
+       torture_assert_int_equal(tctx, d1->panningwidth, d2->panningwidth, "panningwidth mismatch");
+       torture_assert_int_equal(tctx, d1->panningheight, d2->panningheight, "panningheight mismatch");
+       torture_assert_data_blob_equal(tctx, d1->driverextra_data, d2->driverextra_data, "driverextra_data mismatch");
+
+       return true;
+}
+
+static bool call_OpenPrinterEx(struct torture_context *tctx,
+                              struct dcerpc_pipe *p,
+                              const char *name,
+                              struct spoolss_DeviceMode *devmode,
+                              struct policy_handle *handle);
+
+static bool test_ClosePrinter(struct torture_context *tctx,
+                             struct dcerpc_pipe *p,
+                             struct policy_handle *handle);
+
+static bool test_PrinterInfo_DevModes(struct torture_context *tctx,
+                                     struct dcerpc_pipe *p,
+                                     struct policy_handle *handle,
+                                     const char *name)
+{
+       union spoolss_PrinterInfo info;
+       struct spoolss_DeviceMode *devmode;
+       struct spoolss_DeviceMode *devmode2;
+       struct policy_handle handle_devmode;
+
+       /* simply compare level8 and level2 devmode */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 8, &info), "");
+
+       devmode = info.info8.devmode;
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       devmode2 = info.info2.devmode;
+
+       torture_assert(tctx, test_devicemode_equal(tctx, devmode, devmode2), "");
+
+
+       /* change formname upon open and see if it persists in getprinter calls */
+
+       devmode->formname = talloc_strdup(tctx, "A4");
+
+       torture_assert(tctx, call_OpenPrinterEx(tctx, p, name, devmode, &handle_devmode),
+               "failed to open printer handle");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, &handle_devmode, 8, &info), "");
+
+       devmode2 = info.info8.devmode;
+
+       if (strequal(devmode->devicename, devmode2->devicename)) {
+               torture_fail(tctx, "devicename is the same");
+       }
+
+       if (strequal(devmode->formname, devmode2->formname)) {
+               torture_fail(tctx, "formname is the same");
+       }
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, &handle_devmode, 2, &info), "");
+
+       devmode2 = info.info2.devmode;
+
+       if (strequal(devmode->devicename, devmode2->devicename)) {
+               torture_fail(tctx, "devicename is the same");
+       }
+
+       if (strequal(devmode->formname, devmode2->formname)) {
+               torture_fail(tctx, "formname is the same");
+       }
+
+       test_ClosePrinter(tctx, p, &handle_devmode);
+
+
+       /* set devicemode level 8 and see if it persists */
+
+       devmode->copies = 93;
+       devmode->formname = talloc_strdup(tctx, "Legal");
+
+       torture_assert(tctx, test_devmode_set_level(tctx, p, handle, 8, devmode), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 8, &info), "");
+
+       devmode2 = info.info8.devmode;
+
+       torture_assert(tctx, test_devicemode_equal(tctx, devmode, devmode2), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       devmode2 = info.info2.devmode;
+
+       torture_assert(tctx, test_devicemode_equal(tctx, devmode, devmode2), "");
+
+
+       /* set devicemode level 2 and see if it persists */
+
+       devmode->copies = 39;
+       devmode->formname = talloc_strdup(tctx, "Letter");
+
+       torture_assert(tctx, test_devmode_set_level(tctx, p, handle, 8, devmode), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 8, &info), "");
+
+       devmode2 = info.info8.devmode;
+
+       torture_assert(tctx, test_devicemode_equal(tctx, devmode, devmode2), "");
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info), "");
+
+       devmode2 = info.info2.devmode;
+
+       torture_assert(tctx, test_devicemode_equal(tctx, devmode, devmode2), "");
+
+
+       return true;
+}
+
+/*
+ * wrapper call that saves original devmode, runs tests, and restores devmode
+ */
+
+static bool test_PrinterInfo_DevMode(struct torture_context *tctx,
+                                    struct dcerpc_pipe *p,
+                                    struct policy_handle *handle,
+                                    const char *name)
+{
+       union spoolss_PrinterInfo info;
+       struct spoolss_DeviceMode *devmode;
+       bool ret = true;
+
+       /* save original devmode */
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 8, &info),
+               "failed to get initial global devicemode");
+
+       devmode = info.info8.devmode;
+
+       /* run tests */
+
+       ret = test_PrinterInfo_DevModes(tctx, p, handle, name);
+
+       /* restore original devmode */
+
+       torture_assert(tctx, test_devmode_set_level(tctx, p, handle, 8, devmode),
+               "failed to restore initial global device mode");
+
+       return ret;
+}
 
 static bool test_ClosePrinter(struct torture_context *tctx,
                              struct dcerpc_pipe *p,
@@ -2618,6 +3177,181 @@ static bool test_SetPrinterDataEx(struct torture_context *tctx,
        return true;
 }
 
+static bool test_GetChangeID_PrinterData(struct torture_context *tctx,
+                                        struct dcerpc_pipe *p,
+                                        struct policy_handle *handle,
+                                        uint32_t *change_id)
+{
+       enum winreg_Type type;
+       union spoolss_PrinterData data;
+
+       torture_assert(tctx,
+               test_GetPrinterData(tctx, p, handle, "ChangeID", &type, &data),
+               "failed to call GetPrinterData");
+
+       torture_assert(tctx, type == REG_DWORD, "unexpected type");
+
+       *change_id = data.value;
+
+       return true;
+}
+
+static bool test_GetChangeID_PrinterDataEx(struct torture_context *tctx,
+                                          struct dcerpc_pipe *p,
+                                          struct policy_handle *handle,
+                                          uint32_t *change_id)
+{
+       enum winreg_Type type;
+       union spoolss_PrinterData data;
+
+       torture_assert(tctx,
+               test_GetPrinterDataEx(tctx, p, handle, "PrinterDriverData", "ChangeID", &type, &data),
+               "failed to call GetPrinterData");
+
+       torture_assert(tctx, type == REG_DWORD, "unexpected type");
+
+       *change_id = data.value;
+
+       return true;
+}
+
+static bool test_GetChangeID_PrinterInfo(struct torture_context *tctx,
+                                        struct dcerpc_pipe *p,
+                                        struct policy_handle *handle,
+                                        uint32_t *change_id)
+{
+       union spoolss_PrinterInfo info;
+
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 0, &info),
+               "failed to query Printer level 0");
+
+       *change_id = info.info0.change_id;
+
+       return true;
+}
+
+static bool test_ChangeID(struct torture_context *tctx,
+                         struct dcerpc_pipe *p,
+                         struct policy_handle *handle)
+{
+       uint32_t change_id, change_id_ex, change_id_info;
+       uint32_t change_id2, change_id_ex2, change_id_info2;
+       union spoolss_PrinterInfo info;
+       const char *comment;
+
+
+       torture_comment(tctx, "Testing ChangeID: id change test #1\n");
+
+       torture_assert(tctx, test_GetChangeID_PrinterData(tctx, p, handle, &change_id),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterDataEx(tctx, p, handle, &change_id_ex),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterInfo(tctx, p, handle, &change_id_info),
+               "failed to query for ChangeID");
+
+       torture_assert_int_equal(tctx, change_id, change_id_ex,
+               "change_ids should all be equal");
+       torture_assert_int_equal(tctx, change_id_ex, change_id_info,
+               "change_ids should all be equal");
+
+
+       torture_comment(tctx, "Testing ChangeID: id change test #2\n");
+
+       torture_assert(tctx, test_GetChangeID_PrinterData(tctx, p, handle, &change_id),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info),
+               "failed to query Printer level 2");
+       torture_assert(tctx, test_GetChangeID_PrinterDataEx(tctx, p, handle, &change_id_ex),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterInfo(tctx, p, handle, &change_id_info),
+               "failed to query for ChangeID");
+       torture_assert_int_equal(tctx, change_id, change_id_ex,
+               "change_id should not have changed");
+       torture_assert_int_equal(tctx, change_id_ex, change_id_info,
+               "change_id should not have changed");
+
+
+       torture_comment(tctx, "Testing ChangeID: id change test #3\n");
+
+       torture_assert(tctx, test_GetChangeID_PrinterData(tctx, p, handle, &change_id),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterDataEx(tctx, p, handle, &change_id_ex),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterInfo(tctx, p, handle, &change_id_info),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetPrinter_level(tctx, p, handle, 2, &info),
+               "failed to query Printer level 2");
+       comment = talloc_strdup(tctx, info.info2.comment);
+
+       {
+               struct spoolss_SetPrinterInfoCtr info_ctr;
+               struct spoolss_DevmodeContainer devmode_ctr;
+               struct sec_desc_buf secdesc_ctr;
+               struct spoolss_SetPrinterInfo2 info2;
+
+               ZERO_STRUCT(info_ctr);
+               ZERO_STRUCT(devmode_ctr);
+               ZERO_STRUCT(secdesc_ctr);
+
+               info2.servername        = info.info2.servername;
+               info2.printername       = info.info2.printername;
+               info2.sharename         = info.info2.sharename;
+               info2.portname          = info.info2.portname;
+               info2.drivername        = info.info2.drivername;
+               info2.comment           = "torture_comment";
+               info2.location          = info.info2.location;
+               info2.devmode_ptr       = 0;
+               info2.sepfile           = info.info2.sepfile;
+               info2.printprocessor    = info.info2.printprocessor;
+               info2.datatype          = info.info2.datatype;
+               info2.parameters        = info.info2.parameters;
+               info2.secdesc_ptr       = 0;
+               info2.attributes        = info.info2.attributes;
+               info2.priority          = info.info2.priority;
+               info2.defaultpriority   = info.info2.defaultpriority;
+               info2.starttime         = info.info2.starttime;
+               info2.untiltime         = info.info2.untiltime;
+               info2.status            = info.info2.status;
+               info2.cjobs             = info.info2.cjobs;
+               info2.averageppm        = info.info2.averageppm;
+
+               info_ctr.level = 2;
+               info_ctr.info.info2 = &info2;
+
+               torture_assert(tctx, test_SetPrinter(tctx, p, handle, &info_ctr, &devmode_ctr, &secdesc_ctr, 0),
+                       "failed to call SetPrinter");
+
+               info2.comment           = comment;
+
+               torture_assert(tctx, test_SetPrinter(tctx, p, handle, &info_ctr, &devmode_ctr, &secdesc_ctr, 0),
+                       "failed to call SetPrinter");
+
+       }
+
+       torture_assert(tctx, test_GetChangeID_PrinterData(tctx, p, handle, &change_id2),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterDataEx(tctx, p, handle, &change_id_ex2),
+               "failed to query for ChangeID");
+       torture_assert(tctx, test_GetChangeID_PrinterInfo(tctx, p, handle, &change_id_info2),
+               "failed to query for ChangeID");
+
+       torture_assert_int_equal(tctx, change_id2, change_id_ex2,
+               "change_ids should all be equal");
+       torture_assert_int_equal(tctx, change_id_ex2, change_id_info2,
+               "change_ids should all be equal");
+
+       torture_assert(tctx, (change_id < change_id2),
+               talloc_asprintf(tctx, "change_id %d needs to be larger than change_id %d",
+               change_id2, change_id));
+       torture_assert(tctx, (change_id_ex < change_id_ex2),
+               talloc_asprintf(tctx, "change_id %d needs to be larger than change_id %d",
+               change_id_ex2, change_id_ex));
+       torture_assert(tctx, (change_id_info < change_id_info2),
+               talloc_asprintf(tctx, "change_id %d needs to be larger than change_id %d",
+               change_id_info2, change_id_info));
+
+       return true;
+}
 
 static bool test_SecondaryClosePrinter(struct torture_context *tctx,
                                       struct dcerpc_pipe *p,
@@ -2753,7 +3487,9 @@ static bool test_OpenPrinter(struct torture_context *tctx,
 
 static bool call_OpenPrinterEx(struct torture_context *tctx,
                               struct dcerpc_pipe *p,
-                              const char *name, struct policy_handle *handle)
+                              const char *name,
+                              struct spoolss_DeviceMode *devmode,
+                              struct policy_handle *handle)
 {
        struct spoolss_OpenPrinterEx r;
        struct spoolss_UserLevel1 userlevel1;
@@ -2768,7 +3504,7 @@ static bool call_OpenPrinterEx(struct torture_context *tctx,
        }
 
        r.in.datatype           = NULL;
-       r.in.devmode_ctr.devmode= NULL;
+       r.in.devmode_ctr.devmode= devmode;
        r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
        r.in.level              = 1;
        r.in.userlevel.level1   = &userlevel1;
@@ -2800,10 +3536,14 @@ static bool test_OpenPrinterEx(struct torture_context *tctx,
        struct policy_handle handle;
        bool ret = true;
 
-       if (!call_OpenPrinterEx(tctx, p, name, &handle)) {
+       if (!call_OpenPrinterEx(tctx, p, name, NULL, &handle)) {
                return false;
        }
 
+       if (!test_PrinterInfo_SD(tctx, p, &handle)) {
+               ret = false;
+       }
+
        if (!test_GetPrinter(tctx, p, &handle)) {
                ret = false;
        }
@@ -2848,6 +3588,10 @@ static bool test_OpenPrinterEx(struct torture_context *tctx,
                ret = false;
        }
 
+       if (!test_ChangeID(tctx, p, &handle)) {
+               ret = false;
+       }
+
        if (!torture_setting_bool(tctx, "samba3", false)) {
                if (!test_SecondaryClosePrinter(tctx, p, &handle)) {
                        ret = false;
@@ -3273,6 +4017,7 @@ static bool test_AddPrinter_normal(struct torture_context *tctx,
        struct spoolss_UserLevelCtr userlevel_ctr;
        struct policy_handle handle;
        bool found = false;
+       bool existing_printer_deleted = false;
 
        ZERO_STRUCT(devmode_ctr);
        ZERO_STRUCT(secdesc_ctr);
@@ -3321,7 +4066,11 @@ static bool test_AddPrinter_normal(struct torture_context *tctx,
        if (W_ERROR_EQUAL(result, WERR_PRINTER_ALREADY_EXISTS)) {
                struct policy_handle printer_handle;
 
-               torture_assert(tctx, call_OpenPrinterEx(tctx, p, printername, &printer_handle),
+               if (existing_printer_deleted) {
+                       torture_fail(tctx, "already deleted printer still existing?");
+               }
+
+               torture_assert(tctx, call_OpenPrinterEx(tctx, p, printername, NULL, &printer_handle),
                        "failed to open printer handle");
 
                torture_assert(tctx, test_DeletePrinter(tctx, p, &printer_handle),
@@ -3330,6 +4079,8 @@ static bool test_AddPrinter_normal(struct torture_context *tctx,
                torture_assert(tctx, test_ClosePrinter(tctx, p, &printer_handle),
                        "failed to close server handle");
 
+               existing_printer_deleted = true;
+
                goto again;
        }
 
@@ -3351,17 +4102,22 @@ static bool test_AddPrinter_normal(struct torture_context *tctx,
                                              dcerpc_spoolss_AddPrinter(p, tctx, &r),
                "failed to add printer");
        result = ex ? rex.out.result : r.out.result;
-       torture_assert_werr_equal(tctx, result, WERR_UNKNOWN_PRINTPROCESSOR,
-               "unexpected result code");
 
-       info2.printprocessor = "winprint";
+       /* w2k8r2 allows to add printer w/o defining printprocessor */
 
-       torture_assert_ntstatus_ok(tctx, ex ? dcerpc_spoolss_AddPrinterEx(p, tctx, &rex) :
-                                             dcerpc_spoolss_AddPrinter(p, tctx, &r),
-               "failed to add printer");
-       result = ex ? rex.out.result : r.out.result;
-       torture_assert_werr_ok(tctx, result,
-               "failed to add printer");
+       if (!W_ERROR_IS_OK(result)) {
+               torture_assert_werr_equal(tctx, result, WERR_UNKNOWN_PRINTPROCESSOR,
+                       "unexpected result code");
+
+               info2.printprocessor = "winprint";
+
+               torture_assert_ntstatus_ok(tctx, ex ? dcerpc_spoolss_AddPrinterEx(p, tctx, &rex) :
+                                                     dcerpc_spoolss_AddPrinter(p, tctx, &r),
+                       "failed to add printer");
+               result = ex ? rex.out.result : r.out.result;
+               torture_assert_werr_ok(tctx, result,
+                       "failed to add printer");
+       }
 
        *handle_p = handle;
 
@@ -3560,6 +4316,32 @@ bool test_printer_keys(struct torture_context *tctx,
        return true;
 }
 
+static bool test_one_printer(struct torture_context *tctx,
+                            struct dcerpc_pipe *p,
+                            struct policy_handle *handle,
+                            const char *name)
+{
+       bool ret = true;
+
+       if (!test_printer_info(tctx, p, handle)) {
+               ret = false;
+       }
+
+       if (!test_PrinterInfo_SD(tctx, p, handle)) {
+               ret = false;
+       }
+
+       if (!test_PrinterInfo_DevMode(tctx, p, handle, name)) {
+               ret = false;
+       }
+
+       if (!test_printer_keys(tctx, p, handle)) {
+               ret = false;
+       }
+
+       return ret;
+}
+
 static bool test_printer(struct torture_context *tctx,
                         struct dcerpc_pipe *p)
 {
@@ -3575,11 +4357,7 @@ static bool test_printer(struct torture_context *tctx,
                return false;
        }
 
-       if (!test_printer_info(tctx, p, &handle[0])) {
-               ret = false;
-       }
-
-       if (!test_printer_keys(tctx, p, &handle[0])) {
+       if (!test_one_printer(tctx, p, &handle[0], TORTURE_PRINTER)) {
                ret = false;
        }
 
@@ -3600,11 +4378,7 @@ static bool test_printer(struct torture_context *tctx,
                return false;
        }
 
-       if (!test_printer_info(tctx, p, &handle[1])) {
-               ret = false;
-       }
-
-       if (!test_printer_keys(tctx, p, &handle[1])) {
+       if (!test_one_printer(tctx, p, &handle[1], TORTURE_PRINTER_EX)) {
                ret = false;
        }