Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 1 | // |
| 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 Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 20 | #include <utility> |
| 21 | #include <vector> |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 25 | using std::pair; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 26 | using std::string; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 27 | using std::vector; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | class OmahaRequestBuilderXmlTest : public ::testing::Test {}; |
| 32 | |
| 33 | TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeTest) { |
| 34 | string output; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 35 | vector<pair<string, string>> xml_encode_pairs = { |
| 36 | {"ab", "ab"}, |
| 37 | {"a<b", "a<b"}, |
| 38 | {"<&>\"\'\\", "<&>"'\\"}, |
| 39 | {"<&>", "&lt;&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 Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 46 | // 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 | |
| 52 | TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeWithDefaultTest) { |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 53 | EXPECT_EQ("", XmlEncodeWithDefault("")); |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 54 | EXPECT_EQ("<&>", XmlEncodeWithDefault("<&>", "something else")); |
| 55 | EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>")); |
| 56 | } |
| 57 | |
| 58 | } // namespace chromeos_update_engine |