qpdf-json.test 6.85 KB
#!/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',
    );

$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);