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
7 #include <memory>
8 #include <sstream>
9 #include <type_traits>
10
11 #include "base/value_conversions.h"
12 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16
TestSmallerThanOperator(const UnguessableToken & a,const UnguessableToken & b)17 void TestSmallerThanOperator(const UnguessableToken& a,
18 const UnguessableToken& b) {
19 EXPECT_TRUE(a < b);
20 EXPECT_FALSE(b < a);
21 }
22
TEST(UnguessableTokenTest,VerifyEqualityOperators)23 TEST(UnguessableTokenTest, VerifyEqualityOperators) {
24 // Deserialize is used for testing purposes.
25 // Use UnguessableToken::Create() in production code instead.
26 UnguessableToken token = UnguessableToken::Deserialize(1, 2);
27 UnguessableToken same_token = UnguessableToken::Deserialize(1, 2);
28 UnguessableToken diff_token = UnguessableToken::Deserialize(1, 3);
29
30 EXPECT_TRUE(token == token);
31 EXPECT_FALSE(token != token);
32
33 EXPECT_TRUE(token == same_token);
34 EXPECT_FALSE(token != same_token);
35
36 EXPECT_FALSE(token == diff_token);
37 EXPECT_FALSE(diff_token == token);
38 EXPECT_TRUE(token != diff_token);
39 EXPECT_TRUE(diff_token != token);
40 }
41
TEST(UnguessableTokenTest,VerifyConstructors)42 TEST(UnguessableTokenTest, VerifyConstructors) {
43 UnguessableToken token = UnguessableToken::Create();
44 EXPECT_FALSE(token.is_empty());
45 EXPECT_TRUE(token);
46
47 UnguessableToken copied_token(token);
48 EXPECT_TRUE(copied_token);
49 EXPECT_EQ(token, copied_token);
50
51 UnguessableToken uninitialized;
52 EXPECT_TRUE(uninitialized.is_empty());
53 EXPECT_FALSE(uninitialized);
54
55 EXPECT_TRUE(UnguessableToken().is_empty());
56 EXPECT_FALSE(UnguessableToken());
57 }
58
TEST(UnguessableTokenTest,VerifySerialization)59 TEST(UnguessableTokenTest, VerifySerialization) {
60 UnguessableToken token = UnguessableToken::Create();
61
62 uint64_t high = token.GetHighForSerialization();
63 uint64_t low = token.GetLowForSerialization();
64
65 EXPECT_TRUE(high);
66 EXPECT_TRUE(low);
67
68 UnguessableToken Deserialized = UnguessableToken::Deserialize(high, low);
69 EXPECT_EQ(token, Deserialized);
70 }
71
TEST(UnguessableTokenTest,VerifyValueSerialization)72 TEST(UnguessableTokenTest, VerifyValueSerialization) {
73 UnguessableToken token = UnguessableToken::Create();
74 std::unique_ptr<Value> value = CreateUnguessableTokenValue(token);
75
76 UnguessableToken deserialized;
77 EXPECT_TRUE(GetValueAsUnguessableToken(*value, &deserialized));
78 EXPECT_EQ(token, deserialized);
79 }
80
81 // Common case (~88% of the time) - no leading zeroes in high_ nor low_.
TEST(UnguessableTokenTest,VerifyToString1)82 TEST(UnguessableTokenTest, VerifyToString1) {
83 UnguessableToken token =
84 UnguessableToken::Deserialize(0x1234567890ABCDEF, 0xFEDCBA0987654321);
85 std::string expected = "1234567890ABCDEFFEDCBA0987654321";
86
87 EXPECT_EQ(expected, token.ToString());
88
89 std::string expected_stream = "(1234567890ABCDEFFEDCBA0987654321)";
90 std::stringstream stream;
91 stream << token;
92 EXPECT_EQ(expected_stream, stream.str());
93 }
94
95 // Less common case - leading zeroes in high_ or low_ (testing with both).
TEST(UnguessableTokenTest,VerifyToString2)96 TEST(UnguessableTokenTest, VerifyToString2) {
97 UnguessableToken token = UnguessableToken::Deserialize(0x123, 0xABC);
98 std::string expected = "00000000000001230000000000000ABC";
99
100 EXPECT_EQ(expected, token.ToString());
101
102 std::string expected_stream = "(00000000000001230000000000000ABC)";
103 std::stringstream stream;
104 stream << token;
105 EXPECT_EQ(expected_stream, stream.str());
106 }
107
TEST(UnguessableTokenTest,VerifyToStringUniqueness)108 TEST(UnguessableTokenTest, VerifyToStringUniqueness) {
109 const UnguessableToken token1 =
110 UnguessableToken::Deserialize(0x0000000012345678, 0x0000000123456789);
111 const UnguessableToken token2 =
112 UnguessableToken::Deserialize(0x0000000123456781, 0x0000000023456789);
113 EXPECT_NE(token1.ToString(), token2.ToString());
114 }
115
TEST(UnguessableTokenTest,VerifySmallerThanOperator)116 TEST(UnguessableTokenTest, VerifySmallerThanOperator) {
117 // Deserialize is used for testing purposes.
118 // Use UnguessableToken::Create() in production code instead.
119 {
120 SCOPED_TRACE("a.low < b.low and a.high == b.high.");
121 TestSmallerThanOperator(UnguessableToken::Deserialize(0, 1),
122 UnguessableToken::Deserialize(0, 5));
123 }
124 {
125 SCOPED_TRACE("a.low == b.low and a.high < b.high.");
126 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 0),
127 UnguessableToken::Deserialize(5, 0));
128 }
129 {
130 SCOPED_TRACE("a.low < b.low and a.high < b.high.");
131 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 1),
132 UnguessableToken::Deserialize(5, 5));
133 }
134 {
135 SCOPED_TRACE("a.low > b.low and a.high < b.high.");
136 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 10),
137 UnguessableToken::Deserialize(10, 1));
138 }
139 }
140
TEST(UnguessableTokenTest,VerifyHash)141 TEST(UnguessableTokenTest, VerifyHash) {
142 UnguessableToken token = UnguessableToken::Create();
143
144 EXPECT_EQ(base::HashInts64(token.GetHighForSerialization(),
145 token.GetLowForSerialization()),
146 UnguessableTokenHash()(token));
147 }
148
TEST(UnguessableTokenTest,VerifyBasicUniqueness)149 TEST(UnguessableTokenTest, VerifyBasicUniqueness) {
150 EXPECT_NE(UnguessableToken::Create(), UnguessableToken::Create());
151
152 UnguessableToken token = UnguessableToken::Create();
153 EXPECT_NE(token.GetHighForSerialization(), token.GetLowForSerialization());
154 }
155 }
156