1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "update_engine/update_manager/real_random_provider.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 
23 #include "update_engine/update_manager/umtest_utils.h"
24 
25 using std::unique_ptr;
26 
27 namespace chromeos_update_manager {
28 
29 class UmRealRandomProviderTest : public ::testing::Test {
30  protected:
SetUp()31   void SetUp() override {
32     // The provider initializes correctly.
33     provider_.reset(new RealRandomProvider());
34     ASSERT_NE(nullptr, provider_.get());
35     ASSERT_TRUE(provider_->Init());
36 
37     provider_->var_seed();
38   }
39 
40   unique_ptr<RealRandomProvider> provider_;
41 };
42 
TEST_F(UmRealRandomProviderTest,InitFinalize)43 TEST_F(UmRealRandomProviderTest, InitFinalize) {
44   // The provider initializes all variables with valid objects.
45   EXPECT_NE(nullptr, provider_->var_seed());
46 }
47 
TEST_F(UmRealRandomProviderTest,GetRandomValues)48 TEST_F(UmRealRandomProviderTest, GetRandomValues) {
49   // Should not return the same random seed repeatedly.
50   unique_ptr<const uint64_t> value(
51       provider_->var_seed()->GetValue(UmTestUtils::DefaultTimeout(), nullptr));
52   ASSERT_NE(nullptr, value.get());
53 
54   // Test that at least the returned values are different. This test fails,
55   // by design, once every 2^320 runs.
56   bool is_same_value = true;
57   for (int i = 0; i < 5; i++) {
58     unique_ptr<const uint64_t> other_value(provider_->var_seed()->GetValue(
59         UmTestUtils::DefaultTimeout(), nullptr));
60     ASSERT_NE(nullptr, other_value.get());
61     is_same_value = is_same_value && *other_value == *value;
62   }
63   EXPECT_FALSE(is_same_value);
64 }
65 
66 }  // namespace chromeos_update_manager
67