Sorin Jianu | 039032b | 2018-10-12 21:48:13 | [diff] [blame] | 1 | // Copyright 2016 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 <string> |
| 6 | |
| 7 | #include "components/update_client/protocol_serializer.h" |
| 8 | #include "components/update_client/updater_state.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "third_party/re2/src/re2/re2.h" |
| 11 | |
| 12 | using std::string; |
| 13 | |
| 14 | namespace update_client { |
| 15 | |
| 16 | TEST(SerializeRequest, Serialize) { |
| 17 | // When no updater state is provided, then check that the elements and |
| 18 | // attributes related to the updater state are not serialized. |
| 19 | const auto request = |
| 20 | ProtocolSerializer::Create()->Serialize(MakeProtocolRequest( |
| 21 | "15160585-8ADE-4D3C-839B-1281A6035D1F", "prod_id", "1.0", "lang", |
| 22 | "channel", "OS", "cacheable", {{"extra", "params"}}, nullptr, {})); |
| 23 | constexpr char regex[] = |
| 24 | R"(<\?xml version="1\.0" encoding="UTF-8"\?>)" |
| 25 | R"(<request protocol="3\.1" )" |
| 26 | R"(dedup="cr" acceptformat="crx2,crx3" extra="params" )" |
| 27 | R"(sessionid="{[-\w]{36}}" requestid="{[-\w]{36}}" )" |
| 28 | R"(updater="prod_id" updaterversion="1\.0" prodversion="1\.0" )" |
| 29 | R"(lang="lang" os="\w+" arch="\w+" nacl_arch="[-\w]+" (wow64="1" )?)" |
| 30 | R"(updaterchannel="channel" prodchannel="channel" dlpref="cacheable">)" |
| 31 | R"(<hw physmemory="[0-9]+"/>)" |
| 32 | R"(<os platform="OS" arch="[,-.\w]+" version="[-.\w]+"( sp="[\s\w]+")?/>)" |
| 33 | R"(</request>)"; |
| 34 | EXPECT_TRUE(RE2::FullMatch(request, regex)) << request; |
| 35 | } |
| 36 | |
| 37 | TEST(BuildProtocolRequest, DownloadPreference) { |
| 38 | // Verifies that an empty |download_preference| is not serialized. |
| 39 | const auto serializer = ProtocolSerializer::Create(); |
| 40 | auto request = serializer->Serialize( |
| 41 | MakeProtocolRequest("15160585-8ADE-4D3C-839B-1281A6035D1F", "", "", "", |
| 42 | "", "", "", {}, nullptr, {})); |
| 43 | EXPECT_FALSE(RE2::PartialMatch(request, " dlpref=")) << request; |
| 44 | |
| 45 | // Verifies that |download_preference| is serialized. |
| 46 | request = serializer->Serialize( |
| 47 | MakeProtocolRequest("15160585-8ADE-4D3C-839B-1281A6035D1F", "", "", "", |
| 48 | "", "", "cacheable", {}, nullptr, {})); |
| 49 | EXPECT_TRUE(RE2::PartialMatch(request, R"( dlpref="cacheable")")) << request; |
| 50 | } |
| 51 | |
| 52 | // When present, updater state attributes are only serialized for Google builds, |
| 53 | // except the |domainjoined| attribute, which is serialized in all cases. |
| 54 | TEST(BuildProtocolRequest, UpdaterStateAttributes) { |
| 55 | const auto serializer = ProtocolSerializer::Create(); |
| 56 | UpdaterState::Attributes attributes; |
| 57 | attributes["ismachine"] = "1"; |
| 58 | attributes["domainjoined"] = "1"; |
| 59 | attributes["name"] = "Omaha"; |
| 60 | attributes["version"] = "1.2.3.4"; |
| 61 | attributes["laststarted"] = "1"; |
| 62 | attributes["lastchecked"] = "2"; |
| 63 | attributes["autoupdatecheckenabled"] = "0"; |
| 64 | attributes["updatepolicy"] = "-1"; |
| 65 | const auto request = serializer->Serialize(MakeProtocolRequest( |
| 66 | "15160585-8ADE-4D3C-839B-1281A6035D1F", "prod_id", "1.0", "lang", |
| 67 | "channel", "OS", "cacheable", {{"extra", "params"}}, &attributes, {})); |
| 68 | constexpr char regex[] = |
| 69 | R"(<\?xml version="1\.0" encoding="UTF-8"\?>)" |
| 70 | R"(<request protocol="3\.1" )" |
| 71 | R"(dedup="cr" acceptformat="crx2,crx3" extra="params" )" |
| 72 | R"(sessionid="{[-\w]{36}}" requestid="{[-\w]{36}}" )" |
| 73 | R"(updater="prod_id" updaterversion="1\.0" prodversion="1\.0" )" |
| 74 | R"(lang="lang" os="\w+" arch="\w+" nacl_arch="[-\w]+" (wow64="1" )?)" |
| 75 | R"(updaterchannel="channel" prodchannel="channel" dlpref="cacheable" )" |
| 76 | R"(domainjoined="1">)" |
| 77 | R"(<hw physmemory="[0-9]+"/>)" |
| 78 | R"(<os platform="OS" arch="[,-.\w]+" version="[-.\w]+"( sp="[\s\w]+")?/>)" |
| 79 | #if defined(GOOGLE_CHROME_BUILD) |
| 80 | R"(<updater name="Omaha" version="1\.2\.3\.4" lastchecked="2" )" |
| 81 | R"(laststarted="1" ismachine="1" autoupdatecheckenabled="0" )" |
| 82 | R"(updatepolicy="-1"/>)" |
| 83 | #endif // GOOGLE_CHROME_BUILD |
| 84 | R"(</request>)"; |
| 85 | |
| 86 | EXPECT_TRUE(RE2::FullMatch(request, regex)) << request; |
| 87 | } |
| 88 | |
| 89 | } // namespace update_client |