unguessable_token_unittest.cc 5.5 KB

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