| 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 PoDoFo; |
| 12 | using namespace std; |
| 13 | |
| 14 | void TestEscapedName(const string_view& nameStr, const string_view& expectedEncoded); |
| 15 | void TestEncodedName(const string_view& pszString, const string_view& pszExpected); |
| 16 | void TestNameEquality(const string_view& name1, const string_view& name2); |
| 17 | void TestNameWrite(const string_view& view, const string_view& expected); |
| 18 | void TestFromEscape(const string_view& name1, const string_view& name2); |
| 19 | |
| 20 | TEST_CASE("testParseAndWrite") |
| 21 | { |
| 22 | const char* data = "/#E5#8A#A8#E6#80#81#E8#BF#9E#E6#8E#A5#E7#BA#BF"; |
| 23 | auto device = std::make_shared<SpanStreamDevice>(data); |
| 24 | PdfTokenizer tokenizer; |
| 25 | |
| 26 | string_view token; |
| 27 | PdfTokenType type; |
| 28 | bool gotToken = tokenizer.TryReadNextToken(*device, token, type); |
| 29 | |
| 30 | REQUIRE(gotToken); |
| 31 | REQUIRE(type == PdfTokenType::Slash); |
| 32 | |
| 33 | gotToken = tokenizer.TryReadNextToken(*device, token, type); |
| 34 | |
| 35 | REQUIRE(gotToken); |
| 36 | REQUIRE(type == PdfTokenType::Literal); |
| 37 | |
| 38 | // Test with const char* constructor |
| 39 | PdfName name = PdfName::FromEscaped(token); |
| 40 | PdfVariant var(name); |
| 41 | string str; |
| 42 | var.ToString(str); |
| 43 | |
| 44 | REQUIRE(str == data); |
| 45 | // str.c_str() + 1 <- ignore leading slash |
| 46 | REQUIRE(name.GetEscapedName() == (string_view)str.substr(1)); |
| 47 | |
| 48 | // Test with string constructor |
| 49 | PdfName name2 = PdfName::FromEscaped(token); |
| 50 | PdfVariant var2(name); |
| 51 | string str2; |
| 52 | var.ToString(str2); |
| 53 | |
| 54 | REQUIRE(str2 == data); |
| 55 | // str.c_str() + 1 <- ignore leading slash |
| 56 | REQUIRE(name2.GetEscapedName() == (string_view)str2.substr(1)); |
| 57 | } |
| 58 | |
| 59 | TEST_CASE("testNameEncoding") |
| 60 | { |
| 61 | // Test some names. The first argument is the unencoded representation, the second |
| 62 | // is the expected encoded result. The result must not only be /a/ correct encoded |
| 63 | // name for the unencoded form, but must be the exact one PoDoFo should produce. |
| 64 | TestEscapedName("Length With Spaces", "Length#20With#20Spaces"); |
| 65 | TestEscapedName("Length\001\002\003Spaces", "Length#01#02#03Spaces"); |
| 66 | TestEscapedName("Length#01#02#03Spaces#7F", "Length#2301#2302#2303Spaces#237F"); |
| 67 | TestEscapedName("Tab\tTest", "Tab#09Test"); |
| 68 | } |
| 69 | |
| 70 | TEST_CASE("testEncodedNames") |
| 71 | { |
| 72 | // Test some pre-encoded names. The first argument is the encoded name that'll be |
| 73 | // read from the PDF; the second is the expected representation. |
| 74 | TestEncodedName("PANTONE#205757#20CV", "PANTONE 5757 CV"); |
| 75 | TestEncodedName("paired#28#29parentheses", "paired()parentheses"); |
| 76 | TestEncodedName("The_Key_of_F#23_Minor", "The_Key_of_F#_Minor"); |
| 77 | TestEncodedName("A#42", "AB"); |
| 78 | TestEncodedName("ANPA#20723-0#20AdPro", "ANPA 723-0 AdPro"); |
| 79 | } |
| 80 | |
| 81 | TEST_CASE("testEquality") |
| 82 | { |
| 83 | // Make sure differently encoded names compare equal if their decoded values |
| 84 | // are equal. |
| 85 | TestNameEquality("With Spaces", "With#20Spaces"); |
| 86 | TestNameEquality("#57#69#74#68#20#53#70#61#63#65#73", "With#20Spaces"); |
| 87 | } |
| 88 | |
| 89 | TEST_CASE("testWrite") |
| 90 | { |
| 91 | // Make sure all names are written correctly to an output device! |
| 92 | TestNameWrite("Length With Spaces", "/Length#20With#20Spaces"); |
| 93 | TestNameWrite("Length\001\002\003Spaces", "/Length#01#02#03Spaces"); |
| 94 | TestNameWrite("Tab\tTest", "/Tab#09Test"); |
| 95 | TestNameWrite("ANPA 723-0 AdPro", "/ANPA#20723-0#20AdPro"); |
| 96 | } |
| 97 | |
| 98 | TEST_CASE("testFromEscaped") |
| 99 | { |
| 100 | TestFromEscape("ANPA#20723-0#20AdPro", "ANPA 723-0 AdPro"); |
| 101 | TestFromEscape("Length#20With#20Spaces", "Length With Spaces"); |
| 102 | } |
| 103 | |
| 104 | // |
| 105 | // Test encoding of names. |
| 106 | // pszString : internal representation, ie unencoded name |
| 107 | // pszExpectedEncoded: the encoded string PoDoFo should produce |
| 108 | // |
| 109 | void TestEscapedName(const string_view& nameStr, const string_view& expectedEncoded) |
| 110 | { |
| 111 | INFO(utls::Format("Testing name: {}", nameStr)); |
| 112 | |
| 113 | PdfName name(nameStr); |
| 114 | INFO(utls::Format(" -> Expected Value: {}", expectedEncoded)); |
| 115 | INFO(utls::Format(" -> Got Value: {}", name.GetEscapedName())); |
| 116 | INFO(utls::Format(" -> Unescaped Value: {}", name.GetString())); |
| 117 | |
| 118 | REQUIRE(name.GetEscapedName() == expectedEncoded); |
| 119 | |
| 120 | // Ensure the encoded string compares equal to its unencoded |
| 121 | // variant |
| 122 | REQUIRE(PdfName::FromEscaped(expectedEncoded) == name); |
| 123 | } |
| 124 | |
| 125 | void TestEncodedName(const string_view& escaped, const string_view& expected) |
| 126 | { |
| 127 | PdfName name(PdfName::FromEscaped(escaped)); |
| 128 | INFO(utls::Format("Testing encoded name: {}", escaped)); |
| 129 | INFO(utls::Format(" -> Expected Value: {}", expected)); |
| 130 | INFO(utls::Format(" -> Got Value: {}", name.GetString())); |
| 131 | INFO(utls::Format(" -> Escaped Value: {}", name.GetEscapedName())); |
| 132 | |
| 133 | REQUIRE(name == expected); |
| 134 | |
| 135 | // Ensure the name compares equal with one constructed from the |
| 136 | // expected unescaped form |
| 137 | REQUIRE(PdfName(expected) == name); |
| 138 | } |
| 139 | |
| 140 | void TestNameEquality(const string_view& name1Str, const string_view& name2Str) |
| 141 | { |
| 142 | PdfName name1(PdfName::FromEscaped(name1Str)); |
| 143 | PdfName name2(PdfName::FromEscaped(name2Str)); |
| 144 | |
| 145 | INFO(utls::Format("Testing equality of encoded names '{}' and '{}'", name1Str, name2Str)); |
| 146 | INFO(utls::Format(" -> Name1 Decoded Value: {}", name1.GetString())); |
| 147 | INFO(utls::Format(" -> Name2 Decoded Value: {}", name2.GetString())); |
| 148 | |
| 149 | REQUIRE(name1 == name2); // use operator== |
| 150 | REQUIRE(!(name1 != name2)); // use operator!= |
| 151 | } |
| 152 | |
| 153 | void TestNameWrite(const string_view& view, const string_view& expected) |
| 154 | { |
| 155 | PdfName name(view); |
| 156 | REQUIRE(name.ToString() == expected); |
| 157 | } |
| 158 | |
| 159 | void TestFromEscape(const string_view& name1, const string_view& name2) |
| 160 | { |
| 161 | PdfName name = PdfName::FromEscaped(name1); |
| 162 | REQUIRE(name == name2); |
| 163 | } |