Allow comparison of password-protected files

This commit is contained in:
Jay Berkenbilt 2023-12-20 08:17:55 -05:00
parent 321f9e79db
commit 38042fa273
2 changed files with 19 additions and 8 deletions

View File

@ -129,12 +129,12 @@ cleanTrailer(QPDFObjectHandle& trailer)
} }
std::string std::string
compare(char const* actual_filename, char const* expected_filename) compare(char const* actual_filename, char const* expected_filename, char const* password)
{ {
QPDF actual; QPDF actual;
actual.processFile(actual_filename); actual.processFile(actual_filename, password);
QPDF expected; QPDF expected;
expected.processFile(expected_filename); expected.processFile(expected_filename, password);
// The motivation behind this program is to compare files in a way that allows for // The motivation behind this program is to compare files in a way that allows for
// differences in the exact bytes of zlib compression. If all zlib implementations produced // differences in the exact bytes of zlib compression. If all zlib implementations produced
// exactly the same output, we would just be able to use straight comparison, but since they // exactly the same output, we would just be able to use straight comparison, but since they
@ -191,16 +191,20 @@ main(int argc, char* argv[])
exit(0); exit(0);
} }
if (argc != 3) { if (argc < 3 || argc > 4) {
usage(); usage();
} }
bool show_why = QUtil::get_env("QPDF_COMPARE_WHY"); bool show_why = QUtil::get_env("QPDF_COMPARE_WHY");
try { try {
char const* to_output; char const* to_output;
auto actual = argv[1]; char const* actual = argv[1];
auto expected = argv[2]; char const* expected = argv[2];
auto difference = compare(actual, expected); char const* password{nullptr};
if (argc == 4) {
password = argv[3];
}
auto difference = compare(actual, expected, password);
if (difference.empty()) { if (difference.empty()) {
// The files are identical; write the expected file. This way, tests can be written // The files are identical; write the expected file. This way, tests can be written
// that compare the output of this program to the expected file. // that compare the output of this program to the expected file.

View File

@ -73,7 +73,7 @@ foreach my $f (@diff)
} }
# Repeat for encrypted files. # Repeat for encrypted files.
$n_tests += 3; $n_tests += 5;
$td->runtest("byte-wise compare encrypted files", $td->runtest("byte-wise compare encrypted files",
{$td->COMMAND => "cmp enc1.pdf enc2.pdf"}, {$td->COMMAND => "cmp enc1.pdf enc2.pdf"},
{$td->REGEXP => ".*", $td->EXIT_STATUS => "!0"}); {$td->REGEXP => ".*", $td->EXIT_STATUS => "!0"});
@ -84,6 +84,13 @@ $td->runtest("compare encrypted files (different)",
{$td->COMMAND => "env QPDF_COMPARE_WHY=1 qpdf-test-compare enc1.pdf diff-data-enc.pdf"}, {$td->COMMAND => "env QPDF_COMPARE_WHY=1 qpdf-test-compare enc1.pdf diff-data-enc.pdf"},
{$td->STRING => "4,0: stream data differs\n", $td->EXIT_STATUS => 2}, {$td->STRING => "4,0: stream data differs\n", $td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES); $td->NORMALIZE_NEWLINES);
$td->runtest("with password (same)",
{$td->COMMAND => "env QPDF_COMPARE_WHY=1 qpdf-test-compare enc1.pdf enc2.pdf o"},
{$td->FILE => "enc2.pdf", $td->EXIT_STATUS => 0});
$td->runtest("with password (different)",
{$td->COMMAND => "env QPDF_COMPARE_WHY=1 qpdf-test-compare enc1.pdf diff-data-enc.pdf o"},
{$td->STRING => "4,0: stream data differs\n", $td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES);
# Object streams # Object streams
$n_tests += 1; $n_tests += 1;