| 1 |
// SPDX-FileCopyrightText: 2008 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]> |
| 3 |
// SPDX-License-Identifier: MIT-0 |
| 4 |
|
| 5 |
#include <PdfTest.h> |
| 6 |
#include <podofo/optional/PdfNames.h> |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
using namespace PoDoFo; |
| 10 |
|
| 11 |
/** This class tests the basic integer and other types PoDoFo uses |
| 12 |
* to make sure they satisfy its requirements for behaviour, size, etc. |
| 13 |
*/ |
| 14 |
|
| 15 |
TEST_CASE("NullableTest") |
| 16 |
{ |
| 17 |
int intval = 15; |
| 18 |
nullable<int&> nullint1(intval); |
| 19 |
nullable<int&> nullint2(&intval); |
| 20 |
nullable<int&> nullint3; |
| 21 |
REQUIRE(nullint1 == &intval); |
| 22 |
REQUIRE(&intval == nullint2); |
| 23 |
REQUIRE(nullint1 != nullptr); |
| 24 |
REQUIRE(nullptr != nullint2); |
| 25 |
REQUIRE(nullint1 == 15); |
| 26 |
REQUIRE(16 != nullint1); |
| 27 |
REQUIRE(nullint3 == nullptr); |
| 28 |
REQUIRE(nullptr == nullint3); |
| 29 |
REQUIRE(nullint3 != &intval); |
| 30 |
REQUIRE(&intval != nullint3); |
| 31 |
} |
| 32 |
|
| 33 |
TEST_CASE("BasicTypeTest") |
| 34 |
{ |
| 35 |
REQUIRE(std::numeric_limits<uint64_t>::max() >= 9999999999); |
| 36 |
} |
| 37 |
|
| 38 |
TEST_CASE("TestIterations") |
| 39 |
{ |
| 40 |
PdfMemDocument doc; |
| 41 |
PdfPage* pages[] = { |
| 42 |
&doc.GetPages().CreatePage(PdfPageSize::A4), |
| 43 |
&doc.GetPages().CreatePage(PdfPageSize::A4), |
| 44 |
&doc.GetPages().CreatePage(PdfPageSize::A4), |
| 45 |
}; |
| 46 |
|
| 47 |
unsigned i = 0; |
| 48 |
for (auto page : doc.GetPages()) |
| 49 |
{ |
| 50 |
REQUIRE(page == pages[i]); |
| 51 |
i++; |
| 52 |
} |
| 53 |
|
| 54 |
auto& page1 = *pages[0]; |
| 55 |
PdfAnnotation* annots[] = { |
| 56 |
&page1.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect()), |
| 57 |
&page1.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect()), |
| 58 |
&page1.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect()), |
| 59 |
}; |
| 60 |
|
| 61 |
i = 0; |
| 62 |
for (auto annot : page1.GetAnnotations()) |
| 63 |
{ |
| 64 |
REQUIRE(annot == annots[i]); |
| 65 |
i++; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("TestIterations2") |
| 70 |
{ |
| 71 |
PdfMemDocument doc; |
| 72 |
vector<PdfField*> fields; |
| 73 |
for (auto field : doc.GetFieldsIterator()) |
| 74 |
{ |
| 75 |
fields.push_back(field); |
| 76 |
} |
| 77 |
|
| 78 |
REQUIRE(fields.size() == 0); |
| 79 |
|
| 80 |
doc.GetPages().CreatePage(Rect(0, 0, 300, 300)); |
| 81 |
for (auto field : doc.GetPages().GetPageAt(0).GetFieldsIterator()) |
| 82 |
{ |
| 83 |
fields.push_back(field); |
| 84 |
} |
| 85 |
REQUIRE(fields.size() == 0); |
| 86 |
|
| 87 |
doc.Load(TestUtils::GetTestInputFilePath("Hierarchies1.pdf")); |
| 88 |
for (auto field : doc.GetFieldsIterator()) |
| 89 |
{ |
| 90 |
fields.push_back(field); |
| 91 |
} |
| 92 |
|
| 93 |
REQUIRE(fields.size() == 25); |
| 94 |
|
| 95 |
fields.clear(); |
| 96 |
for (auto field : doc.GetPages().GetPageAt(0).GetFieldsIterator()) |
| 97 |
{ |
| 98 |
fields.push_back(field); |
| 99 |
} |
| 100 |
|
| 101 |
REQUIRE(fields.size() == 23); |
| 102 |
} |
| 103 |
|
| 104 |
TEST_CASE("ErrorFilePath") |
| 105 |
{ |
| 106 |
try |
| 107 |
{ |
| 108 |
PdfObject test; |
| 109 |
test.GetString(); |
| 110 |
} |
| 111 |
catch (const PdfError& err) |
| 112 |
{ |
| 113 |
auto path = err.GetCallStack().front().GetFilePath(); |
| 114 |
REQUIRE(fs::u8path(path) == fs::u8path("main") / "PdfVariant.cpp"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
TEST_CASE("TestMetadataSet") |
| 119 |
{ |
| 120 |
PdfMemDocument doc; |
| 121 |
auto& metadata = doc.GetMetadata(); |
| 122 |
metadata.SetTitle(PdfString("TestTitle")); |
| 123 |
REQUIRE(metadata.GetTitle()->GetString() == "TestTitle"); |
| 124 |
metadata.SetTitle(PdfString("TestTitle2")); |
| 125 |
REQUIRE(metadata.GetTitle()->GetString() == "TestTitle2"); |
| 126 |
metadata.SetTitle(nullptr); |
| 127 |
REQUIRE(metadata.GetTitle() == nullptr); |
| 128 |
} |
| 129 |
|
| 130 |
TEST_CASE("TestNormalizeRangeRotations") |
| 131 |
{ |
| 132 |
ASSERT_EQUAL(utls::NormalizeCircularRange(370, 0, 360), 10); |
| 133 |
ASSERT_EQUAL(utls::NormalizeCircularRange(-370, 0, 360), 350); |
| 134 |
ASSERT_EQUAL(utls::NormalizeCircularRange(360, 0, 360), 0); |
| 135 |
ASSERT_EQUAL(utls::NormalizeCircularRange(0, 0, 360), 0); |
| 136 |
ASSERT_EQUAL(utls::NormalizeCircularRange(10, 0, 360), 10); |
| 137 |
ASSERT_EQUAL(utls::NormalizeCircularRange(-190, -180, 180), 170); |
| 138 |
ASSERT_EQUAL(utls::NormalizeCircularRange(190, -180, 180), -170); |
| 139 |
ASSERT_EQUAL(utls::NormalizeCircularRange(180, -180, 180), -180); |
| 140 |
ASSERT_EQUAL(utls::NormalizeCircularRange(0, -180, 180), 0); |
| 141 |
ASSERT_EQUAL(utls::NormalizeCircularRange(10, -180, 180), 10); |
| 142 |
ASSERT_EQUAL(utls::NormalizeCircularRange(-10, -180, 180), -10); |
| 143 |
|
| 144 |
// The following page rotation normalizations have |
| 145 |
// been verified in Adobe Reader 2024.002.20759 |
| 146 |
ASSERT_EQUAL(utls::NormalizePageRotation(0), 0); |
| 147 |
ASSERT_EQUAL(utls::NormalizePageRotation(90), 90); |
| 148 |
ASSERT_EQUAL(utls::NormalizePageRotation(180), 180); |
| 149 |
ASSERT_EQUAL(utls::NormalizePageRotation(270), 270); |
| 150 |
ASSERT_EQUAL(utls::NormalizePageRotation(360), 0); |
| 151 |
ASSERT_EQUAL(utls::NormalizePageRotation(0.1), 0); |
| 152 |
ASSERT_EQUAL(utls::NormalizePageRotation(0.499999999), 0); |
| 153 |
ASSERT_EQUAL(utls::NormalizePageRotation(0.5), 90); |
| 154 |
ASSERT_EQUAL(utls::NormalizePageRotation(360.00000001), 0); |
| 155 |
ASSERT_EQUAL(utls::NormalizePageRotation(360.499999999), 0); |
| 156 |
ASSERT_EQUAL(utls::NormalizePageRotation(360.5), 90); |
| 157 |
ASSERT_EQUAL(utls::NormalizePageRotation(359.499999999), 270); |
| 158 |
ASSERT_EQUAL(utls::NormalizePageRotation(359.5), 0); |
| 159 |
ASSERT_EQUAL(utls::NormalizePageRotation(179.499999999), 90); |
| 160 |
ASSERT_EQUAL(utls::NormalizePageRotation(179.5), 180); |
| 161 |
ASSERT_EQUAL(utls::NormalizePageRotation(180.499999999), 180); |
| 162 |
ASSERT_EQUAL(utls::NormalizePageRotation(180.5), 270); |
| 163 |
} |
| 164 |
|
| 165 |
TEST_CASE("TestFileSpecAttachment") |
| 166 |
{ |
| 167 |
PdfMemDocument doc; |
| 168 |
doc.GetPages().CreatePage(); |
| 169 |
shared_ptr<PdfFileSpec> fs = doc.CreateFileSpec(); |
| 170 |
fs->SetFilename(PdfString("Test.xml")); |
| 171 |
fs->SetEmbeddedData(charbuff(string("<?xml version=\"1.0\"?><catalog></catalog>"))); |
| 172 |
auto& names = doc.GetOrCreateNames(); |
| 173 |
auto& embeddedFiles = names.GetOrCreateTree<PdfEmbeddedFiles>(); |
| 174 |
REQUIRE(embeddedFiles.GetValue(*fs->GetFilename()) == nullptr); |
| 175 |
REQUIRE(!embeddedFiles.HasKey(*fs->GetFilename())); |
| 176 |
embeddedFiles.AddValue(*fs->GetFilename(), fs); |
| 177 |
REQUIRE(embeddedFiles.HasKey(*fs->GetFilename())); |
| 178 |
REQUIRE(embeddedFiles.GetValue(*fs->GetFilename()) == fs.get()); |
| 179 |
|
| 180 |
PdfNameTree<PdfFileSpec>::Map map; |
| 181 |
embeddedFiles.ToDictionary(map); |
| 182 |
REQUIRE(map.size() == 1); |
| 183 |
REQUIRE(map[*fs->GetFilename()] == fs); |
| 184 |
|
| 185 |
doc.Save(TestUtils::GetTestOutputFilePath("TestFileSpecAttachment.pdf")); |
| 186 |
} |
| 187 |
|
| 188 |
TEST_CASE("TestPdfNames") |
| 189 |
{ |
| 190 |
// Just include and use "optional/PdfNames.h" |
| 191 |
PdfDictionary dict; |
| 192 |
dict.AddKey(PdfNames::Length, PdfObject(static_cast<int64_t >(100))); |
| 193 |
} |
| 194 |
|
| 195 |
TEST_CASE("TestMoveSemantics") |
| 196 |
{ |
| 197 |
string_view refs1 = "S1"; |
| 198 |
string_view refs2 = "S2"; |
| 199 |
PdfString s1 = refs1; |
| 200 |
REQUIRE(s1 == refs1); |
| 201 |
PdfString s2 = refs2; |
| 202 |
REQUIRE(s2 == refs2); |
| 203 |
s2 = std::move(s1); |
| 204 |
REQUIRE(s2 == refs1); |
| 205 |
REQUIRE(s1 == ""); |
| 206 |
|
| 207 |
string_view refn1 = "N1"; |
| 208 |
string_view refn2 = "N2"; |
| 209 |
PdfName n1 = refn1; |
| 210 |
REQUIRE(n1 == refn1); |
| 211 |
PdfName n2 = refn2; |
| 212 |
REQUIRE(n2 == refn2); |
| 213 |
n2 = std::move(n1); |
| 214 |
REQUIRE(n2 == refn1); |
| 215 |
REQUIRE(n1 == ""); |
| 216 |
} |
| 217 |
|
| 218 |
TEST_CASE("TestAssignObjects") |
| 219 |
{ |
| 220 |
PdfMemDocument doc; |
| 221 |
doc.Load(TestUtils::GetTestInputFilePath("Hierarchies1.pdf")); |
| 222 |
|
| 223 |
{ |
| 224 |
auto& annotsObj = doc.GetObjects().MustGetObject(PdfReference(100, 0)); |
| 225 |
REQUIRE(!annotsObj.IsDirty()); |
| 226 |
annotsObj.GetArray()[15] = PdfObject(nullptr); |
| 227 |
REQUIRE(annotsObj.IsDirty()); |
| 228 |
|
| 229 |
auto& pageObj = doc.GetObjects().MustGetObject(PdfReference(39, 0)); |
| 230 |
REQUIRE(!pageObj.IsDirty()); |
| 231 |
auto& resx = pageObj.GetDictionary().MustGetKey("Contents"); |
| 232 |
resx = PdfObject(nullptr); |
| 233 |
REQUIRE(pageObj.IsDirty()); |
| 234 |
} |
| 235 |
|
| 236 |
auto outputFilePath = TestUtils::GetTestOutputFilePath("TestAssignObjects.pdf"); |
| 237 |
doc.Save(outputFilePath); |
| 238 |
|
| 239 |
doc.Load(outputFilePath); |
| 240 |
{ |
| 241 |
auto& annotsObj = doc.GetObjects().MustGetObject(PdfReference(100, 0)); |
| 242 |
REQUIRE(annotsObj.GetArray()[15].IsNull()); |
| 243 |
|
| 244 |
auto& pageObj = doc.GetObjects().MustGetObject(PdfReference(39, 0)); |
| 245 |
auto& resx = pageObj.GetDictionary().MustGetKey("Contents"); |
| 246 |
REQUIRE(resx.IsNull()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
TEST_CASE("TestObjectAdapter") |
| 251 |
{ |
| 252 |
PdfMemDocument doc; |
| 253 |
doc.Load(TestUtils::GetTestInputFilePath("blank.pdf")); |
| 254 |
const auto& info = doc.GetTrailer().GetDictionary().FindKeyAs<PdfDictionary>("Info"); |
| 255 |
REQUIRE(info.GetKeyAs<PdfString>("Producer") == "PoDoFo - http://podofo.sf.net"); |
| 256 |
REQUIRE(info.GetKeyAsSafe<PdfString>("Prod", "fallback") == "fallback"); |
| 257 |
REQUIRE(info.FindKeyAs<PdfString>("Producer") == "PoDoFo - http://podofo.sf.net"); |
| 258 |
REQUIRE(info.FindKeyAsSafe<PdfString>("Prod", "fallback") == "fallback"); |
| 259 |
REQUIRE(info.FindKeyParentAs<PdfString>("Producer") == "PoDoFo - http://podofo.sf.net"); |
| 260 |
REQUIRE(info.FindKeyParentAsSafe<PdfString>("Prod", "fallback") == "fallback"); |
| 261 |
} |
| 262 |
|
| 263 |
TEST_CASE("TestGetDocKeywords") |
| 264 |
{ |
| 265 |
PdfMemDocument doc; |
| 266 |
vector<string> keywords = { "one", "two", "three" }; |
| 267 |
doc.GetMetadata().SetKeywords(keywords); |
| 268 |
REQUIRE(keywords == doc.GetMetadata().GetKeywords()); |
| 269 |
} |
| 270 |
|
| 271 |
TEST_CASE("DocumentMagicOffsetReset") |
| 272 |
{ |
| 273 |
// Normal PDF |
| 274 |
string_view pdf = R"(%PDF-2.0 |
| 275 |
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj |
| 276 |
2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj |
| 277 |
3 0 obj<</Type/Page/Parent 2 0 R/Resources<<>>/MediaBox[0 0 9 9]>>endobj |
| 278 |
xref |
| 279 |
0 4 |
| 280 |
0000000000 65535 f |
| 281 |
0000000009 00000 n |
| 282 |
0000000052 00000 n |
| 283 |
0000000101 00000 n |
| 284 |
trailer<</Root 1 0 R/Size 4/ID[(1234567890123456)(1234567890123456)]>> |
| 285 |
startxref |
| 286 |
174 |
| 287 |
%%EOF)"; |
| 288 |
|
| 289 |
// PDF with a 10-byte prefix |
| 290 |
string pdfWithOffset = string("OFFSET1234").append(pdf); |
| 291 |
|
| 292 |
PdfMemDocument doc; |
| 293 |
doc.LoadFromBuffer(pdf); |
| 294 |
REQUIRE(doc.GetMagicOffset() == 0); |
| 295 |
|
| 296 |
doc.LoadFromBuffer(pdfWithOffset); |
| 297 |
REQUIRE(doc.GetMagicOffset() == 10); |
| 298 |
doc.Reset(); |
| 299 |
REQUIRE(doc.GetMagicOffset() == 0); |
| 300 |
} |