LCOV - code coverage report
Current view: top level - source4/lib/registry/tests - diff.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 132 141 93.6 %
Date: 2024-01-11 09:59:51 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local testing of registry diff functionality
       5             : 
       6             :    Copyright (C) Jelmer Vernooij 2007
       7             :    Copyright (C) Wilco Baan Hofman 2008
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      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 "param/param.h"
      28             : #include "lib/registry/tests/proto.h"
      29             : 
      30             : struct diff_tcase_data {
      31             :         struct registry_context *r1_ctx;
      32             :         struct registry_context *r2_ctx;
      33             :         struct reg_diff_callbacks *callbacks;
      34             :         void *callback_data;
      35             :         char *tempdir;
      36             :         char *filename;
      37             : };
      38             : 
      39           2 : static bool test_generate_diff(struct torture_context *tctx, void *tcase_data)
      40             : {
      41           2 :         WERROR error;
      42           2 :         struct diff_tcase_data *td = tcase_data;
      43             : 
      44           2 :         error = reg_generate_diff(td->r1_ctx, td->r2_ctx, 
      45           2 :                         td->callbacks,
      46             :                         td->callback_data);
      47           2 :         torture_assert_werr_ok(tctx, error, "reg_generate_diff");
      48             : 
      49           0 :         return true;
      50             : }
      51             : 
      52             : #if 0
      53             : static bool test_diff_load(struct torture_context *tctx, void *tcase_data)
      54             : {
      55             :         struct diff_tcase_data *td = tcase_data;
      56             :         struct reg_diff_callbacks *callbacks;
      57             :         void *data;
      58             :         WERROR error;
      59             : 
      60             :         error = reg_diff_load(td->filename, callbacks, data);
      61             :         torture_assert_werr_ok(tctx, error, "reg_diff_load");
      62             : 
      63             :         return true;
      64             : }
      65             : #endif
      66           2 : static bool test_diff_apply(struct torture_context *tctx, void *tcase_data)
      67             : {
      68           2 :         struct diff_tcase_data *td = tcase_data;
      69           2 :         struct registry_key *key;
      70           2 :         WERROR error;
      71             : 
      72           2 :         error = reg_diff_apply(td->r1_ctx, td->filename);
      73           2 :         torture_assert_werr_ok(tctx, error, "reg_diff_apply");
      74             : 
      75           2 :         error = td->r1_ctx->ops->get_predefined_key(td->r1_ctx, HKEY_LOCAL_MACHINE, &key);
      76           2 :         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
      77             : 
      78             :         /* If this generates an error it could be that the apply doesn't work,
      79             :          * but also that the reg_generate_diff didn't work. */
      80           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Software", &key);
      81           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software failed");
      82           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Microsoft", &key);
      83           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software\\Microsoft failed");
      84           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Windows", &key);
      85           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Microsoft\\Windows failed");
      86           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "CurrentVersion", &key);
      87           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Windows\\CurrentVersion failed");
      88           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Policies", &key);
      89           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\CurrentVersion\\Policies failed");
      90           2 :         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Explorer", &key);
      91           2 :         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Policies\\Explorer failed");
      92             : 
      93           0 :         return true;
      94             : }
      95             : 
      96             : static const char *added_key = NULL;
      97             : 
      98           0 : static WERROR test_add_key(void *callback_data, const char *key_name)
      99             : {
     100           0 :         added_key = talloc_strdup(callback_data, key_name);
     101             : 
     102           0 :         return WERR_OK;
     103             : }
     104             : 
     105           2 : static bool test_generate_diff_key_add(struct torture_context *tctx, void *tcase_data)
     106             : {
     107           2 :         struct reg_diff_callbacks cb;
     108           2 :         struct registry_key rk;
     109             : 
     110           2 :         return true;
     111             : 
     112             :         ZERO_STRUCT(cb);
     113             : 
     114             :         cb.add_key = test_add_key;
     115             : 
     116             :         if (W_ERROR_IS_OK(reg_generate_diff_key(&rk, NULL, "bla", &cb, tctx)))
     117             :                 return false;
     118             : 
     119             :         torture_assert_str_equal(tctx, added_key, "bla", "key added");
     120             : 
     121             :         return true;
     122             : }
     123             : 
     124           2 : static bool test_generate_diff_key_null(struct torture_context *tctx, void *tcase_data)
     125             : {
     126           2 :         struct reg_diff_callbacks cb;
     127             : 
     128           2 :         ZERO_STRUCT(cb);
     129             : 
     130           2 :         if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL, NULL, "", &cb, NULL)))
     131           0 :                 return false;
     132             : 
     133           0 :         return true;
     134             : }
     135             : 
     136        4716 : static void tcase_add_tests (struct torture_tcase *tcase) 
     137             : {
     138        4716 :         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_add",
     139             :                         test_generate_diff_key_add);
     140        4716 :         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_null",
     141             :                         test_generate_diff_key_null);
     142        4716 :         torture_tcase_add_simple_test(tcase, "test_generate_diff",
     143             :                         test_generate_diff);
     144        4716 :         torture_tcase_add_simple_test(tcase, "test_diff_apply",
     145             :                         test_diff_apply);
     146             : /*      torture_tcase_add_simple_test(tcase, "test_diff_load",
     147             :                         test_diff_load);
     148             : */
     149        4716 : }
     150             : 
     151           2 : static bool diff_setup_tcase(struct torture_context *tctx, void **data)
     152             : {
     153           2 :         struct registry_context *r1_ctx, *r2_ctx;
     154           2 :         WERROR error;
     155           2 :         NTSTATUS status;
     156           2 :         struct hive_key *r1_hklm, *r1_hkcu;
     157           2 :         struct hive_key *r2_hklm, *r2_hkcu;
     158           2 :         const char *filename;
     159           2 :         struct diff_tcase_data *td;
     160           2 :         struct registry_key *key, *newkey;
     161           2 :         DATA_BLOB blob;
     162             : 
     163           2 :         td = talloc(tctx, struct diff_tcase_data);
     164             : 
     165             :         /* Create two registry contexts */
     166           2 :         error = reg_open_local(tctx, &r1_ctx);
     167           2 :         torture_assert_werr_ok(tctx, error, "Opening registry 1 for patch tests failed");
     168             :         
     169           2 :         error = reg_open_local(tctx, &r2_ctx);
     170           2 :         torture_assert_werr_ok(tctx, error, "Opening registry 2 for patch tests failed");
     171             : 
     172             :         /* Create temp directory */
     173           2 :         status = torture_temp_dir(tctx, "patchfile", &td->tempdir);
     174           2 :         torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
     175             : 
     176             :         /* Create and mount HKLM and HKCU hives for registry 1 */
     177           2 :         filename = talloc_asprintf(tctx, "%s/r1_local_machine.ldb", td->tempdir);
     178           2 :         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hklm);
     179           2 :         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
     180             : 
     181           2 :         error = reg_mount_hive(r1_ctx, r1_hklm, HKEY_LOCAL_MACHINE, NULL);
     182           2 :         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
     183             :         
     184           2 :         filename = talloc_asprintf(tctx, "%s/r1_current_user.ldb", td->tempdir);
     185           2 :         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hkcu);
     186           2 :         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
     187             : 
     188           2 :         error = reg_mount_hive(r1_ctx, r1_hkcu, HKEY_CURRENT_USER, NULL);
     189           2 :         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
     190             :         
     191             :         /* Create and mount HKLM and HKCU hives for registry 2 */
     192           2 :         filename = talloc_asprintf(tctx, "%s/r2_local_machine.ldb", td->tempdir);
     193           2 :         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hklm);
     194           2 :         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
     195             : 
     196           2 :         error = reg_mount_hive(r2_ctx, r2_hklm, HKEY_LOCAL_MACHINE, NULL);
     197           2 :         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
     198             :         
     199           2 :         filename = talloc_asprintf(tctx, "%s/r2_current_user.ldb", td->tempdir);
     200           2 :         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hkcu);
     201           2 :         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
     202             :         
     203           2 :         error = reg_mount_hive(r2_ctx, r2_hkcu, HKEY_CURRENT_USER, NULL);
     204           2 :         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
     205             : 
     206           2 :         error = r1_ctx->ops->get_predefined_key(r1_ctx, HKEY_CURRENT_USER, &key);
     207           2 :         torture_assert_werr_ok(tctx, error, "Opening HKEY_CURRENT_USER failed");
     208           2 :         error = r1_ctx->ops->create_key(r1_ctx, key, "Network", NULL, NULL, &newkey);
     209           2 :         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network failed");
     210           2 :         error = r1_ctx->ops->create_key(r1_ctx, newkey, "L", NULL, NULL, &newkey);
     211           2 :         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network\\L failed");
     212             : 
     213           2 :         error = r2_ctx->ops->get_predefined_key(r2_ctx, HKEY_LOCAL_MACHINE, &key);
     214           2 :         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
     215           2 :         error = r2_ctx->ops->create_key(r2_ctx, key, "Software", NULL, NULL, &newkey);
     216           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software failed");
     217           2 :         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Microsoft", NULL, NULL, &newkey);
     218           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft failed");
     219           2 :         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Windows", NULL, NULL, &newkey);
     220           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft\\Windows failed");
     221           2 :         error = r2_ctx->ops->create_key(r2_ctx, newkey, "CurrentVersion", NULL, NULL, &newkey);
     222           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Windows\\CurrentVersion failed");
     223           2 :         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Policies", NULL, NULL, &newkey);
     224           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\CurrentVersion\\Policies failed");
     225           2 :         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Explorer", NULL, NULL, &newkey);
     226           2 :         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Policies\\Explorer failed");
     227             : 
     228           2 :         blob.data = talloc_array(r2_ctx, uint8_t, 4);
     229             :         /* set "0x03FFFFFF" in little endian format */
     230           2 :         blob.data[0] = 0xFF; blob.data[1] = 0xFF;
     231           2 :         blob.data[2] = 0xFF; blob.data[3] = 0x03;
     232           2 :         blob.length = 4;
     233             : 
     234           2 :         r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
     235             : 
     236             :         /* Set test case data */
     237           2 :         td->r1_ctx = r1_ctx;
     238           2 :         td->r2_ctx = r2_ctx;
     239             : 
     240           2 :         *data = td;
     241             : 
     242           2 :         return true;
     243             : }
     244             : 
     245           1 : static bool diff_setup_preg_tcase (struct torture_context *tctx, void **data)
     246             : {
     247           1 :         struct diff_tcase_data *td;
     248           1 :         WERROR error;
     249             : 
     250           1 :         diff_setup_tcase(tctx, data);
     251           1 :         td = *data;
     252             : 
     253           1 :         td->filename = talloc_asprintf(tctx, "%s/test.pol", td->tempdir);
     254           1 :         error = reg_preg_diff_save(tctx, td->filename,  &td->callbacks,
     255             :                                                            &td->callback_data);
     256           1 :         torture_assert_werr_ok(tctx, error, "reg_preg_diff_save");
     257             : 
     258           0 :         return true;
     259             : }
     260             : 
     261           1 : static bool diff_setup_dotreg_tcase (struct torture_context *tctx, void **data)
     262             : {
     263           1 :         struct diff_tcase_data *td;
     264           1 :         WERROR error;
     265             : 
     266           1 :         diff_setup_tcase(tctx, data);
     267           1 :         td = *data;
     268             : 
     269           1 :         td->filename = talloc_asprintf(tctx, "%s/test.reg", td->tempdir);
     270           1 :         error = reg_dotreg_diff_save(tctx, td->filename, &td->callbacks,
     271             :                                                                  &td->callback_data);
     272           1 :         torture_assert_werr_ok(tctx, error, "reg_dotreg_diff_save");
     273             : 
     274           0 :         return true;
     275             : }
     276             : 
     277        2358 : struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx)
     278             : {
     279         125 :         struct torture_tcase *tcase;
     280        2358 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "diff");
     281             : 
     282        2358 :         tcase = torture_suite_add_tcase(suite, "PReg");
     283        2358 :         torture_tcase_set_fixture(tcase, diff_setup_preg_tcase, NULL);
     284        2358 :         tcase_add_tests(tcase);
     285             : 
     286        2358 :         tcase = torture_suite_add_tcase(suite, "dotreg");
     287        2358 :         torture_tcase_set_fixture(tcase, diff_setup_dotreg_tcase, NULL);
     288        2358 :         tcase_add_tests(tcase);
     289             : 
     290        2358 :         return suite;
     291             : }

Generated by: LCOV version 1.14