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 | |
Jae Hoon Kim | 6ada591 | 2019-06-14 10:11:34 -0700 | [diff] [blame^] | 23 | #include <base/guid.h> |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
Jae Hoon Kim | 6ada591 | 2019-06-14 10:11:34 -0700 | [diff] [blame^] | 26 | #include "update_engine/fake_system_state.h" |
| 27 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 28 | using std::pair; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 29 | using std::string; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 30 | using std::vector; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
Jae Hoon Kim | 6ada591 | 2019-06-14 10:11:34 -0700 | [diff] [blame^] | 34 | namespace { |
| 35 | // Helper to find key and extract value from the given string |xml|, instead |
| 36 | // of using a full parser. The attribute key will be followed by "=\"" as xml |
| 37 | // attribute values must be within double quotes (not single quotes). |
| 38 | static string FindAttributeKeyValueInXml(const string& xml, |
| 39 | const string& key, |
| 40 | const size_t val_size) { |
| 41 | string key_with_quotes = key + "=\""; |
| 42 | const size_t val_start_pos = xml.find(key); |
| 43 | if (val_start_pos == string::npos) |
| 44 | return ""; |
| 45 | return xml.substr(val_start_pos + key_with_quotes.size(), val_size); |
| 46 | } |
| 47 | } // namespace |
| 48 | |
| 49 | class OmahaRequestBuilderXmlTest : public ::testing::Test { |
| 50 | protected: |
| 51 | void SetUp() override {} |
| 52 | void TearDown() override {} |
| 53 | |
| 54 | FakeSystemState fake_system_state_; |
| 55 | static constexpr size_t kGuidSize = 36; |
| 56 | }; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 57 | |
| 58 | TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeTest) { |
| 59 | string output; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 60 | vector<pair<string, string>> xml_encode_pairs = { |
| 61 | {"ab", "ab"}, |
| 62 | {"a<b", "a<b"}, |
| 63 | {"<&>\"\'\\", "<&>"'\\"}, |
| 64 | {"<&>", "&lt;&amp;&gt;"}}; |
| 65 | for (const auto& xml_encode_pair : xml_encode_pairs) { |
| 66 | const auto& before_encoding = xml_encode_pair.first; |
| 67 | const auto& after_encoding = xml_encode_pair.second; |
| 68 | EXPECT_TRUE(XmlEncode(before_encoding, &output)); |
| 69 | EXPECT_EQ(after_encoding, output); |
| 70 | } |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 71 | // Check that unterminated UTF-8 strings are handled properly. |
| 72 | EXPECT_FALSE(XmlEncode("\xc2", &output)); |
| 73 | // Fail with invalid ASCII-7 chars. |
| 74 | EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output)); |
| 75 | } |
| 76 | |
| 77 | TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeWithDefaultTest) { |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 78 | EXPECT_EQ("", XmlEncodeWithDefault("")); |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 79 | EXPECT_EQ("<&>", XmlEncodeWithDefault("<&>", "something else")); |
| 80 | EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>")); |
| 81 | } |
| 82 | |
Jae Hoon Kim | 6ada591 | 2019-06-14 10:11:34 -0700 | [diff] [blame^] | 83 | TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlRequestIdTest) { |
| 84 | OmahaEvent omaha_event; |
| 85 | OmahaRequestParams omaha_request_params{&fake_system_state_}; |
| 86 | OmahaRequestBuilderXml omaha_request{&omaha_event, |
| 87 | &omaha_request_params, |
| 88 | false, |
| 89 | false, |
| 90 | 0, |
| 91 | 0, |
| 92 | 0, |
| 93 | fake_system_state_.prefs()}; |
| 94 | const string request_xml = omaha_request.GetRequest(); |
| 95 | const string key = "requestid"; |
| 96 | const string request_id = |
| 97 | FindAttributeKeyValueInXml(request_xml, key, kGuidSize); |
| 98 | // A valid |request_id| is either a GUID version 4 or empty string. |
| 99 | if (!request_id.empty()) |
| 100 | EXPECT_TRUE(base::IsValidGUID(request_id)); |
| 101 | } |
| 102 | |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 103 | } // namespace chromeos_update_engine |