s4:torture:winreg: improve error messages in test_HKLM_wellknown
[amitay/samba.git] / source4 / torture / rpc / winreg.c
index ba82e888966a2d7a7fe580806e5763f280eef2a5..370bd6771c4fe676973b49f115c762eeadd625dc 100644 (file)
@@ -4,7 +4,7 @@
 
    Copyright (C) Tim Potter 2003
    Copyright (C) Jelmer Vernooij 2004-2007
-   Copyright (C) Günther Deschner 2007
+   Copyright (C) Günther Deschner 2007,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
@@ -24,7 +24,7 @@
 #include "librpc/gen_ndr/ndr_winreg_c.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "libcli/security/security.h"
-#include "torture/rpc/rpc.h"
+#include "torture/rpc/torture_rpc.h"
 #include "param/param.h"
 #include "lib/registry/registry.h"
 
@@ -193,7 +193,7 @@ static bool test_CreateKey_sd(struct dcerpc_binding_handle *b,
                                        NULL);
 
        torture_assert_ndr_success(tctx,
-               ndr_push_struct_blob(&sdblob, tctx, NULL, sd,
+               ndr_push_struct_blob(&sdblob, tctx, sd,
                                     (ndr_push_flags_fn_t)ndr_push_security_descriptor),
                                     "Failed to push security_descriptor ?!\n");
 
@@ -260,7 +260,7 @@ static bool _test_GetKeySecurity(struct dcerpc_pipe *p,
        sd = talloc_zero(tctx, struct security_descriptor);
 
        torture_assert_ndr_success(tctx,
-               ndr_pull_struct_blob(&sdblob, tctx, NULL, sd,
+               ndr_pull_struct_blob(&sdblob, tctx, sd,
                                     (ndr_pull_flags_fn_t)ndr_pull_security_descriptor),
                                     "pull_security_descriptor failed");
 
@@ -305,7 +305,7 @@ static bool _test_SetKeySecurity(struct dcerpc_pipe *p,
        }
 
        torture_assert_ndr_success(tctx,
-               ndr_push_struct_blob(&sdblob, tctx, NULL, sd,
+               ndr_push_struct_blob(&sdblob, tctx, sd,
                                     (ndr_push_flags_fn_t)ndr_push_security_descriptor),
                                     "push_security_descriptor failed");
 
@@ -1601,7 +1601,6 @@ static bool test_QueryMultipleValues(struct dcerpc_binding_handle *b,
                                     const char *valuename)
 {
        struct winreg_QueryMultipleValues r;
-       NTSTATUS status;
        uint32_t bufsize=0;
 
        ZERO_STRUCT(r);
@@ -1621,10 +1620,9 @@ static bool test_QueryMultipleValues(struct dcerpc_binding_handle *b,
                r.in.buffer = r.out.buffer = talloc_zero_array(tctx, uint8_t,
                                                               *r.in.buffer_size);
 
-               status = dcerpc_winreg_QueryMultipleValues_r(b, tctx, &r);
-
-               if(NT_STATUS_IS_ERR(status))
-                       torture_fail(tctx, "QueryMultipleValues failed");
+               torture_assert_ntstatus_ok(tctx,
+                       dcerpc_winreg_QueryMultipleValues_r(b, tctx, &r),
+                       "QueryMultipleValues failed");
 
                talloc_free(r.in.buffer);
                bufsize += 0x20;
@@ -1679,9 +1677,11 @@ static bool test_QueryValue_full(struct dcerpc_binding_handle *b,
        uint32_t data_length = 0;
        uint8_t *data = NULL;
        WERROR expected_error = WERR_BADFILE;
+       const char *errmsg_nonexisting = "expected WERR_BADFILE for nonexisting value";
 
        if (valuename == NULL) {
                expected_error = WERR_INVALID_PARAM;
+               errmsg_nonexisting = "expected WERR_INVALID_PARAM for NULL valuename";
        }
 
        ZERO_STRUCT(r);
@@ -1703,21 +1703,21 @@ static bool test_QueryValue_full(struct dcerpc_binding_handle *b,
        torture_assert_ntstatus_ok(tctx, dcerpc_winreg_QueryValue_r(b, tctx, &r),
                "QueryValue failed");
        torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
-               "QueryValue failed");
+               "expected WERR_INVALID_PARAM for missing type length and size");
 
        r.in.type = &type;
        r.out.type = &type;
        torture_assert_ntstatus_ok(tctx, dcerpc_winreg_QueryValue_r(b, tctx, &r),
                "QueryValue failed");
        torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
-               "QueryValue failed");
+               "expected WERR_INVALID_PARAM for missing length and size");
 
        r.in.data_length = &data_length;
        r.out.data_length = &data_length;
        torture_assert_ntstatus_ok(tctx, dcerpc_winreg_QueryValue_r(b, tctx, &r),
                "QueryValue failed");
        torture_assert_werr_equal(tctx, r.out.result, WERR_INVALID_PARAM,
-               "QueryValue failed");
+               "expected WERR_INVALID_PARAM for missing size");
 
        r.in.data_size = &data_size;
        r.out.data_size = &data_size;
@@ -1728,7 +1728,7 @@ static bool test_QueryValue_full(struct dcerpc_binding_handle *b,
                        "QueryValue failed");
        } else {
                torture_assert_werr_equal(tctx, r.out.result, expected_error,
-                       "QueryValue failed");
+                       errmsg_nonexisting);
        }
 
        real_data_size = *r.out.data_size;
@@ -1742,10 +1742,10 @@ static bool test_QueryValue_full(struct dcerpc_binding_handle *b,
                "QueryValue failed");
        if (existing_value) {
                torture_assert_werr_equal(tctx, r.out.result, WERR_MORE_DATA,
-                       "QueryValue failed");
+                       "expected WERR_MORE_DATA for query with too small buffer");
        } else {
                torture_assert_werr_equal(tctx, r.out.result, expected_error,
-                       "QueryValue failed");
+                       errmsg_nonexisting);
        }
 
        data = talloc_zero_array(tctx, uint8_t, real_data_size);
@@ -1760,7 +1760,7 @@ static bool test_QueryValue_full(struct dcerpc_binding_handle *b,
                        "QueryValue failed");
        } else {
                torture_assert_werr_equal(tctx, r.out.result, expected_error,
-                       "QueryValue failed");
+                       errmsg_nonexisting);
        }
 
        return true;
@@ -1921,6 +1921,7 @@ static bool test_SetValue_simple(struct dcerpc_binding_handle *b,
        uint32_t value = 0x12345678;
        uint64_t value2 = 0x12345678;
        const char *string = "torture";
+       const char *array[2];
        DATA_BLOB blob;
        enum winreg_Type types[] = {
                REG_DWORD,
@@ -1932,6 +1933,9 @@ static bool test_SetValue_simple(struct dcerpc_binding_handle *b,
        };
        int t;
 
+       array[0] = "array0";
+       array[1] = NULL;
+
        torture_comment(tctx, "Testing SetValue (standard formats)\n");
 
        for (t=0; t < ARRAY_SIZE(types); t++) {
@@ -1954,26 +1958,10 @@ static bool test_SetValue_simple(struct dcerpc_binding_handle *b,
                        blob = data_blob_string_const("binary_blob");
                        break;
                case REG_SZ:
-                       torture_assert(tctx,
-                               convert_string_talloc_convenience(tctx, lp_iconv_convenience(tctx->lp_ctx),
-                                                                 CH_UNIX, CH_UTF16,
-                                                                 string,
-                                                                 strlen(string)+1,
-                                                                 (void **)&blob.data,
-                                                                 &blob.length,
-                                                                 false), "");
+                       torture_assert(tctx, push_reg_sz(tctx, &blob, string), "failed to push REG_SZ");
                        break;
                case REG_MULTI_SZ:
-                       torture_assert(tctx,
-                               convert_string_talloc_convenience(tctx, lp_iconv_convenience(tctx->lp_ctx),
-                                                                 CH_UNIX, CH_UTF16,
-                                                                 string,
-                                                                 strlen(string)+1,
-                                                                 (void **)&blob.data,
-                                                                 &blob.length,
-                                                                 false), "");
-                       torture_assert(tctx, data_blob_realloc(tctx, &blob, blob.length + 2), "");
-                       memset(&blob.data[blob.length - 2], '\0', 2);
+                       torture_assert(tctx, push_reg_multi_sz(tctx, &blob, array), "failed to push REG_MULTI_SZ");
                        break;
                default:
                        break;
@@ -2105,11 +2093,11 @@ static bool test_HKLM_wellknown(struct torture_context *tctx,
        torture_assert(tctx, test_QueryValue_full(b, tctx, &newhandle, VALUE_CURRENT_VERSION, true),
                "failed to query current version");
        torture_assert(tctx, test_QueryValue_full(b, tctx, &newhandle, "IDoNotExist", false),
-               "failed to query current version");
+               "succeeded to query nonexistent value");
        torture_assert(tctx, test_QueryValue_full(b, tctx, &newhandle, NULL, false),
-               "test_QueryValue_full for NULL value failed");
+               "succeeded to query value with NULL name");
        torture_assert(tctx, test_QueryValue_full(b, tctx, &newhandle, "", false),
-               "test_QueryValue_full for \"\" value failed");
+               "succeeded to query nonexistent default value (\"\")");
 
        torture_assert(tctx, test_CloseKey(b, tctx, &newhandle),
                "failed to close current version key");
@@ -2117,14 +2105,65 @@ static bool test_HKLM_wellknown(struct torture_context *tctx,
        return true;
 }
 
+static bool test_OpenHive(struct torture_context *tctx,
+                         struct dcerpc_binding_handle *b,
+                         struct policy_handle *handle,
+                         int hkey)
+{
+       struct winreg_OpenHKLM r;
+
+       r.in.system_name = 0;
+       r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
+       r.out.handle = handle;
+
+       switch (hkey) {
+       case HKEY_LOCAL_MACHINE:
+               torture_assert_ntstatus_ok(tctx,
+                       dcerpc_winreg_OpenHKLM_r(b, tctx, &r),
+                       "failed to open HKLM");
+               torture_assert_werr_ok(tctx, r.out.result,
+                       "failed to open HKLM");
+               break;
+       case HKEY_CURRENT_USER:
+               torture_assert_ntstatus_ok(tctx,
+                       dcerpc_winreg_OpenHKCU_r(b, tctx, (struct winreg_OpenHKCU *)&r),
+                       "failed to open HKCU");
+               torture_assert_werr_ok(tctx, r.out.result,
+                       "failed to open HKCU");
+               break;
+       case HKEY_USERS:
+               torture_assert_ntstatus_ok(tctx,
+                       dcerpc_winreg_OpenHKU_r(b, tctx, (struct winreg_OpenHKU *)&r),
+                       "failed to open HKU");
+               torture_assert_werr_ok(tctx, r.out.result,
+                       "failed to open HKU");
+               break;
+       case HKEY_CLASSES_ROOT:
+               torture_assert_ntstatus_ok(tctx,
+                       dcerpc_winreg_OpenHKCR_r(b, tctx, (struct winreg_OpenHKCR *)&r),
+                       "failed to open HKCR");
+               torture_assert_werr_ok(tctx, r.out.result,
+                       "failed to open HKCR");
+               break;
+       default:
+               torture_warning(tctx, "unsupported hkey: 0x%08x\n", hkey);
+               return false;
+       }
+
+       return true;
+}
+
 static bool test_volatile_keys(struct torture_context *tctx,
                               struct dcerpc_binding_handle *b,
-                              struct policy_handle *handle)
+                              struct policy_handle *handle,
+                              int hkey)
 {
        struct policy_handle new_handle;
        enum winreg_CreateAction action_taken;
 
-       torture_comment(tctx, "Testing REG_OPTION_VOLATILE key\n");
+       torture_comment(tctx, "Testing VOLATILE key\n");
+
+       test_DeleteKey(b, tctx, handle, TEST_KEY_VOLATILE);
 
        torture_assert(tctx,
                test_CreateKey_opts(tctx, b, handle, TEST_KEY_VOLATILE, NULL,
@@ -2189,19 +2228,64 @@ static bool test_volatile_keys(struct torture_context *tctx,
                "failed to open volatile key");
 
        torture_assert(tctx,
-               test_DeleteKey(b, tctx, handle, TEST_KEY_VOLATILE),
-               "failed to delete key");
+               test_CloseKey(b, tctx, &new_handle),
+               "failed to close");
 
        torture_assert(tctx,
-               test_CloseKey(b, tctx, &new_handle),
+               test_CloseKey(b, tctx, handle),
                "failed to close");
 
+       torture_assert(tctx,
+               test_OpenHive(tctx, b, handle, hkey),
+               "failed top open hive");
+
+       torture_assert(tctx,
+               test_OpenKey_opts(tctx, b, handle, TEST_KEY_VOLATILE,
+                                 REG_OPTION_VOLATILE,
+                                 SEC_FLAG_MAXIMUM_ALLOWED,
+                                 &new_handle,
+                                 WERR_BADFILE),
+               "failed to open volatile key");
+
+       torture_assert(tctx,
+               test_OpenKey_opts(tctx, b, handle, TEST_KEY_VOLATILE,
+                                 REG_OPTION_NON_VOLATILE,
+                                 SEC_FLAG_MAXIMUM_ALLOWED,
+                                 &new_handle,
+                                 WERR_BADFILE),
+               "failed to open volatile key");
+
+       torture_comment(tctx, "Testing VOLATILE key succeeded\n");
+
        return true;
 }
 
+static const char *kernel_mode_registry_path(struct torture_context *tctx,
+                                            int hkey,
+                                            const char *sid_string,
+                                            const char *path)
+{
+       switch (hkey) {
+       case HKEY_LOCAL_MACHINE:
+               return talloc_asprintf(tctx, "\\Registry\\MACHINE\\%s", path);
+       case HKEY_CURRENT_USER:
+               return talloc_asprintf(tctx, "\\Registry\\USER\\%s\\%s", sid_string, path);
+       case HKEY_USERS:
+               return talloc_asprintf(tctx, "\\Registry\\USER\\%s", path);
+       case HKEY_CLASSES_ROOT:
+               return talloc_asprintf(tctx, "\\Registry\\MACHINE\\Software\\Classes\\%s", path);
+       default:
+               torture_warning(tctx, "unsupported hkey: 0x%08x\n", hkey);
+               return NULL;
+       }
+
+       return NULL;
+}
+
 static bool test_symlink_keys(struct torture_context *tctx,
                              struct dcerpc_binding_handle *b,
-                             struct policy_handle *handle)
+                             struct policy_handle *handle,
+                             int hkey)
 {
        struct policy_handle new_handle;
        enum winreg_CreateAction action_taken;
@@ -2260,7 +2344,8 @@ static bool test_symlink_keys(struct torture_context *tctx,
 
 static bool test_CreateKey_keytypes(struct torture_context *tctx,
                                    struct dcerpc_binding_handle *b,
-                                   struct policy_handle *handle)
+                                   struct policy_handle *handle,
+                                   int hkey)
 {
 
        if (torture_setting_bool(tctx, "samba3", false) ||
@@ -2269,11 +2354,11 @@ static bool test_CreateKey_keytypes(struct torture_context *tctx,
        }
 
        torture_assert(tctx,
-               test_volatile_keys(tctx, b, handle),
+               test_volatile_keys(tctx, b, handle, hkey),
                "failed to test volatile keys");
 
        torture_assert(tctx,
-               test_symlink_keys(tctx, b, handle),
+               test_symlink_keys(tctx, b, handle, hkey),
                "failed to test symlink keys");
 
        return true;
@@ -2281,7 +2366,8 @@ static bool test_CreateKey_keytypes(struct torture_context *tctx,
 
 static bool test_key_base(struct torture_context *tctx,
                          struct dcerpc_binding_handle *b,
-                         struct policy_handle *handle)
+                         struct policy_handle *handle,
+                         int hkey)
 {
        struct policy_handle newhandle;
        bool ret = true, created = false, deleted = false;
@@ -2312,12 +2398,14 @@ static bool test_key_base(struct torture_context *tctx,
                                     "CreateKey failed (OpenKey after Create didn't work)\n");
                }
 
-               torture_assert(tctx, test_SetValue_simple(b, tctx, &newhandle),
-                       "simple SetValue test failed");
-               torture_assert(tctx, test_SetValue_extended(b, tctx, &newhandle),
-                       "extended SetValue test failed");
-               torture_assert(tctx, test_CreateKey_keytypes(tctx, b, &newhandle),
-                       "keytype test failed");
+               if (hkey == HKEY_CURRENT_USER) {
+                       torture_assert(tctx, test_SetValue_simple(b, tctx, &newhandle),
+                               "simple SetValue test failed");
+                       torture_assert(tctx, test_SetValue_extended(b, tctx, &newhandle),
+                               "extended SetValue test failed");
+                       torture_assert(tctx, test_CreateKey_keytypes(tctx, b, &newhandle, hkey),
+                               "keytype test failed");
+               }
 
                if (!test_CloseKey(b, tctx, &newhandle)) {
                        torture_fail(tctx,
@@ -2438,8 +2526,9 @@ static bool test_Open(struct torture_context *tctx, struct dcerpc_pipe *p,
        bool ret = true;
        struct winreg_OpenHKLM r;
        struct dcerpc_binding_handle *b = p->binding_handle;
+       int hkey = 0;
 
-       winreg_open_fn open_fn = userdata;
+       winreg_open_fn open_fn = (winreg_open_fn)userdata;
 
        r.in.system_name = 0;
        r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
@@ -2453,13 +2542,25 @@ static bool test_Open(struct torture_context *tctx, struct dcerpc_pipe *p,
                ret = false;
        }
 
-       if (open_fn == (void *)dcerpc_winreg_OpenHKLM_r) {
+       if (open_fn == (winreg_open_fn)dcerpc_winreg_OpenHKLM_r) {
+               hkey = HKEY_LOCAL_MACHINE;
+       } else if (open_fn == (winreg_open_fn)dcerpc_winreg_OpenHKU_r) {
+               hkey = HKEY_USERS;
+       } else if (open_fn == (winreg_open_fn)dcerpc_winreg_OpenHKCR_r) {
+               hkey = HKEY_CLASSES_ROOT;
+       } else if (open_fn == (winreg_open_fn)dcerpc_winreg_OpenHKCU_r) {
+               hkey = HKEY_CURRENT_USER;
+       } else {
+               torture_fail(tctx, "unsupported hkey");
+       }
+
+       if (hkey == HKEY_LOCAL_MACHINE) {
                torture_assert(tctx,
                        test_HKLM_wellknown(tctx, b, &handle),
                        "failed to test HKLM wellknown keys");
        }
 
-       if (!test_key_base(tctx, b, &handle)) {
+       if (!test_key_base(tctx, b, &handle, hkey)) {
                torture_warning(tctx, "failed to test TEST_KEY_BASE");
                ret = false;
        }
@@ -2470,7 +2571,7 @@ static bool test_Open(struct torture_context *tctx, struct dcerpc_pipe *p,
        }
 
        /* The HKCR hive has a very large fanout */
-       if (open_fn == (void *)dcerpc_winreg_OpenHKCR_r) {
+       if (hkey == HKEY_CLASSES_ROOT) {
                if(!test_key(p, tctx, &handle, MAX_DEPTH - 1, false)) {
                        ret = false;
                }