#!/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('encryption-parameters');

my $n_tests = 13;

# Encrypt files whose /ID strings are other than 32 bytes long (bug
# 2991412). Also linearize these files, which was reported in a
# separate bug by email.
foreach my $file (qw(short-id long-id))
{
    $td->runtest("encrypt $file.pdf",
                 {$td->COMMAND =>
                      "qpdf --allow-weak-crypto".
                      " --encrypt '' pass 40 -- $file.pdf a.pdf"},
                 {$td->STRING => "",
                  $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);

    $td->runtest("check $file.pdf",
                 {$td->COMMAND => "qpdf --check --show-encryption-key a.pdf"},
                 {$td->FILE => "$file-check.out",
                  $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);

    $td->runtest("linearize $file.pdf",
                 {$td->COMMAND =>
                      "qpdf --deterministic-id --linearize $file.pdf a.pdf"},
                 {$td->STRING => "",
                  $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);

    $td->runtest("check output",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "$file-linearized.pdf"});

    $td->runtest("check $file.pdf",
                 {$td->COMMAND => "qpdf --check a.pdf"},
                 {$td->FILE => "$file-linearized-check.out",
                  $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}

# A user provided a file that was missing /ID in its trailer even
# though it is encrypted and also has a space instead of a newline
# after its xref keyword. This file has those same properties.
$td->runtest("check broken file",
             {$td->COMMAND => "qpdf --check invalid-id-xref.pdf"},
             {$td->FILE => "invalid-id-xref.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

# A file was emailed privately with issue 96. short-O-U.pdf was
# created by copying encryption parameters from that file. It exhibits
# the same behavior as the original file.
$td->runtest("short /O or /U",
             {$td->COMMAND =>
                  "qpdf --password=19723102477 --check short-O-U.pdf"},
             {$td->FILE => "short-O-U.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# A file was sent to me privately as part of issue 212. This file was
# encrypted and had /R=3 and /V=1 and was using a 40-bit key. qpdf was
# failing to work properly on files with /R=3 and 40-bit keys. The
# test file is not this private file, but the encryption parameters
# were copied from it. Like the bug file, qpdf < 8.1 can't decrypt it.
$td->runtest("/R 3 with 40-bit key",
             {$td->COMMAND =>
                  "qpdf --password=623 --check --show-encryption-key" .
                  " encrypted-40-bit-R3.pdf"},
             {$td->FILE => "encrypted-40-bit-R3.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

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