File: insert-keys.pl

package info (click to toggle)
hivex 1.3.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,924 kB
  • sloc: ansic: 7,385; sh: 5,314; ml: 4,424; makefile: 559; perl: 528; python: 253; ruby: 123; xml: 104; sed: 16
file content (84 lines) | stat: -rwxr-xr-x 2,524 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env perl
# hivex extra tests
# Copyright (C) 2013 Red Hat Inc.
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# This is a test of RHBZ#987463: Take the real hives and try inserting
# a new key as a subkey of every key in the hive, and verify that we
# get the expected data and don't get any errors.

use strict;
use warnings;

use Win::Hivex;

my @hives = glob "*.hive";
die "no hive files found in current directory" unless @hives > 0;

foreach my $hive (@hives) {
    print "$hive\n";

    # Note we open it for writing, but never commit, so no changes
    # are saved back to disk.
    my $h = Win::Hivex->open ($hive, write => 1);
    my $root = $h->root ();

    my $added = test_node ($h, $root);
    print "inserted $added nodes successfully\n";
}

sub test_node
{
    my $h = shift;
    my $node = shift;

    local $_;
    my $added = 0;

    # Child nodes before adding the child.
    my @children_before = $h->node_children ($node);

    # Choose a random name for the new key, so that it won't clash
    # with an existing key and so that over time we test inserting at
    # all positions within the key.
    my @chars = ("A".."Z", "a".."z", "0".."9");
    my $string = "";
    $string .= $chars[rand @chars] for 1..8;
    $h->node_add_child ($node, $string);
    $added++;

    # Child nodes after adding the child.
    my @children_after = $h->node_children ($node);

    die "expected ", 0+@children_before, " + 1 == ", 0+@children_after
        unless @children_before+1 == @children_after;

    foreach (@children_after) {
        if ($h->node_name ($_) eq $string) {
            goto found;
        }
    }
    die "did not find new node in node children";

  found:
    # Iterate into the child nodes.  Note don't iterate into the
    # newly created child node.
    $added += test_node ($h, $_) foreach @children_before;

    return $added;
}

exit 0