regedit: Fix syntax for deleting registry keys (RHBZ#737944).
[hivex.git] / perl / t / 560-regedit-import.t
1 # Win::Hivex::Regedit test -*- perl -*-
2 # Copyright (C) 2010 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 use strict;
19 use warnings;
20
21 use IO::Scalar;
22
23 use Test::More tests => 16;
24
25 use Win::Hivex;
26 use Win::Hivex::Regedit qw(reg_import reg_export);
27
28 my $srcdir = $ENV{srcdir} || ".";
29
30 my $h = Win::Hivex->open ("$srcdir/../images/minimal", write => 1);
31 ok ($h);
32
33 my ($data, $expected);
34
35 # Note that we don't clear the hive between tests, so results of
36 # next test depend on the previous test.
37
38 $data = '
39 [\A]
40
41 [\B]
42
43 [\C]
44 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
45 "Key2"=str(2):"Hello"
46 "Key3"=hex:48,00,65,00,6c,00,6c,00,6f,00,\
47   48,00,65,00,6c,00,6c,00,6f,00
48 "Key4"=dword:ff123456';
49 $expected = '[\]
50
51 [\A]
52
53 [\B]
54
55 [\C]
56 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
57 "Key2"=hex(2):48,00,65,00,6c,00,6c,00,6f,00,00,00
58 "Key3"=hex(3):48,00,65,00,6c,00,6c,00,6f,00,48,00,65,00,6c,00,6c,00,6f,00
59 "Key4"=dword:ff123456
60
61 ';
62
63 run_test ($data, $expected);
64
65 $data = '
66 [\A]
67 @="Hello"
68
69 [-\B]
70 ';
71 $expected = '[\]
72
73 [\A]
74 @=hex(1):48,00,65,00,6c,00,6c,00,6f,00,00,00
75
76 [\C]
77 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
78 "Key2"=hex(2):48,00,65,00,6c,00,6c,00,6f,00,00,00
79 "Key3"=hex(3):48,00,65,00,6c,00,6c,00,6f,00,48,00,65,00,6c,00,6c,00,6f,00
80 "Key4"=dword:ff123456
81
82 ';
83
84 run_test ($data, $expected);
85
86 $data = '
87 [\A]
88 @=-
89
90 [-\C]
91
92 [\A\B]
93 ';
94 $expected = '[\]
95
96 [\A]
97
98 [\A\B]
99
100 ';
101
102 run_test ($data, $expected);
103
104 # In the next test, the value of ValueContainingEscapes in the
105 # imported data is \\W\\, which will become \W\ in the final hive.
106 # However Perl has complex and inconsistent rules on quoting
107 # backslashes.  See:
108 # http://en.wikibooks.org/wiki/Perl_Programming/Strings#Single_Quoted_Strings
109 $data = '
110 [\A]
111 "NotExistant"=-
112
113 [\A\B]
114 "Key\"Containing\"Quotes"=hex(0):
115 "ValueContainingEscapes"="\\\\W\\\\"
116 ';
117 $expected = '[\]
118
119 [\A]
120
121 [\A\B]
122 "Key\"Containing\"Quotes"=hex(0):
123 "ValueContainingEscapes"=hex(1):5c,00,57,00,5c,00,00,00
124
125 ';
126
127 run_test ($data, $expected);
128
129 $data = '
130 [\A\B]
131 "Key\"Containing\"Quotes"=-
132 "ValueContainingEscapes"=-
133
134 [-\A]
135 ';
136 $expected = '[\]
137
138 ';
139
140 run_test ($data, $expected);
141
142 #----------------------------------------------------------------------
143
144 sub run_test {
145     my $data = shift;
146     my $expected = shift;
147
148     my $fh = new IO::Scalar \$data;
149     reg_import ($fh, $h);
150     ok (1);
151
152     $fh = new IO::Scalar;
153     reg_export ($h, "\\", $fh);
154     ok (1);
155
156     my $actual = ${$fh->sref};
157     warn "\n\n----- ACTUAL -----\n$actual\n----- EXPECTED -----\n$expected\n\n"
158         if $actual ne $expected;
159
160     ok ($actual eq $expected)
161 }