aboutsummaryrefslogtreecommitdiffstats
path: root/t/01packdrakeng.t
blob: 2321ad7f3bd1ed8ca9561cf8a6aa3c49bcb2f405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl

# $Id$

use strict;
use Test::More tests => 32;
use Digest::MD5;

use_ok('Packdrakeng');

-d "test" || mkdir "test" or die "Can't create directory test";

my $coin = "
 ___________
< Coin coin >
 -----------
 \     ,~~.
  \ __( o  )
    `--'==( ___/)
       ( (   . /
        \ '-' /
    ~'`~'`~'`~'`~
";

sub clean_test_files {
    -d "test" or return;
    system("rm -fr $_") foreach (glob("test/*"));
}

sub create_test_files {
    my ($number) = @_;
    my %created;
        foreach my $n (1 .. $number||10) {
        my $size = int(rand(1024));
        # push(@created, "test/$size");
        system("dd if=/dev/urandom of=test/$size bs=1024 count=$size >/dev/null 2>&1");
        open(my $h, "test/$size");
        $created{"test/$size"} = Digest::MD5->new->addfile($h)->hexdigest;
        close $h;
    }
    %created 
}

sub check_files {
    my %files = @_;
    my $ok = 1;
    foreach my $f (keys %files) {
        open(my $h, $f);
        Digest::MD5->new->addfile($h)->hexdigest ne $files{$f} and do {
            print STDERR "$f differ\n";
            $ok = 0;
        };
        close $h;
    }
    $ok
}

###################################
#                                 #
# Test series, packing, unpacking #
#                                 #
###################################

sub test_packing {
    my ($pack_param, $listfiles) = @_;

    ok(my $pack = Packdrakeng->new(%$pack_param), "Creating an archive");
    $pack or return;
    ok($pack->add(undef, keys %$listfiles), "packing files");
    $pack = undef; # closing the archive.
    
    clean_test_files();
    
    ok($pack = Packdrakeng->open(%$pack_param), "Re-opening the archive");
    $pack or die;
    ok($pack->extract(undef, keys(%$listfiles)), "extracting files");
    ok(check_files(%$listfiles), "Checking md5sum for extracted files");

    $pack = undef;
}


# Single test:
{
clean_test_files();

ok(my $pack = Packdrakeng->new(archive => "packtest.cz"), "Create a new archive");
open(my $fh, "+> test/test") or die "Can't open test file $!";
syswrite($fh, $coin);
sysseek($fh, 0, 0);
ok($pack->add_virtual('f', "coin", $fh), "Adding data from file");
close($fh);
unlink("test/test");

ok($pack->add_virtual('d', "dir"), "Adding a dir");
ok($pack->add_virtual('l', "symlink", "dest"), "Adding a symlink");
$pack = undef;

ok($pack = Packdrakeng->open(archive => "packtest.cz"), "Opening the archive");
ok($pack->extract("test", "dir"), "Extracting dir");
ok(-d "test/dir", "dir succefully restore");
ok($pack->extract("test", "symlink"), "Extracting symlink");
ok(readlink("test/symlink") eq "dest", "symlink succefully restore");

open($fh, "+> test/test") or die "Can't open file $!";
ok($pack->extract_virtual($fh, "coin"), "Extracting data");
sysseek($fh, 0, 0);
sysread($fh, my $data, 1000);
close($fh);
ok($data eq $coin, "Data are correct");

} 

print "Test: using external cat function:\n";
    clean_test_files();
    test_packing({ archive => "packtest-cat.cz", compress => 'cat', uncompress => 'cat', noargs => 1 }, { create_test_files(30) });
    clean_test_files();

print "Test: using internal gzip function:\n";
    clean_test_files();
    test_packing({ archive => "packtest-gzipi.cz" }, { create_test_files(30) });
    clean_test_files();

print "Test: using external gzip function:\n";
    clean_test_files();
    test_packing({ archive => "packtest-gzip.cz", compress => "gzip", extern => 1}, { create_test_files(30) });
    clean_test_files();
   
print "Test: using external bzip function:\n";
    clean_test_files();
    test_packing({ archive => "packtest-bzip2.cz", compress => "bzip2", extern => 1}, { create_test_files(30) });
    clean_test_files();