1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/component_updater/chrome_component_updater_configurator.h"
#include <memory>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/component_updater/component_updater_command_line_config_policy.h"
#include "components/component_updater/component_updater_switches.h"
#include "components/component_updater/component_updater_url_constants.h"
#include "components/component_updater/configurator_impl.h"
#include "components/prefs/testing_pref_service.h"
#include "components/update_client/configurator.h"
#include "components/update_client/update_client.h"
#include "components/update_client/update_query_params.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace component_updater {
class ChromeComponentUpdaterConfiguratorTest : public testing::Test {
protected:
// Overrides from testing::Test.
void SetUp() override;
TestingPrefServiceSimple* pref_service() { return pref_service_.get(); }
private:
base::test::TaskEnvironment environment_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
};
void ChromeComponentUpdaterConfiguratorTest::SetUp() {
pref_service_ = std::make_unique<TestingPrefServiceSimple>();
update_client::RegisterPrefs(pref_service_->registry());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestDisablePings) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
cmdline.AppendSwitchASCII(switches::kComponentUpdater, "disable-pings");
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
const std::vector<GURL> pingUrls = config->PingUrl();
EXPECT_TRUE(pingUrls.empty());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestFastUpdate) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
cmdline.AppendSwitchASCII(switches::kComponentUpdater, "fast-update");
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
CHECK_EQ(base::Seconds(10), config->InitialDelay());
CHECK_EQ(base::Hours(5), config->NextCheckDelay());
CHECK_EQ(base::Seconds(2), config->OnDemandDelay());
CHECK_EQ(base::Seconds(10), config->UpdateDelay());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestOverrideUrl) {
const char overrideUrl[] = "http://0.0.0.0/";
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
std::string val = "url-source";
val.append("=");
val.append(overrideUrl);
cmdline.AppendSwitchASCII(switches::kComponentUpdater, val.c_str());
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
const std::vector<GURL> urls = config->UpdateUrl();
ASSERT_EQ(1U, urls.size());
ASSERT_EQ(overrideUrl, urls.at(0).possibly_invalid_spec());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestSwitchRequestParam) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
cmdline.AppendSwitchASCII(switches::kComponentUpdater, "test-request");
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
EXPECT_FALSE(config->ExtraRequestParams().empty());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestUpdaterDefaultUrl) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
const auto urls = config->UpdateUrl();
// Expect the default url to be cryptographically secure.
EXPECT_GE(urls.size(), 1u);
EXPECT_TRUE(urls.front().SchemeIsCryptographic());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestEnabledCupSigning) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
EXPECT_TRUE(config->EnabledCupSigning());
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestUseEncryption) {
base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
{
const auto config(
MakeChromeComponentUpdaterConfigurator(cmdline, pref_service()));
const auto urls = config->UpdateUrl();
ASSERT_EQ(2u, urls.size());
ASSERT_STREQ(kUpdaterJSONDefaultUrl, urls[0].spec().c_str());
ASSERT_STREQ(kUpdaterJSONFallbackUrl, urls[1].spec().c_str());
ASSERT_EQ(config->UpdateUrl(), config->PingUrl());
}
// Use the configurator implementation to test the filtering of
// unencrypted URLs.
{
const ConfiguratorImpl config(
ComponentUpdaterCommandLineConfigPolicy(cmdline), true);
const auto urls = config.UpdateUrl();
ASSERT_EQ(1u, urls.size());
ASSERT_STREQ(kUpdaterJSONDefaultUrl, urls[0].spec().c_str());
ASSERT_EQ(config.UpdateUrl(), config.PingUrl());
}
{
const ConfiguratorImpl config(
ComponentUpdaterCommandLineConfigPolicy(cmdline), false);
const auto urls = config.UpdateUrl();
ASSERT_EQ(2u, urls.size());
ASSERT_STREQ(kUpdaterJSONDefaultUrl, urls[0].spec().c_str());
ASSERT_STREQ(kUpdaterJSONFallbackUrl, urls[1].spec().c_str());
ASSERT_EQ(config.UpdateUrl(), config.PingUrl());
}
}
TEST_F(ChromeComponentUpdaterConfiguratorTest, TestProdId) {
base::CommandLine cmdline(*base::CommandLine::ForCurrentProcess());
const auto config(
MakeChromeComponentUpdaterConfigurator(&cmdline, pref_service()));
EXPECT_STREQ(update_client::UpdateQueryParams::GetProdIdString(
update_client::UpdateQueryParams::ProdId::CHROME),
config->GetProdId().c_str());
}
} // namespace component_updater
|