2021-11-16 20:02:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Simon Woertz <simon@woertz.at>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-11-16 22:51:03 +00:00
|
|
|
#include <AK/Forward.h>
|
2021-11-16 20:02:55 +00:00
|
|
|
#include <LibCore/MappedFile.h>
|
|
|
|
#include <LibPDF/Document.h>
|
|
|
|
#include <LibTest/Macros.h>
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
|
|
|
TEST_CASE(linearized_pdf)
|
|
|
|
{
|
2023-07-11 14:02:10 +00:00
|
|
|
auto file = MUST(Core::MappedFile::map("linearized.pdf"sv));
|
2023-05-07 18:14:06 +00:00
|
|
|
auto document = MUST(PDF::Document::create(file->bytes()));
|
|
|
|
MUST(document->initialize());
|
|
|
|
EXPECT_EQ(document->get_page_count(), 1U);
|
2021-11-16 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(non_linearized_pdf)
|
|
|
|
{
|
2023-07-11 14:02:10 +00:00
|
|
|
auto file = MUST(Core::MappedFile::map("non-linearized.pdf"sv));
|
2023-05-07 18:14:06 +00:00
|
|
|
auto document = MUST(PDF::Document::create(file->bytes()));
|
|
|
|
MUST(document->initialize());
|
|
|
|
EXPECT_EQ(document->get_page_count(), 1U);
|
2021-11-16 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(complex_pdf)
|
|
|
|
{
|
2023-07-11 14:02:10 +00:00
|
|
|
auto file = MUST(Core::MappedFile::map("complex.pdf"sv));
|
2023-05-07 18:14:06 +00:00
|
|
|
auto document = MUST(PDF::Document::create(file->bytes()));
|
|
|
|
MUST(document->initialize());
|
|
|
|
EXPECT_EQ(document->get_page_count(), 3U);
|
2021-11-16 20:02:55 +00:00
|
|
|
}
|
2021-11-16 22:51:03 +00:00
|
|
|
|
|
|
|
TEST_CASE(empty_file_issue_10702)
|
|
|
|
{
|
|
|
|
AK::ReadonlyBytes empty;
|
|
|
|
auto document = PDF::Document::create(empty);
|
2022-03-06 00:30:55 +00:00
|
|
|
EXPECT(document.is_error());
|
2021-11-16 22:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(truncated_pdf_header_issue_10717)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
AK::DeprecatedString string { "%PDF-2.11%" };
|
2021-11-16 22:51:03 +00:00
|
|
|
auto document = PDF::Document::create(string.bytes());
|
2022-03-06 00:30:55 +00:00
|
|
|
EXPECT(document.is_error());
|
2021-11-16 22:51:03 +00:00
|
|
|
}
|
2023-07-11 14:49:38 +00:00
|
|
|
|
|
|
|
TEST_CASE(encrypted_with_aes)
|
|
|
|
{
|
|
|
|
auto file = MUST(Core::MappedFile::map("password-is-sup.pdf"sv));
|
|
|
|
auto document = MUST(PDF::Document::create(file->bytes()));
|
|
|
|
EXPECT(document->security_handler()->try_provide_user_password("sup"sv));
|
|
|
|
MUST(document->initialize());
|
|
|
|
EXPECT_EQ(document->get_page_count(), 1U);
|
2023-07-11 16:00:54 +00:00
|
|
|
|
|
|
|
auto info_dict = MUST(document->info_dict()).value();
|
|
|
|
EXPECT_EQ(MUST(info_dict.title()).value(), "sup");
|
|
|
|
EXPECT_EQ(MUST(info_dict.creator()).value(), "TextEdit");
|
2023-07-11 14:49:38 +00:00
|
|
|
}
|
2023-07-11 16:02:16 +00:00
|
|
|
|
|
|
|
TEST_CASE(encrypted_object_stream)
|
|
|
|
{
|
|
|
|
auto file = MUST(Core::MappedFile::map("encryption_nocopy.pdf"sv));
|
|
|
|
auto document = MUST(PDF::Document::create(file->bytes()));
|
|
|
|
MUST(document->initialize());
|
|
|
|
EXPECT_EQ(document->get_page_count(), 1U);
|
|
|
|
|
|
|
|
auto info_dict = MUST(document->info_dict()).value();
|
|
|
|
EXPECT_EQ(MUST(info_dict.author()).value(), "van der Knijff");
|
|
|
|
EXPECT_EQ(MUST(info_dict.creator()).value(), "Acrobat PDFMaker 9.1 voor Word");
|
|
|
|
}
|
2023-10-25 22:45:56 +00:00
|
|
|
|
|
|
|
TEST_CASE(malformed_pdf_document)
|
|
|
|
{
|
|
|
|
Array test_inputs = {
|
|
|
|
"oss-fuzz-testcase-62065.pdf"sv
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto test_input : test_inputs) {
|
|
|
|
auto file = MUST(Core::MappedFile::map(test_input));
|
|
|
|
auto document_or_error = PDF::Document::create(file->bytes());
|
|
|
|
EXPECT(document_or_error.is_error());
|
|
|
|
}
|
|
|
|
}
|