Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame^] | 1 | // 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 | |
| 12 | using base::TimeDelta; |
| 13 | using std::string; |
| 14 | |
| 15 | namespace chromeos_policy_manager { |
| 16 | |
| 17 | class PMRandomProviderTest : public ::testing::Test { |
| 18 | protected: |
| 19 | virtual void SetUp() { |
| 20 | ASSERT_TRUE(var_random_seed == NULL); |
| 21 | EXPECT_TRUE(provider_.Init()); |
| 22 | } |
| 23 | |
| 24 | virtual void TearDown() { |
| 25 | provider_.Finalize(); |
| 26 | ASSERT_TRUE(var_random_seed == NULL); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | RandomProvider provider_; |
| 31 | }; |
| 32 | |
| 33 | TEST_F(PMRandomProviderTest, InitFinalize) { |
| 34 | // The provider should initialize the variable with a valid object. |
| 35 | ASSERT_TRUE(var_random_seed != NULL); |
| 36 | } |
| 37 | |
| 38 | TEST_F(PMRandomProviderTest, GetRandomValues) { |
| 39 | string errmsg; |
| 40 | scoped_ptr<const uint64> value( |
| 41 | var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg)); |
| 42 | ASSERT_TRUE(value != NULL); |
| 43 | |
| 44 | bool returns_allways_the_same_value = true; |
| 45 | // Test that at least the returned values are different. This test fails, |
| 46 | // by design, once every 2^320 runs. |
| 47 | for (int i = 0; i < 5; i++) { |
| 48 | scoped_ptr<const uint64> other_value( |
| 49 | var_random_seed->GetValue(TimeDelta::FromSeconds(1.), &errmsg)); |
| 50 | ASSERT_TRUE(other_value != NULL); |
| 51 | returns_allways_the_same_value = returns_allways_the_same_value && |
| 52 | *other_value == *value; |
| 53 | } |
| 54 | EXPECT_FALSE(returns_allways_the_same_value); |
| 55 | } |
| 56 | |
| 57 | } // namespace chromeos_policy_manager |