153 lines
5.1 KiB
C++
153 lines
5.1 KiB
C++
#include "document_test_helpers.hpp"
|
|
|
|
namespace pdfengine {
|
|
|
|
TEST(DocumentLoadTest, NonExistentFileReturnsFileNotFound) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
auto result = PdfDocument::loadFromFile("nonexistent_file_12345.pdf");
|
|
ASSERT_FALSE(result.has_value());
|
|
EXPECT_EQ(result.error(), EngineError::FileNotFound);
|
|
}
|
|
|
|
TEST(DocumentLoadTest, InvalidFileReturnsInvalidFormat) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
std::string path = "invalid_format_test.pdf";
|
|
{
|
|
std::ofstream out(path, std::ios::binary);
|
|
out << "NOT A PDF FILE!";
|
|
}
|
|
|
|
auto result = PdfDocument::loadFromFile(path);
|
|
std::filesystem::remove(path);
|
|
|
|
ASSERT_FALSE(result.has_value());
|
|
EXPECT_EQ(result.error(), EngineError::InvalidFormat);
|
|
}
|
|
|
|
TEST(DocumentLoadTest, EncryptedPdfRequiresPassword) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
auto path = getCorpusPath("edge-cases", "encrypted.pdf");
|
|
if (!std::filesystem::exists(path)) {
|
|
GTEST_SKIP() << "encrypted.pdf not found in corpus.";
|
|
}
|
|
|
|
auto result = PdfDocument::loadFromFile(path.string());
|
|
ASSERT_FALSE(result.has_value());
|
|
EXPECT_EQ(result.error(), EngineError::PasswordRequired);
|
|
}
|
|
|
|
TEST(DocumentLoadTest, EncryptedPdfInvalidPassword) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
auto path = getCorpusPath("edge-cases", "encrypted.pdf");
|
|
if (!std::filesystem::exists(path)) {
|
|
GTEST_SKIP() << "encrypted.pdf not found in corpus.";
|
|
}
|
|
|
|
auto result = PdfDocument::loadFromFile(path.string(), "wrong_password");
|
|
ASSERT_FALSE(result.has_value());
|
|
EXPECT_EQ(result.error(), EngineError::InvalidPassword);
|
|
}
|
|
|
|
TEST(DocumentLoadTest, EncryptedPdfCorrectPassword) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
auto path = getCorpusPath("edge-cases", "encrypted.pdf");
|
|
if (!std::filesystem::exists(path)) {
|
|
GTEST_SKIP() << "encrypted.pdf not found in corpus.";
|
|
}
|
|
|
|
std::vector<std::string> candidates = {"tessy", "test", "password", "123456", "1234", "foobar", "user", "owner", ""};
|
|
bool success = false;
|
|
for (const auto& pw : candidates) {
|
|
auto result = PdfDocument::loadFromFile(path.string(), pw);
|
|
if (result.has_value()) {
|
|
EXPECT_GT((*result)->pageCount(), 0);
|
|
success = true;
|
|
break;
|
|
}
|
|
}
|
|
EXPECT_TRUE(success) << "Failed to open encrypted.pdf with any of the candidate passwords.";
|
|
}
|
|
|
|
TEST(DocumentLoadTest, LoadFromMemorySuccess) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
auto path = getCorpusPath("basic", "hello_world.pdf");
|
|
if (!std::filesystem::exists(path)) {
|
|
GTEST_SKIP() << "hello_world.pdf not found in corpus.";
|
|
}
|
|
|
|
auto buffer = readFile(path);
|
|
ASSERT_FALSE(buffer.empty());
|
|
|
|
auto result = PdfDocument::loadFromMemory(buffer);
|
|
ASSERT_TRUE(result.has_value());
|
|
EXPECT_EQ((*result)->pageCount(), 1);
|
|
}
|
|
|
|
TEST(DocumentLoadTest, LoadFromMemoryEmptyBuffer) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
std::vector<uint8_t> empty_buf;
|
|
auto result = PdfDocument::loadFromMemory(empty_buf);
|
|
ASSERT_FALSE(result.has_value());
|
|
EXPECT_EQ(result.error(), EngineError::InvalidFormat);
|
|
}
|
|
|
|
TEST(PageCountTest, CorpusPageCounts) {
|
|
SKIP_IF_NO_PDFIUM();
|
|
|
|
struct ExpectedPageCount {
|
|
std::string folder;
|
|
std::string file;
|
|
int count;
|
|
};
|
|
|
|
std::vector<ExpectedPageCount> targets = {
|
|
{"basic", "about_blank.pdf", 1},
|
|
{"basic", "black.pdf", 1},
|
|
{"basic", "hello_world.pdf", 1},
|
|
{"basic", "hello_world_2_pages.pdf", 2},
|
|
{"basic", "rectangles_multi_pages.pdf", 5}
|
|
};
|
|
|
|
for (const auto& t : targets) {
|
|
auto path = getCorpusPath(t.folder, t.file);
|
|
if (!std::filesystem::exists(path)) {
|
|
continue;
|
|
}
|
|
auto docRes = PdfDocument::loadFromFile(path.string());
|
|
ASSERT_TRUE(docRes.has_value()) << "Failed to open " << t.file;
|
|
EXPECT_EQ((*docRes)->pageCount(), t.count) << "Mismatch for " << t.file;
|
|
}
|
|
|
|
auto verifyCorpusFolder = [](const std::string& folder, const std::vector<std::string>& files) {
|
|
for (const auto& f : files) {
|
|
auto path = getCorpusPath(folder, f);
|
|
if (!std::filesystem::exists(path)) {
|
|
ADD_FAILURE() << "Missing corpus file: " << path.string();
|
|
continue;
|
|
}
|
|
|
|
std::vector<std::string> passwords = {""};
|
|
if (f == "encrypted.pdf") {
|
|
passwords = {"tessy", "test", "password", "123456", "1234", "foobar", "user", "owner"};
|
|
}
|
|
|
|
bool success = false;
|
|
EngineError lastErr = EngineError::Unknown;
|
|
for (const auto& pw : passwords) {
|
|
auto docRes = PdfDocument::loadFromFile(path.string(), pw);
|
|
if (docRes.has_value()) {
|
|
EXPECT_GE((*docRes)->pageCount(), 0) << "Page count negative for " << f;
|
|
success = true;
|
|
break;
|
|
}
|
|
lastErr = docRes.error();
|
|
}
|
|
EXPECT_TRUE(success) << "Failed to open " << f << " error: " << static_cast<int>(lastErr);
|
|
}
|
|
};
|
|
|
|
verifyCorpusFolder("basic", corpus_basic);
|
|
verifyCorpusFolder("fonts", corpus_fonts);
|
|
verifyCorpusFolder("edge-cases", corpus_edge);
|
|
}
|
|
} |