aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/deterministic-id.test
blob: a975806a3f07a4d056d4b77dddf6c07e6fc8cd75 (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
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('deterministic-id');

my $n_tests = 19;

# Do not use qpdf-test-compare in this test suite since it ignores
# /ID[1].

foreach my $d ('nn', 'ny', 'yn', 'yy')
{
    my $linearize = ($d =~ m/^y/);
    my $ostream = ($d =~ m/y$/);
    # The deterministic ID is a function of all the data in the file.
    # As such, it is affected by which zlib implementation is in use.
    # The important thing is that the ID is the same if a file is
    # generated the same way more than once, so rather than comparing
    # the output file to a known output, compare subsequent outputs
    # with each other.
    foreach my $out ('a.pdf', 'b.pdf')
    {
        $td->runtest("deterministic ID: linearize/ostream=$d",
                     {$td->COMMAND =>
                          "qpdf -deterministic-id" .
                          ($linearize ? " -linearize" : "") .
                          " -object-streams=" . ($ostream ? "generate" : "disable") .
                          " deterministic-id-in.pdf $out"},
                     {$td->STRING => "",
                          $td->EXIT_STATUS => 0});
    }
    $td->runtest("compare files",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "b.pdf"});
    check_id('a.pdf');
    if ($d eq 'nn')
    {
        # Save for the C API test
        copy("a.pdf", 'c.pdf');
    }
}

$td->runtest("deterministic ID with encryption",
             {$td->COMMAND => "qpdf -deterministic-id encrypted-with-images.pdf a.pdf"},
             {$td->STRING => "qpdf: INTERNAL ERROR: QPDFWriter::generateID" .
                  " has no data for deterministic ID." .
                  "  This may happen if deterministic ID and" .
                  " file encryption are requested together.\n",
              $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("deterministic ID (C API)",
             {$td->COMMAND =>
                  "qpdf-ctest 19 deterministic-id-in.pdf '' a.pdf"},
             {$td->STRING => "C test 19 done\n",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("compare files",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "c.pdf"});

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

sub check_id
{
    my $f = shift;
    chomp(my $id = `qpdf --show-object=trailer $f`);
    if ($id =~ m,.*/ID \[ <(9b1c69409fc9a5f50e44b9588e3e60f8)> <(.{32})>,)
    {
        my $id0 = $1;
        my $id1 = $2;
        $td->runtest("ID fields differ",
                     {$td->STRING => $id0 ne $id1 ? "YES\n" : "$id0 $id1\n"},
                     {$td->STRING => "YES\n"});
    }
    else
    {
        $td->runtest("checked ID",
                     {$td->STRING => "YES\n"},
                     {$td->STRING => "$id\n"});
    }
}