Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 183 lines (153 sloc) 3.887 kb
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#!/usr/bin/perl
# $Id$

# Test the usage of RPC::XML::base64 with filehandles

use strict;
use vars qw($dir $file $tmpfile $value $enc_value $obj $fh $pos $md5_able);

# This is what we're testing
use RPC::XML;

use Test;
use File::Spec;
use IO::File;
use MIME::Base64;

(undef, $dir, undef) = File::Spec->splitpath(File::Spec->rel2abs($0));
$file = File::Spec->catfile($dir, 'svsm_text.gif');
$tmpfile = File::Spec->catfile($dir, "__tmp__${$}__");

BEGIN
{
    eval "use Digest::MD5";
    $md5_able = $@ ? 0 : 1;

    plan tests => 23;
}

END
{
    unlink $tmpfile;
}

$value = 'Short string for easy tests';
$enc_value = encode_base64($value, '');

if (ref($fh = IO::File->new_tmpfile))
{
    $fh->autoflush(1);
    print $fh $value;
    $pos = $fh->tell;

    # We now have a ready-to-use FH, and we know the seek-pos on it
    $obj = RPC::XML::base64->new($fh);
    ok(ref $obj);
    ok($fh->tell() == $pos);
    ok($obj->value() eq $value);
    ok($fh->tell() == $pos);
    ok($obj->as_string() eq "<base64>$enc_value</base64>");
    ok($fh->tell() == $pos);

    # Done with this for now
    $fh->close;
}
else
{
    skip('Skipped: opening IO::File::new_tmpfile failed', 0) for (1 .. 6);
}

# Same tests, but init the FH with the encoded data rather than the cleartext
if (ref($fh = IO::File->new_tmpfile))
{
    $fh->autoflush(1);
    print $fh $enc_value;
    $pos = $fh->tell;

    # We now have a ready-to-use FH, and we know the seek-pos on it
    $obj = RPC::XML::base64->new($fh, 'encoded');
    ok(ref $obj);
    ok($fh->tell() == $pos);
    ok($obj->value() eq $value);
    ok($fh->tell() == $pos);
    ok($obj->as_string() eq "<base64>$enc_value</base64>");
    ok($fh->tell() == $pos);

    # Done with this for now
    $fh->close;
}
else
{
    skip('Skipped: opening IO::File::new_tmpfile failed', 0) for (1 .. 6);
}

# Test ordinary filehandles as well
local *F;
if (open(F, "> $tmpfile"))
{
    print F $value;
    close(F);
}
else
{
    die "Cannot create local file $tmpfile: $!";
}

if (open(F, "< $tmpfile"))
{
    autoflush F 1;
    seek(F, 0, 0);
    $pos = tell(F);

    $obj = RPC::XML::base64->new(\*F);
    ok(ref $obj);
    ok(tell(F) == $pos);
    ok($obj->value() eq $value);
    ok(tell(F) == $pos);
    ok($obj->as_string() eq "<base64>$enc_value</base64>");
    ok(tell(F) == $pos);

    close(F);
}
else
{
    skip("Skipped: Opening $tmpfile failed: $!", 0) for (1 .. 6);
}

# Test with a larger file
if ($fh = IO::File->new("< $file"))
{
    $obj = RPC::XML::base64->new($fh);
    $enc_value = ''; $value = '';

    while (read($fh, $value, 60*57))
    {
        $enc_value .= encode_base64($value, '');
    }
    ok($obj->as_string(), "<base64>$enc_value</base64>");
    ok(length($obj->as_string), $obj->length);
    $fh->seek(0, 0);

    if ($md5_able)
    {
        my $md5 = Digest::MD5->new;

        $md5->addfile($fh);
        $value = $md5->hexdigest;
        $md5->new; # Clear the digest
        $md5->add($obj->value);
        ok($value, $md5->hexdigest);
    }
    else
    {
        skip("Skipped: Digest::MD5 unavailable", 0);
    }

    $fh->close;
}
else
{
    skip("Skipped: Opening file $file failed: $!", 0) for (1 .. 2);
}

# Test the to_file method
if ($md5_able and $fh = IO::File->new("< $file"))
{
    $obj = RPC::XML::base64->new($fh);

    my $md5 = Digest::MD5->new;

    # Start by trying to write the new file
    $obj->to_file($tmpfile);
    ok(1); # Whee! It didn't die!

    $md5->addfile($fh);
    $value = $md5->hexdigest;
    $md5->new; # Clear the digest

    # Now get an MD5 on the new file
    my $ofh;
    die "Error opening $tmpfile for reading: $!"
        unless ($ofh = IO::File->new("< $tmpfile"));
    $md5->addfile($ofh);
    ok($value, $md5->hexdigest);
    $ofh->close;
    $fh->close;
}
else
{
    skip("Skipped: Digest::MD5 not availabe or open of $file failed", 0)
        for (1 .. 2);
}

exit;
Something went wrong with that request. Please try again.