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

Unified Diff: base/nonce_unittest.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Addressed comments. Added CHECKs, const time ==. 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 side-by-side diff with in-line comments
Download patch
« base/nonce.cc ('K') | « base/nonce.cc ('k') | ipc/ipc_message_utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/nonce_unittest.cc
diff --git a/base/nonce_unittest.cc b/base/nonce_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2f6894c4fd5ae2db301b60d8920d1b81f43f0ec0
--- /dev/null
+++ b/base/nonce_unittest.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/nonce.h"
+
+#include <type_traits>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+void TestSmallerThanOperator(const Nonce& a, const Nonce& b) {
+ EXPECT_TRUE(a < b);
+ EXPECT_FALSE(b < a);
+}
+
+TEST(NonceTest, VerifyEqualityOperators) {
+ // Deserialize is used for testing purposes.
+ // Use Nonce::Generate() to generate nonces in productions code instead.
+ Nonce nonce = Nonce::Deserialize(1, 2);
+ Nonce same_nonce = Nonce::Deserialize(1, 2);
+ Nonce diff_nonce = Nonce::Deserialize(1, 3);
+
+ EXPECT_TRUE(nonce == nonce);
+ EXPECT_FALSE(nonce != nonce);
+
+ EXPECT_TRUE(nonce == same_nonce);
+ EXPECT_FALSE(nonce != same_nonce);
+
+ EXPECT_FALSE(nonce == diff_nonce);
+ EXPECT_FALSE(diff_nonce == nonce);
+ EXPECT_TRUE(nonce != diff_nonce);
+ EXPECT_TRUE(diff_nonce != nonce);
+}
+
+TEST(NonceTest, VerifyConstructors) {
+ Nonce nonce = Nonce::Generate();
+ EXPECT_FALSE(nonce.is_empty());
+
+ EXPECT_EQ(nonce, Nonce::Deserialize(nonce.high(), nonce.low()));
+ EXPECT_EQ(nonce, Nonce(nonce));
+
+ Nonce uninitialized;
+ EXPECT_TRUE(uninitialized.is_empty());
+ EXPECT_TRUE(Nonce().is_empty());
+}
+
+TEST(NonceTest, VerifySmallerThanOperator) {
+ // Deserialize is used for testing purposes.
+ // Use Nonce::Generate() to generate nonces in productions code instead.
+
+ // a.low < b.low and a.high == b.high.
+ TestSmallerThanOperator(Nonce::Deserialize(0, 1), Nonce::Deserialize(0, 5));
+
+ // a.low == b.low and a.high < b.high.
+ TestSmallerThanOperator(Nonce::Deserialize(1, 0), Nonce::Deserialize(5, 0));
+
+ // a.low < b.low and a.high < b.high.
+ TestSmallerThanOperator(Nonce::Deserialize(1, 1), Nonce::Deserialize(5, 5));
+
+ // a.low > b.low and a.high < b.high.
+ TestSmallerThanOperator(Nonce::Deserialize(1, 10), Nonce::Deserialize(10, 1));
+}
+
+TEST(NonceTest, VerifyHash) {
+ Nonce nonce = Nonce::Generate();
+ EXPECT_EQ(base::HashInts64(nonce.high(), nonce.low()), NonceHash{}(nonce));
+}
+
+TEST(NonceTest, VerifyBasicUniqueness) {
+ EXPECT_NE(Nonce::Generate(), Nonce::Generate());
+}
+}
« base/nonce.cc ('K') | « base/nonce.cc ('k') | ipc/ipc_message_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698