tguilbert | 76e690d | 2016-09-20 06:11:25 +0900 | [diff] [blame] | 1 | // Copyright (c) 2016 The Chromium 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/unguessable_token.h" |
| 6 | |
tguilbert | 84d2d03 | 2016-09-20 11:04:50 +0900 | [diff] [blame^] | 7 | #include <sstream> |
tguilbert | 76e690d | 2016-09-20 06:11:25 +0900 | [diff] [blame] | 8 | #include <type_traits> |
| 9 | |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | void TestSmallerThanOperator(const UnguessableToken& a, |
| 15 | const UnguessableToken& b) { |
| 16 | EXPECT_TRUE(a < b); |
| 17 | EXPECT_FALSE(b < a); |
| 18 | } |
| 19 | |
| 20 | TEST(UnguessableTokenTest, VerifyEqualityOperators) { |
| 21 | // Deserialize is used for testing purposes. |
| 22 | // Use UnguessableToken::Create() in production code instead. |
| 23 | UnguessableToken token = UnguessableToken::Deserialize(1, 2); |
| 24 | UnguessableToken same_token = UnguessableToken::Deserialize(1, 2); |
| 25 | UnguessableToken diff_token = UnguessableToken::Deserialize(1, 3); |
| 26 | |
| 27 | EXPECT_TRUE(token == token); |
| 28 | EXPECT_FALSE(token != token); |
| 29 | |
| 30 | EXPECT_TRUE(token == same_token); |
| 31 | EXPECT_FALSE(token != same_token); |
| 32 | |
| 33 | EXPECT_FALSE(token == diff_token); |
| 34 | EXPECT_FALSE(diff_token == token); |
| 35 | EXPECT_TRUE(token != diff_token); |
| 36 | EXPECT_TRUE(diff_token != token); |
| 37 | } |
| 38 | |
| 39 | TEST(UnguessableTokenTest, VerifyConstructors) { |
| 40 | UnguessableToken token = UnguessableToken::Create(); |
| 41 | EXPECT_FALSE(token.is_empty()); |
| 42 | EXPECT_TRUE(token); |
| 43 | |
| 44 | UnguessableToken copied_token(token); |
| 45 | EXPECT_TRUE(copied_token); |
| 46 | EXPECT_EQ(token, copied_token); |
| 47 | |
| 48 | UnguessableToken uninitialized; |
| 49 | EXPECT_TRUE(uninitialized.is_empty()); |
| 50 | EXPECT_FALSE(uninitialized); |
| 51 | |
| 52 | EXPECT_TRUE(UnguessableToken().is_empty()); |
| 53 | EXPECT_FALSE(UnguessableToken()); |
| 54 | } |
| 55 | |
| 56 | TEST(UnguessableTokenTest, VerifySerialization) { |
| 57 | UnguessableToken token = UnguessableToken::Create(); |
| 58 | |
| 59 | uint64_t high = token.GetHighForSerialization(); |
| 60 | uint64_t low = token.GetLowForSerialization(); |
| 61 | |
| 62 | EXPECT_TRUE(high); |
| 63 | EXPECT_TRUE(low); |
| 64 | |
| 65 | UnguessableToken Deserialized = UnguessableToken::Deserialize(high, low); |
| 66 | EXPECT_EQ(token, Deserialized); |
| 67 | } |
| 68 | |
| 69 | TEST(UnguessableTokenTest, VerifyToString) { |
| 70 | UnguessableToken token = UnguessableToken::Deserialize(0x123, 0xABC); |
| 71 | std::string expected = "(0000012300000ABC)"; |
| 72 | |
| 73 | EXPECT_EQ(expected, token.ToString()); |
tguilbert | 84d2d03 | 2016-09-20 11:04:50 +0900 | [diff] [blame^] | 74 | |
| 75 | std::stringstream stream; |
| 76 | stream << token; |
| 77 | EXPECT_EQ(expected, stream.str()); |
tguilbert | 76e690d | 2016-09-20 06:11:25 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(UnguessableTokenTest, VerifySmallerThanOperator) { |
| 81 | // Deserialize is used for testing purposes. |
| 82 | // Use UnguessableToken::Create() in production code instead. |
| 83 | { |
| 84 | SCOPED_TRACE("a.low < b.low and a.high == b.high."); |
| 85 | TestSmallerThanOperator(UnguessableToken::Deserialize(0, 1), |
| 86 | UnguessableToken::Deserialize(0, 5)); |
| 87 | } |
| 88 | { |
| 89 | SCOPED_TRACE("a.low == b.low and a.high < b.high."); |
| 90 | TestSmallerThanOperator(UnguessableToken::Deserialize(1, 0), |
| 91 | UnguessableToken::Deserialize(5, 0)); |
| 92 | } |
| 93 | { |
| 94 | SCOPED_TRACE("a.low < b.low and a.high < b.high."); |
| 95 | TestSmallerThanOperator(UnguessableToken::Deserialize(1, 1), |
| 96 | UnguessableToken::Deserialize(5, 5)); |
| 97 | } |
| 98 | { |
| 99 | SCOPED_TRACE("a.low > b.low and a.high < b.high."); |
| 100 | TestSmallerThanOperator(UnguessableToken::Deserialize(1, 10), |
| 101 | UnguessableToken::Deserialize(10, 1)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | TEST(UnguessableTokenTest, VerifyHash) { |
| 106 | UnguessableToken token = UnguessableToken::Create(); |
| 107 | |
| 108 | EXPECT_EQ(base::HashInts64(token.GetHighForSerialization(), |
| 109 | token.GetLowForSerialization()), |
| 110 | UnguessableTokenHash()(token)); |
| 111 | } |
| 112 | |
| 113 | TEST(UnguessableTokenTest, VerifyBasicUniqueness) { |
| 114 | EXPECT_NE(UnguessableToken::Create(), UnguessableToken::Create()); |
| 115 | |
| 116 | UnguessableToken token = UnguessableToken::Create(); |
| 117 | EXPECT_NE(token.GetHighForSerialization(), token.GetLowForSerialization()); |
| 118 | } |
| 119 | } |