aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/qpdf-json.test
blob: 9542bccfc29bfa4e3444f0012fa9a275aa98274f (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/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',
    'qpdf-not-array',
    'qpdf-array-too-long',
    'no-pdf-version',
    'top-level-scalar',
    'bad-pdf-version1',
    'bad-pdf-version2',
    'top-level-array',
    'objects-not-dict',
    'bad-object-key',
    'object-not-dict',
    'object-value-indirect',
    'stream-not-dict',
    'stream-dict-not-dict',
    'trailer-not-dict',
    'trailer-stream',
    'missing-trailer',
    'missing-objects',
    'obj-key-errors',
    'bad-data',
    'bad-datafile',
    'bad-data2',
    'bad-datafile2',
    );

$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',
    'weird-tokens.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" .
                      " --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 --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"});
}

# Exercise object description
$n_tests += 2;
$td->runtest("json-input object description",
             {$td->COMMAND => "test_driver 89 manual-qpdf-json.json"},
             {$td->FILE => "test-89.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("update-from-json object description",
             {$td->COMMAND => "test_driver 90 good13.pdf various-updates.json"},
             {$td->FILE => "test-90.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Exercise pushedinheritedpageresources and calledgetallpages
$n_tests += 12;
$td->runtest("call getAllPages",
             {$td->COMMAND =>
                  "qpdf --json-output duplicate-page-inherited.pdf" .
                  " --json-key=pages a.json"},
             {$td->FILE => "duplicate-page-inherited.out",
                  $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check json (1)",
             {$td->FILE => "a.json"},
             {$td->FILE => "duplicate-page-inherited-1.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("update (1)",
             {$td->COMMAND =>
                  "qpdf" .
                  " --update-from-json=duplicate-page-inherited-update.json" .
                  " --json-output duplicate-page-inherited.pdf" .
                  " a.json"},
             {$td->FILE => "duplicate-page-inherited.out",
                  $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check json (2)",
             {$td->FILE => "a.json"},
             {$td->FILE => "duplicate-page-inherited-1-fixed.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("create PDF (1)",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --json-input a.json a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check PDF (1)",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "duplicate-page-inherited-1-fixed.pdf"});

$td->runtest("call pushInheritedAttributesToPage",
             {$td->COMMAND =>
                  "qpdf --json-output duplicate-page-inherited.pdf" .
                  " --json-key=pages --pages . -- a.json"},
             {$td->FILE => "duplicate-page-inherited.out",
                  $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check json (2)",
             {$td->FILE => "a.json"},
             {$td->FILE => "duplicate-page-inherited-2.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("update (2)",
             {$td->COMMAND =>
                  "qpdf" .
                  " --update-from-json=duplicate-page-inherited-update2.json" .
                  " --json-output duplicate-page-inherited.pdf" .
                  " a.json"},
             {$td->FILE => "duplicate-page-inherited.out",
                  $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check json (3)",
             {$td->FILE => "a.json"},
             {$td->FILE => "duplicate-page-inherited-2-fixed.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("create PDF (2)",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --json-input a.json a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check PDF (2)",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "duplicate-page-inherited-2-fixed.pdf"});

$n_tests += 1;
$td->runtest("simple version of writeJSON",
             {$td->COMMAND => "test_driver 91 minimal.pdf"},
             {$td->FILE => "minimal-write-json.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$n_tests += 13;
$td->runtest("C API create from json file",
             {$td->COMMAND => "qpdf-ctest 42 minimal.json '' a.pdf"},
             {$td->STRING => "C test 42 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API create from file",
             {$td->COMMAND => "qpdf-test-compare a.pdf qpdf-ctest-42-43.pdf"},
             {$td->FILE => "qpdf-ctest-42-43.pdf", $td->EXIT_STATUS => 0});
$td->runtest("C API create from json buffer",
             {$td->COMMAND => "qpdf-ctest 43 minimal.json '' a.pdf"},
             {$td->STRING => "C test 43 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API create from buffer",
             {$td->COMMAND => "qpdf-test-compare a.pdf qpdf-ctest-42-43.pdf"},
             {$td->FILE => "qpdf-ctest-42-43.pdf", $td->EXIT_STATUS => 0});
$td->runtest("C API update from json file",
             {$td->COMMAND =>
                  "qpdf-ctest 44 minimal.pdf '' a.pdf minimal-update.json"},
             {$td->STRING => "C test 44 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API update from file",
             {$td->COMMAND => "qpdf-test-compare a.pdf qpdf-ctest-44-45.pdf"},
             {$td->FILE => "qpdf-ctest-44-45.pdf", $td->EXIT_STATUS => 0});
$td->runtest("C API update from json buffer",
             {$td->COMMAND =>
                  "qpdf-ctest 45 minimal.pdf '' a.pdf minimal-update.json"},
             {$td->STRING => "C test 45 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API update from buffer",
             {$td->COMMAND => "qpdf-test-compare a.pdf qpdf-ctest-44-45.pdf"},
             {$td->FILE => "qpdf-ctest-44-45.pdf", $td->EXIT_STATUS => 0});
$td->runtest("C API write to JSON 1",
             {$td->COMMAND =>
                  "qpdf-ctest 46 minimal.pdf '' a.json"},
             {$td->STRING => "C test 46 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API write to JSON 1",
             {$td->FILE => "a.json"},
             {$td->FILE => "qpdf-ctest-46.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("C API write to JSON 2",
             {$td->COMMAND =>
                  "qpdf-ctest 47 minimal.pdf '' a.json auto"},
             {$td->STRING => "C test 47 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API write to JSON 2",
             {$td->FILE => "a.json"},
             {$td->FILE => "qpdf-ctest-47.json"},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check C API write to JSON stream",
             {$td->FILE => "auto-4"},
             {$td->FILE => "qpdf-ctest-47-4"});

# Bugs #1072 and #1079 illustrate cases that qpdf-json got wrong. In
# #1072, it was noticed that name tokens containing binary characters
# (using #xx) would generate invalid JSON, even though qpdf's own JSON
# parser would accept it. Also, the JSON spec allows real numbers in
# scientific notation, but the PDF spec does not.
$n_tests += 4;
$td->runtest("handle binary names",
             {$td->COMMAND =>
                  "qpdf --json-output weird-tokens.pdf a.json"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
# Round-trip back to PDF is tested above.
$td->runtest("check json",
             {$td->FILE => "a.json"},
             {$td->FILE => "weird-tokens.json"},
             $td->NORMALIZE_NEWLINES);
# Make sure we can properly handle JSON with scientific notation.
$td->runtest("weird tokens round trip json",
             {$td->COMMAND =>
                  "qpdf --json-input --json-output weird-tokens.json -"},
             {$td->FILE => "weird-tokens.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("weird tokens with scientific notation",
             {$td->COMMAND =>
                  "qpdf --json-input --json-output weird-tokens-alt.json -"},
             {$td->FILE => "weird-tokens.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

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