1 # Copyright (c) 2005-2008 Zmanda Inc. All Rights Reserved.
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.
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
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
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 use Test::More tests => 20;
23 # This test only puts the perl wrappers through their paces -- the underlying
24 # library is well-covered by amar-test.
26 use lib "@amperldir@";
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);
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" ];
40 # set up a large file full of data
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++) {
48 $onek = $onemeg = undef;
51 # utility functions for creating a "fake" archive file
54 my ($fh, $version) = @_;
55 my $hdr = "AMANDA ARCHIVE FORMAT $version";
56 $hdr .= "\0" x (28 - length $hdr);
61 my ($fh, $filenum, $attrid, $data, $eoa) = @_;
62 my $size = length($data);
66 print $fh pack("nnN", $filenum, $attrid, $size);
73 open($fh, ">", $arch_filename) or die("opening $arch_filename: $!");
74 $ar = Amanda::Archive->new(fileno($fh), ">");
75 pass("Create a new archive");
77 $f1 = $ar->new_file("filename1");
78 pass("Start an archive file");
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");
89 pass("Close an attribute with the close() method");
91 $a1 = Amanda::Archive::Attr->new($f1, 99);
92 pass("Create an attribute with its constructor");
94 open($dfh, "<", $data_filename);
95 $a1->add_data_fd(fileno($dfh), 1);
97 pass("Add data from a file descriptor");
100 pass("Close attribute when its refcount hits zero");
102 $f2 = Amanda::Archive::File->new($ar, "filename2");
103 pass("Add a new file (filename2)");
105 $a1 = $f2->new_attr(82);
106 $a1->add_data("word", 1);
107 pass("Add data to it");
109 $a2->add_data("barrrrr?", 0); # note no EOA
110 pass("Add more data to first attribute");
112 ($f1, $posn) = $ar->new_file("posititioned file", 1);
113 ok($posn > 0, "new_file returns a positive position");
116 pass("unref archive early");
118 ($ar, $f1, $f2, $a1, $a2) = ();
119 pass("Close remaining objects");
126 open($fh, ">", $arch_filename);
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);
136 open($fh, "<", $arch_filename);
137 $ar = Amanda::Archive->new(fileno($fh), "<");
138 pass("Create a new archive for reading");
143 push @res, [ "file_start", @_ ];
147 push @res, [ "file_finish", @_ ];
150 push @res, [ "frag", @_ ];
153 user_data => $user_data,
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));
168 open($fh, "<", $arch_filename);
169 $ar = Amanda::Archive->new(fileno($fh), "<");
170 pass("Create a new archive for reading");
175 push @res, [ "file_start", @_ ];
179 push @res, [ "file_finish", @_ ];
182 push @res, [ "frag", @_ ];
185 user_data => $user_data,
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?
194 open($fh, "<", $arch_filename);
195 $ar = Amanda::Archive->new(fileno($fh), "<");
200 push @res, [ "file_start", @_ ];
204 push @res, [ "file_finish", @_ ];
207 push @res, [ "fragbuf", @_ ];
211 push @res, [ "frag", @_ ];
214 user_data => $user_data,
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));
226 open($fh, "<", $arch_filename);
227 $ar = Amanda::Archive->new(fileno($fh), "<");
233 push @res, [ "file_start", @_ ];
236 user_data => $user_data,
239 like($@, qr/uh oh at .*/, "exception propagated correctly");
241 [ 'file_start', $user_data, 16, '/etc/passwd' ],
242 ], "file_start called before exception was rasied")
243 or diag(Dumper(\@res));
246 unlink($data_filename);