560f5484933ef489d90bae42cd6a8d02055c7403
[debian/amanda] / installcheck / Amanda_Archive.pl
1 # Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 20;
20 use strict;
21 use warnings;
22
23 # This test only puts the perl wrappers through their paces -- the underlying
24 # library is well-covered by amar-test.
25
26 use lib "@amperldir@";
27 use Amanda::Archive;
28 use Amanda::Paths;
29 use Data::Dumper;
30
31 my $arch_filename = "$AMANDA_TMPDIR/amanda_archive.bin";
32 my $data_filename = "$AMANDA_TMPDIR/some_data.bin";
33 my ($fh, $dfh, $ar, $f1, $f2, $a1, $a2, @res, $posn);
34
35 # some versions of Test::More will fail tests if the identity
36 # relationships of the two objects passed to is_deeply do not
37 # match, so we use the same object for $user_data throughout.
38 my $user_data = [ "x", "y", "z" ];
39
40 # set up a large file full of data
41
42 open($dfh, ">", $data_filename);
43 my $onek = "abcd" x 256;
44 my $onemeg = $onek x 1024;
45 for (my $i = 0; $i < 5; $i++) {
46     print $dfh $onemeg;
47 }
48 $onek = $onemeg = undef;
49 close($dfh);
50
51 # utility functions for creating a "fake" archive file
52
53 sub make_header {
54     my ($fh, $version) = @_;
55     my $hdr = "AMANDA ARCHIVE FORMAT $version";
56     $hdr .= "\0" x (28 - length $hdr);
57     print $fh $hdr;
58 }
59
60 sub make_record {
61     my ($fh, $filenum, $attrid, $data, $eoa) = @_;
62     my $size = length($data);
63     if ($eoa) {
64         $size |= 0x80000000;
65     }
66     print $fh pack("nnN", $filenum, $attrid, $size);
67     print $fh $data;
68 }
69
70 ####
71 ## TEST WRITING
72
73 open($fh, ">", $arch_filename) or die("opening $arch_filename: $!");
74 $ar = Amanda::Archive->new(fileno($fh), ">");
75 pass("Create a new archive");
76
77 $f1 = $ar->new_file("filename1");
78 pass("Start an archive file");
79
80 $a1 = $f1->new_attr(18);
81 $a1->add_data("foo!", 0);
82 $a2 = $f1->new_attr(19);
83 $a2->add_data("BAR!", 0);
84 $a1->add_data("FOO.", 1);
85 $a2->add_data("bar.", 0);
86 pass("Write some interleaved data");
87
88 $a1->close();
89 pass("Close an attribute with the close() method");
90
91 $a1 = Amanda::Archive::Attr->new($f1, 99);
92 pass("Create an attribute with its constructor");
93
94 open($dfh, "<", $data_filename);
95 $a1->add_data_fd(fileno($dfh), 1);
96 close($dfh);
97 pass("Add data from a file descriptor");
98
99 $a1 = undef;
100 pass("Close attribute when its refcount hits zero");
101
102 $f2 = Amanda::Archive::File->new($ar, "filename2");
103 pass("Add a new file (filename2)");
104
105 $a1 = $f2->new_attr(82);
106 $a1->add_data("word", 1);
107 pass("Add data to it");
108
109 $a2->add_data("barrrrr?", 0);   # note no EOA
110 pass("Add more data to first attribute");
111
112 ($f1, $posn) = $ar->new_file("posititioned file", 1);
113 ok($posn > 0, "new_file returns a positive position");
114
115 $ar = undef;
116 pass("unref archive early");
117
118 ($ar, $f1, $f2, $a1, $a2) = ();
119 pass("Close remaining objects");
120
121 close($fh);
122
123 ####
124 ## TEST READING
125
126 open($fh, ">", $arch_filename);
127 make_header($fh, 1);
128 make_record($fh, 16, 0, "/etc/passwd", 1);
129 make_record($fh, 16, 20, "root:foo", 1);
130 make_record($fh, 16, 21, "boot:foot", 0);
131 make_record($fh, 16, 22, "dustin:snazzy", 1);
132 make_record($fh, 16, 21, "..more-boot:foot", 1);
133 make_record($fh, 16, 1, "", 1);
134 close($fh);
135
136 open($fh, "<", $arch_filename);
137 $ar = Amanda::Archive->new(fileno($fh), "<");
138 pass("Create a new archive for reading");
139
140 @res = ();
141 $ar->read(
142     file_start => sub {
143         push @res, [ "file_start", @_ ];
144         return "cows";
145     },
146     file_finish => sub {
147         push @res, [ "file_finish", @_ ];
148     },
149     0 => sub {
150         push @res, [ "frag", @_ ];
151         return "ants";
152     },
153     user_data => $user_data,
154 );
155 is_deeply([@res], [
156         [ 'file_start', $user_data, 16, '/etc/passwd' ],
157         [ 'frag', $user_data, 16, "cows", 20, undef, 'root:foo', 1, 0 ],
158         [ 'frag', $user_data, 16, "cows", 21, undef, 'boot:foot', 0, 0 ],
159         [ 'frag', $user_data, 16, "cows", 22, undef, 'dustin:snazzy', 1, 0 ],
160         [ 'frag', $user_data, 16, "cows", 21, "ants", '..more-boot:foot', 1, 0 ],
161         [ 'file_finish', $user_data, "cows", 16, 0 ]
162 ], "simple read callbacks called in the right order")
163     or diag(Dumper(\@res));
164 $ar->close();
165 close($fh);
166
167
168 open($fh, "<", $arch_filename);
169 $ar = Amanda::Archive->new(fileno($fh), "<");
170 pass("Create a new archive for reading");
171
172 @res = ();
173 $ar->read(
174     file_start => sub {
175         push @res, [ "file_start", @_ ];
176         return "IGNORE";
177     },
178     file_finish => sub {
179         push @res, [ "file_finish", @_ ];
180     },
181     0 => sub {
182         push @res, [ "frag", @_ ];
183         return "ants";
184     },
185     user_data => $user_data,
186 );
187 is_deeply([@res], [
188         [ 'file_start', $user_data, 16, '/etc/passwd' ],
189 ], "'IGNORE' handled correctly")
190     or diag(Dumper(\@res));
191 # TODO: check that file data gets dumped appropriately?
192
193
194 open($fh, "<", $arch_filename);
195 $ar = Amanda::Archive->new(fileno($fh), "<");
196
197 @res = ();
198 $ar->read(
199     file_start => sub {
200         push @res, [ "file_start", @_ ];
201         return "dogs";
202     },
203     file_finish => sub {
204         push @res, [ "file_finish", @_ ];
205     },
206     21 => [ 100, sub {
207         push @res, [ "fragbuf", @_ ];
208         return "pants";
209     } ],
210     0 => sub {
211         push @res, [ "frag", @_ ];
212         return "ants";
213     },
214     user_data => $user_data,
215 );
216 is_deeply([@res], [
217         [ 'file_start', $user_data, 16, '/etc/passwd' ],
218         [ 'frag', $user_data, 16, "dogs", 20, undef, 'root:foo', 1, 0 ],
219         [ 'frag', $user_data, 16, "dogs", 22, undef, 'dustin:snazzy', 1, 0 ],
220         [ 'fragbuf', $user_data, 16, "dogs", 21, undef, 'boot:foot..more-boot:foot', 1, 0 ],
221         [ 'file_finish', $user_data, "dogs", 16, 0 ]
222 ], "buffering parameters parsed correctly")
223     or diag(Dumper(\@res));
224
225
226 open($fh, "<", $arch_filename);
227 $ar = Amanda::Archive->new(fileno($fh), "<");
228
229 @res = ();
230 eval {
231     $ar->read(
232         file_start => sub {
233             push @res, [ "file_start", @_ ];
234             die "uh oh";
235         },
236         user_data => $user_data,
237     );
238 };
239 like($@, qr/uh oh at .*/, "exception propagated correctly");
240 is_deeply([@res], [
241         [ 'file_start', $user_data, 16, '/etc/passwd' ],
242 ], "file_start called before exception was rasied")
243     or diag(Dumper(\@res));
244 $ar->close();
245
246 unlink($data_filename);