PolicyManager: New main PolicyManager class.
The PolicyManager class is the singleton instance used by the Update
Engine to make policy requests. The PolicyManager uses a Policy pure
virtual class with the definition of all the public policy
implementation. Different platforms can implement a different
subclass of it or extend the existing one.
The Policy class comprises the set of policy related decision and
the logic implementing those.
This patch includes a new PolicyManager class and a sample Policy
for ChromeOS that will be extended when the actual policies are
migrated to this framework. A default policy is also included and
is used whenever the actual policy fails, as a safe default.
BUG=chromium:338591
TEST=Unit tests pass.
Change-Id: If60c87d8cb9031eb15b6d4d5e937f65f56c79a04
Reviewed-on: https://chromium-review.googlesource.com/186884
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/policy_manager_unittest.cc b/policy_manager/policy_manager_unittest.cc
new file mode 100644
index 0000000..dc906f1
--- /dev/null
+++ b/policy_manager/policy_manager_unittest.cc
@@ -0,0 +1,92 @@
+// 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 <base/memory/scoped_ptr.h>
+#include <base/time.h>
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+#include <string>
+
+#include "update_engine/policy_manager/default_policy.h"
+#include "update_engine/policy_manager/mock_policy.h"
+#include "update_engine/policy_manager/pmtest_utils.h"
+#include "update_engine/policy_manager/policy_manager.h"
+
+using base::TimeDelta;
+using std::string;
+
+using testing::_;
+using testing::Return;
+using testing::StrictMock;
+
+namespace chromeos_policy_manager {
+
+class PmPolicyManagerTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ EXPECT_TRUE(pmut_.Init());
+ }
+
+ PolicyManager pmut_; // PolicyManager undert test.
+};
+
+// The FailingPolicy implements a single method and make it always fail. This
+// 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,
+ string* error,
+ bool* result) const {
+ *error = "FailingPolicy failed.";
+ return EvalStatusFailed;
+ }
+};
+
+// The LazyPolicy always returns
+class LazyPolicy : public DefaultPolicy {
+ virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
+ string* error,
+ bool* result) const {
+ return EvalStatusAskMeAgainLater;
+ }
+};
+
+TEST_F(PmPolicyManagerTest, PolicyRequestCall) {
+ bool result;
+ EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
+ EXPECT_EQ(status, EvalStatusSucceeded);
+}
+
+TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
+ StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
+ pmut_.policy_.reset(policy);
+ bool result;
+
+ // Tests that the method is called on the policy_ instance.
+ EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _))
+ .WillOnce(Return(EvalStatusSucceeded));
+ EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
+ EXPECT_EQ(status, EvalStatusSucceeded);
+}
+
+TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
+ pmut_.policy_.reset(new FailingPolicy());
+
+ // Tests that the DefaultPolicy instance is called when the method fails,
+ // which will set this as true.
+ bool result = false;
+ EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
+ EXPECT_EQ(status, EvalStatusSucceeded);
+ EXPECT_TRUE(result);
+}
+
+TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
+ pmut_.policy_.reset(new LazyPolicy());
+ bool result;
+
+ EvalStatus status = pmut_.PolicyRequest(&Policy::UpdateCheckAllowed, &result);
+ EXPECT_EQ(status, EvalStatusAskMeAgainLater);
+}
+
+} // namespace chromeos_policy_manager