aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/encryption-parameters.test
blob: f6d852c04c8ca5dd0fde8a7608e05bd60f28c734 (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
#!/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);