blob: 265050e1e7092b6c7e18f61c25b1e19ad2fbf1ec [file] [log] [blame]
Alex Deymoca0aaa62014-01-06 10:39:58 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <base/memory/scoped_ptr.h>
6#include <gtest/gtest.h>
7#include <string>
8
9#include "policy_manager/random_provider.h"
10#include "policy_manager/random_vars.h"
11
12using base::TimeDelta;
13using std::string;
14
15namespace chromeos_policy_manager {
16
Gilad Arnoldb33e1982014-01-27 14:46:27 -080017class PmRandomProviderTest : public ::testing::Test {
Alex Deymoca0aaa62014-01-06 10:39:58 -080018 protected:
19 virtual void SetUp() {
20 ASSERT_TRUE(var_random_seed == NULL);
Gilad Arnoldb33e1982014-01-27 14:46:27 -080021 provider_ = new RandomProvider();
22 ASSERT_TRUE(provider_);
23 EXPECT_TRUE(provider_->Init());
Alex Deymoca0aaa62014-01-06 10:39:58 -080024 }
25
26 virtual void TearDown() {
Gilad Arnoldb33e1982014-01-27 14:46:27 -080027 delete provider_;
28 provider_ = NULL;
Alex Deymoca0aaa62014-01-06 10:39:58 -080029 ASSERT_TRUE(var_random_seed == NULL);
30 }
31
32 private:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080033 RandomProvider* provider_;
Alex Deymoca0aaa62014-01-06 10:39:58 -080034};
35
Gilad Arnoldb33e1982014-01-27 14:46:27 -080036TEST_F(PmRandomProviderTest, InitFinalize) {
Alex Deymoca0aaa62014-01-06 10:39:58 -080037 // The provider should initialize the variable with a valid object.
38 ASSERT_TRUE(var_random_seed != NULL);
39}
40
Gilad Arnoldb33e1982014-01-27 14:46:27 -080041TEST_F(PmRandomProviderTest, GetRandomValues) {
Alex Deymoca0aaa62014-01-06 10:39:58 -080042 string errmsg;
Gilad Arnold570ecb12014-01-29 10:52:29 -080043 scoped_ptr<const uint64_t> value(
Alex Deymoca0aaa62014-01-06 10:39:58 -080044 var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
45 ASSERT_TRUE(value != NULL);
46
Gilad Arnoldb33e1982014-01-27 14:46:27 -080047 bool always_returns_the_same_value = true;
Alex Deymoca0aaa62014-01-06 10:39:58 -080048 // Test that at least the returned values are different. This test fails,
49 // by design, once every 2^320 runs.
50 for (int i = 0; i < 5; i++) {
Gilad Arnold570ecb12014-01-29 10:52:29 -080051 scoped_ptr<const uint64_t> other_value(
Alex Deymoca0aaa62014-01-06 10:39:58 -080052 var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg));
53 ASSERT_TRUE(other_value != NULL);
Gilad Arnoldb33e1982014-01-27 14:46:27 -080054 always_returns_the_same_value = always_returns_the_same_value &&
Alex Deymoca0aaa62014-01-06 10:39:58 -080055 *other_value == *value;
56 }
Gilad Arnoldb33e1982014-01-27 14:46:27 -080057 EXPECT_FALSE(always_returns_the_same_value);
Alex Deymoca0aaa62014-01-06 10:39:58 -080058}
59
60} // namespace chromeos_policy_manager