summaryrefslogtreecommitdiffstats
path: root/libtests/qtest/aes.test
blob: a468b0b413f1b9476c9d88f101f247a468c22e99 (plain) (blame)
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
#!/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?");
}