aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/qpdf-json.test
blob: 22b714c596bba4d6ca361e5b798f724c7bfa520f (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/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('qpdf-json');

my $n_tests = 0;

my @badfiles = (
    'no-qpdf-object',
    'no-pdf-version',
    'top-level-scalar',
    'bad-pdf-version1',
    'bad-pdf-version2',
    'top-level-array',
    'objects-not-dict',
    'bad-object-key',
    'object-not-dict',
    'stream-not-dict',
    'stream-dict-not-dict',
    'trailer-not-dict',
    'trailer-stream',
    'missing-trailer',
    'missing-objects',
    'obj-key-errors',
    'bad-data',
    'bad-datafile',
    );

$n_tests += scalar(@badfiles);

foreach my $f (@badfiles)
{
    $td->runtest("bad: $f",
                 {$td->COMMAND =>
                      "qpdf --json-input qjson-$f.json a.pdf"},
                 {$td->FILE => "qjson-$f.out", $td->EXIT_STATUS => 2},
                 $td->NORMALIZE_NEWLINES);
}

my @goodfiles = (
    'good1.pdf',
    'good9.pdf',
    'good13.pdf',
    'good15.pdf',
    'inline-images.pdf',
    ['20-pages.pdf', '--password=user'],
    'outlines-with-actions.pdf',
    'form-fields-and-annotations.pdf',
    'need-appearances.pdf',
    'fxo-blue.pdf',
    );
$n_tests += 6 * scalar(@goodfiles);

foreach my $i (@goodfiles)
{
    my $f = $i;
    my $xargs = "";
    if (ref($i) eq 'ARRAY') {
        ($f, $xargs) = @$i;
    }
    # explicit "latest" as --json-output version
    $td->runtest("good: $f -> JSON",
                 {$td->COMMAND => "qpdf $xargs --decode-level=none" .
                      " --json-output=latest $f a.json"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    # default --json-output version
    $td->runtest("good: $f JSON -> JSON",
                 {$td->COMMAND =>
                      "qpdf --decode-level=none" .
                      " --json-input --json-output a.json b.json"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("good: $f JSON -> QDF",
                 {$td->COMMAND =>
                      "qpdf --qdf --json-input --stream-data=preserve" .
                      " --static-id a.json a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("good: $f compare JSON",
                 {$td->FILE => "a.json"},
                 {$td->FILE => "b.json"});
    my $exp = "json-changed-$f";
    if (! -f $exp)
    {
        $td->runtest("good: $f -> aqdf",
                     {$td->COMMAND =>
                          "qpdf $xargs --object-streams=disable --qdf" .
                          " --stream-data=preserve --static-id $f b.pdf"},
                     {$td->STRING => "", $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
        $exp = "b.pdf";
    }
    else
    {
        # Sometimes passing through json may make semantically
        # equivalent changes such as
        #
        # * adding leading 0 to a floating point (.1 -> 0.1)
        # * changing the Unicode representation of a string
        # * changing the representation of a name (/n#65st -> /nest)
        $td->runtest("good: json changes $f",
                     {$td->STRING => ""},
                     {$td->STRING => ""});
    }
    $td->runtest("good: $f compare qdf",
                 {$td->FILE => "a.pdf"}, # from json
                 {$td->FILE => $exp});   # from original PDF
}

$n_tests += 6;
$td->runtest("manual JSON to PDF",
             {$td->COMMAND => "qpdf --json-input --static-id --qdf" .
                  " manual-qpdf-json.json a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check manual JSON to PDF",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "manual-qpdf-json.pdf"});
$td->runtest("check manual JSON to PDF to JSON",
             {$td->COMMAND => "qpdf --json-output=2 a.pdf -"},
             {$td->FILE => "manual-qpdf-json-pdf.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("manual JSON to JSON",
             {$td->COMMAND => "qpdf --json-input --json-output=2" .
                  " manual-qpdf-json.json a.json"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check manual JSON to JSON",
             {$td->FILE => "a.json"},
             {$td->FILE => "manual-qpdf-json-out.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check manual JSON to JSON to JSON",
             {$td->COMMAND => "qpdf --json-output=2 --json-input a.json -"},
             {$td->FILE => "a.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$n_tests += 6;
$td->runtest("json-output with file",
             {$td->COMMAND => "qpdf --json-output=2" .
                  " --json-stream-prefix=auto-1 --json-stream-data=file" .
                  " minimal.pdf a.json"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check file mode",
             {$td->FILE => "a.json"},
             {$td->FILE => "minimal-json-file.out"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("JSON to JSON with file",
             {$td->COMMAND => "qpdf --json-input --json-output=2" .
                  " --json-stream-prefix=auto-2 --json-stream-data=file" .
                  " a.json -"},
             {$td->FILE => "minimal-json-file-2.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("JSON with file to qdf",
             {$td->COMMAND => "qpdf --json-input --qdf --static-id" .
                  " a.json a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("PDF to qdf",
             {$td->COMMAND => "qpdf --qdf --static-id minimal.pdf b.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check PDF",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "b.pdf"});

# Replace mode tests

$n_tests += 1;
$td->runtest("create PDF for replace",
             {$td->COMMAND => "qpdf good13.pdf a.pdf" .
                  " --update-from-json=qpdf-json-update-errors.json"},
             {$td->FILE => "update-from-json-errors.out",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

my @update_files = (
    "update-stream-dict-only",
    "update-stream-data",
    "replace-with-stream",
    "various-updates",
    );
$n_tests += 2 * scalar(@update_files);

foreach my $f (@update_files) {
    $td->runtest("update: $f",
                 {$td->COMMAND =>
                      "qpdf good13.pdf a.pdf --qdf --static-id" .
                      " --update-from-json=$f.json"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("$f: check updated",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "$f-updated.pdf"});
}


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