#!/usr/bin/env perl
require 5.008;
BEGIN { $^W = 1; }
use strict;
use File::stat;

chdir("aes") or die "chdir testdir failed: $!\n";

require TestDriver;

my $td = new TestDriver('AES');

my @providers = ();
if (exists $ENV{'QPDF_CRYPTO_PROVIDER'})
{
    push(@providers, $ENV{'QPDF_CRYPTO_PROVIDER'});
}
else
{
    open(Q, "qpdf --show-crypto|") or die;
    while (<Q>)
    {
        s/\s+$//s;
        push(@providers, $_);
    }
    close(Q);
}
foreach my $p (@providers)
{
    $ENV{'QPDF_CRYPTO_PROVIDER'} = $p;

    cleanup();

    my $key = '000102030405060708090a0b0c0d0e0f';
    $td->runtest("encrypt test vector ($p)",
                 {$td->COMMAND =>
                      "aes -cbc -encrypt $key test-vector.clear tmp1"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("check output",
                 {$td->FILE => "tmp1"},
                 {$td->FILE => "test-vector.cipher"});
    $td->runtest("decrypt test vector ($p)",
                 {$td->COMMAND => "aes -cbc -decrypt $key tmp1 tmp2"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("check output",
                 {$td->FILE => "tmp2"},
                 {$td->FILE => "test-vector.clear"});

    $key = '243f6a8885243f6a8885243f6a888524';
    foreach my $d (['data1', 17072], ['data2', 16032])
    {
        my ($file, $size) = @$d;
        $td->runtest("encrypt $file ($p)",
                     {$td->COMMAND => "aes +cbc -encrypt $key $file tmp1"},
                     {$td->STRING => "", $td->EXIT_STATUS => 0});
        # sleep one second so random number will get a different seed
        sleep(1);
        $td->runtest("encrypt $file again ($p)",
                     {$td->COMMAND => "aes +cbc -encrypt $key $file tmp2"},
                     {$td->STRING => "", $td->EXIT_STATUS => 0});
        foreach my $f (qw(tmp1 tmp2))
        {
            $td->runtest("check size",
                         {$td->STRING => sprintf("%d\n", stat($f)->size)},
                         {$td->STRING => "$size\n"});
        }
        $td->runtest("verify files are different",
                     {$td->COMMAND => "cmp tmp1 tmp2"},
                     {$td->REGEXP => '.*', $td->EXIT_STATUS => '!0'});
        $td->runtest("decrypt $file ($p)",
                     {$td->COMMAND => "aes +cbc -decrypt $key tmp1 tmp3"},
                     {$td->STRING => "", $td->EXIT_STATUS => 0});
        $td->runtest("decrypt $file again ($p)",
                     {$td->COMMAND => "aes +cbc -decrypt $key tmp2 tmp4"},
                     {$td->STRING => "", $td->EXIT_STATUS => 0});
        $td->runtest("check output",
                     {$td->FILE => "tmp3"},
                     {$td->FILE => $file});
        $td->runtest("check output",
                     {$td->FILE => "tmp4"},
                     {$td->FILE => $file});
    }

    cleanup();
}

$td->report(22 * scalar(@providers));

sub cleanup
{
    system("rm -f tmp?");
}
