blob: d7b2d98b9f5cd1c9715061c1ddac82bdcc401173 [file] [log] [blame]
Alex Deymobb019fe2014-02-03 20:12:17 -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
Alex Deymobb019fe2014-02-03 20:12:17 -08005#include <gtest/gtest.h>
6
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Alex Deymo63784a52014-05-28 10:46:14 -07009#include "update_engine/update_manager/real_random_provider.h"
10#include "update_engine/update_manager/umtest_utils.h"
Alex Deymobb019fe2014-02-03 20:12:17 -080011
12using base::TimeDelta;
Ben Chan02f7c1d2014-10-18 15:18:02 -070013using std::unique_ptr;
Alex Deymobb019fe2014-02-03 20:12:17 -080014
Alex Deymo63784a52014-05-28 10:46:14 -070015namespace chromeos_update_manager {
Alex Deymobb019fe2014-02-03 20:12:17 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017class UmRealRandomProviderTest : public ::testing::Test {
Alex Deymobb019fe2014-02-03 20:12:17 -080018 protected:
19 virtual void SetUp() {
Alex Deymobb019fe2014-02-03 20:12:17 -080020 // The provider initializes correctly.
Gilad Arnold0244f462014-03-14 17:53:59 -070021 provider_.reset(new RealRandomProvider());
Alex Vakulenko88b591f2014-08-28 16:48:57 -070022 ASSERT_NE(nullptr, provider_.get());
Gilad Arnold0244f462014-03-14 17:53:59 -070023 ASSERT_TRUE(provider_->Init());
Alex Deymobb019fe2014-02-03 20:12:17 -080024
Gilad Arnold0244f462014-03-14 17:53:59 -070025 provider_->var_seed();
Alex Deymobb019fe2014-02-03 20:12:17 -080026 }
27
Ben Chan02f7c1d2014-10-18 15:18:02 -070028 unique_ptr<RealRandomProvider> provider_;
Alex Deymobb019fe2014-02-03 20:12:17 -080029};
30
Alex Deymo63784a52014-05-28 10:46:14 -070031TEST_F(UmRealRandomProviderTest, InitFinalize) {
Alex Deymobb019fe2014-02-03 20:12:17 -080032 // The provider initializes all variables with valid objects.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070033 EXPECT_NE(nullptr, provider_->var_seed());
Alex Deymobb019fe2014-02-03 20:12:17 -080034}
35
Alex Deymo63784a52014-05-28 10:46:14 -070036TEST_F(UmRealRandomProviderTest, GetRandomValues) {
Alex Deymobb019fe2014-02-03 20:12:17 -080037 // Should not return the same random seed repeatedly.
Ben Chan02f7c1d2014-10-18 15:18:02 -070038 unique_ptr<const uint64_t> value(
Alex Deymo63784a52014-05-28 10:46:14 -070039 provider_->var_seed()->GetValue(UmTestUtils::DefaultTimeout(), nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -070040 ASSERT_NE(nullptr, value.get());
Alex Deymobb019fe2014-02-03 20:12:17 -080041
42 // Test that at least the returned values are different. This test fails,
43 // by design, once every 2^320 runs.
44 bool is_same_value = true;
45 for (int i = 0; i < 5; i++) {
Ben Chan02f7c1d2014-10-18 15:18:02 -070046 unique_ptr<const uint64_t> other_value(
Alex Deymo63784a52014-05-28 10:46:14 -070047 provider_->var_seed()->GetValue(UmTestUtils::DefaultTimeout(),
Gilad Arnold67ed78d2014-04-23 13:17:46 -070048 nullptr));
Alex Vakulenko88b591f2014-08-28 16:48:57 -070049 ASSERT_NE(nullptr, other_value.get());
Alex Deymobb019fe2014-02-03 20:12:17 -080050 is_same_value = is_same_value && *other_value == *value;
51 }
52 EXPECT_FALSE(is_same_value);
53}
54
Alex Deymo63784a52014-05-28 10:46:14 -070055} // namespace chromeos_update_manager