kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 1 | // Copyright 2018 The PDFium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "xfa/fxfa/parser/cxfa_document_builder.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "core/fxcrt/cfx_read_only_span_stream.h" |
| 10 | #include "core/fxcrt/xml/cfx_xmldocument.h" |
| 11 | #include "core/fxcrt/xml/cfx_xmlparser.h" |
| 12 | #include "testing/fxgc_unittest.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "v8/include/cppgc/allocation.h" |
| 15 | #include "v8/include/cppgc/persistent.h" |
| 16 | #include "xfa/fxfa/parser/cxfa_document.h" |
| 17 | |
| 18 | class CXFA_DocumentBuilderTest : public FXGCUnitTest { |
| 19 | public: |
| 20 | void SetUp() override { |
| 21 | FXGCUnitTest::SetUp(); |
| 22 | doc_ = cppgc::MakeGarbageCollected<CXFA_Document>( |
| 23 | heap()->GetAllocationHandle(), nullptr, heap(), nullptr); |
| 24 | } |
| 25 | |
| 26 | void TearDown() override { |
| 27 | doc_ = nullptr; |
| 28 | FXGCUnitTest::TearDown(); |
| 29 | } |
| 30 | |
| 31 | CXFA_Document* GetDoc() const { return doc_; } |
| 32 | |
| 33 | CXFA_Node* ParseAndBuild(const RetainPtr<CFX_ReadOnlySpanStream>& stream) { |
| 34 | xml_ = CFX_XMLParser(stream).Parse(); |
| 35 | if (!xml_) |
| 36 | return nullptr; |
| 37 | |
| 38 | CXFA_DocumentBuilder builder(doc_); |
| 39 | if (!builder.BuildDocument(xml_.get(), XFA_PacketType::Config)) |
| 40 | return nullptr; |
| 41 | return builder.GetRootNode(); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | std::unique_ptr<CFX_XMLDocument> xml_; |
| 46 | cppgc::Persistent<CXFA_Document> doc_; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(CXFA_DocumentBuilderTest, EmptyInput) { |
| 50 | static const char kInput[] = ""; |
| 51 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 52 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 53 | EXPECT_FALSE(ParseAndBuild(stream)); |
| 54 | } |
| 55 | |
| 56 | TEST_F(CXFA_DocumentBuilderTest, BadInput) { |
| 57 | static const char kInput[] = "<<<>bar?>>>>>>>"; |
| 58 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 59 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 60 | EXPECT_FALSE(ParseAndBuild(stream)); |
| 61 | } |
| 62 | |
| 63 | TEST_F(CXFA_DocumentBuilderTest, XMLInstructionsScriptOff) { |
| 64 | static const char kInput[] = |
| 65 | "<config>\n" |
| 66 | "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 " |
| 67 | "v2.7-scripting:0 ?>\n" |
| 68 | "</config>"; |
| 69 | EXPECT_FALSE(GetDoc()->is_scripting()); |
| 70 | |
| 71 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 72 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 73 | |
| 74 | CXFA_Node* root = ParseAndBuild(stream); |
| 75 | ASSERT_TRUE(root); |
| 76 | EXPECT_FALSE(GetDoc()->is_scripting()); |
| 77 | } |
| 78 | |
| 79 | TEST_F(CXFA_DocumentBuilderTest, XMLInstructionsScriptOn) { |
| 80 | static const char kInput[] = |
| 81 | "<config>\n" |
| 82 | "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 " |
| 83 | "v2.7-scripting:1 ?>\n" |
| 84 | "</config>"; |
| 85 | |
| 86 | EXPECT_FALSE(GetDoc()->is_scripting()); |
| 87 | |
| 88 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 89 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 90 | |
| 91 | CXFA_Node* root = ParseAndBuild(stream); |
| 92 | ASSERT_TRUE(root); |
| 93 | EXPECT_TRUE(GetDoc()->is_scripting()); |
| 94 | } |
| 95 | |
| 96 | TEST_F(CXFA_DocumentBuilderTest, XMLInstructionsStrictScope) { |
| 97 | static const char kInput[] = |
| 98 | "<config>" |
| 99 | "<?acrobat JavaScript strictScoping ?>\n" |
| 100 | "</config>"; |
| 101 | |
| 102 | EXPECT_FALSE(GetDoc()->is_strict_scoping()); |
| 103 | |
| 104 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 105 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 106 | |
| 107 | CXFA_Node* root = ParseAndBuild(stream); |
| 108 | ASSERT_TRUE(root); |
| 109 | EXPECT_TRUE(GetDoc()->is_strict_scoping()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(CXFA_DocumentBuilderTest, XMLInstructionsStrictScopeBad) { |
| 113 | static const char kInput[] = |
| 114 | "<config>" |
| 115 | "<?acrobat JavaScript otherScoping ?>\n" |
| 116 | "</config>"; |
| 117 | |
| 118 | EXPECT_FALSE(GetDoc()->is_strict_scoping()); |
| 119 | |
| 120 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 121 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 122 | |
| 123 | CXFA_Node* root = ParseAndBuild(stream); |
| 124 | ASSERT_TRUE(root); |
| 125 | EXPECT_FALSE(GetDoc()->is_strict_scoping()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(CXFA_DocumentBuilderTest, MultipleXMLInstructions) { |
| 129 | static const char kInput[] = |
| 130 | "<config>" |
| 131 | "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 " |
| 132 | "v2.7-scripting:1 ?>\n" |
| 133 | "<?acrobat JavaScript strictScoping ?>\n" |
| 134 | "</config>"; |
| 135 | |
| 136 | EXPECT_FALSE(GetDoc()->is_scripting()); |
| 137 | EXPECT_FALSE(GetDoc()->is_strict_scoping()); |
| 138 | |
| 139 | auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>( |
| 140 | pdfium::as_bytes(pdfium::make_span(kInput))); |
| 141 | |
| 142 | CXFA_Node* root = ParseAndBuild(stream); |
| 143 | ASSERT_TRUE(root); |
| 144 | EXPECT_TRUE(GetDoc()->is_scripting()); |
| 145 | EXPECT_TRUE(GetDoc()->is_strict_scoping()); |
| 146 | } |