PolicyManager: Add a new State class grouping the state providers.
The policy implementations need access to the providers to discover
the variables exposed by them. Instead of having global variables to
access each providers, we add a new State interface that groups all
the providers in the same way a provider groups all the variables.
This interface class allows to create a mock/fake class for testing
the policies.
Other minor fixes to the fakes are included in this patch.
BUG=chromium:338591
TEST=Simple unit test passes.
Change-Id: I7fe46dfc8416ee39ace3290628b7bae440213b29
Reviewed-on: https://chromium-review.googlesource.com/187705
Reviewed-by: Alex Deymo <[email protected]>
Tested-by: Alex Deymo <[email protected]>
Commit-Queue: Alex Deymo <[email protected]>
diff --git a/SConstruct b/SConstruct
index 4bd8f31..666fced 100644
--- a/SConstruct
+++ b/SConstruct
@@ -239,10 +239,11 @@
payload_signer.cc
payload_state.cc
policy_manager/chromeos_policy.cc
+ policy_manager/evaluation_context.cc
policy_manager/policy_manager.cc
policy_manager/real_random_provider.cc
- policy_manager/evaluation_context.cc
policy_manager/real_shill_provider.cc
+ policy_manager/real_state.cc
postinstall_runner_action.cc
prefs.cc
proxy_resolver.cc
@@ -294,10 +295,12 @@
payload_state_unittest.cc
policy_manager/boxed_value_unittest.cc
policy_manager/evaluation_context_unittest.cc
+ policy_manager/fake_state.cc
policy_manager/generic_variables_unittest.cc
policy_manager/policy_manager_unittest.cc
policy_manager/real_random_provider_unittest.cc
policy_manager/real_shill_provider_unittest.cc
+ policy_manager/real_state_unittest.cc
policy_manager/variable_unittest.cc
postinstall_runner_action_unittest.cc
prefs_unittest.cc
diff --git a/policy_manager/chromeos_policy.cc b/policy_manager/chromeos_policy.cc
index 4588053..95578aa 100644
--- a/policy_manager/chromeos_policy.cc
+++ b/policy_manager/chromeos_policy.cc
@@ -11,7 +11,7 @@
namespace chromeos_policy_manager {
EvalStatus ChromeOSPolicy::UpdateCheckAllowed(EvaluationContext* ec,
- string* error,
+ State* state, string* error,
bool* result) const {
// TODO(deymo): Write this policy implementation with the actual policy.
*result = true;
diff --git a/policy_manager/chromeos_policy.h b/policy_manager/chromeos_policy.h
index 1247d93..1eede27 100644
--- a/policy_manager/chromeos_policy.h
+++ b/policy_manager/chromeos_policy.h
@@ -16,7 +16,7 @@
virtual ~ChromeOSPolicy() {}
// Policy overrides.
- virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
std::string* error,
bool* result) const;
diff --git a/policy_manager/default_policy.h b/policy_manager/default_policy.h
index 1d10084..20f6668 100644
--- a/policy_manager/default_policy.h
+++ b/policy_manager/default_policy.h
@@ -18,7 +18,7 @@
virtual ~DefaultPolicy() {}
// Policy overrides.
- virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
std::string* error,
bool* result) const {
*result = true;
diff --git a/policy_manager/fake_random_provider.h b/policy_manager/fake_random_provider.h
index 10c6cb7..81d3ebe 100644
--- a/policy_manager/fake_random_provider.h
+++ b/policy_manager/fake_random_provider.h
@@ -12,9 +12,12 @@
// Fake implementation of the RandomProvider base class.
class FakeRandomProvider : public RandomProvider {
+ public:
+ FakeRandomProvider() {}
+
protected:
virtual bool DoInit() {
- seed_.reset(new FakeVariable<uint64_t>("random_seed"));
+ seed_.reset(new FakeVariable<uint64_t>("random_seed", kVariableModeConst));
return true;
}
diff --git a/policy_manager/fake_shill_provider.h b/policy_manager/fake_shill_provider.h
new file mode 100644
index 0000000..65ddf38
--- /dev/null
+++ b/policy_manager/fake_shill_provider.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_SHILL_PROVIDER_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_SHILL_PROVIDER_H
+
+#include "update_engine/policy_manager/fake_variable.h"
+#include "update_engine/policy_manager/shill_provider.h"
+
+namespace chromeos_policy_manager {
+
+// Fake implementation of the ShillProvider base class.
+class FakeShillProvider : public ShillProvider {
+ public:
+ FakeShillProvider() {}
+
+ protected:
+ virtual bool DoInit() {
+ var_is_connected_.reset(
+ new FakeVariable<bool>("is_connected", kVariableModeAsync));
+ var_conn_type_.reset(
+ new FakeVariable<ShillConnType>("conn_type", kVariableModeAsync));
+ var_conn_last_changed_.reset(
+ new FakeVariable<Time>("conn_last_changed", kVariableModeAsync));
+ return true;
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FakeShillProvider);
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_SHILL_PROVIDER_H
diff --git a/policy_manager/fake_state.cc b/policy_manager/fake_state.cc
new file mode 100644
index 0000000..977e9d3
--- /dev/null
+++ b/policy_manager/fake_state.cc
@@ -0,0 +1,16 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/policy_manager/fake_random_provider.h"
+#include "update_engine/policy_manager/fake_shill_provider.h"
+#include "update_engine/policy_manager/fake_state.h"
+
+namespace chromeos_policy_manager {
+
+FakeState::FakeState() {
+ set_random_provider(new FakeRandomProvider());
+ set_shill_provider(new FakeShillProvider());
+}
+
+} // namespace chromeos_policy_manager
diff --git a/policy_manager/fake_state.h b/policy_manager/fake_state.h
new file mode 100644
index 0000000..4d97a37
--- /dev/null
+++ b/policy_manager/fake_state.h
@@ -0,0 +1,25 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_STATE_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_STATE_H
+
+#include "update_engine/policy_manager/state.h"
+
+namespace chromeos_policy_manager {
+
+// A fake State class that creates Fake providers for all the providers.
+class FakeState : public State {
+ public:
+ // Initializes the State with fake providers.
+ FakeState();
+ virtual ~FakeState() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FakeState);
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_STATE_H
diff --git a/policy_manager/mock_policy.h b/policy_manager/mock_policy.h
index 744dff1..8fcb9bc 100644
--- a/policy_manager/mock_policy.h
+++ b/policy_manager/mock_policy.h
@@ -18,8 +18,9 @@
virtual ~MockPolicy() {}
// Policy overrides.
- MOCK_CONST_METHOD3(UpdateCheckAllowed,
- EvalStatus(EvaluationContext*, std::string*, bool*));
+ MOCK_CONST_METHOD4(UpdateCheckAllowed,
+ EvalStatus(EvaluationContext*, State*, std::string*,
+ bool*));
private:
DISALLOW_COPY_AND_ASSIGN(MockPolicy);
diff --git a/policy_manager/policy.h b/policy_manager/policy.h
index b4a5a1e..7554824 100644
--- a/policy_manager/policy.h
+++ b/policy_manager/policy.h
@@ -6,6 +6,7 @@
#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_H
#include "update_engine/policy_manager/evaluation_context.h"
+#include "update_engine/policy_manager/state.h"
namespace chromeos_policy_manager {
@@ -27,15 +28,15 @@
virtual ~Policy() {}
// List of policy requests. A policy request takes an EvaluationContext as the
- // first argument, a returned error message, a returned value and optionally
- // followed by one or more arbitrary constant arguments.
+ // first argument, a State instance, a returned error message, a returned
+ // value and optionally followed by one or more arbitrary constant arguments.
//
// When the implementation fails, the method returns EvalStatusFailed and sets
// the |error| string.
// UpdateCheckAllowed returns whether it is allowed to request an update check
// to Omaha.
- virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
std::string* error,
bool* result) const = 0;
diff --git a/policy_manager/policy_manager-inl.h b/policy_manager/policy_manager-inl.h
index 6c99be2..e4b9666 100644
--- a/policy_manager/policy_manager-inl.h
+++ b/policy_manager/policy_manager-inl.h
@@ -17,13 +17,14 @@
std::string error;
// First try calling the actual policy.
- EvalStatus status = (policy_.get()->*policy_method)(&ec, &error, result,
- args...);
+ EvalStatus status = (policy_.get()->*policy_method)(&ec, state_.get(), &error,
+ result, args...);
if (status == EvalStatusFailed) {
LOG(WARNING) << "PolicyRequest() failed with error: " << error;
error.clear();
- status = (default_policy_.*policy_method)(&ec, &error, result, args...);
+ status = (default_policy_.*policy_method)(&ec, state_.get(), &error,
+ result, args...);
if (status == EvalStatusFailed) {
LOG(WARNING) << "Request to DefaultPolicy also failed, passing error.";
diff --git a/policy_manager/policy_manager.cc b/policy_manager/policy_manager.cc
index 6bfbd0e..e94df93 100644
--- a/policy_manager/policy_manager.cc
+++ b/policy_manager/policy_manager.cc
@@ -4,7 +4,7 @@
#include "update_engine/policy_manager/chromeos_policy.h"
#include "update_engine/policy_manager/policy_manager.h"
-#include "update_engine/policy_manager/real_random_provider.h"
+#include "update_engine/policy_manager/real_state.h"
namespace chromeos_policy_manager {
@@ -19,13 +19,9 @@
// implementation with a build-time flag.
policy_.reset(new ChromeOSPolicy());
- bool result = true;
+ state_.reset(new RealState());
- // Initialize all the providers.
- result = result && InitProvider<RandomProvider>(&random_,
- new RealRandomProvider());
-
- return result;
+ return state_->Init();
}
} // namespace chromeos_policy_manager
diff --git a/policy_manager/policy_manager.h b/policy_manager/policy_manager.h
index d952899..d0be4e2 100644
--- a/policy_manager/policy_manager.h
+++ b/policy_manager/policy_manager.h
@@ -9,7 +9,7 @@
#include "update_engine/policy_manager/default_policy.h"
#include "update_engine/policy_manager/policy.h"
-#include "update_engine/policy_manager/random_provider.h"
+#include "update_engine/policy_manager/state.h"
namespace chromeos_policy_manager {
@@ -38,7 +38,7 @@
// An example call to this method is:
// pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
template<typename T, typename R, typename... Args>
- EvalStatus PolicyRequest(T policy_method , R* result, Args... args);
+ EvalStatus PolicyRequest(T policy_method, R* result, Args... args);
private:
friend class PmPolicyManagerTest;
@@ -54,8 +54,8 @@
// a policy implementation fails with EvalStatusFailed.
const DefaultPolicy default_policy_;
- // Providers.
- scoped_ptr<RandomProvider> random_;
+ // State Providers.
+ scoped_ptr<State> state_;
DISALLOW_COPY_AND_ASSIGN(PolicyManager);
};
diff --git a/policy_manager/policy_manager_unittest.cc b/policy_manager/policy_manager_unittest.cc
index dc906f1..ba001c9 100644
--- a/policy_manager/policy_manager_unittest.cc
+++ b/policy_manager/policy_manager_unittest.cc
@@ -35,7 +35,7 @@
// class extends the DefaultPolicy class to allow extensions of the Policy
// class without extending nor changing this test.
class FailingPolicy : public DefaultPolicy {
- virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
string* error,
bool* result) const {
*error = "FailingPolicy failed.";
@@ -45,7 +45,7 @@
// The LazyPolicy always returns
class LazyPolicy : public DefaultPolicy {
- virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
string* error,
bool* result) const {
return EvalStatusAskMeAgainLater;
@@ -64,7 +64,7 @@
bool result;
// Tests that the method is called on the policy_ instance.
- EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _))
+ EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
.WillOnce(Return(EvalStatusSucceeded));
EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
EXPECT_EQ(status, EvalStatusSucceeded);
diff --git a/policy_manager/real_state.cc b/policy_manager/real_state.cc
new file mode 100644
index 0000000..90b0b3d
--- /dev/null
+++ b/policy_manager/real_state.cc
@@ -0,0 +1,16 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/policy_manager/real_random_provider.h"
+#include "update_engine/policy_manager/real_shill_provider.h"
+#include "update_engine/policy_manager/real_state.h"
+
+namespace chromeos_policy_manager {
+
+RealState::RealState() {
+ set_random_provider(new RealRandomProvider());
+ set_shill_provider(new RealShillProvider());
+}
+
+} // namespace chromeos_policy_manager
diff --git a/policy_manager/real_state.h b/policy_manager/real_state.h
new file mode 100644
index 0000000..b46126e
--- /dev/null
+++ b/policy_manager/real_state.h
@@ -0,0 +1,24 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_STATE_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_STATE_H
+
+#include "update_engine/policy_manager/state.h"
+
+namespace chromeos_policy_manager {
+
+// State implementation class.
+class RealState : public State {
+ public:
+ RealState();
+ ~RealState() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RealState);
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_STATE_H
diff --git a/policy_manager/real_state_unittest.cc b/policy_manager/real_state_unittest.cc
new file mode 100644
index 0000000..a90fba1
--- /dev/null
+++ b/policy_manager/real_state_unittest.cc
@@ -0,0 +1,20 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <gtest/gtest.h>
+
+#include "update_engine/policy_manager/real_state.h"
+#include "update_engine/policy_manager/pmtest_utils.h"
+
+namespace chromeos_policy_manager {
+
+TEST(PmRealStateTest, InitTest) {
+ RealState state;
+ EXPECT_TRUE(state.Init());
+ // Check that the providers are being initialized.
+ PMTEST_ASSERT_NOT_NULL(state.random_provider());
+ PMTEST_EXPECT_NOT_NULL(state.random_provider()->seed());
+}
+
+} // namespace chromeos_policy_manager
diff --git a/policy_manager/state.h b/policy_manager/state.h
new file mode 100644
index 0000000..412925d
--- /dev/null
+++ b/policy_manager/state.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_STATE_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_STATE_H
+
+#include "update_engine/policy_manager/random_provider.h"
+#include "update_engine/policy_manager/shill_provider.h"
+
+namespace chromeos_policy_manager {
+
+// The State class is an interface to the ensemble of providers. This class
+// gives visibility of the state providers to policy implementations.
+class State {
+ public:
+ virtual ~State() {}
+
+ // Initializes the State instance. Returns whether the initialization
+ // succeeded.
+ bool Init() {
+ return (random_provider_ && random_provider_->Init() &&
+ shill_provider_ && shill_provider_->Init());
+ }
+
+ // These functions return the given provider.
+ RandomProvider* random_provider() { return random_provider_.get(); }
+ ShillProvider* shill_provider() { return shill_provider_.get(); }
+
+ protected:
+ // Initialize the private scoped_ptr for each provider.
+ void set_random_provider(RandomProvider* random_provider) {
+ return random_provider_.reset(random_provider);
+ }
+
+ void set_shill_provider(ShillProvider* shill_provider) {
+ return shill_provider_.reset(shill_provider);
+ }
+
+ private:
+ // Instances of the providers.
+ scoped_ptr<RandomProvider> random_provider_;
+ scoped_ptr<ShillProvider> shill_provider_;
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_STATE_H