PolicyManager: New DevicePolicy provider.
This provider gives access to the members of the DevicePolicy
protobuf using the libpolicy to access it. The libpolicy doesn't
provide a way to get a notification when the policy was updated, but
that could be fixed on the future monitoring the file on disk. This
patch attempts to refresh the device policy every hour and updates
all the variables accordingly.
The variables exposed by this provider are only those used by the
update_engine code currently. If new variables need to be exposed
this can easily extended.
To achieve this, a new generic Variable class is introduce, named
AsyncCopyVariable, that allows you to Set or Unset the current value
of the variable and notify the subscribed observers while doing that.
BUG=chromium:358326
TEST=Unit tests added and pass.
Change-Id: Ieee6c9b33160f7dfe40c033db685a79d8fd57fe7
Reviewed-on: https://chromium-review.googlesource.com/194179
Reviewed-by: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/generic_variables_unittest.cc b/policy_manager/generic_variables_unittest.cc
index 7783e9f..490f949 100644
--- a/policy_manager/generic_variables_unittest.cc
+++ b/policy_manager/generic_variables_unittest.cc
@@ -8,8 +8,10 @@
#include <gtest/gtest.h>
#include "update_engine/policy_manager/pmtest_utils.h"
+#include "update_engine/test_utils.h"
using base::TimeDelta;
+using chromeos_update_engine::RunGMainLoopMaxIterations;
namespace chromeos_policy_manager {
@@ -101,4 +103,78 @@
EXPECT_EQ(5, *copy);
}
+
+class PmAsyncCopyVariableTest : public ::testing::Test {
+ public:
+ void TearDown() {
+ // No remaining event on the main loop.
+ EXPECT_EQ(0, RunGMainLoopMaxIterations(1));
+ }
+
+ protected:
+ TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
+};
+
+TEST_F(PmAsyncCopyVariableTest, ConstructorTest) {
+ AsyncCopyVariable<int> var("var");
+ PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
+ EXPECT_EQ(kVariableModeAsync, var.GetMode());
+}
+
+TEST_F(PmAsyncCopyVariableTest, SetValueTest) {
+ AsyncCopyVariable<int> var("var");
+ var.SetValue(5);
+ scoped_ptr<const int> copy(var.GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(copy.get());
+ EXPECT_EQ(5, *copy);
+ // Execute all the pending observers.
+ RunGMainLoopMaxIterations(100);
+}
+
+TEST_F(PmAsyncCopyVariableTest, UnsetValueTest) {
+ AsyncCopyVariable<int> var("var", 42);
+ var.UnsetValue();
+ PMTEST_EXPECT_NULL(var.GetValue(default_timeout_, NULL));
+ // Execute all the pending observers.
+ RunGMainLoopMaxIterations(100);
+}
+
+class CallCounterObserver : public BaseVariable::ObserverInterface {
+ public:
+ void ValueChanged(BaseVariable* variable) {
+ calls_count_++;
+ }
+
+ int calls_count_ = 0;
+};
+
+TEST_F(PmAsyncCopyVariableTest, ObserverCalledTest) {
+ AsyncCopyVariable<int> var("var", 42);
+ CallCounterObserver observer;
+ var.AddObserver(&observer);
+ EXPECT_EQ(0, observer.calls_count_);
+
+ // Check that a different value fires the notification.
+ var.SetValue(5);
+ RunGMainLoopMaxIterations(100);
+ EXPECT_EQ(1, observer.calls_count_);
+
+ // Check the same value doesn't.
+ var.SetValue(5);
+ RunGMainLoopMaxIterations(100);
+ EXPECT_EQ(1, observer.calls_count_);
+
+ // Check that unsetting a previously set value fires the notification.
+ var.UnsetValue();
+ RunGMainLoopMaxIterations(100);
+ EXPECT_EQ(2, observer.calls_count_);
+
+ // Check that unsetting again doesn't.
+ var.UnsetValue();
+ RunGMainLoopMaxIterations(100);
+ EXPECT_EQ(2, observer.calls_count_);
+
+ var.RemoveObserver(&observer);
+}
+
} // namespace chromeos_policy_manager