blob: 82ee5cc442c8faa444c4c0d0ee0edb67925ca1a1 [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
17class 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
33TEST_F(PMRandomProviderTest, InitFinalize) {
34 // The provider should initialize the variable with a valid object.
35 ASSERT_TRUE(var_random_seed != NULL);
36}
37
38TEST_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