| 1 | /** |
| 2 | * Copyright (C) 2007 by Dominik Seichter <[email protected]> |
| 3 | * Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 | * |
| 5 | * Licensed under GNU Library General Public 2.0 or later. |
| 6 | * Some rights reserved. See COPYING, AUTHORS. |
| 7 | */ |
| 8 | |
| 9 | #include <PdfTest.h> |
| 10 | |
| 11 | using namespace std; |
| 12 | using namespace PoDoFo; |
| 13 | |
| 14 | static void TestWriteEscapeSequences(const string_view& str, const string_view& expected); |
| 15 | |
| 16 | TEST_CASE("testStringUtf8") |
| 17 | { |
| 18 | string_view str = "Hallo PoDoFo!"; |
| 19 | REQUIRE(PdfString(str) == str); |
| 20 | |
| 21 | string_view stringJapUtf8 = "「PoDoFo」は今から日本語も話せます。"; |
| 22 | REQUIRE(PdfString(stringJapUtf8) == stringJapUtf8); |
| 23 | } |
| 24 | |
| 25 | TEST_CASE("testPdfDocEncoding") |
| 26 | { |
| 27 | const string src = "This string contains PdfDocEncoding Characters: ÄÖÜ"; |
| 28 | |
| 29 | // Normal ascii string should be converted to UTF8 |
| 30 | PdfString str(src); |
| 31 | REQUIRE(str == src); |
| 32 | REQUIRE(str.GetState() == PdfStringState::PdfDocEncoding); |
| 33 | |
| 34 | // Serialize the string |
| 35 | string strPdfDocEncoding; |
| 36 | str.ToString(strPdfDocEncoding); |
| 37 | |
| 38 | // Deserialize the string (remove the surrounding parenthesis '(' ')' ) |
| 39 | str = PdfString::FromRaw(strPdfDocEncoding.substr(1, strPdfDocEncoding.length() - 2)); |
| 40 | REQUIRE(str.GetString() == src); |
| 41 | } |
| 42 | |
| 43 | TEST_CASE("testEscapeBrackets") |
| 44 | { |
| 45 | // Test balanced brackets ansi |
| 46 | string_view balanced = "Hello (balanced) World"; |
| 47 | string_view balancedExpect = "(Hello \\(balanced\\) World)"; |
| 48 | |
| 49 | PdfString pdfStrAscii(balanced); |
| 50 | PdfVariant varAscii(pdfStrAscii); |
| 51 | string strAscii; |
| 52 | varAscii.ToString(strAscii); |
| 53 | |
| 54 | REQUIRE(pdfStrAscii.GetState() == PdfStringState::Ascii); |
| 55 | REQUIRE(strAscii == balancedExpect); |
| 56 | |
| 57 | // Test un-balanced brackets ansi |
| 58 | string_view unbalanced = "Hello ((unbalanced World"; |
| 59 | string_view unbalancedExpect = "(Hello \\(\\(unbalanced World)"; |
| 60 | |
| 61 | PdfString pdfStrAscii2(unbalanced); |
| 62 | PdfVariant varAscii2(pdfStrAscii2); |
| 63 | string strAscii2; |
| 64 | varAscii2.ToString(strAscii2); |
| 65 | |
| 66 | REQUIRE(strAscii2 == unbalancedExpect); |
| 67 | |
| 68 | string_view utf16HexStr = |
| 69 | "<FEFF00480065006C006C006F0020002800280075006E00620061006C0061006E00630065006400200057006F0072006C00640029>"; |
| 70 | |
| 71 | string_view utf16Expected = "Hello ((unbalanced World)"; |
| 72 | // Test reading the unicode string back in |
| 73 | PdfVariant varRead; |
| 74 | PdfTokenizer tokenizer; |
| 75 | SpanStreamDevice input(utf16HexStr); |
| 76 | (void)tokenizer.ReadNextVariant(input, varRead); |
| 77 | REQUIRE(varRead.GetDataType() == PdfDataType::String); |
| 78 | auto str = varRead.GetString().GetString(); |
| 79 | REQUIRE(varRead.GetString() == utf16Expected); |
| 80 | } |
| 81 | |
| 82 | TEST_CASE("testWriteEscapeSequences") |
| 83 | { |
| 84 | TestWriteEscapeSequences("(1Hello\\nWorld)", "(1Hello\\nWorld)"); |
| 85 | TestWriteEscapeSequences("(Hello\nWorld)", "(Hello\\nWorld)"); |
| 86 | TestWriteEscapeSequences("(Hello\012World)", "(Hello\\nWorld)"); |
| 87 | TestWriteEscapeSequences("(Hello\\012World)", "(Hello\\nWorld)"); |
| 88 | |
| 89 | TestWriteEscapeSequences("(2Hello\\rWorld)", "(2Hello\\rWorld)"); |
| 90 | TestWriteEscapeSequences("(Hello\rWorld)", "(Hello\\rWorld)"); |
| 91 | TestWriteEscapeSequences("(Hello\015World)", "(Hello\\rWorld)"); |
| 92 | TestWriteEscapeSequences("(Hello\\015World)", "(Hello\\rWorld)"); |
| 93 | |
| 94 | TestWriteEscapeSequences("(3Hello\\tWorld)", "(3Hello\\tWorld)"); |
| 95 | TestWriteEscapeSequences("(Hello\tWorld)", "(Hello\\tWorld)"); |
| 96 | TestWriteEscapeSequences("(Hello\011World)", "(Hello\\tWorld)"); |
| 97 | TestWriteEscapeSequences("(Hello\\011World)", "(Hello\\tWorld)"); |
| 98 | |
| 99 | TestWriteEscapeSequences("(4Hello\\fWorld)", "(4Hello\\fWorld)"); |
| 100 | TestWriteEscapeSequences("(Hello\fWorld)", "(Hello\\fWorld)"); |
| 101 | TestWriteEscapeSequences("(Hello\014World)", "(Hello\\fWorld)"); |
| 102 | TestWriteEscapeSequences("(Hello\\014World)", "(Hello\\fWorld)"); |
| 103 | |
| 104 | TestWriteEscapeSequences("(5Hello\\(World)", "(5Hello\\(World)"); |
| 105 | TestWriteEscapeSequences("(Hello\\050World)", "(Hello\\(World)"); |
| 106 | |
| 107 | TestWriteEscapeSequences("(6Hello\\)World)", "(6Hello\\)World)"); |
| 108 | TestWriteEscapeSequences("(Hello\\051World)", "(Hello\\)World)"); |
| 109 | |
| 110 | TestWriteEscapeSequences("(7Hello\\\\World)", "(7Hello\\\\World)"); |
| 111 | TestWriteEscapeSequences("(Hello\\\134World)", "(Hello\\\\World)"); |
| 112 | |
| 113 | // Special case, \ at end of line |
| 114 | TestWriteEscapeSequences("(8Hello\\\nWorld)", "(8HelloWorld)"); |
| 115 | |
| 116 | |
| 117 | TestWriteEscapeSequences("(9Hello\003World)", "(9Hello\003World)"); |
| 118 | } |
| 119 | |
| 120 | TEST_CASE("testEmptyString") |
| 121 | { |
| 122 | const char* empty = ""; |
| 123 | string strEmpty; |
| 124 | string strEmpty2(empty); |
| 125 | |
| 126 | PdfString str1; |
| 127 | PdfString str2(strEmpty); |
| 128 | PdfString str3(strEmpty2); |
| 129 | PdfString str4(empty); |
| 130 | |
| 131 | REQUIRE(str1.GetString().length() == 0u); |
| 132 | REQUIRE(str1.GetString() == strEmpty); |
| 133 | REQUIRE(str1.GetString() == strEmpty2); |
| 134 | |
| 135 | REQUIRE(str2.GetString().length() == 0u); |
| 136 | REQUIRE(str2.GetString() == strEmpty); |
| 137 | REQUIRE(str2.GetString() == strEmpty2); |
| 138 | |
| 139 | REQUIRE(str3.GetString().length() == 0u); |
| 140 | REQUIRE(str3.GetString() == strEmpty); |
| 141 | REQUIRE(str3.GetString() == strEmpty2); |
| 142 | |
| 143 | REQUIRE(str4.GetString().length() == 0u); |
| 144 | REQUIRE(str4.GetString() == strEmpty); |
| 145 | REQUIRE(str4.GetString() == strEmpty2); |
| 146 | } |
| 147 | |
| 148 | TEST_CASE("testInitFromUtf8") |
| 149 | { |
| 150 | string_view utf8 = "This string contains non PdfDocEncoding Characters: ЙКЛМ"; |
| 151 | const PdfString str(utf8); |
| 152 | |
| 153 | REQUIRE(str.GetState() == PdfStringState::Unicode); |
| 154 | REQUIRE(str.GetString().length() == utf8.length()); |
| 155 | REQUIRE(str.GetString() == string(utf8)); |
| 156 | } |
| 157 | |
| 158 | TEST_CASE("TestEscapeAllCharacters") |
| 159 | { |
| 160 | PdfMemDocument doc; |
| 161 | doc.Load(TestUtils::GetTestInputFilePath("TestEscapeAllCharacters.pdf")); |
| 162 | |
| 163 | // NOTE: Escaped new line '\n', '\r' characters are ignored |
| 164 | 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; |
| 165 | |
| 166 | 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; |
| 167 | |
| 168 | auto& obj = doc.GetObjects().MustGetObject(PdfReference(5, 0)); |
| 169 | |
| 170 | REQUIRE(obj.GetDictionary().FindKeyAs<PdfString>("V").GetRawData() == VRefString); |
| 171 | REQUIRE(obj.GetDictionary().FindKeyAs<PdfString>("Test").GetRawData() == TestRefString); |
| 172 | } |
| 173 | |
| 174 | // NOTE: Unfortunately the following test is disabled |
| 175 | // because it's not working on 0.10.x, probably because of |
| 176 | // a bug in the PDF decryption code that was fixed in 1.0 |
| 177 | // but a backport of the fix is hard, or it just |
| 178 | // requires resources that we don't have |
| 179 | TEST_CASE("TestEncryptedStringsEscaped", "[.]") |
| 180 | { |
| 181 | PdfMemDocument doc; |
| 182 | doc.Load(TestUtils::GetTestInputFilePath("TestEncryptedStringsEscaped.pdf"), "userpass"); |
| 183 | |
| 184 | REQUIRE(doc.GetMetadata().GetTitle()->GetString() == "Test title"); |
| 185 | |
| 186 | // This has a escaped `\r` character that shall be ignored. This currently works in Pdf.js but |
| 187 | // not on Adobe. It's a rare edge case anyway |
| 188 | REQUIRE(doc.GetMetadata().GetModifyDate()->ToString() == "D:20250403231507+02'00'"); |
| 189 | |
| 190 | doc.Load(TestUtils::GetTestInputFilePath("TestEncryptedStringsEscaped2.pdf"), "userpass"); |
| 191 | |
| 192 | // Next title has a escaped `\0` |
| 193 | REQUIRE(doc.GetMetadata().GetTitle()->GetString() == "Test title 2"); |
| 194 | |
| 195 | // Next producer has a escaped `\n` character that shall be ignored. This |
| 196 | // works also in Adobe |
| 197 | REQUIRE(doc.GetMetadata().GetProducer()->GetString() == "PoDoFo - https://github.com/podofo/podofo"); |
| 198 | } |
| 199 | |
| 200 | void TestWriteEscapeSequences(const string_view& str, const string_view& expected) |
| 201 | { |
| 202 | PdfVariant variant; |
| 203 | string ret; |
| 204 | |
| 205 | INFO(utls::Format("Testing with value: {}", str)); |
| 206 | PdfPostScriptTokenizer tokenizer; |
| 207 | SpanStreamDevice device(str); |
| 208 | |
| 209 | tokenizer.TryReadNextVariant(device, variant); |
| 210 | REQUIRE(variant.GetDataType() == PdfDataType::String); |
| 211 | |
| 212 | variant.ToString(ret); |
| 213 | INFO(utls::Format(" -> Convert To String: {}", ret)); |
| 214 | |
| 215 | REQUIRE(expected == ret); |
| 216 | } |