blob: 5c37571e790340e109c924dfd42f3caf356b1573 [file] [log] [blame]
Amin Hassani7fca2862019-03-28 16:09:22 -07001//
2// Copyright (C) 2019 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/omaha_request_builder_xml.h"
18
19#include <string>
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070020#include <utility>
21#include <vector>
Amin Hassani7fca2862019-03-28 16:09:22 -070022
23#include <gtest/gtest.h>
24
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070025using std::pair;
Amin Hassani7fca2862019-03-28 16:09:22 -070026using std::string;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070027using std::vector;
Amin Hassani7fca2862019-03-28 16:09:22 -070028
29namespace chromeos_update_engine {
30
31class OmahaRequestBuilderXmlTest : public ::testing::Test {};
32
33TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeTest) {
34 string output;
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070035 vector<pair<string, string>> xml_encode_pairs = {
36 {"ab", "ab"},
37 {"a<b", "a&lt;b"},
38 {"<&>\"\'\\", "&lt;&amp;&gt;&quot;&apos;\\"},
39 {"&lt;&amp;&gt;", "&amp;lt;&amp;amp;&amp;gt;"}};
40 for (const auto& xml_encode_pair : xml_encode_pairs) {
41 const auto& before_encoding = xml_encode_pair.first;
42 const auto& after_encoding = xml_encode_pair.second;
43 EXPECT_TRUE(XmlEncode(before_encoding, &output));
44 EXPECT_EQ(after_encoding, output);
45 }
Amin Hassani7fca2862019-03-28 16:09:22 -070046 // Check that unterminated UTF-8 strings are handled properly.
47 EXPECT_FALSE(XmlEncode("\xc2", &output));
48 // Fail with invalid ASCII-7 chars.
49 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
50}
51
52TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeWithDefaultTest) {
Jae Hoon Kimb7ee3872019-06-06 14:59:03 -070053 EXPECT_EQ("", XmlEncodeWithDefault(""));
Amin Hassani7fca2862019-03-28 16:09:22 -070054 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
55 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
56}
57
58} // namespace chromeos_update_engine