AWS-Signature-V4
view release on metacpan or search on metacpan
t/trailer.t view on Meta::CPAN
sub crc32c_ref ($data) {
my $crc = 0xFFFFFFFF;
for my $byte (unpack 'C*', $data) {
$crc ^= $byte;
$crc = ($crc & 1) ? (($crc >> 1) ^ 0x82F63B78) : ($crc >> 1) for 1 .. 8;
}
return encode_base64(pack('N', $crc ^ 0xFFFFFFFF), '');
}
my $long = join '', map { chr(($_ * 7) % 256) } 1 .. 5000;
my $ck = start($x, streaming => 'unsigned', checksum => 'crc32c',
decoded_content_length => length $long)->{chunker};
$ck->chunk(substr($long, $_ * 1000, 1000)) for 0 .. 4;
like $ck->finish, qr/x-amz-checksum-crc32c:\Q${\ crc32c_ref($long)}\E\r\n/, 'crc32c on longer data';
# the test here will exercise whatever is available; we don't explicitly
# test the pure-perl version shipped with the module in case
# String::CRC32 is available. FIXME figure out how to do this withouth
# going back to using package variables.
for my $size (1 .. 9, 65535 .. 65537, 70001) {
my $sum = AWS::Signature::V4::Checksum->new('crc32c');
my $data = substr $long x 15, 0, $size;
my ($head, $tail) = (substr($data, 0, $size / 3), substr($data, $size / 3));
$sum->add(\$head)->add(\$tail);
is $sum->base64, crc32c_ref($data), "crc32c, $size bytes";
}
# wide characters are refused by every algorithm, references to anything else
# than a defined string too; upgraded byte strings are fine
for my $algo (sort keys %expect) {
my $sum = AWS::Signature::V4::Checksum->new($algo);
is dies { $sum->add(\"\x{263a}") }, bad(qr/byte string/), "$algo: wide character refused";
is dies { $sum->add('123') }, bad(qr/reference/), "$algo: not a reference";
is dies { $sum->add(\undef) }, bad(qr/reference/), "$algo: undef";
# stored as UTF-8, with bytes that differ from the characters: what is
# summed must be the bytes, and the caller's string must not be touched
my $raw = "\xe9\xff\x41" . '123456789';
my $upgraded = $raw;
utf8::upgrade($upgraded);
is $sum->add(\$upgraded)->base64,
AWS::Signature::V4::Checksum->new($algo)->add(\$raw)->base64,
"$algo: upgraded byte string sums as bytes";
# the digest modules may clear the UTF-8 flag of the scalar they are
# given, but the string the caller sees must be the same one
is $upgraded, $raw, "$algo: and the input string still holds those bytes";
}
{
my $ck = start($x, streaming => 'unsigned', checksum => 'crc32c')->{chunker};
is dies { $ck->chunk("\x{263a}") }, bad(qr/chunk must be a byte string/), 'unsigned crc32c chunker: wide character refused';
is dies { $ck->chunk(['123456789']) }, bad(qr/reference to one/), 'chunk of an array reference';
is dies { $ck->chunk(bless \my $o, 'Foo') }, bad(qr/reference to one/), 'chunk of an object';
is $ck->chunk(\'123456789'), "9\r\n123456789\r\n", 'and a scalar reference is fine';
like $ck->finish, qr/x-amz-checksum-crc32c:\Q$expect{crc32c}\E\r\n/, 'the refused pieces were not counted';
}
# --- request side
my $r = start($x, streaming => 'unsigned', checksum => 'crc32c');
is $r->{headers}{'x-amz-content-sha256'}, 'STREAMING-UNSIGNED-PAYLOAD-TRAILER', 'unsigned marker';
is $r->{headers}{'x-amz-trailer'}, 'x-amz-checksum-crc32c', 'x-amz-trailer';
like $r->{signed_headers}, qr/x-amz-trailer/, 'and it is signed';
is $r->{headers}{'x-amz-decoded-content-length'}, 9, 'decoded length';
is $r->{headers}{'content-encoding'}, 'aws-chunked', 'content encoding';
ok !defined $r->{chunker}->_mac, 'unsigned chunker has no key';
$r = start($s3, streaming => 'signed', checksum => 'sha256');
ok !(grep { defined && !ref && ($_ eq $key || $_ eq unpack 'H*', $key) } values $r->{chunker}->%*),
'signed chunker: the key is not in the object';
is $r->{headers}{'x-amz-content-sha256'}, 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER', 'signed trailer marker';
$r = start($s3, streaming => 1);
is $r->{headers}{'x-amz-content-sha256'}, 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD', 'no trailer: plain marker';
ok !exists $r->{headers}{'x-amz-trailer'}, 'and no x-amz-trailer';
# --- signed chunks and trailer, against a straightforward reimplementation
{
my $r = start($s3, streaming => 'signed', checksum => 'crc32');
my $prev = $r->{signature};
my $sig1 = hmac_sha256_hex($sts->('PAYLOAD', $prev, sha256_hex(''), sha256_hex('123456789')), $key);
my $sig2 = hmac_sha256_hex($sts->('PAYLOAD', $sig1, sha256_hex(''), sha256_hex('')), $key);
my $line = "x-amz-checksum-crc32:$expect{crc32}";
my $tsig = hmac_sha256_hex($sts->('TRAILER', $sig2, sha256_hex("$line\n")), $key);
my $ck = $r->{chunker};
is $ck->chunk('123456789'), "9;chunk-signature=$sig1\r\n123456789\r\n", 'signed chunk';
is $ck->finish,
"0;chunk-signature=$sig2\r\n$line\r\nx-amz-trailer-signature:$tsig\r\n\r\n",
'final chunk, trailer and trailer signature';
}
# --- caller-supplied trailers
{
my $r = start($x, streaming => 'unsigned', trailers => ['X-Amz-Checksum-Crc64nvme']);
is $r->{headers}{'x-amz-trailer'}, 'x-amz-checksum-crc64nvme', 'declared, lowercased';
my $ck = $r->{chunker};
$ck->chunk('123456789');
is $ck->finish('x-amz-checksum-crc64nvme' => 'AAAAAAAAAAA='),
"0\r\nx-amz-checksum-crc64nvme:AAAAAAAAAAA=\r\n\r\n", 'value given at finish';
$r = start($s3, streaming => 1, checksum => 'sha1', trailers => ['x-foo', 'x-bar']);
is $r->{headers}{'x-amz-trailer'}, 'x-amz-checksum-sha1,x-foo,x-bar', 'built-in first, then declared';
$ck = $r->{chunker};
$ck->chunk('123456789');
my $sig1 = hmac_sha256_hex($sts->('PAYLOAD', $r->{signature}, sha256_hex(''), sha256_hex('123456789')), $key);
my $sig2 = hmac_sha256_hex($sts->('PAYLOAD', $sig1, sha256_hex(''), sha256_hex('')), $key);
my @lines = ("x-amz-checksum-sha1:$expect{sha1}", 'x-foo:1', 'x-bar:2');
my $tsig = hmac_sha256_hex($sts->('TRAILER', $sig2, sha256_hex(join '', map { "$_\n" } @lines)), $key);
is $ck->finish('x-foo' => '1', 'x-bar' => '2'),
"0;chunk-signature=$sig2\r\n" . join('', map { "$_\r\n" } @lines)
. "x-amz-trailer-signature:$tsig\r\n\r\n",
'all trailers, in order, signed';
my $mk = sub ($signer = $x) {
my $c = start($signer, streaming => $signer == $x ? 'unsigned' : 1, checksum => 'crc32',
trailers => ['X-Foo'])->{chunker};
$c->chunk('123456789');
$c;
};
is dies { $mk->()->finish }, bad(qr/missing value for trailer 'x-foo'/), 'missing value';
is dies { $mk->()->finish('x-foo' => 1, 'x-nope' => 2) }, bad(qr/undeclared trailers: x-nope/),
'undeclared value';
is dies { $mk->()->finish('x-foo' => 1, 'x-amz-checksum-crc32' => 'x') },
bad(qr/'x-amz-checksum-crc32' is computed/), 'computed value given';
( run in 0.550 second using v1.01-cache-2.11-cpan-39bc3f1cbff )