LCOV - code coverage report
Current view: top level - source4/lib/registry/tests - hive.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 204 221 92.3 %
Date: 2024-04-21 15:09:00 Functions: 16 16 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local testing of registry library - hives
       5             : 
       6             :    Copyright (C) Jelmer Vernooij 2005-2007
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program; if not, write to the Free Software
      20             :    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "lib/registry/registry.h"
      25             : #include "torture/torture.h"
      26             : #include "librpc/gen_ndr/winreg.h"
      27             : #include "system/filesys.h"
      28             : #include "param/param.h"
      29             : #include "libcli/security/security.h"
      30             : #include "lib/registry/tests/proto.h"
      31             : 
      32           2 : static bool test_del_nonexistent_key(struct torture_context *tctx,
      33             :                                      const void *test_data)
      34             : {
      35           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
      36           2 :         WERROR error = hive_key_del(tctx, root, "bla");
      37           2 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
      38             :                                   "invalid return code");
      39             : 
      40           0 :         return true;
      41             : }
      42             : 
      43           2 : static bool test_keyinfo_root(struct torture_context *tctx,
      44             :                               const void *test_data)
      45             : {
      46           2 :         uint32_t num_subkeys, num_values;
      47           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
      48           2 :         WERROR error;
      49             : 
      50             :         /* This is a new backend. There should be no subkeys and no
      51             :          * values */
      52           2 :         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
      53             :                                   NULL, NULL, NULL, NULL);
      54           2 :         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
      55             : 
      56           2 :         torture_assert_int_equal(tctx, num_subkeys, 0,
      57             :                                  "New key has non-zero subkey count");
      58             : 
      59           2 :         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
      60             : 
      61           2 :         torture_assert_int_equal(tctx, num_values, 0,
      62             :                                  "New key has non-zero value count");
      63             : 
      64           0 :         return true;
      65             : }
      66             : 
      67           2 : static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
      68             : {
      69           2 :         uint32_t num_subkeys, num_values;
      70           2 :         struct hive_key *root = (struct hive_key *)test_data;
      71           2 :         WERROR error;
      72           2 :         struct hive_key *subkey;
      73           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
      74           2 :         DATA_BLOB db = { d, 4 };
      75             : 
      76           2 :         error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
      77             :                                   NULL, &subkey);
      78           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
      79             : 
      80           2 :         error = hive_key_set_value(root, "Answer", REG_DWORD, db);
      81           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
      82             : 
      83             :         /* This is a new backend. There should be no subkeys and no
      84             :          * values */
      85           2 :         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
      86             :                                   NULL, NULL, NULL, NULL);
      87           2 :         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
      88             : 
      89           2 :         torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
      90             : 
      91           2 :         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
      92             : 
      93           2 :         torture_assert_int_equal(tctx, num_values, 1, "value count");
      94             : 
      95           0 :         return true;
      96             : }
      97             : 
      98           2 : static bool test_add_subkey(struct torture_context *tctx,
      99             :                             const void *test_data)
     100             : {
     101           2 :         WERROR error;
     102           2 :         struct hive_key *subkey;
     103           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     104           2 :         TALLOC_CTX *mem_ctx = tctx;
     105             : 
     106           2 :         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
     107             :                                   NULL, &subkey);
     108           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     109             : 
     110           2 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     111           2 :         torture_assert_werr_ok(tctx, error, "reg_key_del");
     112             : 
     113           0 :         return true;
     114             : }
     115             : 
     116           2 : static bool test_del_recursive(struct torture_context *tctx,
     117             :                                const void *test_data)
     118             : {
     119           2 :         WERROR error;
     120           2 :         struct hive_key *subkey;
     121           2 :         struct hive_key *subkey2;
     122           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     123           2 :         TALLOC_CTX *mem_ctx = tctx;
     124           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     125           2 :         DATA_BLOB db = { d, 4 };
     126             : 
     127             :         /* Create a new key under the root */
     128           2 :         error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
     129             :                                   NULL, &subkey);
     130           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     131             : 
     132             :         /* Create a new key under "Parent Key" */
     133           2 :         error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
     134             :                                   NULL, &subkey2);
     135           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     136             : 
     137             :         /* Create a new value under "Child Key" */
     138           2 :         error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD, db);
     139           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     140             : 
     141             :         /* Deleting "Parent Key" will also delete "Child Key" and the value. */
     142           2 :         error = hive_key_del(mem_ctx, root, "Parent Key");
     143           2 :         torture_assert_werr_ok(tctx, error, "hive_key_del");
     144             : 
     145           0 :         return true;
     146             : }
     147             : 
     148           2 : static bool test_flush_key(struct torture_context *tctx, void *test_data)
     149             : {
     150           2 :         struct hive_key *root = (struct hive_key *)test_data;
     151             : 
     152           2 :         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
     153             : 
     154           2 :         return true;
     155             : }
     156             : 
     157           2 : static bool test_del_key(struct torture_context *tctx, const void *test_data)
     158             : {
     159           2 :         WERROR error;
     160           2 :         struct hive_key *subkey;
     161           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     162           2 :         TALLOC_CTX *mem_ctx = tctx;
     163             : 
     164           2 :         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
     165             :                                   NULL, &subkey);
     166           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     167             : 
     168           2 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     169           2 :         torture_assert_werr_ok(tctx, error, "reg_key_del");
     170             : 
     171           2 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     172           2 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND, "reg_key_del");
     173             : 
     174           0 :         return true;
     175             : }
     176             : 
     177           2 : static bool test_set_value(struct torture_context *tctx,
     178             :                            const void *test_data)
     179             : {
     180           2 :         WERROR error;
     181           2 :         struct hive_key *subkey;
     182           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     183           2 :         TALLOC_CTX *mem_ctx = tctx;
     184           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     185           2 :         DATA_BLOB db = { d, 4 };
     186             : 
     187           2 :         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
     188             :                                   NULL, &subkey);
     189           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     190             : 
     191           2 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     192           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     193             : 
     194           0 :         return true;
     195             : }
     196             : 
     197           2 : static bool test_get_value(struct torture_context *tctx, const void *test_data)
     198             : {
     199           2 :         WERROR error;
     200           2 :         struct hive_key *subkey;
     201           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     202           2 :         TALLOC_CTX *mem_ctx = tctx;
     203           2 :         uint32_t type;
     204           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     205           2 :         DATA_BLOB db = { d, 4 }, data;
     206             : 
     207           2 :         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
     208             :                                   NULL, &subkey);
     209           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     210             : 
     211           2 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
     212           2 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
     213             :                                   "getting missing value");
     214             : 
     215           2 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     216           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     217             : 
     218           2 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
     219           2 :         torture_assert_werr_ok(tctx, error, "getting value");
     220             : 
     221           2 :         torture_assert_int_equal(tctx, data.length, 4, "value length");
     222           2 :         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
     223             : 
     224           2 :         torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
     225             : 
     226           0 :         return true;
     227             : }
     228             : 
     229           2 : static bool test_del_value(struct torture_context *tctx, const void *test_data)
     230             : {
     231           2 :         WERROR error;
     232           2 :         struct hive_key *subkey;
     233           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     234           2 :         TALLOC_CTX *mem_ctx = tctx;
     235           2 :         uint32_t type;
     236           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     237           2 :         DATA_BLOB db = { d, 4 };
     238             : 
     239           2 :         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
     240             :                                                          NULL, &subkey);
     241           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     242             : 
     243           2 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     244           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     245             : 
     246           2 :         error = hive_key_del_value(mem_ctx, subkey, "Answer");
     247           2 :         torture_assert_werr_ok(tctx, error, "deleting value");
     248             : 
     249           2 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &db);
     250           2 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND, "getting value");
     251             : 
     252           2 :         error = hive_key_del_value(mem_ctx, subkey, "Answer");
     253           2 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
     254             :                                   "deleting value");
     255             : 
     256           0 :         return true;
     257             : }
     258             : 
     259           2 : static bool test_list_values(struct torture_context *tctx,
     260             :                              const void *test_data)
     261             : {
     262           2 :         WERROR error;
     263           2 :         struct hive_key *subkey;
     264           2 :         const struct hive_key *root = (const struct hive_key *)test_data;
     265           2 :         TALLOC_CTX *mem_ctx = tctx;
     266           2 :         uint32_t type;
     267           2 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     268           2 :         DATA_BLOB db = { d, 4 }, data;
     269           2 :         const char *name;
     270             : 
     271           2 :         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
     272             :                                   NULL, &subkey);
     273           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     274             : 
     275           2 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     276           2 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     277             : 
     278           2 :         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
     279             :                                         &type, &data);
     280           2 :         torture_assert_werr_ok(tctx, error, "getting value");
     281             : 
     282           2 :         torture_assert_str_equal(tctx, name, "Answer", "value name");
     283             : 
     284           2 :         torture_assert_int_equal(tctx, data.length, 4, "value length");
     285           2 :         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
     286             :         
     287           2 :         torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
     288             :         
     289           2 :         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
     290             :                                         &type, &data);
     291           2 :         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
     292             :                                   "getting missing value");
     293             : 
     294           0 :         return true;
     295             : }
     296             : 
     297           2 : static bool test_hive_security(struct torture_context *tctx, const void *_data)
     298             : {
     299           2 :         struct hive_key *subkey = NULL;
     300           2 :         const struct hive_key *root = _data;
     301           2 :         WERROR error;
     302           2 :         struct security_descriptor *osd, *nsd;
     303             :         
     304           2 :         osd = security_descriptor_dacl_create(tctx,
     305             :                                          0,
     306             :                                          NULL, NULL,
     307             :                                          SID_NT_AUTHENTICATED_USERS,
     308             :                                          SEC_ACE_TYPE_ACCESS_ALLOWED,
     309             :                                          SEC_GENERIC_ALL,
     310             :                                          SEC_ACE_FLAG_OBJECT_INHERIT,
     311             :                                          NULL);
     312             : 
     313             : 
     314           2 :         error = hive_key_add_name(tctx, root, "SecurityKey", NULL,
     315             :                                   osd, &subkey);
     316           2 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     317             : 
     318           2 :         error = hive_get_sec_desc(tctx, subkey, &nsd);
     319           2 :         torture_assert_werr_ok (tctx, error, "getting security descriptor");
     320             : 
     321           1 :         torture_assert(tctx, security_descriptor_equal(osd, nsd),
     322             :                        "security descriptor changed!");
     323             : 
     324             :         /* Create a fresh security descriptor */        
     325           1 :         talloc_free(osd);
     326           1 :         osd = security_descriptor_dacl_create(tctx,
     327             :                                          0,
     328             :                                          NULL, NULL,
     329             :                                          SID_NT_AUTHENTICATED_USERS,
     330             :                                          SEC_ACE_TYPE_ACCESS_ALLOWED,
     331             :                                          SEC_GENERIC_ALL,
     332             :                                          SEC_ACE_FLAG_OBJECT_INHERIT,
     333             :                                          NULL);
     334             : 
     335           1 :         error = hive_set_sec_desc(subkey, osd);
     336           1 :         torture_assert_werr_ok(tctx, error, "setting security descriptor");
     337             :         
     338           1 :         error = hive_get_sec_desc(tctx, subkey, &nsd);
     339           1 :         torture_assert_werr_ok (tctx, error, "getting security descriptor");
     340             :         
     341           1 :         torture_assert(tctx, security_descriptor_equal(osd, nsd),
     342             :                        "security descriptor changed!");
     343             : 
     344           0 :         return true;
     345             : }
     346             : 
     347        4708 : static void tcase_add_tests(struct torture_tcase *tcase)
     348             : {
     349        4708 :         torture_tcase_add_simple_test_const(tcase, "del_nonexistent_key",
     350             :                                                 test_del_nonexistent_key);
     351        4708 :         torture_tcase_add_simple_test_const(tcase, "add_subkey",
     352             :                                                 test_add_subkey);
     353        4708 :         torture_tcase_add_simple_test(tcase, "flush_key",
     354             :                                                 test_flush_key);
     355             :         /* test_del_recursive() test must run before test_keyinfo_root().
     356             :            test_keyinfo_root() checks the number of subkeys, which verifies
     357             :            the recursive delete worked properly. */
     358        4708 :         torture_tcase_add_simple_test_const(tcase, "del_recursive",
     359             :                                                 test_del_recursive);
     360        4708 :         torture_tcase_add_simple_test_const(tcase, "get_info",
     361             :                                                 test_keyinfo_root);
     362        4708 :         torture_tcase_add_simple_test(tcase, "get_info_nums",
     363             :                                                 test_keyinfo_nums);
     364        4708 :         torture_tcase_add_simple_test_const(tcase, "set_value",
     365             :                                                 test_set_value);
     366        4708 :         torture_tcase_add_simple_test_const(tcase, "get_value",
     367             :                                                 test_get_value);
     368        4708 :         torture_tcase_add_simple_test_const(tcase, "list_values",
     369             :                                                 test_list_values);
     370        4708 :         torture_tcase_add_simple_test_const(tcase, "del_key",
     371             :                                                 test_del_key);
     372        4708 :         torture_tcase_add_simple_test_const(tcase, "del_value",
     373             :                                                 test_del_value);
     374        4708 :         torture_tcase_add_simple_test_const(tcase, "check hive security",
     375             :                                                 test_hive_security);
     376        4708 : }
     377             : 
     378           1 : static bool hive_setup_ldb(struct torture_context *tctx, void **data)
     379             : {
     380           1 :         struct hive_key *key;
     381           1 :         WERROR error;
     382           1 :         char *dirname;
     383           1 :         NTSTATUS status;
     384             : 
     385           1 :         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
     386           1 :         if (!NT_STATUS_IS_OK(status))
     387           0 :                 return false;
     388             : 
     389           1 :         rmdir(dirname);
     390             : 
     391           1 :         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
     392           1 :         if (!W_ERROR_IS_OK(error)) {
     393           0 :                 fprintf(stderr, "Unable to initialize ldb hive\n");
     394           0 :                 return false;
     395             :         }
     396             : 
     397           1 :         *data = key;
     398             : 
     399           1 :         return true;
     400             : }
     401             : 
     402           1 : static bool hive_setup_regf(struct torture_context *tctx, void **data)
     403             : {
     404           1 :         struct hive_key *key;
     405           1 :         WERROR error;
     406           1 :         char *dirname;
     407           1 :         NTSTATUS status;
     408             : 
     409           1 :         status = torture_temp_dir(tctx, "hive-regf", &dirname);
     410           1 :         if (!NT_STATUS_IS_OK(status))
     411           0 :                 return false;
     412             : 
     413           1 :         rmdir(dirname);
     414             : 
     415           1 :         error = reg_create_regf_file(tctx, dirname, 5, &key);
     416           1 :         if (!W_ERROR_IS_OK(error)) {
     417           0 :                 fprintf(stderr, "Unable to create new regf file\n");
     418           0 :                 return false;
     419             :         }
     420             : 
     421           1 :         *data = key;
     422             : 
     423           1 :         return true;
     424             : }
     425             : 
     426        2354 : struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
     427             : {
     428         125 :         struct torture_tcase *tcase;
     429        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "hive");
     430             : 
     431        2354 :         tcase = torture_suite_add_tcase(suite, "ldb");
     432        2354 :         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
     433        2354 :         tcase_add_tests(tcase);
     434             : 
     435        2354 :         tcase = torture_suite_add_tcase(suite, "regf");
     436        2354 :         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
     437        2354 :         tcase_add_tests(tcase);
     438             : 
     439        2354 :         return suite;
     440             : }

Generated by: LCOV version 1.14