blob: b0a6a0ad4b89f0a12df72a10c9495b8451f44474 [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
Gilad Arnolda87340b2014-01-30 11:10:18 -08005#include <string>
6
Alex Deymoca0aaa62014-01-06 10:39:58 -08007#include <base/memory/scoped_ptr.h>
8#include <gtest/gtest.h>
Alex Deymoca0aaa62014-01-06 10:39:58 -08009
10#include "policy_manager/random_provider.h"
11#include "policy_manager/random_vars.h"
Gilad Arnolda87340b2014-01-30 11:10:18 -080012#include "policy_manager/pmtest_utils.h"
Alex Deymoca0aaa62014-01-06 10:39:58 -080013
14using base::TimeDelta;
15using std::string;
16
17namespace chromeos_policy_manager {
18
Gilad Arnoldb33e1982014-01-27 14:46:27 -080019class PmRandomProviderTest : public ::testing::Test {
Alex Deymoca0aaa62014-01-06 10:39:58 -080020 protected:
21 virtual void SetUp() {
Gilad Arnolda87340b2014-01-30 11:10:18 -080022 default_timeout_ = TimeDelta::FromSeconds(1);
23
24 // All variables are initially null.
25 PMTEST_ASSERT_NULL(var_random_seed);
26
27 // The provider initializes correctly.
Gilad Arnoldb33e1982014-01-27 14:46:27 -080028 provider_ = new RandomProvider();
Gilad Arnolda87340b2014-01-30 11:10:18 -080029 PMTEST_ASSERT_NOT_NULL(provider_);
30 ASSERT_TRUE(provider_->Init());
31
32 // The provider initializes all variables with valid objects.
33 PMTEST_EXPECT_NOT_NULL(var_random_seed);
Alex Deymoca0aaa62014-01-06 10:39:58 -080034 }
35
36 virtual void TearDown() {
Gilad Arnoldb33e1982014-01-27 14:46:27 -080037 delete provider_;
38 provider_ = NULL;
Gilad Arnolda87340b2014-01-30 11:10:18 -080039 PMTEST_EXPECT_NULL(var_random_seed);
Alex Deymoca0aaa62014-01-06 10:39:58 -080040 }
41
Gilad Arnolda87340b2014-01-30 11:10:18 -080042 TimeDelta default_timeout_;
43
Alex Deymoca0aaa62014-01-06 10:39:58 -080044 private:
Gilad Arnoldb33e1982014-01-27 14:46:27 -080045 RandomProvider* provider_;
Alex Deymoca0aaa62014-01-06 10:39:58 -080046};
47
Alex Deymoca0aaa62014-01-06 10:39:58 -080048
Gilad Arnoldb33e1982014-01-27 14:46:27 -080049TEST_F(PmRandomProviderTest, GetRandomValues) {
Gilad Arnolda87340b2014-01-30 11:10:18 -080050 // Should not return the same random seed repeatedly.
Gilad Arnold570ecb12014-01-29 10:52:29 -080051 scoped_ptr<const uint64_t> value(
Gilad Arnolda87340b2014-01-30 11:10:18 -080052 var_random_seed->GetValue(default_timeout_, NULL));
53 PMTEST_ASSERT_NOT_NULL(value.get());
Alex Deymoca0aaa62014-01-06 10:39:58 -080054
Alex Deymoca0aaa62014-01-06 10:39:58 -080055 // Test that at least the returned values are different. This test fails,
56 // by design, once every 2^320 runs.
Gilad Arnolda87340b2014-01-30 11:10:18 -080057 bool is_same_value = true;
Alex Deymoca0aaa62014-01-06 10:39:58 -080058 for (int i = 0; i < 5; i++) {
Gilad Arnold570ecb12014-01-29 10:52:29 -080059 scoped_ptr<const uint64_t> other_value(
Gilad Arnolda87340b2014-01-30 11:10:18 -080060 var_random_seed->GetValue(default_timeout_, NULL));
61 PMTEST_ASSERT_NOT_NULL(other_value.get());
62 is_same_value = is_same_value && *other_value == *value;
Alex Deymoca0aaa62014-01-06 10:39:58 -080063 }
Gilad Arnolda87340b2014-01-30 11:10:18 -080064 EXPECT_FALSE(is_same_value);
Alex Deymoca0aaa62014-01-06 10:39:58 -080065}
66
67} // namespace chromeos_policy_manager