Debian Codegraph @

podofo/test/unit/StringTest.cpp

raw
1 // SPDX-FileCopyrightText: 2007 Dominik Seichter <[email protected]>
2 // SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]>
3 // SPDX-License-Identifier: MIT-0
4
5 #include <PdfTest.h>
6
7 using namespace std;
8 using namespace PoDoFo;
9
10 static void TestWriteEscapeSequences(const string_view& str, const string_view& expected);
11
12 TEST_CASE("TestEscapeAllCharacters")
13 {
14 PdfMemDocument doc;
15 doc.Load(TestUtils::GetTestInputFilePath("TestEscapeAllCharacters.pdf"));
16
17 // NOTE: Escaped new line '\n', '\r' characters are ignored
18 constexpr string_view VRefString = "\1\2\3\4\5\6\7\10\11\13\14\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\1\2\3\4\5\6\7\70\71\72\73\74\75\76\77\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\b\143\144\145\f\147\150\151\152\153\154\155\n\157\160\161\r\163\t\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"sv;
19
20 constexpr string_view TestRefString = "\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\1\2\3\4\5\6\7\70\71\72\73\74\75\76\77\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\b\143\144\145\f\147\150\151\152\153\154\155\n\157\160\161\r\163\t\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"sv;
21
22 auto& obj = doc.GetObjects().MustGetObject(PdfReference(5, 0));
23
24 REQUIRE(obj.GetDictionary().FindKeyAs<PdfString>("V").GetRawData() == VRefString);
25 REQUIRE(obj.GetDictionary().FindKeyAs<PdfString>("Test").GetRawData() == TestRefString);
26 }
27
28 TEST_CASE("TestEncryptedStringsEscaped")
29 {
30 PdfMemDocument doc;
31 doc.Load(TestUtils::GetTestInputFilePath("TestEncryptedStringsEscaped.pdf"), "userpass");
32
33 REQUIRE(*doc.GetMetadata().GetTitle() == "Test title");
34
35 // This has a escaped `\r` character that shall be ignored. This currently works in Pdf.js but
36 // not on Adobe. It's a rare edge case anyway
37 REQUIRE(doc.GetMetadata().GetModifyDate()->ToString() == "D:20250403231507+02'00'");
38
39 doc.Load(TestUtils::GetTestInputFilePath("TestEncryptedStringsEscaped2.pdf"), "userpass");
40
41 // Next title has a escaped `\0`
42 REQUIRE(*doc.GetMetadata().GetTitle() == "Test title 2");
43
44 // Next producer has a escaped `\n` character that shall be ignored. This
45 // works also in Adobe
46 REQUIRE(*doc.GetMetadata().GetProducer() == "PoDoFo - https://github.com/podofo/podofo");
47 }
48
49 TEST_CASE("TestStringUtf8")
50 {
51 string_view str = "Hallo PoDoFo!";
52 REQUIRE(PdfString(str) == str);
53
54 string_view stringJapUtf8 = "「PoDoFo」は今から日本語も話せます。";
55 REQUIRE(PdfString(stringJapUtf8) == stringJapUtf8);
56 }
57
58 TEST_CASE("TestPdfDocEncoding")
59 {
60 const string src = "This string contains PdfDocEncoding Characters: ÄÖÜ";
61 string_view ref = "(This string contains PdfDocEncoding Characters: \304\326\334)"sv;
62 // Normal ascii string should be converted to UTF8
63 PdfString str(src);
64 REQUIRE(str == src);
65 REQUIRE(str.GetCharset() == PdfStringCharset::PdfDocEncoding);
66
67 // Serialize the string
68 string serialized;
69 str.ToString(serialized);
70 REQUIRE(serialized == ref);
71
72 // Deserialize the string (remove the surrounding parenthesis '(' ')' )
73 str = PdfString::FromRaw(serialized.substr(1, serialized.length() - 2));
74 REQUIRE(str.GetString() == src);
75 }
76
77 TEST_CASE("TestEscapeBrackets")
78 {
79 // Test balanced brackets ansi
80 string_view balanced = "Hello (balanced) World";
81 string_view balancedExpect = "(Hello \\(balanced\\) World)";
82
83 PdfString pdfStrAscii(balanced);
84 PdfVariant varAscii(pdfStrAscii);
85 string strAscii;
86 varAscii.ToString(strAscii);
87
88 REQUIRE(pdfStrAscii.GetCharset() == PdfStringCharset::Ascii);
89 REQUIRE(strAscii == balancedExpect);
90
91 // Test un-balanced brackets ansi
92 string_view unbalanced = "Hello ((unbalanced World";
93 string_view unbalancedExpect = "(Hello \\(\\(unbalanced World)";
94
95 PdfString pdfStrAscii2(unbalanced);
96 PdfVariant varAscii2(pdfStrAscii2);
97 string strAscii2;
98 varAscii2.ToString(strAscii2);
99
100 REQUIRE(strAscii2 == unbalancedExpect);
101
102 string_view utf16HexStr =
103 "<FEFF00480065006C006C006F0020002800280075006E00620061006C0061006E00630065006400200057006F0072006C00640029>";
104
105 string_view utf16Expected = "Hello ((unbalanced World)";
106 // Test reading the unicode string back in
107 PdfVariant varRead;
108 PdfTokenizer tokenizer;
109 SpanStreamDevice input(utf16HexStr);
110 (void)tokenizer.ReadNextVariant(input, varRead);
111 REQUIRE(varRead.GetDataType() == PdfDataType::String);
112 REQUIRE(varRead.GetString() == utf16Expected);
113 }
114
115 TEST_CASE("TestWriteEscapeSequences")
116 {
117 TestWriteEscapeSequences("(1Hello\\nWorld)", "(1Hello\\nWorld)");
118 TestWriteEscapeSequences("(Hello\nWorld)", "(Hello\\nWorld)");
119 TestWriteEscapeSequences("(Hello\012World)", "(Hello\\nWorld)");
120 TestWriteEscapeSequences("(Hello\\012World)", "(Hello\\nWorld)");
121
122 TestWriteEscapeSequences("(2Hello\\rWorld)", "(2Hello\\rWorld)");
123 TestWriteEscapeSequences("(Hello\rWorld)", "(Hello\\rWorld)");
124 TestWriteEscapeSequences("(Hello\015World)", "(Hello\\rWorld)");
125 TestWriteEscapeSequences("(Hello\\015World)", "(Hello\\rWorld)");
126
127 TestWriteEscapeSequences("(3Hello\\tWorld)", "(3Hello\\tWorld)");
128 TestWriteEscapeSequences("(Hello\tWorld)", "(Hello\\tWorld)");
129 TestWriteEscapeSequences("(Hello\011World)", "(Hello\\tWorld)");
130 TestWriteEscapeSequences("(Hello\\011World)", "(Hello\\tWorld)");
131
132 TestWriteEscapeSequences("(4Hello\\fWorld)", "(4Hello\\fWorld)");
133 TestWriteEscapeSequences("(Hello\fWorld)", "(Hello\\fWorld)");
134 TestWriteEscapeSequences("(Hello\014World)", "(Hello\\fWorld)");
135 TestWriteEscapeSequences("(Hello\\014World)", "(Hello\\fWorld)");
136
137 TestWriteEscapeSequences("(5Hello\\(World)", "(5Hello\\(World)");
138 TestWriteEscapeSequences("(Hello\\050World)", "(Hello\\(World)");
139
140 TestWriteEscapeSequences("(6Hello\\)World)", "(6Hello\\)World)");
141 TestWriteEscapeSequences("(Hello\\051World)", "(Hello\\)World)");
142
143 TestWriteEscapeSequences("(7Hello\\\\World)", "(7Hello\\\\World)");
144 TestWriteEscapeSequences("(Hello\\\134World)", "(Hello\\\\World)");
145
146 // Special case, \ at end of line
147 TestWriteEscapeSequences("(8Hello\\\nWorld)", "(8HelloWorld)");
148
149
150 TestWriteEscapeSequences("(9Hello\003World)", "(9Hello\003World)");
151 }
152
153 TEST_CASE("TestEmptyString")
154 {
155 const char* empty = "";
156 string strEmpty;
157 string strEmpty2(empty);
158
159 PdfString str1;
160 PdfString str2(strEmpty);
161 PdfString str3(strEmpty2);
162 PdfString str4(empty);
163
164 REQUIRE(str1.GetString().length() == 0u);
165 REQUIRE(str1.GetString() == strEmpty);
166 REQUIRE(str1.GetString() == strEmpty2);
167
168 REQUIRE(str2.GetString().length() == 0u);
169 REQUIRE(str2.GetString() == strEmpty);
170 REQUIRE(str2.GetString() == strEmpty2);
171
172 REQUIRE(str3.GetString().length() == 0u);
173 REQUIRE(str3.GetString() == strEmpty);
174 REQUIRE(str3.GetString() == strEmpty2);
175
176 REQUIRE(str4.GetString().length() == 0u);
177 REQUIRE(str4.GetString() == strEmpty);
178 REQUIRE(str4.GetString() == strEmpty2);
179 }
180
181 TEST_CASE("TestInitFromUtf8")
182 {
183 string_view utf8 = "This string contains non PdfDocEncoding Characters: ЙКЛМ";
184 string_view ref = "(\376\377\0\124\0\150\0\151\0\163\0\40\0\163\0\164\0\162\0\151\0\156\0\147\0\40\0\143\0\157\0\156\0\164\0\141\0\151\0\156\0\163\0\40\0\156\0\157\0\156\0\40\0\120\0\144\0\146\0\104\0\157\0\143\0\105\0\156\0\143\0\157\0\144\0\151\0\156\0\147\0\40\0\103\0\150\0\141\0\162\0\141\0\143\0\164\0\145\0\162\0\163\0\72\0\40\4\31\4\32\4\33\4\34)"sv;
185
186 const PdfString str(utf8);
187
188 REQUIRE(str.GetCharset() == PdfStringCharset::Unicode);
189 string serialized;
190 str.ToString(serialized);
191 REQUIRE(serialized == ref);
192 REQUIRE(str.GetString().length() == utf8.length());
193 REQUIRE(str.GetString() == string(utf8));
194 }
195
196 void TestWriteEscapeSequences(const string_view& str, const string_view& expected)
197 {
198 PdfVariant variant;
199 string ret;
200
201 INFO(utls::Format("Testing with value: {}", str));
202 PdfPostScriptTokenizer tokenizer;
203 SpanStreamDevice device(str);
204
205 tokenizer.TryReadNextVariant(device, variant);
206 REQUIRE(variant.GetDataType() == PdfDataType::String);
207
208 variant.ToString(ret);
209 INFO(utls::Format(" -> Convert To String: {}", ret));
210
211 REQUIRE(expected == ret);
212 }