aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/arg-parsing.test
blob: 238f19cfe33fec6b756afbd9ff167dab5413229e (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
#!/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('arg-parsing');

my $n_tests = 25;

$td->runtest("required argument",
             {$td->COMMAND => "qpdf --password minimal.pdf"},
             {$td->REGEXP => "must be given as --password=pass",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("required argument with choices",
             {$td->COMMAND => "qpdf --decode-level minimal.pdf"},
             {$td->REGEXP => "must be given as --decode-level=\\{.*all.*\\}",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
copy("minimal.pdf", '@file.pdf');
$td->runtest("\@file exists and file doesn't",
             {$td->COMMAND => "qpdf --check \@file.pdf"},
             {$td->FILE => "check-at-file.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("missing underlay filename",
             {$td->COMMAND => "qpdf --underlay --"},
             {$td->REGEXP => ".*underlay file not specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("extra overlay filename",
             {$td->COMMAND => "qpdf --overlay x x --"},
             {$td->REGEXP => ".*overlay file already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("multiple pages options",
             {$td->COMMAND => "qpdf --pages . --password=x -- --pages . --"},
             {$td->REGEXP => ".*--pages may only be specified one time.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad numeric range detects unclosed --pages",
             {$td->COMMAND => "qpdf --pages . --pages . --"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("bad file detected as unclosed --pages",
             {$td->COMMAND => "qpdf --pages . 1 --xyz out"},
             {$td->REGEXP => ".*pages options must be terminated with --.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages range",
             {$td->COMMAND => "qpdf --pages --range=1 . --password=z --"},
             {$td->REGEXP => ".*range must follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("duplicate pages range",
             {$td->COMMAND => "qpdf --pages --file=." .
                  " --range=1 --range=2 . --password=z --"},
             {$td->REGEXP => ".*range already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("misplaced pages password",
             {$td->COMMAND => "qpdf --pages --password=z . 1 --"},
             {$td->REGEXP => ".*password must follow a file name.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("duplicated pages password",
             {$td->COMMAND => "qpdf --pages . --password=z --password=z --"},
             {$td->REGEXP => ".*password already specified.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objects json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objects minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v1-only objectinfo json-key",
             {$td->COMMAND => "qpdf --json=2 --json-key=objectinfo minimal.pdf"},
             {$td->REGEXP => ".*\"objects\" and \"objectinfo\" are " .
                  "only valid for json version 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("v2-only qpdf json-key",
             {$td->COMMAND => "qpdf --json=1 --json-key=qpdf minimal.pdf"},
             {$td->REGEXP => ".*\"qpdf\" is only valid for json version > 1.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("wrong number of collate args",
             {$td->COMMAND =>
                  "qpdf --collate=2,3 collate-odd.pdf" .
                  " --pages . minimal.pdf collate-even.pdf -- a.pdf"},
             {$td->REGEXP => ".*--collate has more than one value.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("wrong number of collate args",
             {$td->COMMAND =>
                  "qpdf collate-odd.pdf --pages . minimal.pdf collate-even.pdf --" .
                  " --collate=2,3 a.pdf"},
             {$td->REGEXP => ".*--collate has more than one value.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

# Ignoring -- at the top level was never intended but turned out to
# have been there for a long time so that people relied on it. It is
# intentionally not documented.
$td->runtest("ignore -- at top level",
             {$td->COMMAND => "qpdf -- --check -- minimal.pdf --"},
             {$td->REGEXP => ".*No syntax or stream encoding errors found.*",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("empty and replace-input",
             {$td->COMMAND => "qpdf --empty --replace-input"},
             {$td->REGEXP => ".*--replace-input may not be used with --empty.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("missing key length",
             {$td->COMMAND => "qpdf --encrypt --"},
             {$td->REGEXP => ".*encryption key length is required",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

# Disallow mixing positional and flag-style encryption arguments.
my @bad_enc = (
    "u --owner-password=x",
    "u o --bits=128",
    "--user-password=u o",
    "--user-password=u --owner-password=o 256",
    );
foreach my $arg (@bad_enc)
{
    $td->runtest("mixed encryption args ($arg)",
                 {$td->COMMAND => "qpdf --encrypt $arg"},
                 {$td->REGEXP => ".*positional and dashed encryption arguments may not be mixed",
                      $td->EXIT_STATUS => 2},
                 $td->NORMALIZE_NEWLINES);
}

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