1 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use Test::More tests => 20;
24 # This test only puts the perl wrappers through their paces -- the underlying
25 # library is well-covered by amar-test.
27 use lib "@amperldir@";
33 my $arch_filename = "$Installcheck::TMP/amanda_archive.bin";
34 my $data_filename = "$Installcheck::TMP/some_data.bin";
35 my ($fh, $dfh, $ar, $f1, $f2, $a1, $a2, @res, $posn);
37 # some versions of Test::More will fail tests if the identity
38 # relationships of the two objects passed to is_deeply do not
39 # match, so we use the same object for $user_data throughout.
40 my $user_data = [ "x", "y", "z" ];
42 # set up a large file full of data
44 open($dfh, ">", $data_filename);
45 my $onek = "abcd" x 256;
46 my $onemeg = $onek x 1024;
47 for (my $i = 0; $i < 5; $i++) {
50 $onek = $onemeg = undef;
53 # utility functions for creating a "fake" archive file
56 my ($fh, $version) = @_;
57 my $hdr = "AMANDA ARCHIVE FORMAT $version";
58 $hdr .= "\0" x (28 - length $hdr);
63 my ($fh, $filenum, $attrid, $data, $eoa) = @_;
64 my $size = length($data);
68 print $fh pack("nnN", $filenum, $attrid, $size);
75 open($fh, ">", $arch_filename) or die("opening $arch_filename: $!");
76 $ar = Amanda::Archive->new(fileno($fh), ">");
77 pass("Create a new archive");
79 $f1 = $ar->new_file("filename1");
80 pass("Start an archive file");
82 $a1 = $f1->new_attr(18);
83 $a1->add_data("foo!", 0);
84 $a2 = $f1->new_attr(19);
85 $a2->add_data("BAR!", 0);
86 $a1->add_data("FOO.", 1);
87 $a2->add_data("bar.", 0);
88 pass("Write some interleaved data");
91 pass("Close an attribute with the close() method");
93 $a1 = Amanda::Archive::Attr->new($f1, 99);
94 pass("Create an attribute with its constructor");
96 open($dfh, "<", $data_filename);
97 $a1->add_data_fd(fileno($dfh), 1);
99 pass("Add data from a file descriptor");
102 pass("Close attribute when its refcount hits zero");
104 $f2 = Amanda::Archive::File->new($ar, "filename2");
105 pass("Add a new file (filename2)");
107 $a1 = $f2->new_attr(82);
108 $a1->add_data("word", 1);
109 pass("Add data to it");
111 $a2->add_data("barrrrr?", 0); # note no EOA
112 pass("Add more data to first attribute");
114 ($f1, $posn) = $ar->new_file("posititioned file", 1);
115 ok($posn > 0, "new_file returns a positive position");
118 pass("unref archive early");
120 ($ar, $f1, $f2, $a1, $a2) = ();
121 pass("Close remaining objects");
128 open($fh, ">", $arch_filename);
130 make_record($fh, 16, 0, "/etc/passwd", 1);
131 make_record($fh, 16, 20, "root:foo", 1);
132 make_record($fh, 16, 21, "boot:foot", 0);
133 make_record($fh, 16, 22, "dustin:snazzy", 1);
134 make_record($fh, 16, 21, "..more-boot:foot", 1);
135 make_record($fh, 16, 1, "", 1);
138 open($fh, "<", $arch_filename);
139 $ar = Amanda::Archive->new(fileno($fh), "<");
140 pass("Create a new archive for reading");
145 push @res, [ "file_start", @_ ];
149 push @res, [ "file_finish", @_ ];
152 push @res, [ "frag", @_ ];
155 user_data => $user_data,
158 [ 'file_start', $user_data, 16, '/etc/passwd' ],
159 [ 'frag', $user_data, 16, "cows", 20, undef, 'root:foo', 1, 0 ],
160 [ 'frag', $user_data, 16, "cows", 21, undef, 'boot:foot', 0, 0 ],
161 [ 'frag', $user_data, 16, "cows", 22, undef, 'dustin:snazzy', 1, 0 ],
162 [ 'frag', $user_data, 16, "cows", 21, "ants", '..more-boot:foot', 1, 0 ],
163 [ 'file_finish', $user_data, "cows", 16, 0 ]
164 ], "simple read callbacks called in the right order")
165 or diag(Dumper(\@res));
170 open($fh, "<", $arch_filename);
171 $ar = Amanda::Archive->new(fileno($fh), "<");
172 pass("Create a new archive for reading");
177 push @res, [ "file_start", @_ ];
181 push @res, [ "file_finish", @_ ];
184 push @res, [ "frag", @_ ];
187 user_data => $user_data,
190 [ 'file_start', $user_data, 16, '/etc/passwd' ],
191 ], "'IGNORE' handled correctly")
192 or diag(Dumper(\@res));
193 # TODO: check that file data gets dumped appropriately?
196 open($fh, "<", $arch_filename);
197 $ar = Amanda::Archive->new(fileno($fh), "<");
202 push @res, [ "file_start", @_ ];
206 push @res, [ "file_finish", @_ ];
209 push @res, [ "fragbuf", @_ ];
213 push @res, [ "frag", @_ ];
216 user_data => $user_data,
219 [ 'file_start', $user_data, 16, '/etc/passwd' ],
220 [ 'frag', $user_data, 16, "dogs", 20, undef, 'root:foo', 1, 0 ],
221 [ 'frag', $user_data, 16, "dogs", 22, undef, 'dustin:snazzy', 1, 0 ],
222 [ 'fragbuf', $user_data, 16, "dogs", 21, undef, 'boot:foot..more-boot:foot', 1, 0 ],
223 [ 'file_finish', $user_data, "dogs", 16, 0 ]
224 ], "buffering parameters parsed correctly")
225 or diag(Dumper(\@res));
228 open($fh, "<", $arch_filename);
229 $ar = Amanda::Archive->new(fileno($fh), "<");
235 push @res, [ "file_start", @_ ];
238 user_data => $user_data,
241 like($@, qr/uh oh at .*/, "exception propagated correctly");
243 [ 'file_start', $user_data, 16, '/etc/passwd' ],
244 ], "file_start called before exception was rasied")
245 or diag(Dumper(\@res));
248 unlink($data_filename);
249 unlink($arch_filename);