LCOV - code coverage report
Current view: top level - source4/lib/registry/tests - hive.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 23 186 12.4 %
Date: 2024-06-13 04:01:37 Functions: 2 16 12.5 %

          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           0 : static bool test_del_nonexistent_key(struct torture_context *tctx,
      33             :                                      const void *test_data)
      34             : {
      35           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
      36           0 :         WERROR error = hive_key_del(tctx, root, "bla");
      37           0 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
      38             :                                   "invalid return code");
      39             : 
      40           0 :         return true;
      41             : }
      42             : 
      43           0 : static bool test_keyinfo_root(struct torture_context *tctx,
      44             :                               const void *test_data)
      45             : {
      46             :         uint32_t num_subkeys, num_values;
      47           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
      48             :         WERROR error;
      49             : 
      50             :         /* This is a new backend. There should be no subkeys and no
      51             :          * values */
      52           0 :         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
      53             :                                   NULL, NULL, NULL, NULL);
      54           0 :         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
      55             : 
      56           0 :         torture_assert_int_equal(tctx, num_subkeys, 0,
      57             :                                  "New key has non-zero subkey count");
      58             : 
      59           0 :         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
      60             : 
      61           0 :         torture_assert_int_equal(tctx, num_values, 0,
      62             :                                  "New key has non-zero value count");
      63             : 
      64           0 :         return true;
      65             : }
      66             : 
      67           0 : static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
      68             : {
      69             :         uint32_t num_subkeys, num_values;
      70           0 :         struct hive_key *root = (struct hive_key *)test_data;
      71             :         WERROR error;
      72             :         struct hive_key *subkey;
      73           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
      74           0 :         DATA_BLOB db = { d, 4 };
      75             : 
      76           0 :         error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
      77             :                                   NULL, &subkey);
      78           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
      79             : 
      80           0 :         error = hive_key_set_value(root, "Answer", REG_DWORD, db);
      81           0 :         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           0 :         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
      86             :                                   NULL, NULL, NULL, NULL);
      87           0 :         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
      88             : 
      89           0 :         torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
      90             : 
      91           0 :         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
      92             : 
      93           0 :         torture_assert_int_equal(tctx, num_values, 1, "value count");
      94             : 
      95           0 :         return true;
      96             : }
      97             : 
      98           0 : static bool test_add_subkey(struct torture_context *tctx,
      99             :                             const void *test_data)
     100             : {
     101             :         WERROR error;
     102             :         struct hive_key *subkey;
     103           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     104           0 :         TALLOC_CTX *mem_ctx = tctx;
     105             : 
     106           0 :         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
     107             :                                   NULL, &subkey);
     108           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     109             : 
     110           0 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     111           0 :         torture_assert_werr_ok(tctx, error, "reg_key_del");
     112             : 
     113           0 :         return true;
     114             : }
     115             : 
     116           0 : static bool test_del_recursive(struct torture_context *tctx,
     117             :                                const void *test_data)
     118             : {
     119             :         WERROR error;
     120             :         struct hive_key *subkey;
     121             :         struct hive_key *subkey2;
     122           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     123           0 :         TALLOC_CTX *mem_ctx = tctx;
     124           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     125           0 :         DATA_BLOB db = { d, 4 };
     126             : 
     127             :         /* Create a new key under the root */
     128           0 :         error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
     129             :                                   NULL, &subkey);
     130           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     131             : 
     132             :         /* Create a new key under "Parent Key" */
     133           0 :         error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
     134             :                                   NULL, &subkey2);
     135           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     136             : 
     137             :         /* Create a new value under "Child Key" */
     138           0 :         error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD, db);
     139           0 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     140             : 
     141             :         /* Deleting "Parent Key" will also delete "Child Key" and the value. */
     142           0 :         error = hive_key_del(mem_ctx, root, "Parent Key");
     143           0 :         torture_assert_werr_ok(tctx, error, "hive_key_del");
     144             : 
     145           0 :         return true;
     146             : }
     147             : 
     148           0 : static bool test_flush_key(struct torture_context *tctx, void *test_data)
     149             : {
     150           0 :         struct hive_key *root = (struct hive_key *)test_data;
     151             : 
     152           0 :         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
     153             : 
     154           0 :         return true;
     155             : }
     156             : 
     157           0 : static bool test_del_key(struct torture_context *tctx, const void *test_data)
     158             : {
     159             :         WERROR error;
     160             :         struct hive_key *subkey;
     161           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     162           0 :         TALLOC_CTX *mem_ctx = tctx;
     163             : 
     164           0 :         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
     165             :                                   NULL, &subkey);
     166           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     167             : 
     168           0 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     169           0 :         torture_assert_werr_ok(tctx, error, "reg_key_del");
     170             : 
     171           0 :         error = hive_key_del(mem_ctx, root, "Nested Key");
     172           0 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND, "reg_key_del");
     173             : 
     174           0 :         return true;
     175             : }
     176             : 
     177           0 : static bool test_set_value(struct torture_context *tctx,
     178             :                            const void *test_data)
     179             : {
     180             :         WERROR error;
     181             :         struct hive_key *subkey;
     182           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     183           0 :         TALLOC_CTX *mem_ctx = tctx;
     184           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     185           0 :         DATA_BLOB db = { d, 4 };
     186             : 
     187           0 :         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
     188             :                                   NULL, &subkey);
     189           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     190             : 
     191           0 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     192           0 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     193             : 
     194           0 :         return true;
     195             : }
     196             : 
     197           0 : static bool test_get_value(struct torture_context *tctx, const void *test_data)
     198             : {
     199             :         WERROR error;
     200             :         struct hive_key *subkey;
     201           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     202           0 :         TALLOC_CTX *mem_ctx = tctx;
     203             :         uint32_t type;
     204           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     205           0 :         DATA_BLOB db = { d, 4 }, data;
     206             : 
     207           0 :         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
     208             :                                   NULL, &subkey);
     209           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     210             : 
     211           0 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
     212           0 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
     213             :                                   "getting missing value");
     214             : 
     215           0 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     216           0 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     217             : 
     218           0 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
     219           0 :         torture_assert_werr_ok(tctx, error, "getting value");
     220             : 
     221           0 :         torture_assert_int_equal(tctx, data.length, 4, "value length");
     222           0 :         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
     223             : 
     224           0 :         torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
     225             : 
     226           0 :         return true;
     227             : }
     228             : 
     229           0 : static bool test_del_value(struct torture_context *tctx, const void *test_data)
     230             : {
     231             :         WERROR error;
     232             :         struct hive_key *subkey;
     233           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     234           0 :         TALLOC_CTX *mem_ctx = tctx;
     235             :         uint32_t type;
     236           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     237           0 :         DATA_BLOB db = { d, 4 };
     238             : 
     239           0 :         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
     240             :                                                          NULL, &subkey);
     241           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     242             : 
     243           0 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     244           0 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     245             : 
     246           0 :         error = hive_key_del_value(mem_ctx, subkey, "Answer");
     247           0 :         torture_assert_werr_ok(tctx, error, "deleting value");
     248             : 
     249           0 :         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &db);
     250           0 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND, "getting value");
     251             : 
     252           0 :         error = hive_key_del_value(mem_ctx, subkey, "Answer");
     253           0 :         torture_assert_werr_equal(tctx, error, WERR_FILE_NOT_FOUND,
     254             :                                   "deleting value");
     255             : 
     256           0 :         return true;
     257             : }
     258             : 
     259           0 : static bool test_list_values(struct torture_context *tctx,
     260             :                              const void *test_data)
     261             : {
     262             :         WERROR error;
     263             :         struct hive_key *subkey;
     264           0 :         const struct hive_key *root = (const struct hive_key *)test_data;
     265           0 :         TALLOC_CTX *mem_ctx = tctx;
     266             :         uint32_t type;
     267           0 :         uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
     268           0 :         DATA_BLOB db = { d, 4 }, data;
     269             :         const char *name;
     270             : 
     271           0 :         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
     272             :                                   NULL, &subkey);
     273           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     274             : 
     275           0 :         error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
     276           0 :         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
     277             : 
     278           0 :         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
     279             :                                         &type, &data);
     280           0 :         torture_assert_werr_ok(tctx, error, "getting value");
     281             : 
     282           0 :         torture_assert_str_equal(tctx, name, "Answer", "value name");
     283             : 
     284           0 :         torture_assert_int_equal(tctx, data.length, 4, "value length");
     285           0 :         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
     286             :         
     287           0 :         torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
     288             :         
     289           0 :         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
     290             :                                         &type, &data);
     291           0 :         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
     292             :                                   "getting missing value");
     293             : 
     294           0 :         return true;
     295             : }
     296             : 
     297           0 : static bool test_hive_security(struct torture_context *tctx, const void *_data)
     298             : {
     299           0 :         struct hive_key *subkey = NULL;
     300           0 :         const struct hive_key *root = _data;
     301             :         WERROR error;
     302             :         struct security_descriptor *osd, *nsd;
     303             :         
     304           0 :         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           0 :         error = hive_key_add_name(tctx, root, "SecurityKey", NULL,
     315             :                                   osd, &subkey);
     316           0 :         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
     317             : 
     318           0 :         error = hive_get_sec_desc(tctx, subkey, &nsd);
     319           0 :         torture_assert_werr_ok (tctx, error, "getting security descriptor");
     320             : 
     321           0 :         torture_assert(tctx, security_descriptor_equal(osd, nsd),
     322             :                        "security descriptor changed!");
     323             : 
     324             :         /* Create a fresh security descriptor */        
     325           0 :         talloc_free(osd);
     326           0 :         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           0 :         error = hive_set_sec_desc(subkey, osd);
     336           0 :         torture_assert_werr_ok(tctx, error, "setting security descriptor");
     337             :         
     338           0 :         error = hive_get_sec_desc(tctx, subkey, &nsd);
     339           0 :         torture_assert_werr_ok (tctx, error, "getting security descriptor");
     340             :         
     341           0 :         torture_assert(tctx, security_descriptor_equal(osd, nsd),
     342             :                        "security descriptor changed!");
     343             : 
     344           0 :         return true;
     345             : }
     346             : 
     347        1928 : static void tcase_add_tests(struct torture_tcase *tcase)
     348             : {
     349        1928 :         torture_tcase_add_simple_test_const(tcase, "del_nonexistent_key",
     350             :                                                 test_del_nonexistent_key);
     351        1928 :         torture_tcase_add_simple_test_const(tcase, "add_subkey",
     352             :                                                 test_add_subkey);
     353        1928 :         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        1928 :         torture_tcase_add_simple_test_const(tcase, "del_recursive",
     359             :                                                 test_del_recursive);
     360        1928 :         torture_tcase_add_simple_test_const(tcase, "get_info",
     361             :                                                 test_keyinfo_root);
     362        1928 :         torture_tcase_add_simple_test(tcase, "get_info_nums",
     363             :                                                 test_keyinfo_nums);
     364        1928 :         torture_tcase_add_simple_test_const(tcase, "set_value",
     365             :                                                 test_set_value);
     366        1928 :         torture_tcase_add_simple_test_const(tcase, "get_value",
     367             :                                                 test_get_value);
     368        1928 :         torture_tcase_add_simple_test_const(tcase, "list_values",
     369             :                                                 test_list_values);
     370        1928 :         torture_tcase_add_simple_test_const(tcase, "del_key",
     371             :                                                 test_del_key);
     372        1928 :         torture_tcase_add_simple_test_const(tcase, "del_value",
     373             :                                                 test_del_value);
     374        1928 :         torture_tcase_add_simple_test_const(tcase, "check hive security",
     375             :                                                 test_hive_security);
     376        1928 : }
     377             : 
     378           0 : static bool hive_setup_ldb(struct torture_context *tctx, void **data)
     379             : {
     380             :         struct hive_key *key;
     381             :         WERROR error;
     382             :         char *dirname;
     383             :         NTSTATUS status;
     384             : 
     385           0 :         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
     386           0 :         if (!NT_STATUS_IS_OK(status))
     387           0 :                 return false;
     388             : 
     389           0 :         rmdir(dirname);
     390             : 
     391           0 :         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
     392           0 :         if (!W_ERROR_IS_OK(error)) {
     393           0 :                 fprintf(stderr, "Unable to initialize ldb hive\n");
     394           0 :                 return false;
     395             :         }
     396             : 
     397           0 :         *data = key;
     398             : 
     399           0 :         return true;
     400             : }
     401             : 
     402           0 : static bool hive_setup_regf(struct torture_context *tctx, void **data)
     403             : {
     404             :         struct hive_key *key;
     405             :         WERROR error;
     406             :         char *dirname;
     407             :         NTSTATUS status;
     408             : 
     409           0 :         status = torture_temp_dir(tctx, "hive-regf", &dirname);
     410           0 :         if (!NT_STATUS_IS_OK(status))
     411           0 :                 return false;
     412             : 
     413           0 :         rmdir(dirname);
     414             : 
     415           0 :         error = reg_create_regf_file(tctx, dirname, 5, &key);
     416           0 :         if (!W_ERROR_IS_OK(error)) {
     417           0 :                 fprintf(stderr, "Unable to create new regf file\n");
     418           0 :                 return false;
     419             :         }
     420             : 
     421           0 :         *data = key;
     422             : 
     423           0 :         return true;
     424             : }
     425             : 
     426         964 : struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
     427             : {
     428             :         struct torture_tcase *tcase;
     429         964 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "hive");
     430             : 
     431         964 :         tcase = torture_suite_add_tcase(suite, "ldb");
     432         964 :         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
     433         964 :         tcase_add_tests(tcase);
     434             : 
     435         964 :         tcase = torture_suite_add_tcase(suite, "regf");
     436         964 :         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
     437         964 :         tcase_add_tests(tcase);
     438             : 
     439         964 :         return suite;
     440             : }

Generated by: LCOV version 1.13