1 // Copyright 2017 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 "testing/xfa_js_embedder_test.h"
6 
7 #include <string>
8 
9 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
10 #include "fpdfsdk/fsdk_define.h"
11 #include "fxjs/cfxjse_engine.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/base/ptr_util.h"
14 
XFAJSEmbedderTest()15 XFAJSEmbedderTest::XFAJSEmbedderTest()
16     : array_buffer_allocator_(pdfium::MakeUnique<FXJS_ArrayBufferAllocator>()) {
17 }
18 
~XFAJSEmbedderTest()19 XFAJSEmbedderTest::~XFAJSEmbedderTest() {}
20 
SetUp()21 void XFAJSEmbedderTest::SetUp() {
22   v8::Isolate::CreateParams params;
23   params.array_buffer_allocator = array_buffer_allocator_.get();
24   isolate_ = v8::Isolate::New(params);
25   ASSERT_TRUE(isolate_);
26 
27   EmbedderTest::SetExternalIsolate(isolate_);
28   EmbedderTest::SetUp();
29 }
30 
TearDown()31 void XFAJSEmbedderTest::TearDown() {
32   value_.reset();
33   script_context_ = nullptr;
34 
35   EmbedderTest::TearDown();
36 
37   isolate_->Dispose();
38   isolate_ = nullptr;
39 }
40 
GetXFADocument()41 CXFA_Document* XFAJSEmbedderTest::GetXFADocument() {
42   return UnderlyingFromFPDFDocument(document())->GetXFADoc()->GetXFADoc();
43 }
44 
OpenDocumentWithOptions(const std::string & filename,const char * password,bool must_linearize)45 bool XFAJSEmbedderTest::OpenDocumentWithOptions(const std::string& filename,
46                                                 const char* password,
47                                                 bool must_linearize) {
48   if (!EmbedderTest::OpenDocumentWithOptions(filename, password,
49                                              must_linearize))
50     return false;
51 
52   script_context_ = GetXFADocument()->GetScriptContext();
53   return true;
54 }
55 
Execute(const ByteStringView & input)56 bool XFAJSEmbedderTest::Execute(const ByteStringView& input) {
57   if (ExecuteHelper(input)) {
58     return true;
59   }
60 
61   CFXJSE_Value msg(GetIsolate());
62   value_->GetObjectPropertyByIdx(1, &msg);
63   fprintf(stderr, "JS: %.*s\n", static_cast<int>(input.GetLength()),
64           input.unterminated_c_str());
65   // If the parsing of the input fails, then v8 will not run, so there will be
66   // no value here to print.
67   if (msg.IsString() && !msg.ToWideString().IsEmpty())
68     fprintf(stderr, "JS ERROR: %ls\n", msg.ToWideString().c_str());
69   return false;
70 }
71 
ExecuteSilenceFailure(const ByteStringView & input)72 bool XFAJSEmbedderTest::ExecuteSilenceFailure(const ByteStringView& input) {
73   return ExecuteHelper(input);
74 }
75 
ExecuteHelper(const ByteStringView & input)76 bool XFAJSEmbedderTest::ExecuteHelper(const ByteStringView& input) {
77   value_ = pdfium::MakeUnique<CFXJSE_Value>(GetIsolate());
78   return script_context_->RunScript(CXFA_Script::Type::Formcalc,
79                                     WideString::FromUTF8(input).AsStringView(),
80                                     value_.get(), GetXFADocument()->GetRoot());
81 }
82