This source file includes following definitions.
- StartInvalidator
- StorePolicy
- StorePolicy
- StorePolicy
- policy_value_cur_
- SetUp
- TearDown
- StartInvalidator
- InitializeInvalidator
- ShutdownInvalidator
- DestroyInvalidator
- ConnectCore
- StartRefreshScheduler
- DisconnectCore
- StorePolicy
- DisableInvalidationService
- EnableInvalidationService
- FireInvalidation
- FireUnknownVersionInvalidation
- CheckInvalidationInfo
- CheckPolicyNotRefreshed
- CheckPolicyRefreshed
- IsUnsent
- CheckPolicyRefreshedWithUnknownVersion
- InvalidationsEnabled
- IsInvalidationAcknowledged
- IsInvalidatorRegistered
- GetCount
- GetInvalidationCount
- AdvanceClock
- CheckPolicyRefreshed
- CheckPolicyRefreshCount
- GetPolicyObjectId
- GetHistogramSamples
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/sample_map.h"
#include "base/metrics/statistics_recorder.h"
#include "base/run_loop.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/invalidation/fake_invalidation_service.h"
#include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/cloud_policy_core.h"
#include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
#include "components/policy/core/common/cloud/enterprise_metrics.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/policy_types.h"
#include "policy/policy_constants.h"
#include "policy/proto/device_management_backend.pb.h"
#include "sync/notifier/invalidation_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
class CloudPolicyInvalidatorTest : public testing::Test {
protected:
enum PolicyObject {
POLICY_OBJECT_NONE,
POLICY_OBJECT_A,
POLICY_OBJECT_B
};
CloudPolicyInvalidatorTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
void StartInvalidator(bool initialize, bool start_refresh_scheduler);
void StartInvalidator() {
StartInvalidator(true , true );
}
void InitializeInvalidator();
void ShutdownInvalidator();
void DestroyInvalidator();
void ConnectCore();
void StartRefreshScheduler();
void DisconnectCore();
void StorePolicy(
PolicyObject object,
int64 invalidation_version,
bool policy_changed,
int64 timestamp);
void StorePolicy(
PolicyObject object,
int64 invalidation_version,
bool policy_changed) {
StorePolicy(object, invalidation_version, policy_changed, ++timestamp_);
}
void StorePolicy(PolicyObject object, int64 invalidation_version) {
StorePolicy(object, invalidation_version, false);
}
void StorePolicy(PolicyObject object) {
StorePolicy(object, 0);
}
void DisableInvalidationService();
void EnableInvalidationService();
syncer::Invalidation FireInvalidation(
PolicyObject object,
int64 version,
const std::string& payload);
syncer::Invalidation FireUnknownVersionInvalidation(PolicyObject object);
bool CheckInvalidationInfo(int64 version, const std::string& payload);
bool CheckPolicyNotRefreshed();
bool CheckPolicyRefreshed();
bool CheckPolicyRefreshedWithUnknownVersion();
bool IsUnsent(const syncer::Invalidation& invalidation);
bool InvalidationsEnabled();
bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation);
bool IsInvalidatorRegistered();
base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
base::HistogramBase::Count GetInvalidationCount(bool with_payload);
void AdvanceClock(base::TimeDelta delta);
private:
bool CheckPolicyRefreshed(base::TimeDelta delay);
bool CheckPolicyRefreshCount(int count);
const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
scoped_ptr<base::HistogramSamples> GetHistogramSamples(
const std::string& name) const;
base::MessageLoop loop_;
invalidation::FakeInvalidationService invalidation_service_;
MockCloudPolicyStore store_;
CloudPolicyCore core_;
MockCloudPolicyClient* client_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::SimpleTestClock* clock_;
scoped_ptr<CloudPolicyInvalidator> invalidator_;
invalidation::ObjectId object_id_a_;
invalidation::ObjectId object_id_b_;
int64 timestamp_;
const char* policy_value_a_;
const char* policy_value_b_;
const char* policy_value_cur_;
scoped_ptr<base::HistogramSamples> refresh_samples_;
scoped_ptr<base::HistogramSamples> invalidations_samples_;
};
CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
: core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
std::string()),
&store_,
loop_.message_loop_proxy()),
client_(NULL),
task_runner_(new base::TestSimpleTaskRunner()),
clock_(new base::SimpleTestClock()),
object_id_a_(135, "asdf"),
object_id_b_(246, "zxcv"),
timestamp_(123456),
policy_value_a_("asdf"),
policy_value_b_("zxcv"),
policy_value_cur_(policy_value_a_) {}
void CloudPolicyInvalidatorTest::SetUp() {
base::StatisticsRecorder::Initialize();
refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
}
void CloudPolicyInvalidatorTest::TearDown() {
if (invalidator_)
invalidator_->Shutdown();
core_.Disconnect();
}
void CloudPolicyInvalidatorTest::StartInvalidator(
bool initialize,
bool start_refresh_scheduler) {
invalidator_.reset(new CloudPolicyInvalidator(
&core_,
task_runner_,
scoped_ptr<base::Clock>(clock_)));
if (start_refresh_scheduler) {
ConnectCore();
StartRefreshScheduler();
}
if (initialize)
InitializeInvalidator();
}
void CloudPolicyInvalidatorTest::InitializeInvalidator() {
invalidator_->Initialize(&invalidation_service_);
}
void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
invalidator_->Shutdown();
}
void CloudPolicyInvalidatorTest::DestroyInvalidator() {
invalidator_.reset();
}
void CloudPolicyInvalidatorTest::ConnectCore() {
client_ = new MockCloudPolicyClient();
client_->SetDMToken("dm");
core_.Connect(scoped_ptr<CloudPolicyClient>(client_));
}
void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
core_.StartRefreshScheduler();
}
void CloudPolicyInvalidatorTest::DisconnectCore() {
client_ = NULL;
core_.Disconnect();
}
void CloudPolicyInvalidatorTest::StorePolicy(
PolicyObject object,
int64 invalidation_version,
bool policy_changed,
int64 timestamp) {
enterprise_management::PolicyData* data =
new enterprise_management::PolicyData();
if (object != POLICY_OBJECT_NONE) {
data->set_invalidation_source(GetPolicyObjectId(object).source());
data->set_invalidation_name(GetPolicyObjectId(object).name());
}
data->set_timestamp(timestamp);
if (policy_changed)
policy_value_cur_ = policy_value_cur_ == policy_value_a_ ?
policy_value_b_ : policy_value_a_;
data->set_policy_value(policy_value_cur_);
store_.invalidation_version_ = invalidation_version;
store_.policy_.reset(data);
base::DictionaryValue policies;
policies.SetInteger(
key::kMaxInvalidationFetchDelay,
CloudPolicyInvalidator::kMaxFetchDelayMin);
store_.policy_map_.LoadFrom(
&policies,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_MACHINE);
store_.NotifyStoreLoaded();
}
void CloudPolicyInvalidatorTest::DisableInvalidationService() {
invalidation_service_.SetInvalidatorState(
syncer::TRANSIENT_INVALIDATION_ERROR);
}
void CloudPolicyInvalidatorTest::EnableInvalidationService() {
invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED);
}
syncer::Invalidation CloudPolicyInvalidatorTest::FireInvalidation(
PolicyObject object,
int64 version,
const std::string& payload) {
syncer::Invalidation invalidation = syncer::Invalidation::Init(
GetPolicyObjectId(object),
version,
payload);
invalidation_service_.EmitInvalidationForTest(invalidation);
return invalidation;
}
syncer::Invalidation CloudPolicyInvalidatorTest::FireUnknownVersionInvalidation(
PolicyObject object) {
syncer::Invalidation invalidation = syncer::Invalidation::InitUnknownVersion(
GetPolicyObjectId(object));
invalidation_service_.EmitInvalidationForTest(invalidation);
return invalidation;
}
bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
int64 version,
const std::string& payload) {
MockCloudPolicyClient* client =
static_cast<MockCloudPolicyClient*>(core_.client());
return version == client->invalidation_version_ &&
payload == client->invalidation_payload_;
}
bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
return CheckPolicyRefreshCount(0);
}
bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed() {
return CheckPolicyRefreshed(base::TimeDelta());
}
bool CloudPolicyInvalidatorTest::IsUnsent(
const syncer::Invalidation& invalidation) {
return invalidation_service_.GetMockAckHandler()->IsUnsent(invalidation);
}
bool CloudPolicyInvalidatorTest::CheckPolicyRefreshedWithUnknownVersion() {
return CheckPolicyRefreshed(base::TimeDelta::FromMinutes(
CloudPolicyInvalidator::kMissingPayloadDelay));
}
bool CloudPolicyInvalidatorTest::InvalidationsEnabled() {
return core_.refresh_scheduler()->invalidations_available();
}
bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
const syncer::Invalidation& invalidation) {
loop_.RunUntilIdle();
EXPECT_FALSE(IsUnsent(invalidation));
return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation);
}
bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
return !invalidation_service_.invalidator_registrar()
.GetRegisteredIds(invalidator_.get()).empty();
}
base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount(
MetricPolicyRefresh metric) {
return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) -
refresh_samples_->GetCount(metric);
}
base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount(
bool with_payload) {
int metric = with_payload ? 1 : 0;
return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) -
invalidations_samples_->GetCount(metric);
}
void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) {
clock_->Advance(delta);
}
bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
CloudPolicyInvalidator::kMaxFetchDelayMin);
if (task_runner_->GetPendingTasks().empty())
return false;
base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
EXPECT_GE(actual_delay, delay);
EXPECT_LE(actual_delay, max_delay);
return CheckPolicyRefreshCount(1);
}
bool CloudPolicyInvalidatorTest::CheckPolicyRefreshCount(int count) {
if (!client_) {
task_runner_->RunUntilIdle();
return count == 0;
}
EXPECT_CALL(*client_, FetchPolicy()).Times(testing::AnyNumber());
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(client_);
EXPECT_CALL(*client_, FetchPolicy()).Times(count);
task_runner_->RunUntilIdle();
base::RunLoop().RunUntilIdle();
return testing::Mock::VerifyAndClearExpectations(client_);
}
const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
PolicyObject object) const {
EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
}
scoped_ptr<base::HistogramSamples>
CloudPolicyInvalidatorTest::GetHistogramSamples(
const std::string& name) const {
base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(name);
if (!histogram)
return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
return histogram->SnapshotSamples();
}
TEST_F(CloudPolicyInvalidatorTest, Uninitialized) {
StartInvalidator(false , true );
StorePolicy(POLICY_OBJECT_A);
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
}
TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) {
StartInvalidator(true , false );
StorePolicy(POLICY_OBJECT_A);
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
}
TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) {
StartInvalidator(false , true );
DisconnectCore();
InitializeInvalidator();
StorePolicy(POLICY_OBJECT_A);
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
}
TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) {
StartInvalidator(true , false );
ConnectCore();
StartRefreshScheduler();
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(IsInvalidatorRegistered());
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
}
TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
StartInvalidator();
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_FALSE(InvalidationsEnabled());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
StorePolicy(POLICY_OBJECT_NONE);
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_FALSE(InvalidationsEnabled());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(IsInvalidatorRegistered());
EXPECT_TRUE(InvalidationsEnabled());
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
}
TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
StartInvalidator();
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(IsInvalidatorRegistered());
EXPECT_TRUE(InvalidationsEnabled());
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
StorePolicy(POLICY_OBJECT_B);
EXPECT_TRUE(IsInvalidatorRegistered());
EXPECT_TRUE(InvalidationsEnabled());
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(CheckPolicyNotRefreshed());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
FireUnknownVersionInvalidation(POLICY_OBJECT_B);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
}
TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
StartInvalidator();
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(IsInvalidatorRegistered());
EXPECT_TRUE(InvalidationsEnabled());
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_FALSE(IsInvalidationAcknowledged(inv));
StorePolicy(POLICY_OBJECT_NONE);
EXPECT_FALSE(IsInvalidatorRegistered());
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_FALSE(InvalidationsEnabled());
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
EXPECT_TRUE(CheckPolicyNotRefreshed());
StorePolicy(POLICY_OBJECT_B);
EXPECT_TRUE(IsInvalidatorRegistered());
EXPECT_TRUE(InvalidationsEnabled());
FireUnknownVersionInvalidation(POLICY_OBJECT_B);
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
}
TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
EXPECT_TRUE(InvalidationsEnabled());
syncer::Invalidation inv =
FireInvalidation(POLICY_OBJECT_A, 12, "test_payload");
EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
EXPECT_TRUE(CheckPolicyRefreshed());
EXPECT_FALSE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload"));
StorePolicy(POLICY_OBJECT_A, 12);
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
}
TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
EXPECT_FALSE(IsInvalidationAcknowledged(inv));
StorePolicy(POLICY_OBJECT_A, -1);
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
}
TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1");
EXPECT_TRUE(CheckInvalidationInfo(1, "test1"));
syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2");
EXPECT_TRUE(CheckInvalidationInfo(2, "test2"));
syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, 3, "test3");
EXPECT_TRUE(CheckInvalidationInfo(3, "test3"));
EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
EXPECT_TRUE(CheckPolicyRefreshed());
StorePolicy(POLICY_OBJECT_A, 1);
EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
StorePolicy(POLICY_OBJECT_A, 2);
EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
StorePolicy(POLICY_OBJECT_A, 3);
EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
}
TEST_F(CloudPolicyInvalidatorTest,
HandleMultipleInvalidationsWithUnknownVersion) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv1 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
syncer::Invalidation inv2 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(CheckInvalidationInfo(-2, std::string()));
syncer::Invalidation inv3 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
EXPECT_TRUE(CheckInvalidationInfo(-3, std::string()));
EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
StorePolicy(POLICY_OBJECT_A, -1);
EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
StorePolicy(POLICY_OBJECT_A, -2);
EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
StorePolicy(POLICY_OBJECT_A, -3);
EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
}
TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 3, "test");
StorePolicy(POLICY_OBJECT_A, 3);
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(CheckPolicyNotRefreshed());
}
TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 3, "test");
ShutdownInvalidator();
EXPECT_TRUE(CheckPolicyNotRefreshed());
DestroyInvalidator();
}
TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
StartInvalidator();
DisableInvalidationService();
EnableInvalidationService();
EXPECT_FALSE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(InvalidationsEnabled());
DisableInvalidationService();
EXPECT_FALSE(InvalidationsEnabled());
DisableInvalidationService();
EXPECT_FALSE(InvalidationsEnabled());
EnableInvalidationService();
EXPECT_TRUE(InvalidationsEnabled());
EnableInvalidationService();
EXPECT_TRUE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_NONE);
EXPECT_FALSE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_NONE);
EXPECT_FALSE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_A);
EXPECT_TRUE(InvalidationsEnabled());
DisableInvalidationService();
EXPECT_FALSE(InvalidationsEnabled());
StorePolicy(POLICY_OBJECT_NONE);
StorePolicy(POLICY_OBJECT_A);
EXPECT_FALSE(InvalidationsEnabled());
}
TEST_F(CloudPolicyInvalidatorTest, Disconnect) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 1, "test");
EXPECT_TRUE(InvalidationsEnabled());
DisconnectCore();
EXPECT_TRUE(CheckPolicyNotRefreshed());
EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 2, "test")));
EXPECT_TRUE(CheckPolicyNotRefreshed());
DisableInvalidationService();
EnableInvalidationService();
ConnectCore();
EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 3, "test")));
EXPECT_TRUE(CheckPolicyNotRefreshed());
DisconnectCore();
EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 4, "test")));
EXPECT_TRUE(CheckPolicyNotRefreshed());
ConnectCore();
StartRefreshScheduler();
EXPECT_TRUE(CheckPolicyNotRefreshed());
EXPECT_TRUE(InvalidationsEnabled());
FireInvalidation(POLICY_OBJECT_A, 5, "test");
EXPECT_TRUE(CheckInvalidationInfo(5, "test"));
EXPECT_TRUE(CheckPolicyRefreshed());
DisableInvalidationService();
EXPECT_FALSE(InvalidationsEnabled());
}
TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) {
StartInvalidator();
StorePolicy(POLICY_OBJECT_NONE, 0, false );
StorePolicy(POLICY_OBJECT_NONE, 0, true );
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
}
TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
AdvanceClock(base::TimeDelta::FromSeconds(1));
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
AdvanceClock(base::TimeDelta::FromSeconds(
CloudPolicyInvalidator::kInvalidationGracePeriod));
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED));
DisableInvalidationService();
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
EnableInvalidationService();
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
AdvanceClock(base::TimeDelta::FromSeconds(
CloudPolicyInvalidator::kInvalidationGracePeriod));
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED));
EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
}
TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) {
StartInvalidator();
StorePolicy(POLICY_OBJECT_A);
AdvanceClock(base::TimeDelta::FromSeconds(
CloudPolicyInvalidator::kInvalidationGracePeriod));
FireInvalidation(POLICY_OBJECT_A, 5, "test");
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
StorePolicy(POLICY_OBJECT_A, 5, true );
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
StorePolicy(POLICY_OBJECT_A, 0, false );
StorePolicy(POLICY_OBJECT_A, 0, true );
StorePolicy(POLICY_OBJECT_A, 0, false );
EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED));
EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
}
TEST_F(CloudPolicyInvalidatorTest, InvalidationMetrics) {
StorePolicy(POLICY_OBJECT_A);
StartInvalidator();
EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_B, 1, "test")));
FireInvalidation(POLICY_OBJECT_A, 1, "test");
FireInvalidation(POLICY_OBJECT_A, 2, "test");
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
FireUnknownVersionInvalidation(POLICY_OBJECT_A);
FireInvalidation(POLICY_OBJECT_A, 3, "test");
FireInvalidation(POLICY_OBJECT_A, 4, "test");
EXPECT_EQ(3, GetInvalidationCount(false ));
EXPECT_EQ(4, GetInvalidationCount(true ));
}
}