1 // Copyright 2018 PDFium Authors. All rights reserved.
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_parser.h"
6 
7 #include "core/fxcrt/cfx_readonlymemorystream.h"
8 #include "core/fxcrt/xml/cfx_xmldocument.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_document.h"
12 
13 class CXFA_DocumentParserTest : public testing::Test {
14  public:
SetUp()15   void SetUp() override {
16     doc_ = pdfium::MakeUnique<CXFA_Document>(nullptr, nullptr);
17     parser_ = pdfium::MakeUnique<CXFA_DocumentParser>(doc_.get());
18   }
19 
TearDown()20   void TearDown() override {
21     // Hold the XML tree until we cleanup the document.
22     std::unique_ptr<CFX_XMLDocument> doc = parser_->GetXMLDoc();
23     parser_ = nullptr;
24     doc_ = nullptr;
25   }
26 
GetDoc() const27   CXFA_Document* GetDoc() const { return doc_.get(); }
GetParser() const28   CXFA_DocumentParser* GetParser() const { return parser_.get(); }
29 
30  private:
31   std::unique_ptr<CXFA_Document> doc_;
32   std::unique_ptr<CXFA_DocumentParser> parser_;
33 };
34 
TEST_F(CXFA_DocumentParserTest,XMLInstructionsScriptOff)35 TEST_F(CXFA_DocumentParserTest, XMLInstructionsScriptOff) {
36   static const char kInput[] =
37       "<config>\n"
38       "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
39       "v2.7-scripting:0 ?>\n"
40       "</config>";
41   EXPECT_FALSE(GetDoc()->is_scripting());
42 
43   auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
44       pdfium::as_bytes(pdfium::make_span(kInput)));
45   ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
46 
47   CXFA_Node* root = GetParser()->GetRootNode();
48   ASSERT_TRUE(root);
49   EXPECT_FALSE(GetDoc()->is_scripting());
50 }
51 
TEST_F(CXFA_DocumentParserTest,XMLInstructionsScriptOn)52 TEST_F(CXFA_DocumentParserTest, XMLInstructionsScriptOn) {
53   static const char kInput[] =
54       "<config>\n"
55       "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
56       "v2.7-scripting:1 ?>\n"
57       "</config>";
58 
59   EXPECT_FALSE(GetDoc()->is_scripting());
60 
61   auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
62       pdfium::as_bytes(pdfium::make_span(kInput)));
63   ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
64 
65   CXFA_Node* root = GetParser()->GetRootNode();
66   ASSERT_TRUE(root);
67   EXPECT_TRUE(GetDoc()->is_scripting());
68 }
69 
TEST_F(CXFA_DocumentParserTest,XMLInstructionsStrictScope)70 TEST_F(CXFA_DocumentParserTest, XMLInstructionsStrictScope) {
71   static const char kInput[] =
72       "<config>"
73       "<?acrobat JavaScript strictScoping ?>\n"
74       "</config>";
75 
76   EXPECT_FALSE(GetDoc()->is_strict_scoping());
77 
78   auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
79       pdfium::as_bytes(pdfium::make_span(kInput)));
80   ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
81 
82   CXFA_Node* root = GetParser()->GetRootNode();
83   ASSERT_TRUE(root);
84   EXPECT_TRUE(GetDoc()->is_strict_scoping());
85 }
86 
TEST_F(CXFA_DocumentParserTest,XMLInstructionsStrictScopeBad)87 TEST_F(CXFA_DocumentParserTest, XMLInstructionsStrictScopeBad) {
88   static const char kInput[] =
89       "<config>"
90       "<?acrobat JavaScript otherScoping ?>\n"
91       "</config>";
92 
93   EXPECT_FALSE(GetDoc()->is_strict_scoping());
94 
95   auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
96       pdfium::as_bytes(pdfium::make_span(kInput)));
97   ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
98 
99   CXFA_Node* root = GetParser()->GetRootNode();
100   ASSERT_TRUE(root);
101   EXPECT_FALSE(GetDoc()->is_strict_scoping());
102 }
103 
TEST_F(CXFA_DocumentParserTest,MultipleXMLInstructions)104 TEST_F(CXFA_DocumentParserTest, MultipleXMLInstructions) {
105   static const char kInput[] =
106       "<config>"
107       "<?originalXFAVersion http://www.xfa.org/schema/xfa-template/2.7 "
108       "v2.7-scripting:1 ?>\n"
109       "<?acrobat JavaScript strictScoping ?>\n"
110       "</config>";
111 
112   EXPECT_FALSE(GetDoc()->is_scripting());
113   EXPECT_FALSE(GetDoc()->is_strict_scoping());
114 
115   auto stream = pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
116       pdfium::as_bytes(pdfium::make_span(kInput)));
117   ASSERT_TRUE(GetParser()->Parse(stream, XFA_PacketType::Config));
118 
119   CXFA_Node* root = GetParser()->GetRootNode();
120   ASSERT_TRUE(root);
121 
122   EXPECT_TRUE(GetDoc()->is_scripting());
123   EXPECT_TRUE(GetDoc()->is_strict_scoping());
124 }
125