2 if( $ENV{PERL_CORE} ) {
3 chdir '../lib/Archive/Tar' if -d '../lib/Archive/Tar';
8 BEGIN { chdir 't' if -d 't' }
10 use Test::More 'no_plan';
20 use File::Spec::Unix ();
21 use File::Basename ();
24 ### need the constants at compile time;
25 use Archive::Tar::Constant;
27 my $Class = 'Archive::Tar';
28 my $FClass = $Class . '::File';
34 ### * change to fullname
35 ### * add tests for global variables
37 ### set up the environment ###
39 ### dirs filename contents
40 [ [], 'c', qr/^iiiiiiiiiiii\s*$/ ],
41 [ [], 'd', qr/^uuuuuuuu\s*$/ ],
44 ### includes binary data
45 my $ALL_CHARS = join '', "\r\n", map( chr, 1..255 ), "zzz\n\r";
47 ### @EXPECTBIN is used to ensure that $tarbin is written in the right
48 ### order and that the contents and order match exactly when extracted
50 ### dirs filename contents ###
51 [ [], 'bIn11', $ALL_CHARS x 11 ],
52 [ [], 'bIn3', $ALL_CHARS x 3 ],
53 [ [], 'bIn4', $ALL_CHARS x 4 ],
54 [ [], 'bIn1', $ALL_CHARS ],
55 [ [], 'bIn2', $ALL_CHARS x 2 ],
58 ### @EXPECTX is used to ensure that $tarx is written in the right
59 ### order and that the contents and order match exactly when extracted
60 ### the 'x/x' extraction used to fail before A::T 1.08
62 ### dirs filename contents
63 [ [ 'x' ], 'k', '', ],
64 [ [ 'x' ], 'x', 'j', ], # failed before A::T 1.08
67 my $LONG_FILE = qq[directory/really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-directory-name/myfile];
69 ### wintendo can't deal with too long paths, so we might have to skip tests ###
70 my $TOO_LONG = ($^O eq 'MSWin32' or $^O eq 'cygwin' or $^O eq 'VMS')
71 && length( cwd(). $LONG_FILE ) > 247;
73 ### warn if we are going to skip long file names
75 diag("No long filename support - long filename extraction disabled") if ! $ENV{PERL_CORE};
77 push @EXPECT_NORMAL, [ [], $LONG_FILE, qr/^hello\s*$/];
80 my @ROOT = grep { length } 'src', $TOO_LONG ? 'short' : 'long';
81 my $NO_UNLINK = $ARGV[0] ? 1 : 0;
85 $Archive::Tar::DEBUG = $Archive::Tar::DEBUG = 1 if $ARGV[1];
87 ### tests for binary and x/x files
88 my $TARBIN = $Class->new;
89 my $TARX = $Class->new;
91 ### paths to a .tar and .tgz file to use for tests
92 my $TAR_FILE = File::Spec->catfile( @ROOT, 'bar.tar' );
93 my $TGZ_FILE = File::Spec->catfile( @ROOT, 'foo.tgz' );
94 my $TBZ_FILE = File::Spec->catfile( @ROOT, 'foo.tbz' );
95 my $OUT_TAR_FILE = File::Spec->catfile( @ROOT, 'out.tar' );
96 my $OUT_TGZ_FILE = File::Spec->catfile( @ROOT, 'out.tgz' );
97 my $OUT_TBZ_FILE = File::Spec->catfile( @ROOT, 'out.tbz' );
99 my $COMPRESS_FILE = 'copy';
100 $^O eq 'VMS' and $COMPRESS_FILE .= '.';
101 copy( File::Basename::basename($0), $COMPRESS_FILE );
102 chmod 0644, $COMPRESS_FILE;
104 ### done setting up environment ###
106 ### check for zlib/bzip2 support
107 { for my $meth ( qw[has_zlib_support has_bzip2_support] ) {
108 can_ok( $Class, $meth );
115 { my $tar = $Class->new;
117 ok( $tar, "Object created" );
118 isa_ok( $tar, $Class );
120 local $Archive::Tar::WARN = 0;
122 ### should be empty to begin with
123 is( $tar->error, '', "The error string is empty" );
125 ### try a read on nothing
126 my @list = $tar->read();
128 ok(!(scalar @list), "Function read returns 0 files on error" );
129 ok( $tar->error, " error string is non empty" );
130 like( $tar->error, qr/No file to read from/,
131 " error string from create()" );
132 unlike( $tar->error, qr/add/, " error string does not contain add" );
134 ### now, add empty data
135 my $obj = $tar->add_data( '' );
137 ok( !$obj, "'add_data' returns undef on error" );
138 ok( $tar->error, " error string is non empty" );
139 like( $tar->error, qr/add/, " error string contains add" );
140 unlike( $tar->error, qr/create/," error string does not contain create" );
142 ### check if ->error eq $error
143 is( $tar->error, $Archive::Tar::error,
144 "Error '$Archive::Tar::error' matches $Class->error method" );
146 ### check that 'contains_file' doesn't warn about missing files.
147 { ### turn on warnings in general!
148 local $Archive::Tar::WARN = 1;
151 local $SIG{__WARN__} = sub { $warnings .= "@_" };
153 my $rv = $tar->contains_file( $$ );
154 ok( !$rv, "Does not contain file '$$'" );
155 is( $warnings, '', " No warnings issued during lookup" );
160 { my @to_try = ($TAR_FILE);
161 push @to_try, $TGZ_FILE if $Class->has_zlib_support;
162 push @to_try, $TBZ_FILE if $Class->has_bzip2_support;
164 for my $type( @to_try ) {
166 ### normal tar + gz compressed file
167 my $tar = $Class->new;
169 ### check we got the object
170 ok( $tar, "Object created" );
171 isa_ok( $tar, $Class );
174 my @list = $tar->read( $type );
175 my $cnt = scalar @list;
176 my $expect = scalar __PACKAGE__->get_expect();
178 ok( $cnt, "Reading '$type' using 'read()'" );
179 is( $cnt, $expect, " All files accounted for" );
181 for my $file ( @list ) {
182 ok( $file, " Got File object" );
183 isa_ok( $file, $FClass );
185 ### whitebox test -- make sure find_entry gets the
187 for my $test ( $file->full_path, $file ) {
188 is( $tar->_find_entry( $test ), $file,
189 " Found proper object" );
192 next unless $file->is_file;
194 my $name = $file->full_path;
195 my($expect_name, $expect_content) =
196 get_expect_name_and_contents( $name, \@EXPECT_NORMAL );
199 ok($expect_name, " Found expected file '$name'" );
201 like($tar->get_content($name), $expect_content,
206 ### list_archive test
207 { my @list = $Class->list_archive( $type );
208 my $cnt = scalar @list;
209 my $expect = scalar __PACKAGE__->get_expect();
211 ok( $cnt, "Reading '$type' using 'list_archive'");
212 is( $cnt, $expect, " All files accounted for" );
214 for my $file ( @list ) {
215 next if __PACKAGE__->is_dir( $file ); # directories
217 my($expect_name, $expect_content) =
218 get_expect_name_and_contents( $file, \@EXPECT_NORMAL );
221 " Found expected file '$file'" );
227 ### add files tests ###
228 { my @add = map { File::Spec->catfile( @ROOT, @$_ ) } ['b'];
229 my @addunix = map { File::Spec::Unix->catfile( @ROOT, @$_ ) } ['b'];
230 my $tar = $Class->new;
232 ### check we got the object
233 ok( $tar, "Object created" );
234 isa_ok( $tar, $Class );
237 { my @files = $tar->add_files( @add );
239 is( scalar @files, scalar @add,
241 is( $files[0]->name,'b', " Proper name" );
244 skip( "You are building perl using symlinks", 1)
245 if ($ENV{PERL_CORE} and $Config{config_args} =~/Dmksymlinks/);
247 is( $files[0]->is_file, 1,
251 like( $files[0]->get_content, qr/^bbbbbbbbbbb\s*$/,
254 ### check if we have then in our tar object
255 for my $file ( @addunix ) {
256 ok( $tar->contains_file($file),
257 " File found in archive" );
261 ### check adding files doesn't conflict with a secondary archive
262 ### old A::T bug, we should keep testing for it
263 { my $tar2 = $Class->new;
264 my @added = $tar2->add_files( $COMPRESS_FILE );
265 my @count = $tar2->list_files;
267 is( scalar @added, 1, " Added files to secondary archive" );
268 is( scalar @added, scalar @count,
269 " No conflict with first archive" );
271 ### check the adding of directories
272 my @add_dirs = File::Spec->catfile( @ROOT );
273 my @dirs = $tar2->add_files( @add_dirs );
274 is( scalar @dirs, scalar @add_dirs,
276 ok( $dirs[0]->is_dir, " Proper type" );
279 ### check if we can add a A::T::File object
280 { my $tar2 = $Class->new;
281 my($added) = $tar2->add_files( $add[0] );
283 ok( $added, " Added a file '$add[0]' to new object" );
284 isa_ok( $added, $FClass, " Object" );
286 my($added2) = $tar2->add_files( $added );
287 ok( $added2, " Added an $FClass object" );
288 isa_ok( $added2, $FClass, " Object" );
290 is_deeply( [$added, $added2], [$tar2->get_files],
291 " All files accounted for" );
292 isnt( $added, $added2, " Different memory allocations" );
296 ### add data tests ###
298 { ### standard data ###
299 my @to_add = ( 'a', 'aaaaa' );
300 my $tar = $Class->new;
302 ### check we got the object
303 ok( $tar, "Object created" );
304 isa_ok( $tar, $Class );
306 ### add a new file item as data
307 my $obj = $tar->add_data( @to_add );
309 ok( $obj, " Adding data" );
310 is( $obj->name, $to_add[0], " Proper name" );
311 is( $obj->is_file, 1, " Proper type" );
312 like( $obj->get_content, qr/^$to_add[1]\s*$/,
317 ### dir/file structure -- x/y always went ok, x/x used to extract
318 ### in the wrong way -- this test catches that
319 for my $list ( [$TARBIN, \@EXPECTBIN],
322 ### XXX GLOBAL! changes may affect other tests!
323 my($tar,$struct) = @$list;
325 for my $aref ( @$struct ) {
326 my ($dirs,$file,$data) = @$aref;
328 my $path = File::Spec::Unix->catfile(
329 grep { length } @$dirs, $file );
331 my $obj = $tar->add_data( $path, $data );
333 ok( $obj, " Adding data '$file'" );
334 is( $obj->full_path, $path,
336 ok( $obj->is_file, " Proper type" );
337 is( $obj->get_content, $data,
344 ### rename/replace_content tests ###
345 { my $tar = $Class->new;
349 ### read in the file, check the proper files are there
350 ok( $tar->read( $TAR_FILE ), "Read in '$TAR_FILE'" );
351 ok( $tar->get_files($from), " Found file '$from'" );
352 { local $Archive::Tar::WARN = 0;
353 ok(!$tar->get_files($to), " File '$to' not yet found" );
356 ### rename an entry, check the rename has happened
357 ok( $tar->rename( $from, $to ), " Renamed '$from' to '$to'" );
358 ok( $tar->get_files($to), " File '$to' now found" );
359 { local $Archive::Tar::WARN = 0;
360 ok(!$tar->get_files($from), " File '$from' no longer found'");
363 ### now, replace the content
364 my($expect_name, $expect_content) =
365 get_expect_name_and_contents( $from, \@EXPECT_NORMAL );
367 like( $tar->get_content($to), $expect_content,
368 "Original content of '$from' in '$to'" );
369 ok( $tar->replace_content( $to, $from ),
370 " Set content for '$to' to '$from'" );
371 is( $tar->get_content($to), $from,
372 " Content for '$to' is indeed '$from'" );
377 my $tar = $Class->new;
379 ok( $tar->read( $TAR_FILE ), "Read in '$TAR_FILE'" );
381 ### remove returns the files left, which should be equal to list_files
382 is( scalar($tar->remove($remove)), scalar($tar->list_files),
383 " Removing file '$remove'" );
385 ### so what's left should be all expected files minus 1
386 is( scalar($tar->list_files), scalar(__PACKAGE__->get_expect) - 1,
387 " Proper files remaining" );
390 ### write + read + extract tests ###
391 SKIP: { ### pesky warnings
392 skip('no IO::String', 326) if !$Archive::Tar::HAS_PERLIO &&
393 !$Archive::Tar::HAS_PERLIO &&
394 !$Archive::Tar::HAS_IO_STRING &&
395 !$Archive::Tar::HAS_IO_STRING;
397 my $tar = $Class->new;
398 my $new = $Class->new;
399 ok( $tar->read( $TAR_FILE ), "Read in '$TAR_FILE'" );
401 for my $aref ( [$tar, \@EXPECT_NORMAL],
402 [$TARBIN, \@EXPECTBIN],
405 my($obj,$struct) = @$aref;
407 ### check if we stringify it ok
408 { my $string = $obj->write;
409 ok( $string, " Stringified tar file has size" );
410 cmp_ok( length($string) % BLOCK, '==', 0,
411 " Tar archive stringified" );
415 { my $out = $OUT_TAR_FILE;
417 ### bug #41798: 'Nonempty $\ when writing a TAR file produces a
418 ### corrupt TAR file' shows that setting $\ breaks writing tar files
419 ### set it here purposely so we can verify NOTHING breaks
423 ok( $obj->write($out),
424 " Wrote tarfile using 'write'" );
425 check_tar_file( $out );
426 check_tar_object( $obj, $struct );
428 ### now read it in again
429 ok( $new->read( $out ),
430 " Read '$out' in again" );
432 check_tar_object( $new, $struct );
434 ### now extract it again
435 ok( $new->extract, " Extracted '$out' with 'extract'" );
436 check_tar_extract( $new, $struct );
438 rm( $out ) unless $NO_UNLINK;
442 { ### create_archive()
443 ok( $Class->create_archive( $out, 0, $COMPRESS_FILE ),
444 " Wrote tarfile using 'create_archive'" );
445 check_tar_file( $out );
447 ### now extract it again
448 ok( $Class->extract_archive( $out ),
449 " Extracted file using 'extract_archive'");
450 rm( $out ) unless $NO_UNLINK;
456 push @out, [ $OUT_TGZ_FILE => 1 ] if $Class->has_zlib_support;
457 push @out, [ $OUT_TBZ_FILE => COMPRESS_BZIP ] if $Class->has_bzip2_support;
459 for my $entry ( @out ) {
461 my( $out, $compression ) = @$entry;
464 ok($obj->write($out, $compression),
465 " Writing compressed file '$out' using 'write'" );
466 check_compressed_file( $out );
468 check_tar_object( $obj, $struct );
470 ### now read it in again
471 ok( $new->read( $out ),
472 " Read '$out' in again" );
473 check_tar_object( $new, $struct );
475 ### now extract it again
477 " Extracted '$out' again" );
478 check_tar_extract( $new, $struct );
480 rm( $out ) unless $NO_UNLINK;
483 { ### create_archive()
484 ok( $Class->create_archive( $out, $compression, $COMPRESS_FILE ),
485 " Wrote '$out' using 'create_archive'" );
486 check_compressed_file( $out );
488 ### now extract it again
489 ok( $Class->extract_archive( $out, $compression ),
490 " Extracted file using 'extract_archive'");
491 rm( $out ) unless $NO_UNLINK;
499 ### limited read + extract tests ###
500 { my $tar = $Class->new;
501 my @files = $tar->read( $TAR_FILE, 0, { limit => 1 } );
504 is( scalar @files, 1, "Limited read" );
506 my ($name,$content) = get_expect_name_and_contents(
507 $obj->full_path, \@EXPECT_NORMAL );
509 is( $obj->name, $name, " Expected file found" );
512 ### extract this single file to cwd()
513 for my $meth (qw[extract extract_file]) {
515 ### extract it by full path and object
516 for my $arg ( $obj, $obj->full_path ) {
518 ok( $tar->$meth( $arg ),
519 " Extract '$name' to cwd() with $meth" );
520 ok( -e $obj->full_path, " Extracted file exists" );
521 rm( $obj->full_path ) unless $NO_UNLINK;
525 ### extract this file to @ROOT
526 ### can only do that with 'extract_file', not with 'extract'
527 for my $meth (qw[extract_file]) {
528 my $outpath = File::Spec->catdir( @ROOT );
529 my $outfile = File::Spec->catfile( $outpath, $$ ); #$obj->full_path );
531 ok( $tar->$meth( $obj->full_path, $outfile ),
532 " Extract file '$name' to $outpath with $meth" );
533 ok( -e $outfile, " Extracted file '$outfile' exists" );
534 rm( $outfile ) unless $NO_UNLINK;
541 { my $tar = $Class->new;
542 my @files = $tar->read( $TAR_FILE );
544 my $cnt = $tar->list_files();
545 ok( $cnt, "Found old data" );
546 ok( $tar->clear, " Clearing old data" );
548 my $new_cnt = $tar->list_files;
549 ok( !$new_cnt, " Old data cleared" );
552 ### $DO_NOT_USE_PREFIX tests
553 { my $tar = $Class->new;
556 ### first write a tar file without prefix
557 { my ($obj) = $tar->add_files( $COMPRESS_FILE );
558 my $dir = ''; # dir is empty!
559 my $file = File::Basename::basename( $COMPRESS_FILE );
561 ok( $obj, "File added" );
562 isa_ok( $obj, $FClass );
564 ### internal storage ###
565 is( $obj->name, $file, " Name set to '$file'" );
566 is( $obj->prefix, $dir, " Prefix set to '$dir'" );
568 ### write the tar file without a prefix in it
570 local $Archive::Tar::DO_NOT_USE_PREFIX = 1;
571 local $Archive::Tar::DO_NOT_USE_PREFIX = 1;
573 ok( $tar->write( $OUT_TAR_FILE ),
574 " Tar file written" );
576 ### and forget all about it...
580 ### now read it back in, there should be no prefix
581 { ok( $tar->read( $OUT_TAR_FILE ),
582 " Tar file read in again" );
584 my ($obj) = $tar->get_files;
585 ok( $obj, " File retrieved" );
586 isa_ok( $obj, $FClass, " Object" );
588 is( $obj->name, $COMPRESS_FILE,
589 " Name now set to '$COMPRESS_FILE'" );
590 is( $obj->prefix, '', " Prefix now empty" );
592 my $re = quotemeta $COMPRESS_FILE;
593 like( $obj->raw, qr/^$re/, " Prefix + name in name slot of header" );
596 rm( $OUT_TAR_FILE ) unless $NO_UNLINK;
601 for my $struct ( \@EXPECT_NORMAL, \@EXPECTBIN, \@EXPECTX ) {
602 for my $aref (@$struct) {
604 my $dir = $aref->[0]->[0];
605 rmtree $dir if $dir && -d $dir && not $NO_UNLINK;
609 my ($dir) = File::Spec::Unix->splitdir( $LONG_FILE );
610 rmtree $dir if $dir && -d $dir && not $NO_UNLINK;
611 1 while unlink $COMPRESS_FILE;
614 ###########################
616 ###########################
621 File::Spec::Unix->catfile(
622 grep { defined } @{$_->[0]}, $_->[1]
629 return $file =~ m|/$| ? 1 : 0;
643 my $filesize = -s $file;
644 my $contents = slurp_binfile( $file );
646 ok( defined( $contents ), " File read" );
647 ok( $filesize, " File written size=$filesize" );
649 cmp_ok( $filesize % BLOCK, '==', 0,
650 " File size is a multiple of 512" );
652 cmp_ok( length($contents), '==', $filesize,
653 " File contents match size" );
655 is( TAR_END x 2, substr( $contents, -(BLOCK*2) ),
656 " Ends with 1024 null bytes" );
661 sub check_compressed_file {
663 my $filesize = -s $file;
664 my $contents = slurp_compressed_file( $file );
665 my $uncompressedsize = length $contents;
667 ok( defined( $contents ), " File read and uncompressed" );
668 ok( $filesize, " File written size=$filesize uncompressed size=$uncompressedsize" );
670 cmp_ok( $uncompressedsize % BLOCK, '==', 0,
671 " Uncompressed size is a multiple of 512" );
673 is( TAR_END x 2, substr($contents, -(BLOCK*2)),
674 " Ends with 1024 null bytes" );
676 cmp_ok( $filesize, '<', $uncompressedsize,
677 " Compressed size < uncompressed size" );
682 sub check_tar_object {
684 my $struct = shift or return;
686 ### amount of files (not dirs!) there should be in the object
687 my $expect = scalar @$struct;
688 my @files = grep { $_->is_file } $obj->get_files;
690 ### count how many files there are in the object
691 ok( scalar @files, " Found some files in the archive" );
692 is( scalar @files, $expect, " Found expected number of files" );
694 for my $file (@files) {
697 #my $path = File::Spec::Unix->catfile(
698 # grep { length } $file->prefix, $file->name );
699 my($ename,$econtent) =
700 get_expect_name_and_contents( $file->full_path, $struct );
702 ok( $file->is_file, " It is a file" );
703 is( $file->full_path, $ename,
704 " Name matches expected name" );
705 like( $file->get_content, $econtent,
706 " Content as expected" );
710 sub check_tar_extract {
715 for my $file ($tar->get_files) {
716 push @dirs, $file && next if $file->is_dir;
719 my $path = $file->full_path;
720 my($ename,$econtent) =
721 get_expect_name_and_contents( $path, $struct );
724 is( $ename, $path, " Expected file found" );
725 ok( -e $path, " File '$path' exists" );
728 open $fh, "$path" or warn "Error opening file '$path': $!\n";
731 ok( $fh, " Opening file" );
733 my $content = do{local $/;<$fh>}; chomp $content;
734 like( $content, qr/$econtent/,
738 $NO_UNLINK or 1 while unlink $path;
740 ### alternate extract path tests
741 ### to abs and rel paths
742 { for my $outpath ( File::Spec->catdir( @ROOT ),
744 File::Spec->catdir( @ROOT )
748 my $outfile = File::Spec->catfile( $outpath, $$ );
750 ok( $tar->extract_file( $file->full_path, $outfile ),
751 " Extracted file '$path' to $outfile" );
752 ok( -e $outfile," Extracted file '$outfile' exists" );
754 rm( $outfile ) unless $NO_UNLINK;
759 ### now check if list_files is returning the same info as get_files
760 is_deeply( [$tar->list_files], [ map { $_->full_path } $tar->get_files],
761 " Verified via list_files as well" );
763 #do { rmtree $_->full_path if -d $_->full_path && not $NO_UNLINK }
769 my $fh = IO::File->new;
771 $fh->open( $file ) or warn( "Error opening '$file': $!" ), return undef;
778 sub slurp_compressed_file {
783 if( $file =~ /.tbz$/ ) {
784 require IO::Uncompress::Bunzip2;
785 $fh = IO::Uncompress::Bunzip2->new( $file )
786 or warn( "Error opening '$file' with IO::Uncompress::Bunzip2" ), return
792 $fh->open( $file, READ_ONLY->(1) )
793 or warn( "Error opening '$file' with IO::Zlib" ), return
798 $str .= $buff while $fh->read( $buff, 4096 ) > 0;
804 sub get_expect_name_and_contents {
806 my $struct = shift or return;
808 ### find the proper name + contents for this file from
809 ### the expect structure
810 my ($name, $content) =
817 File::Spec::Unix->catfile(
818 grep { length } @{$_->[0]}, $_->[1]
826 unless( ref $content ) {
827 my $x = quotemeta ($content || '');
832 warn "Could not find '$find' in " . Dumper $struct;
835 return ($name, $content);