Commit e44132fd authored by Maksim Ivanov's avatar Maksim Ivanov Committed by Commit Bot

Fix KeyRotationDeviceCloudPolicyTest.Basic

Fix a few errors in the KeyRotationDeviceCloudPolicyTest.Basic browser
test - some of them were presumably causing the flakiness that led to
the test being disabled, some of them were accidentally added during
later refactorings:

1. Correctly pipe through LocalPolicyTestServerMixin the needed settings
   of policy_testserver: the usage of signing keys bundled into the
   Python server and the automatic rotation of the signing key with
   every policy request.
2. Fix misplaced calls in the test's SetUp() phase: everything needs to
   be done *before* calling the parent SetUp() in order to run before
   the test.
3. Make the test less fragile by doing two explicit fetches of policy
   inside the test body and checking that the key got rotated in
   between (instead of relying on the device policy stack making the
   first policy fetch during its initialization and on the right parity
   of the PolicyBuilder's policy key version).

Bug: 900631
Test: run the test 10'000 times (browser_tests --gtest_filter="KeyRotationDeviceCloudPolicyTest.Basic")
Change-Id: I5eaa4f6e03a54dcc26319d39323f5edf51904c65
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829383
Commit-Queue: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarDenis Kuznetsov <antrim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702976}
parent 97e992ae
...@@ -51,9 +51,14 @@ void LocalPolicyTestServerMixin::SetUp() { ...@@ -51,9 +51,14 @@ void LocalPolicyTestServerMixin::SetUp() {
policy::PolicyBuilder::kFakeDeviceId, policy::PolicyBuilder::kFakeDeviceId,
{} /* state_keys */); {} /* state_keys */);
CHECK(policy_test_server_->SetSigningKeyAndSignature( if (!canned_signing_keys_enabled_) {
policy::PolicyBuilder::CreateTestSigningKey().get(), CHECK(policy_test_server_->SetSigningKeyAndSignature(
policy::PolicyBuilder::GetTestSigningKeySignature())); policy::PolicyBuilder::CreateTestSigningKey().get(),
policy::PolicyBuilder::GetTestSigningKeySignature()));
}
if (automatic_rotation_of_signing_keys_enabled_)
policy_test_server_->EnableAutomaticRotationOfSigningKeys();
CHECK(policy_test_server_->Start()); CHECK(policy_test_server_->Start());
} }
...@@ -257,6 +262,16 @@ void LocalPolicyTestServerMixin::ConfigureFakeStatisticsForZeroTouch( ...@@ -257,6 +262,16 @@ void LocalPolicyTestServerMixin::ConfigureFakeStatisticsForZeroTouch(
test::kTestHardwareClass); test::kTestHardwareClass);
} }
void LocalPolicyTestServerMixin::EnableCannedSigningKeys() {
DCHECK(!policy_test_server_);
canned_signing_keys_enabled_ = true;
}
void LocalPolicyTestServerMixin::EnableAutomaticRotationOfSigningKeys() {
DCHECK(!policy_test_server_);
automatic_rotation_of_signing_keys_enabled_ = true;
}
LocalPolicyTestServerMixin::~LocalPolicyTestServerMixin() = default; LocalPolicyTestServerMixin::~LocalPolicyTestServerMixin() = default;
} // namespace chromeos } // namespace chromeos
...@@ -112,9 +112,21 @@ class LocalPolicyTestServerMixin : public InProcessBrowserTestMixin { ...@@ -112,9 +112,21 @@ class LocalPolicyTestServerMixin : public InProcessBrowserTestMixin {
void ConfigureFakeStatisticsForZeroTouch( void ConfigureFakeStatisticsForZeroTouch(
system::ScopedFakeStatisticsProvider* provider); system::ScopedFakeStatisticsProvider* provider);
// Enables the usage of keys canned into the policy test server, instead of
// specifying to it the test key to be used. This must be called before
// starting the server.
void EnableCannedSigningKeys();
// Enables the automatic rotation of the policy signing keys with each policy
// fetch request. This must be called before starting the server, and only
// works when the server serves from a temporary directory.
void EnableAutomaticRotationOfSigningKeys();
private: private:
std::unique_ptr<policy::LocalPolicyTestServer> policy_test_server_; std::unique_ptr<policy::LocalPolicyTestServer> policy_test_server_;
base::Value server_config_; base::Value server_config_;
bool canned_signing_keys_enabled_ = false;
bool automatic_rotation_of_signing_keys_enabled_ = false;
DISALLOW_COPY_AND_ASSIGN(LocalPolicyTestServerMixin); DISALLOW_COPY_AND_ASSIGN(LocalPolicyTestServerMixin);
}; };
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "chrome/browser/chromeos/login/test/local_policy_test_server_mixin.h" #include "chrome/browser/chromeos/login/test/local_policy_test_server_mixin.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
#include "chrome/browser/chromeos/policy/device_policy_builder.h" #include "chrome/browser/chromeos/policy/device_policy_builder.h"
#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
...@@ -101,30 +102,25 @@ namespace { ...@@ -101,30 +102,25 @@ namespace {
// rotating the policy key automatically with each policy fetch. // rotating the policy key automatically with each policy fetch.
class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
protected: protected:
const int kTestPolicyValue = 123; const int kInitialPolicyValue = 123;
const int kTestPolicyOtherValue = 456; const int kSecondPolicyValue = 456;
const char* const kTestPolicyKey = key::kDevicePolicyRefreshRate; const int kThirdPolicyValue = 789;
const char* const kPolicyKey = key::kDevicePolicyRefreshRate;
KeyRotationDeviceCloudPolicyTest() {}
KeyRotationDeviceCloudPolicyTest() {
void SetUp() override { UpdateBuiltTestPolicyValue(kInitialPolicyValue);
UpdateBuiltTestPolicyValue(kTestPolicyValue); local_policy_mixin_.EnableCannedSigningKeys();
DevicePolicyCrosBrowserTest::SetUp(); local_policy_mixin_.EnableAutomaticRotationOfSigningKeys();
local_policy_mixin_.server()->EnableAutomaticRotationOfSigningKeys();
UpdateServedTestPolicy();
} }
void SetUpInProcessBrowserTestFixture() override { void SetUpInProcessBrowserTestFixture() override {
DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture(); DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
SetFakeDevicePolicy(); SetFakeDevicePolicy();
UpdateServedTestPolicy();
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
DevicePolicyCrosBrowserTest::SetUpOnMainThread(); DevicePolicyCrosBrowserTest::SetUpOnMainThread();
g_browser_process->platform_part()
->browser_policy_connector_chromeos()
->device_management_service()
->ScheduleInitialization(0);
StartObservingTestPolicy(); StartObservingTestPolicy();
} }
...@@ -146,7 +142,27 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { ...@@ -146,7 +142,27 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
local_policy_mixin_.UpdateDevicePolicy(device_policy()->payload())); local_policy_mixin_.UpdateDevicePolicy(device_policy()->payload()));
} }
int GetTestPolicyValue() { void StartDevicePolicyRefresh() {
g_browser_process->platform_part()
->browser_policy_connector_chromeos()
->GetDeviceCloudPolicyManager()
->RefreshPolicies();
}
std::string GetOwnerPublicKey() const {
return chromeos::DeviceSettingsService::Get()->GetPublicKey()->as_string();
}
int GetInstalledPolicyKeyVersion() const {
return g_browser_process->platform_part()
->browser_policy_connector_chromeos()
->GetDeviceCloudPolicyManager()
->device_store()
->policy()
->public_key_version();
}
int GetInstalledPolicyValue() {
PolicyService* const policy_service = PolicyService* const policy_service =
g_browser_process->platform_part() g_browser_process->platform_part()
->browser_policy_connector_chromeos() ->browser_policy_connector_chromeos()
...@@ -155,22 +171,23 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { ...@@ -155,22 +171,23 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
policy_service policy_service
->GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME, ->GetPolicies(PolicyNamespace(POLICY_DOMAIN_CHROME,
std::string() /* component_id */)) std::string() /* component_id */))
.GetValue(kTestPolicyKey); .GetValue(kPolicyKey);
EXPECT_TRUE(policy_value); EXPECT_TRUE(policy_value);
int refresh_rate = -1; int refresh_rate = -1;
EXPECT_TRUE(policy_value->GetAsInteger(&refresh_rate)); EXPECT_TRUE(policy_value->GetAsInteger(&refresh_rate));
return refresh_rate; return refresh_rate;
} }
void WaitForTestPolicyValue(int expected_policy_value) { void WaitForInstalledPolicyValue(int expected_policy_value) {
if (GetTestPolicyValue() == expected_policy_value) if (GetInstalledPolicyValue() == expected_policy_value)
return; return;
awaited_policy_value_ = expected_policy_value; awaited_policy_value_ = expected_policy_value;
// The run loop will be terminated by TestPolicyChangedCallback() once the // The run loop will be terminated by OnPolicyChanged() once the policy
// policy value becomes equal to the awaited value. // value becomes equal to the awaited value.
DCHECK(!policy_change_waiting_run_loop_); DCHECK(!policy_change_waiting_run_loop_);
policy_change_waiting_run_loop_ = std::make_unique<base::RunLoop>(); policy_change_waiting_run_loop_ = std::make_unique<base::RunLoop>();
policy_change_waiting_run_loop_->Run(); policy_change_waiting_run_loop_->Run();
policy_change_waiting_run_loop_.reset();
} }
private: private:
...@@ -178,7 +195,7 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { ...@@ -178,7 +195,7 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
device_policy() device_policy()
->payload() ->payload()
.mutable_device_policy_refresh_rate() .mutable_device_policy_refresh_rate()
->set_device_policy_refresh_rate(kTestPolicyValue); ->set_device_policy_refresh_rate(kInitialPolicyValue);
device_policy()->Build(); device_policy()->Build();
session_manager_client()->set_device_policy(device_policy()->GetBlob()); session_manager_client()->set_device_policy(device_policy()->GetBlob());
} }
...@@ -191,16 +208,15 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { ...@@ -191,16 +208,15 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
PolicyNamespace(POLICY_DOMAIN_CHROME, PolicyNamespace(POLICY_DOMAIN_CHROME,
std::string() /* component_id */)); std::string() /* component_id */));
policy_change_registrar_->Observe( policy_change_registrar_->Observe(
kTestPolicyKey, kPolicyKey,
base::BindRepeating( base::BindRepeating(&KeyRotationDeviceCloudPolicyTest::OnPolicyChanged,
&KeyRotationDeviceCloudPolicyTest::TestPolicyChangedCallback, base::Unretained(this)));
base::Unretained(this)));
} }
void TestPolicyChangedCallback(const base::Value* old_value, void OnPolicyChanged(const base::Value* old_value,
const base::Value* new_value) { const base::Value* new_value) {
if (policy_change_waiting_run_loop_ && if (policy_change_waiting_run_loop_ &&
GetTestPolicyValue() == awaited_policy_value_) { GetInstalledPolicyValue() == awaited_policy_value_) {
policy_change_waiting_run_loop_->Quit(); policy_change_waiting_run_loop_->Quit();
} }
} }
...@@ -215,30 +231,35 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest { ...@@ -215,30 +231,35 @@ class KeyRotationDeviceCloudPolicyTest : public DevicePolicyCrosBrowserTest {
} // namespace } // namespace
// Disabled due to flake. https://crbug.com/900631 IN_PROC_BROWSER_TEST_F(KeyRotationDeviceCloudPolicyTest, Basic) {
IN_PROC_BROWSER_TEST_F(KeyRotationDeviceCloudPolicyTest, DISABLED_Basic) { // The policy has the initial value from the cache.
// Initially, the policy has the first value. EXPECT_EQ(kInitialPolicyValue, GetInstalledPolicyValue());
EXPECT_EQ(kTestPolicyValue, GetTestPolicyValue());
const std::string original_owner_public_key = // The server is updated to serve the second policy value, and the client
chromeos::DeviceSettingsService::Get()->GetPublicKey()->as_string(); // fetches it.
UpdateBuiltTestPolicyValue(kSecondPolicyValue);
// The server is updated to serve the new policy value, and the client fetches UpdateServedTestPolicy();
// it. StartDevicePolicyRefresh();
UpdateBuiltTestPolicyValue(kTestPolicyOtherValue); WaitForInstalledPolicyValue(kSecondPolicyValue);
EXPECT_EQ(kSecondPolicyValue, GetInstalledPolicyValue());
// Remember the key and the version after the fetch of the second value.
const std::string owner_public_key = GetOwnerPublicKey();
CHECK(!owner_public_key.empty());
const int key_version = GetInstalledPolicyKeyVersion();
// The server is updated to serve the third policy value, and the client
// fetches it.
UpdateBuiltTestPolicyValue(kThirdPolicyValue);
UpdateServedTestPolicy(); UpdateServedTestPolicy();
g_browser_process->platform_part() StartDevicePolicyRefresh();
->browser_policy_connector_chromeos() WaitForInstalledPolicyValue(kThirdPolicyValue);
->GetDeviceCloudPolicyManager() EXPECT_EQ(kThirdPolicyValue, GetInstalledPolicyValue());
->RefreshPolicies();
WaitForTestPolicyValue(kTestPolicyOtherValue); // The owner key got rotated on the client, as requested by the server, and
EXPECT_EQ(kTestPolicyOtherValue, GetTestPolicyValue()); // the key version got incremented.
EXPECT_NE(owner_public_key, GetOwnerPublicKey());
// The owner key has changed due to the key rotation performed by the policy EXPECT_EQ(key_version + 1, GetInstalledPolicyKeyVersion());
// test server.
EXPECT_NE(
original_owner_public_key,
chromeos::DeviceSettingsService::Get()->GetPublicKey()->as_string());
} }
namespace { namespace {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment