regedit: Add implicit nul-termination when importing strings.
[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 Encode qw(from_to);
22 use IO::Scalar;
23
24 use Test::More tests => 16;
25
26 use Win::Hivex;
27 use Win::Hivex::Regedit qw(reg_import reg_export);
28
29 my $srcdir = $ENV{srcdir} || ".";
30
31 my $h = Win::Hivex->open ("$srcdir/../images/minimal", write => 1);
32 ok ($h);
33
34 my ($data, $expected);
35
36 # Note that we don't clear the hive between tests, so results of
37 # next test depend on the previous test.
38
39 $data = '
40 [\A]
41
42 [\B]
43
44 [\C]
45 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
46 "Key2"=str(2):"Hello"
47 "Key3"=hex:48,00,65,00,6c,00,6c,00,6f,00,\
48   48,00,65,00,6c,00,6c,00,6f,00
49 "Key4"=dword:ff123456';
50 $expected = '[\]
51
52 [\A]
53
54 [\B]
55
56 [\C]
57 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
58 "Key2"=hex(2):48,00,65,00,6c,00,6c,00,6f,00,00,00
59 "Key3"=hex(3):48,00,65,00,6c,00,6c,00,6f,00,48,00,65,00,6c,00,6c,00,6f,00
60 "Key4"=dword:ff123456
61
62 ';
63
64 run_test ($data, $expected);
65
66 $data = '
67 [\A]
68 @="Hello"
69
70 -[\B]
71 ';
72 $expected = '[\]
73
74 [\A]
75 @=hex(1):48,00,65,00,6c,00,6c,00,6f,00,00,00
76
77 [\C]
78 "Key1"=hex(2):48,00,65,00,6c,00,6c,00,6f,00
79 "Key2"=hex(2):48,00,65,00,6c,00,6c,00,6f,00,00,00
80 "Key3"=hex(3):48,00,65,00,6c,00,6c,00,6f,00,48,00,65,00,6c,00,6c,00,6f,00
81 "Key4"=dword:ff123456
82
83 ';
84
85 run_test ($data, $expected);
86
87 $data = '
88 [\A]
89 @=-
90
91 -[\C]
92
93 [\A\B]
94 ';
95 $expected = '[\]
96
97 [\A]
98
99 [\A\B]
100
101 ';
102
103 run_test ($data, $expected);
104
105 $data = '
106 [\A]
107 "NotExistant"=-
108
109 [\A\B]
110 "Key\"Containing\"Quotes"=hex(0):
111 ';
112 $expected = '[\]
113
114 [\A]
115
116 [\A\B]
117 "Key\"Containing\"Quotes"=hex(0):
118
119 ';
120
121 run_test ($data, $expected);
122
123 $data = '
124 [\A\B]
125 "Key\"Containing\"Quotes"=-
126
127 -[\A]
128 ';
129 $expected = '[\]
130
131 ';
132
133 run_test ($data, $expected);
134
135 #----------------------------------------------------------------------
136
137 sub run_test {
138     my $data = shift;
139     my $expected = shift;
140
141     my $fh = new IO::Scalar \$data;
142     reg_import ($fh, $h);
143     ok (1);
144
145     $fh = new IO::Scalar;
146     reg_export ($h, "\\", $fh);
147     ok (1);
148
149     my $actual = ${$fh->sref};
150     warn "\n\n----- ACTUAL -----\n$actual\n----- EXPECTED -----\n$expected\n\n"
151         if $actual ne $expected;
152
153     ok ($actual eq $expected)
154 }