#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

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

require TestDriver;

cleanup();

my $td = new TestDriver('signature-dictionary');

my $n_tests = 0;

$n_tests += 6;
foreach my $i (qw(preserve disable generate))
{
    $td->runtest("sig dict contents hex (object-streams=$i)",
                 {$td->COMMAND =>
                      "qpdf --object-streams=$i digitally-signed.pdf a.pdf"},
                 {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
    # Use grep -f rather than writing something in test_driver because
    # the point of the test is to ensure that the contents appears in
    # the output file in the correct format.
    $td->runtest("find desired contents (object-streams=$i)",
                 {$td->COMMAND =>
                      "grep -f digitally-signed-sig-dict-contents.out a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
}

$n_tests += 4;
foreach my $i (qw(preserve disable))
{
    $td->runtest("non sig dict contents text string (object-streams=$i)",
                 {$td->COMMAND =>
                      "qpdf --object-streams=$i comment-annotation.pdf a.pdf"},
                 {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
    $td->runtest("find desired contents as non hex (object-streams=$i)",
                 {$td->COMMAND =>
                      "grep \"/Contents (Salad)\" a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
}

$n_tests += 2;
    $td->runtest("non sig dict contents text string (object-streams=generate)",
                 {$td->COMMAND =>
                      "qpdf --object-streams=generate comment-annotation.pdf a.pdf"},
                 {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
    $td->runtest("plain text not found due to compression (object-streams=generate)",
                 {$td->COMMAND =>
                      "grep \"/Contents (Salad)\" a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 1});

$n_tests += 12;
foreach my $i (qw(40 128 256))
{
    my $x = "";
    if ($i < 256)
    {
        $x = "--allow-weak-crypto";
    }
    $td->runtest("encrypt $i",
                 {$td->COMMAND =>
                      "qpdf $x --encrypt '' o $i --" .
                      " digitally-signed.pdf a.pdf"},
                 {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
    $td->runtest("find desired contents (encrypt $i)",
                 {$td->COMMAND =>
                      "grep -f digitally-signed-sig-dict-contents.out a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
    $td->runtest("decrypt",
                 {$td->COMMAND =>
                      "qpdf --decrypt a.pdf b.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
    $td->runtest("find desired contents (decrypt $i)",
                 {$td->COMMAND =>
                      "grep -f digitally-signed-sig-dict-contents.out b.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
}

$n_tests += 15;
foreach my $i (qw(40 128 256))
{
    my $x = "";
    if ($i < 256)
    {
        $x = "--allow-weak-crypto";
    }
    $td->runtest("non sig dict encrypt $i",
                 {$td->COMMAND =>
                      "qpdf $x --encrypt '' o $i --" .
                      " comment-annotation.pdf a.pdf"},
                 {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
    $td->runtest("plain text not found due to encryption (non sig dict encrypt $i)",
                 {$td->COMMAND =>
                      "grep \"/Contents (Salad)\" a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 1});
    $td->runtest("find encrypted contents (non sig dict encrypt $i)",
                 {$td->COMMAND =>
                      "grep \"/Contents <.*>\" a.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
    $td->runtest("non sig dict decrypt",
                 {$td->COMMAND =>
                      "qpdf --decrypt a.pdf b.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
    $td->runtest("find desired contents (non sig dict decrypt $i)",
                 {$td->COMMAND =>
                      "grep \"/Contents (Salad)\" b.pdf"},
                 {$td->REGEXP => ".*",
                      $td->EXIT_STATUS => 0});
}

cleanup();
$td->report($n_tests);
