Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 1 | // 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 Kasting | 9eb10b0 | 2021-04-29 03:10:48 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
Ken Rockot | 315def7 | 2018-11-10 02:16:10 | [diff] [blame] | 12 | #include "base/token.h" |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 13 | #include "base/values.h" |
| 14 | #include "chrome/test/base/in_process_browser_test.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 15 | #include "content/public/test/browser_test.h" |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 16 | #include "content/public/test/test_utils.h" |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 17 | #include "services/data_decoder/public/cpp/data_decoder.h" |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 18 | #include "services/data_decoder/public/cpp/safe_xml_parser.h" |
Ken Rockot | 19db638 | 2018-02-10 01:41:02 | [diff] [blame] | 19 | #include "services/data_decoder/public/mojom/xml_parser.mojom.h" |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
Jay Civelli | ed29765 | 2017-11-29 04:17:46 | [diff] [blame] | 23 | constexpr char kTestXml[] = "<hello>bonjour</hello>"; |
| 24 | constexpr char kTestJson[] = R"( |
| 25 | {"type": "element", |
| 26 | "tag": "hello", |
| 27 | "children": [{"type": "text", "text": "bonjour"}] |
| 28 | } )"; |
| 29 | |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 30 | class SafeXmlParserTest : public InProcessBrowserTest { |
| 31 | public: |
| 32 | SafeXmlParserTest() = default; |
| 33 | ~SafeXmlParserTest() override = default; |
| 34 | |
| 35 | protected: |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 36 | // Parses |xml| and compares its parsed representation with |expected_json|. |
| 37 | // If |expected_json| is empty, the XML parsing is expected to fail. |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 38 | void TestParse(base::StringPiece xml, const std::string& expected_json) { |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 39 | SCOPED_TRACE(xml); |
| 40 | |
| 41 | base::RunLoop run_loop; |
| 42 | std::unique_ptr<base::Value> expected_value; |
| 43 | if (!expected_json.empty()) { |
Lei Zhang | 582ecd1 | 2019-02-13 20:28:54 | [diff] [blame] | 44 | expected_value = base::JSONReader::ReadDeprecated(expected_json); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 45 | DCHECK(expected_value) << "Bad test, incorrect JSON: " << expected_json; |
| 46 | } |
| 47 | |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 48 | data_decoder::DataDecoder::ParseXmlIsolated( |
Peter Kasting | 9eb10b0 | 2021-04-29 03:10:48 | [diff] [blame] | 49 | std::string(xml), |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 50 | base::BindOnce(&SafeXmlParserTest::XmlParsingDone, |
| 51 | base::Unretained(this), run_loop.QuitClosure(), |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 52 | std::move(expected_value))); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 53 | run_loop.Run(); |
| 54 | } |
| 55 | |
| 56 | private: |
Alexander Cooper | bc87af3 | 2020-07-15 15:59:45 | [diff] [blame] | 57 | void XmlParsingDone(base::OnceClosure quit_loop_closure, |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 58 | std::unique_ptr<base::Value> expected_value, |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 59 | data_decoder::DataDecoder::ValueOrError result) { |
Andres Calderon Jaramillo | a045907 | 2018-05-03 18:59:29 | [diff] [blame] | 60 | base::ScopedClosureRunner runner(std::move(quit_loop_closure)); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 61 | if (!expected_value) { |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 62 | EXPECT_FALSE(result.value); |
| 63 | EXPECT_TRUE(result.error); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 64 | return; |
| 65 | } |
Ken Rockot | bf644009 | 2019-11-02 00:21:32 | [diff] [blame] | 66 | EXPECT_FALSE(result.error); |
| 67 | ASSERT_TRUE(result.value); |
| 68 | EXPECT_EQ(*expected_value, *result.value); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 69 | } |
| 70 | |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 71 | 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). |
| 78 | IN_PROC_BROWSER_TEST_F(SafeXmlParserTest, Parse) { |
| 79 | TestParse("[\"this is JSON not XML\"]", ""); |
Jay Civelli | ed29765 | 2017-11-29 04:17:46 | [diff] [blame] | 80 | TestParse(kTestXml, kTestJson); |
Jay Civelli | 90a4cb1 | 2017-11-28 21:04:52 | [diff] [blame] | 81 | } |