Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: base/unguessable_token_unittest.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Removed MOJO_COMMON_EXPORT Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/unguessable_token.cc ('k') | ipc/ipc_message_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <type_traits>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
13 void TestSmallerThanOperator(const UnguessableToken& a,
14 const UnguessableToken& b) {
15 EXPECT_TRUE(a < b);
16 EXPECT_FALSE(b < a);
17 }
18
19 TEST(UnguessableTokenTest, VerifyEqualityOperators) {
20 // Deserialize is used for testing purposes.
21 // Use UnguessableToken::Create() in production code instead.
22 UnguessableToken token = UnguessableToken::Deserialize(1, 2);
23 UnguessableToken same_token = UnguessableToken::Deserialize(1, 2);
24 UnguessableToken diff_token = UnguessableToken::Deserialize(1, 3);
25
26 EXPECT_TRUE(token == token);
27 EXPECT_FALSE(token != token);
28
29 EXPECT_TRUE(token == same_token);
30 EXPECT_FALSE(token != same_token);
31
32 EXPECT_FALSE(token == diff_token);
33 EXPECT_FALSE(diff_token == token);
34 EXPECT_TRUE(token != diff_token);
35 EXPECT_TRUE(diff_token != token);
36 }
37
38 TEST(UnguessableTokenTest, VerifyConstructors) {
39 UnguessableToken token = UnguessableToken::Create();
40 EXPECT_FALSE(token.is_empty());
41 EXPECT_TRUE(token);
42
43 UnguessableToken copied_token(token);
44 EXPECT_TRUE(copied_token);
45 EXPECT_EQ(token, copied_token);
46
47 UnguessableToken uninitialized;
48 EXPECT_TRUE(uninitialized.is_empty());
49 EXPECT_FALSE(uninitialized);
50
51 EXPECT_TRUE(UnguessableToken().is_empty());
52 EXPECT_FALSE(UnguessableToken());
53 }
54
55 TEST(UnguessableTokenTest, VerifySerialization) {
56 UnguessableToken token = UnguessableToken::Create();
57
58 uint64_t high = token.GetHighForSerialization();
59 uint64_t low = token.GetLowForSerialization();
60
61 EXPECT_TRUE(high);
62 EXPECT_TRUE(low);
63
64 UnguessableToken Deserialized = UnguessableToken::Deserialize(high, low);
65 EXPECT_EQ(token, Deserialized);
66 }
67
68 TEST(UnguessableTokenTest, VerifyToString) {
69 UnguessableToken token = UnguessableToken::Deserialize(0x123, 0xABC);
70 std::string expected = "(0000012300000ABC)";
71
72 EXPECT_EQ(expected, token.ToString());
73 }
74
75 TEST(UnguessableTokenTest, VerifySmallerThanOperator) {
76 // Deserialize is used for testing purposes.
77 // Use UnguessableToken::Create() in production code instead.
78 {
79 SCOPED_TRACE("a.low < b.low and a.high == b.high.");
80 TestSmallerThanOperator(UnguessableToken::Deserialize(0, 1),
81 UnguessableToken::Deserialize(0, 5));
82 }
83 {
84 SCOPED_TRACE("a.low == b.low and a.high < b.high.");
85 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 0),
86 UnguessableToken::Deserialize(5, 0));
87 }
88 {
89 SCOPED_TRACE("a.low < b.low and a.high < b.high.");
90 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 1),
91 UnguessableToken::Deserialize(5, 5));
92 }
93 {
94 SCOPED_TRACE("a.low > b.low and a.high < b.high.");
95 TestSmallerThanOperator(UnguessableToken::Deserialize(1, 10),
96 UnguessableToken::Deserialize(10, 1));
97 }
98 }
99
100 TEST(UnguessableTokenTest, VerifyHash) {
101 UnguessableToken token = UnguessableToken::Create();
102
103 EXPECT_EQ(base::HashInts64(token.GetHighForSerialization(),
104 token.GetLowForSerialization()),
105 UnguessableTokenHash()(token));
106 }
107
108 TEST(UnguessableTokenTest, VerifyBasicUniqueness) {
109 EXPECT_NE(UnguessableToken::Create(), UnguessableToken::Create());
110
111 UnguessableToken token = UnguessableToken::Create();
112 EXPECT_NE(token.GetHighForSerialization(), token.GetLowForSerialization());
113 }
114 }
OLDNEW
« no previous file with comments | « base/unguessable_token.cc ('k') | ipc/ipc_message_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698