blob: 1eacdaaad11b06a9ef16c1a1277223278c81669f [file] [log] [blame]
Jay Civelli90a4cb12017-11-28 21:04:521// Copyright 2017 The Chromium 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 <memory>
6
7#include "base/bind.h"
8#include "base/callback_helpers.h"
9#include "base/json/json_reader.h"
10#include "base/macros.h"
Peter Kasting9eb10b02021-04-29 03:10:4811#include "base/strings/string_piece.h"
Ken Rockot315def72018-11-10 02:16:1012#include "base/token.h"
Jay Civelli90a4cb12017-11-28 21:04:5213#include "base/values.h"
14#include "chrome/test/base/in_process_browser_test.h"
Peter Kasting919ce652020-05-07 10:22:3615#include "content/public/test/browser_test.h"
Jay Civelli90a4cb12017-11-28 21:04:5216#include "content/public/test/test_utils.h"
Ken Rockotbf6440092019-11-02 00:21:3217#include "services/data_decoder/public/cpp/data_decoder.h"
Jay Civelli90a4cb12017-11-28 21:04:5218#include "services/data_decoder/public/cpp/safe_xml_parser.h"
Ken Rockot19db6382018-02-10 01:41:0219#include "services/data_decoder/public/mojom/xml_parser.mojom.h"
Jay Civelli90a4cb12017-11-28 21:04:5220
21namespace {
22
Jay Civellied297652017-11-29 04:17:4623constexpr char kTestXml[] = "<hello>bonjour</hello>";
24constexpr char kTestJson[] = R"(
25 {"type": "element",
26 "tag": "hello",
27 "children": [{"type": "text", "text": "bonjour"}]
28 } )";
29
Jay Civelli90a4cb12017-11-28 21:04:5230class SafeXmlParserTest : public InProcessBrowserTest {
31 public:
32 SafeXmlParserTest() = default;
33 ~SafeXmlParserTest() override = default;
34
35 protected:
Jay Civelli90a4cb12017-11-28 21:04:5236 // Parses |xml| and compares its parsed representation with |expected_json|.
37 // If |expected_json| is empty, the XML parsing is expected to fail.
Ken Rockotbf6440092019-11-02 00:21:3238 void TestParse(base::StringPiece xml, const std::string& expected_json) {
Jay Civelli90a4cb12017-11-28 21:04:5239 SCOPED_TRACE(xml);
40
41 base::RunLoop run_loop;
42 std::unique_ptr<base::Value> expected_value;
43 if (!expected_json.empty()) {
Lei Zhang582ecd12019-02-13 20:28:5444 expected_value = base::JSONReader::ReadDeprecated(expected_json);
Jay Civelli90a4cb12017-11-28 21:04:5245 DCHECK(expected_value) << "Bad test, incorrect JSON: " << expected_json;
46 }
47
Ken Rockotbf6440092019-11-02 00:21:3248 data_decoder::DataDecoder::ParseXmlIsolated(
Peter Kasting9eb10b02021-04-29 03:10:4849 std::string(xml),
Jay Civelli90a4cb12017-11-28 21:04:5250 base::BindOnce(&SafeXmlParserTest::XmlParsingDone,
51 base::Unretained(this), run_loop.QuitClosure(),
Ken Rockotbf6440092019-11-02 00:21:3252 std::move(expected_value)));
Jay Civelli90a4cb12017-11-28 21:04:5253 run_loop.Run();
54 }
55
56 private:
Alexander Cooperbc87af32020-07-15 15:59:4557 void XmlParsingDone(base::OnceClosure quit_loop_closure,
Jay Civelli90a4cb12017-11-28 21:04:5258 std::unique_ptr<base::Value> expected_value,
Ken Rockotbf6440092019-11-02 00:21:3259 data_decoder::DataDecoder::ValueOrError result) {
Andres Calderon Jaramilloa0459072018-05-03 18:59:2960 base::ScopedClosureRunner runner(std::move(quit_loop_closure));
Jay Civelli90a4cb12017-11-28 21:04:5261 if (!expected_value) {
Ken Rockotbf6440092019-11-02 00:21:3262 EXPECT_FALSE(result.value);
63 EXPECT_TRUE(result.error);
Jay Civelli90a4cb12017-11-28 21:04:5264 return;
65 }
Ken Rockotbf6440092019-11-02 00:21:3266 EXPECT_FALSE(result.error);
67 ASSERT_TRUE(result.value);
68 EXPECT_EQ(*expected_value, *result.value);
Jay Civelli90a4cb12017-11-28 21:04:5269 }
70
Jay Civelli90a4cb12017-11-28 21:04:5271 DISALLOW_COPY_AND_ASSIGN(SafeXmlParserTest);
72};
73
74} // namespace
75
76// Tests that SafeXmlParser does parse. (actual XML parsing is tested in the
77// service unit-tests).
78IN_PROC_BROWSER_TEST_F(SafeXmlParserTest, Parse) {
79 TestParse("[\"this is JSON not XML\"]", "");
Jay Civellied297652017-11-29 04:17:4680 TestParse(kTestXml, kTestJson);
Jay Civelli90a4cb12017-11-28 21:04:5281}