Commit d16e74c3 authored by Elad Alon's avatar Elad Alon Committed by Commit Bot

Fix UnguessableToken::ToString()

Make sure that ToString() would produce distinct strings for
distinct tokens.

Bug: 815217
Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel
Change-Id: I38ea5bfad061610e95945eb31187cb3c02f737cf
Reviewed-on: https://chromium-review.googlesource.com/934824Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Commit-Queue: Elad Alon <eladalon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539115}
parent a95cd07e
...@@ -14,7 +14,7 @@ UnguessableToken::UnguessableToken(uint64_t high, uint64_t low) ...@@ -14,7 +14,7 @@ UnguessableToken::UnguessableToken(uint64_t high, uint64_t low)
: high_(high), low_(low) {} : high_(high), low_(low) {}
std::string UnguessableToken::ToString() const { std::string UnguessableToken::ToString() const {
return base::StringPrintf("%08" PRIX64 "%08" PRIX64, high_, low_); return base::StringPrintf("%016" PRIX64 "%016" PRIX64, high_, low_);
} }
// static // static
......
...@@ -78,18 +78,41 @@ TEST(UnguessableTokenTest, VerifyValueSerialization) { ...@@ -78,18 +78,41 @@ TEST(UnguessableTokenTest, VerifyValueSerialization) {
EXPECT_EQ(token, deserialized); EXPECT_EQ(token, deserialized);
} }
TEST(UnguessableTokenTest, VerifyToString) { // Common case (~88% of the time) - no leading zeroes in high_ nor low_.
TEST(UnguessableTokenTest, VerifyToString1) {
UnguessableToken token =
UnguessableToken::Deserialize(0x1234567890ABCDEF, 0xFEDCBA0987654321);
std::string expected = "1234567890ABCDEFFEDCBA0987654321";
EXPECT_EQ(expected, token.ToString());
std::string expected_stream = "(1234567890ABCDEFFEDCBA0987654321)";
std::stringstream stream;
stream << token;
EXPECT_EQ(expected_stream, stream.str());
}
// Less common case - leading zeroes in high_ or low_ (testing with both).
TEST(UnguessableTokenTest, VerifyToString2) {
UnguessableToken token = UnguessableToken::Deserialize(0x123, 0xABC); UnguessableToken token = UnguessableToken::Deserialize(0x123, 0xABC);
std::string expected = "0000012300000ABC"; std::string expected = "00000000000001230000000000000ABC";
EXPECT_EQ(expected, token.ToString()); EXPECT_EQ(expected, token.ToString());
std::string expected_stream = "(0000012300000ABC)"; std::string expected_stream = "(00000000000001230000000000000ABC)";
std::stringstream stream; std::stringstream stream;
stream << token; stream << token;
EXPECT_EQ(expected_stream, stream.str()); EXPECT_EQ(expected_stream, stream.str());
} }
TEST(UnguessableTokenTest, VerifyToStringUniqueness) {
const UnguessableToken token1 =
UnguessableToken::Deserialize(0x0000000012345678, 0x0000000123456789);
const UnguessableToken token2 =
UnguessableToken::Deserialize(0x0000000123456781, 0x0000000023456789);
EXPECT_NE(token1.ToString(), token2.ToString());
}
TEST(UnguessableTokenTest, VerifySmallerThanOperator) { TEST(UnguessableTokenTest, VerifySmallerThanOperator) {
// Deserialize is used for testing purposes. // Deserialize is used for testing purposes.
// Use UnguessableToken::Create() in production code instead. // Use UnguessableToken::Create() in production code instead.
......
...@@ -22,16 +22,19 @@ TEST(LocalSurfaceIdTest, VerifyToString) { ...@@ -22,16 +22,19 @@ TEST(LocalSurfaceIdTest, VerifyToString) {
const viz::LocalSurfaceId small_local_surface_id(11, 22, small_token); const viz::LocalSurfaceId small_local_surface_id(11, 22, small_token);
const std::string verbose_expected = const std::string verbose_expected =
"LocalSurfaceId(11, 22, 0011111100000000)"; "LocalSurfaceId(11, 22, " + token.ToString() + ")";
const std::string brief_expected = "LocalSurfaceId(11, 22, 0011...)"; const std::string brief_expected =
"LocalSurfaceId(11, 22, " + token.ToString().substr(0, 4) + "...)";
const std::string big_verbose_expected = const std::string big_verbose_expected =
"LocalSurfaceId(11, 22, 123456789ABCABCABC)"; "LocalSurfaceId(11, 22, " + big_token.ToString() + ")";
const std::string big_brief_expected = "LocalSurfaceId(11, 22, 1234...)"; const std::string big_brief_expected =
"LocalSurfaceId(11, 22, " + big_token.ToString().substr(0, 4) + "...)";
const std::string small_verbose_expected = const std::string small_verbose_expected =
"LocalSurfaceId(11, 22, 0000000000000001)"; "LocalSurfaceId(11, 22, " + small_token.ToString() + ")";
const std::string small_brief_expected = "LocalSurfaceId(11, 22, 0000...)"; const std::string small_brief_expected =
"LocalSurfaceId(11, 22, " + small_token.ToString().substr(0, 4) + "...)";
int previous_log_lvl = logging::GetMinLogLevel(); int previous_log_lvl = logging::GetMinLogLevel();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment