registry: Add an explicit test for recursive deletion.
[samba.git] / source4 / lib / registry / tests / hive.c
index bc3c82552efc9529f8473b4bcdc73f965a32ce82..915782694f61de5bc9b83fa9bd99e0d34aa99c30 100644 (file)
@@ -31,7 +31,7 @@ static bool test_del_nonexistant_key(struct torture_context *tctx,
 {
        const struct hive_key *root = (const struct hive_key *)test_data;
        WERROR error = hive_key_del(root, "bla");
-       torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
+       torture_assert_werr_equal(tctx, error, WERR_BADFILE,
                                  "invalid return code");
 
        return true;
@@ -47,7 +47,7 @@ static bool test_keyinfo_root(struct torture_context *tctx,
        /* This is a new backend. There should be no subkeys and no
         * values */
        error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
-                                 NULL);
+                                 NULL, NULL, NULL, NULL);
        torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
 
        torture_assert_int_equal(tctx, num_subkeys, 0,
@@ -80,7 +80,7 @@ static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
        /* This is a new backend. There should be no subkeys and no
         * values */
        error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
-                                 NULL);
+                                 NULL, NULL, NULL, NULL);
        torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
 
        torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
@@ -110,6 +110,38 @@ static bool test_add_subkey(struct torture_context *tctx,
        return true;
 }
 
+static bool test_del_recursive(struct torture_context *tctx,
+                              const void *test_data)
+{
+       WERROR error;
+       struct hive_key *subkey;
+       struct hive_key *subkey2;
+       const struct hive_key *root = (const struct hive_key *)test_data;
+       TALLOC_CTX *mem_ctx = tctx;
+       uint32_t data = 42;
+
+       /* Create a new key under the root */
+       error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
+                                 NULL, &subkey);
+       torture_assert_werr_ok(tctx, error, "hive_key_add_name");
+
+       /* Create a new key under "Parent Key" */
+       error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
+                                 NULL, &subkey2);
+       torture_assert_werr_ok(tctx, error, "hive_key_add_name");
+
+       /* Create a new value under "Child Key" */
+       error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
+                              data_blob_talloc(mem_ctx, &data, sizeof(data)));
+       torture_assert_werr_ok(tctx, error, "hive_key_set_value");
+
+       /* Deleting "Parent Key" will also delete "Child Key" and the value. */
+       error = hive_key_del(root, "Parent Key");
+       torture_assert_werr_ok(tctx, error, "hive_key_del");
+
+       return true;
+}
+
 static bool test_flush_key(struct torture_context *tctx, void *test_data)
 {
        struct hive_key *root = (struct hive_key *)test_data;
@@ -134,7 +166,7 @@ static bool test_del_key(struct torture_context *tctx, const void *test_data)
        torture_assert_werr_ok(tctx, error, "reg_key_del");
 
        error = hive_key_del(root, "Nested Key");
-       torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
+       torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
 
        return true;
 }
@@ -174,7 +206,7 @@ static bool test_get_value(struct torture_context *tctx, const void *test_data)
        torture_assert_werr_ok(tctx, error, "hive_key_add_name");
 
        error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
-       torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
+       torture_assert_werr_equal(tctx, error, WERR_BADFILE,
                                  "getting missing value");
 
        error = hive_key_set_value(subkey, "Answer", REG_DWORD,
@@ -186,7 +218,9 @@ static bool test_get_value(struct torture_context *tctx, const void *test_data)
 
        torture_assert_int_equal(tctx, value.length, 4, "value length");
        torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
-       torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
+
+       torture_assert_int_equal(tctx, data, IVAL(value.data, 0),
+                                "value data");
 
        return true;
 }
@@ -213,10 +247,10 @@ static bool test_del_value(struct torture_context *tctx, const void *test_data)
        torture_assert_werr_ok(tctx, error, "deleting value");
 
        error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
-       torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
+       torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
 
        error = hive_key_del_value(subkey, "Answer");
-       torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
+       torture_assert_werr_equal(tctx, error, WERR_BADFILE,
                                  "deleting value");
 
        return true;
@@ -250,7 +284,9 @@ static bool test_list_values(struct torture_context *tctx,
 
        torture_assert_int_equal(tctx, value.length, 4, "value length");
        torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
-       torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
+       
+       
+       torture_assert_int_equal(tctx, data, IVAL(value.data, 0), "value data");
 
        error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
                                        &type, &value);
@@ -268,6 +304,11 @@ static void tcase_add_tests(struct torture_tcase *tcase)
                                                test_add_subkey);
        torture_tcase_add_simple_test(tcase, "flush_key",
                                                test_flush_key);
+       /* test_del_recursive() test must run before test_keyinfo_root().
+          test_keyinfo_root() checks the number of subkeys, which verifies
+          the recursive delete worked properly. */
+       torture_tcase_add_simple_test_const(tcase, "del_recursive",
+                                               test_del_recursive);
        torture_tcase_add_simple_test_const(tcase, "get_info",
                                                test_keyinfo_root);
        torture_tcase_add_simple_test(tcase, "get_info_nums",