PolicyManager: Add a random number provider.
This patch adds a new provider with a single variable that returns a
random value from /dev/urandom every time it is called. The variable
is exported to all the policy implementations (currently none) in the
policy_manager/all_variables.h file.
BUG=None
TEST=unittests.
Change-Id: I6efdecd0f37f1a706b5a0dfd3a77ac4205e35aa0
Reviewed-on: https://chromium-review.googlesource.com/181537
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/random_provider_unittest.cc b/policy_manager/random_provider_unittest.cc
new file mode 100644
index 0000000..82ee5cc
--- /dev/null
+++ b/policy_manager/random_provider_unittest.cc
@@ -0,0 +1,57 @@
+// 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 <gtest/gtest.h>
+#include <string>
+
+#include "policy_manager/random_provider.h"
+#include "policy_manager/random_vars.h"
+
+using base::TimeDelta;
+using std::string;
+
+namespace chromeos_policy_manager {
+
+class PMRandomProviderTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ ASSERT_TRUE(var_random_seed == NULL);
+ EXPECT_TRUE(provider_.Init());
+ }
+
+ virtual void TearDown() {
+ provider_.Finalize();
+ ASSERT_TRUE(var_random_seed == NULL);
+ }
+
+ private:
+ RandomProvider provider_;
+};
+
+TEST_F(PMRandomProviderTest, InitFinalize) {
+ // The provider should initialize the variable with a valid object.
+ ASSERT_TRUE(var_random_seed != NULL);
+}
+
+TEST_F(PMRandomProviderTest, GetRandomValues) {
+ string errmsg;
+ scoped_ptr<const uint64> value(
+ var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
+ ASSERT_TRUE(value != NULL);
+
+ bool returns_allways_the_same_value = true;
+ // Test that at least the returned values are different. This test fails,
+ // by design, once every 2^320 runs.
+ for (int i = 0; i < 5; i++) {
+ scoped_ptr<const uint64> other_value(
+ var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
+ ASSERT_TRUE(other_value != NULL);
+ returns_allways_the_same_value = returns_allways_the_same_value &&
+ *other_value == *value;
+ }
+ EXPECT_FALSE(returns_allways_the_same_value);
+}
+
+} // namespace chromeos_policy_manager