Archive-Zip-SimpleZip

 view release on metacpan or  search on metacpan

t/001main.t  view on Meta::CPAN

    my $mon  = ( ( $dt >> 21 ) & 0x0f ) - 1;
    my $mday = ( ( $dt >> 16 ) & 0x1f );

    my $hour = ( ( $dt >> 11 ) & 0x1f );
    my $min  = ( ( $dt >> 5 ) & 0x3f );
    my $sec  = ( ( $dt << 1 ) & 0x3e );


    use POSIX 'mktime';

    my $time_t = mktime( $sec, $min, $hour, $mday, $mon, $year, 0, 0, -1 );
    return 0 if ! defined $time_t;
    return $time_t;
}

sub roundTripUnixTime
{
    my $t = shift;
    return unixToDosTime(dosToUnixTime($t));
}

{
    title "errors";

    {
        title "no zip filname";
        my $z = Archive::Zip::SimpleZip->new() ;

        is $z, undef ;
        is $SimpleZipError, "Missing Filename",
            "  missing filename";
    }

    {
        title "directory";
        my $lex = LexDir->new(my $dir);
        my $z = Archive::Zip::SimpleZip->new($dir) ;

        is $z, undef ;
        is $SimpleZipError, "Illegal Filename",
            "  missing filename";
    }

    {
        title "zip file in directory that doesn't exist";
        my $lex = LexDir->new(my $dir);
        my $zipfile = File::Spec->catfile($dir, "not", "exist", "x.zip");

        my $z = Archive::Zip::SimpleZip->new($zipfile) ;

        is $z, undef ;
        is $SimpleZipError, "Illegal Filename",
            "  missing filename";
    }

    SKIP:
    {
        title "file not writable";
        my $lex = LexFile->new(my $zipfile);

        chmod 0444, $zipfile
            or skip "Cannot create non-writable file", 3 ;

        skip "Cannot create non-writable file", 3
            if -w $zipfile ;

        ok ! -w $zipfile, "  zip file not writable";

        my $z = Archive::Zip::SimpleZip->new($zipfile) ;

        is $z, undef ;
        is $SimpleZipError, "Illegal Filename",
            "  Illegal Filename";

        chmod 0777, $zipfile ;
    }


    {
        title "filename undef";
        my $z = Archive::Zip::SimpleZip->new(undef);

        is $z, undef ;
        is $SimpleZipError, "Illegal Filename",
            "  missing filename";
    }

    {
        title "Bad parameter in new";
        my $lex = LexFile->new(my $zipfile);
        eval { my $z = Archive::Zip::SimpleZip->new($zipfile, fred => 1); };

        like $@,  qr/Parameter Error: unknown key value\(s\) fred/,
            "  value  is bad";

        like $SimpleZipError, qr/Parameter Error: unknown key value\(s\) fred/,
            "  missing filename";
    }

    {
        title "Bad parameter in add";
        my $lex = LexFile->new(my $zipfile);
        my $lex1 = LexFile->new(my $file1);
        writeFile($file1, "abc");

        my $z = Archive::Zip::SimpleZip->new($zipfile);
        isa_ok $z, "Archive::Zip::SimpleZip";
        eval { $z->add($file1, Fred => 1) ; };

        like $@,  qr/Parameter Error: unknown key value\(s\) Fred/,
            "  value  is bad";

        like $SimpleZipError, qr/Parameter Error: unknown key value\(s\) Fred/,
            "  missing filename";
    }


    {
        title "Name option invalid in constructor";

        my $zipfile ;

        eval { my $z = Archive::Zip::SimpleZip->new(\$zipfile, Name => "fred"); } ;

        like $@,  qr/name option not valid in constructor/,
            "  option invalid";

        like $SimpleZipError, qr/name option not valid in constructor/,
            "  option invalid";
    }

    {
        title "Comment option invalid in constructor";

        my $zipfile ;

t/001main.t  view on Meta::CPAN

        my $z = Archive::Zip::SimpleZip->new(\$zipfile);
        isa_ok $z, "Archive::Zip::SimpleZip";
        eval { $z->addString("abc") ; };

        like $@,  qr/Missing 'Name' parameter in addString/,
            "  value  is bad";

        like $SimpleZipError, qr/Missing 'Name' parameter in addString/,
            "  missing filename";
    }


    {
        title "Missing Name paramter in addFileHandle";

        my $zipfile;

        my $z = Archive::Zip::SimpleZip->new(\$zipfile);
        isa_ok $z, "Archive::Zip::SimpleZip";
        eval { $z->addFileHandle("abc") ; };

        like $@,  qr/Missing 'Name' parameter in addFileHandle/,
            "  value  is bad";

        like $SimpleZipError, qr/Missing 'Name' parameter in addFileHandle/,
            "  missing filename";
    }

}

{
    title "file doesn't exist";

    my $lex1 = LexFile->new(my $zipfile);
    my $file1 = "notexist";

    my $z = Archive::Zip::SimpleZip->new($zipfile) ;
    isa_ok $z, "Archive::Zip::SimpleZip";

    is $z->add($file1), 0, "add not ok";
    is $SimpleZipError, "File '$file1' does not exist";


    ok ! -e $file1, "no zip file created";
}

# {
#     # exports
# }

use Fcntl ':mode';

SKIP:
{
    title "file cannot be read";

    my $lex1 = LexFile->new(my $zipfile);
    my $lex = LexFile->new(my $file1);

    writeFile($file1, "abc");
    chmod 0222, $file1 ;

    skip "Cannot create non-readable file", 6
        if -r $file1 ;

    ok ! -r $file1, "  input file not readable";

    my $z = Archive::Zip::SimpleZip->new($zipfile) ;
    isa_ok $z, "Archive::Zip::SimpleZip";

    is $z->add($file1), 0, "add not ok";
    is $SimpleZipError, "File '$file1' cannot be read";
    ok $z->close, "closed ok";

    ok -z $zipfile, "zip file created, but empty";

    chmod 0777, $file1 ;

}


SKIP:
{
    title "one file cannot be read";

    my $lex1 = LexFile->new(my $zipfile);
    my $lex2 = LexFile->new(my $file1);
    my $lex3 = LexFile->new(my $file2);
    my $lex4 = LexFile->new(my $file3);

    writeFile($file1, $file1);
    writeFile($file2, $file2);
    writeFile($file3, $file3);
    chmod 0222, $file2 ;

    skip "Cannot create non-readable file", 13
        if -r $file2 ;

    ok ! -r $file2, "  input file not readable";

    my $z = Archive::Zip::SimpleZip->new($zipfile) ;
    isa_ok $z, "Archive::Zip::SimpleZip";

    ok $z->add($file1), "add $file1";

    is $z->add($file2), 0, "add not ok";
    is $SimpleZipError, "File '$file2' cannot be read";

    ok ! $z->add($file3), "not add $file3";
    is $SimpleZipError, "File '$file2' cannot be read";

    ok $z->close, "closed ok";

    ok -e $zipfile, "zip file created";

    my @got = getContent($zipfile);
    is @got, 1, "two entries in zip";
    is $got[0]{Name}, canonFile($file1);
    is $got[0]{Payload}, $file1;


    chmod 0777, $file2 ;
}

{
    title "simple" ;

    my $lex1 = LexFile->new(my $zipfile);
    my $lex = LexFile->new(my $file1);

    writeFile($file1, "hello world");

    my $z = Archive::Zip::SimpleZip->new($zipfile) ;
    isa_ok $z, "Archive::Zip::SimpleZip";

    ok $z->add($file1), "add ok";
    ok $z->close, "closed ok";

    my @got = getContent($zipfile);
    is @got, 1, "one entry in zip";
    is $got[0]{Name}, canonFile($file1);
    is $got[0]{Payload}, "hello world";
}


{
    title "simple - no close" ;

    my $lex1 = LexFile->new(my $zipfile);
    my $lex = LexFile->new(my $file1);

    writeFile($file1, "hello world");

    {
        my $z = Archive::Zip::SimpleZip->new($zipfile) ;
        isa_ok $z, "Archive::Zip::SimpleZip";

        ok $z->add($file1), "add ok";
    }


    my @got = getContent($zipfile);
    is @got, 1, "one entry in zip";
    is $got[0]{Name}, canonFile($file1);
    is $got[0]{Payload}, "hello world";
}


{
    title "simple - no add" ;

    my $lex1 = LexFile->new(my $zipfile);
    my $lex = LexFile->new(my $file1);

    writeFile($file1, "hello world");

    {
        my $z = Archive::Zip::SimpleZip->new($zipfile) ;
        isa_ok $z, "Archive::Zip::SimpleZip";

        #ok $z->add($file1), "add ok";
    }



( run in 1.564 second using v1.01-cache-2.11-cpan-39bf76dae61 )