aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/signature-dictionary.test
blob: cda0a7b9b740b0a86cb6386e0685f3f52d37c4ca (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/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 --owner-password=o --bits=$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 --owner-password=o --bits=$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});
}

$n_tests += 2;
$td->runtest("remove security restrictions",
             {$td->COMMAND =>
                  "qpdf --qdf --no-original-object-ids --static-id" .
                  " --remove-restrictions minimal-signed-restricted.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("checkout output (remove restrictions)",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "minimal-signed-restrictions-removed.pdf"});

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