Commit 87119864 authored by benchan's avatar benchan Committed by Commit bot

Make GUID generation on POSIX compliant with GUID v4 format.

Per RFC 4122 section 4.4, the format of GUID version 4 must be
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, A, B].
This CL updates base::GenerateGUID() on POSIX to be compliant with the
GUID v4 format.

This fix is proposed by Alex Vakulenko <avakulenko@chromium.org>

BUG=None
TEST=base_unittests

Review URL: https://codereview.chromium.org/541633005

Cr-Commit-Position: refs/heads/master@{#293518}
parent d6f2fa20
......@@ -11,6 +11,20 @@ namespace base {
std::string GenerateGUID() {
uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() };
// Set the GUID to version 4 as described in RFC 4122, section 4.4.
// The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
// where y is one of [8, 9, A, B].
// Clear the version bits and set the version to 4:
sixteen_bytes[0] &= 0xffffffffffff0fffULL;
sixteen_bytes[0] |= 0x0000000000004000ULL;
// Set the two most significant bits (bits 6 and 7) of the
// clock_seq_hi_and_reserved to zero and one, respectively:
sixteen_bytes[1] &= 0x3fffffffffffffffULL;
sixteen_bytes[1] |= 0x8000000000000000ULL;
return RandomDataToGUIDString(sixteen_bytes);
}
......
......@@ -10,6 +10,20 @@
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
namespace {
bool IsGUIDv4(const std::string& guid) {
// The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
// where y is one of [8, 9, A, B].
return base::IsValidGUID(guid) && guid[14] == '4' &&
(guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' ||
guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b');
}
} // namespace
TEST(GUIDTest, GUIDGeneratesAllZeroes) {
uint64 bytes[] = { 0, 0 };
std::string clientid = base::RandomDataToGUIDString(bytes);
......@@ -41,5 +55,9 @@ TEST(GUIDTest, GUIDBasicUniqueness) {
EXPECT_EQ(36U, guid1.length());
EXPECT_EQ(36U, guid2.length());
EXPECT_NE(guid1, guid2);
#if defined(OS_POSIX)
EXPECT_TRUE(IsGUIDv4(guid1));
EXPECT_TRUE(IsGUIDv4(guid2));
#endif
}
}
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