PolicyManager: Add a fake implementation for RandomProvider.
The FakeRandomProvider creates FakeVariable instances for every
exposed variable. This fake implementation can be used to test
policy implementations faking out the providers.
BUG=chromium:338591
TEST=Headers compile.
Change-Id: I62b85ce62fd6afdbe85801abdd0b7a9710f7cdd5
Reviewed-on: https://chromium-review.googlesource.com/184775
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/fake_variable.h b/policy_manager/fake_variable.h
new file mode 100644
index 0000000..9d349b6
--- /dev/null
+++ b/policy_manager/fake_variable.h
@@ -0,0 +1,50 @@
+// 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_VARIABLE_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H
+
+#include <base/memory/scoped_ptr.h>
+
+#include "policy_manager/variable.h"
+
+namespace chromeos_policy_manager {
+
+// A fake typed variable to use while testing policy implementations. The
+// variable can be instructed to return any object of its type.
+template<typename T>
+class FakeVariable : public Variable<T> {
+ public:
+ explicit FakeVariable(const std::string& name) : Variable<T>(name) {}
+ virtual ~FakeVariable() {}
+
+ // Sets the next value of this variable to the passed |p_value| pointer. Once
+ // returned by GetValue(), the pointer is released and has to be set again.
+ // A value of NULL means that the GetValue() call will fail and return NULL.
+ void reset(const T* p_value) {
+ ptr_.reset(p_value);
+ }
+
+ protected:
+ // Variable<T> overrides.
+ // Returns the pointer set with reset(). The ownership of the object is passed
+ // to the caller and the pointer is release from the FakeVariable. A second
+ // call to GetValue() without reset() will return NULL and set the error
+ // message.
+ virtual const T* GetValue(base::TimeDelta /* timeout */,
+ std::string* errmsg) {
+ if (ptr_ == NULL && errmsg != NULL)
+ *errmsg = this->GetName() + " is an empty FakeVariable";
+ // Passes the pointer ownership to the caller.
+ return ptr_.release();
+ }
+
+ private:
+ // The pointer returned by GetValue().
+ scoped_ptr<const T> ptr_;
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H