0

base/uuid: get rid of FormatRandomDataAsV4

This is an odd function: it uses a subset of the data provided to it to
produce a V4 UUID. It also has no production users, so this change
deletes it and its ForTesting variant. The only use of the ForTesting
variant outside of the unit tests for base/uuid itself was in
SavedTabGroupTest, where it was being used to produce two distinct
UUIDs. Since the probability of two randomly-generated UUIDs colliding
in a test is astronomically remote (literally) and there are many
hundreds of other tests that already use random UUIDs each run, this
change migrates those tests to using random UUIDs as well.

Bug: None
Change-Id: Iba2c7413b7452340486cd4c2c11f11f57958ca76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6442401
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Darryl James <dljames@chromium.org>
Commit-Queue: Elly FJ <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1444920}
This commit is contained in:
Elly
2025-04-09 13:41:09 -07:00
committed by Chromium LUCI CQ
parent da02a608f4
commit e0faa4c786
4 changed files with 5 additions and 74 deletions
base
components/saved_tab_groups/public

@ -78,18 +78,6 @@ Uuid Uuid::GenerateRandomV4() {
return FormatRandomDataAsV4Impl(sixteen_bytes);
}
// static
Uuid Uuid::FormatRandomDataAsV4(
base::span<const uint8_t, 16> input,
base::PassKey<content::FileSystemAccessManagerImpl> /*pass_key*/) {
return FormatRandomDataAsV4Impl(input);
}
// static
Uuid Uuid::FormatRandomDataAsV4ForTesting(base::span<const uint8_t, 16> input) {
return FormatRandomDataAsV4Impl(input);
}
// static
Uuid Uuid::FormatRandomDataAsV4Impl(base::span<const uint8_t, 16> input) {
DCHECK_EQ(input.size_bytes(), kGuidV4InputLength);
@ -105,8 +93,8 @@ Uuid Uuid::FormatRandomDataAsV4Impl(base::span<const uint8_t, 16> input) {
sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
sixteen_bytes[0] |= 0x00000000'00004000ULL;
// Set the two most significant bits (bits 6 and 7) of the
// clock_seq_hi_and_reserved to zero and one, respectively:
// Clear bit 65 and set bit 64, to set the 'var' field to 0b10 per RFC 9562
// section 5.4.
sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
sixteen_bytes[1] |= 0x80000000'00000000ULL;

@ -15,12 +15,6 @@
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
namespace content {
class FileSystemAccessManagerImpl;
}
namespace base {
@ -38,21 +32,6 @@ class BASE_EXPORT Uuid {
// UnguessableToken for greater type-safety if Uuid format is unnecessary.
static Uuid GenerateRandomV4();
// Formats a sequence of 16 random bytes as a Uuid in the form of version 4.
// `input` must:
// - have been randomly generated (e.g. created from an UnguessableToken), and
// - be of length 16 (this is checked at compile-time).
// Despite taking 128 bits of randomness, certain bits will always be
// masked over to adhere to the V4 Uuid format.
// Useful in cases where an opaque identifier that is generated from stable
// inputs needs to be formatted as a V4 Uuid. Currently only exposed to the
// File System Access API to return a V4 Uuid for the getUniqueId() method.
static Uuid FormatRandomDataAsV4(
base::span<const uint8_t, kGuidV4InputLength> input,
base::PassKey<content::FileSystemAccessManagerImpl> pass_key);
static Uuid FormatRandomDataAsV4ForTesting(
base::span<const uint8_t, kGuidV4InputLength> input);
// Returns a valid Uuid if the input string conforms to the Uuid format, and
// an invalid Uuid otherwise. Note that this does NOT check if the hexadecimal
// values "a" through "f" are in lower case characters.

@ -189,33 +189,4 @@ TEST(UuidTest, Compare) {
EXPECT_FALSE(guid_invalid >= guid);
}
TEST(UuidTest, FormatRandomDataAsV4) {
constexpr static const auto bytes1a =
std::to_array<uint64_t>({0x0123456789abcdefull, 0x5a5a5a5aa5a5a5a5ull});
static constexpr uint64_t bytes1b[] = {bytes1a[0], bytes1a[1]};
static constexpr uint64_t bytes2[] = {0xfffffffffffffffdull,
0xfffffffffffffffeull};
static constexpr uint64_t bytes3[] = {0xfffffffffffffffdull,
0xfffffffffffffffcull};
const Uuid guid1a =
Uuid::FormatRandomDataAsV4ForTesting(as_byte_span(bytes1a));
const Uuid guid1b =
Uuid::FormatRandomDataAsV4ForTesting(as_byte_span(bytes1b));
const Uuid guid2 = Uuid::FormatRandomDataAsV4ForTesting(as_byte_span(bytes2));
const Uuid guid3 = Uuid::FormatRandomDataAsV4ForTesting(as_byte_span(bytes3));
EXPECT_TRUE(guid1a.is_valid());
EXPECT_TRUE(guid1b.is_valid());
EXPECT_TRUE(guid2.is_valid());
EXPECT_TRUE(guid3.is_valid());
// The same input should give the same Uuid.
EXPECT_EQ(guid1a, guid1b);
EXPECT_NE(guid1a, guid2);
EXPECT_NE(guid1a, guid3);
EXPECT_NE(guid2, guid3);
}
} // namespace base

@ -27,13 +27,6 @@ MATCHER_P(HasTabGuid, guid, "") {
return arg.saved_tab_guid() == guid;
}
base::Uuid MakeUniqueGUID() {
static uint64_t unique_value = 0;
unique_value++;
uint64_t kBytes[] = {0, unique_value};
return base::Uuid::FormatRandomDataAsV4ForTesting(base::as_byte_span(kBytes));
}
LocalTabID MakeUniqueTabID() {
static uint32_t unique_value = 0;
return unique_value++;
@ -60,8 +53,8 @@ void AddTabToEndOfGroup(SavedTabGroup& group,
} // namespace
TEST(SavedTabGroupTest, GetTabByGUID) {
base::Uuid tab_1_saved_guid = MakeUniqueGUID();
base::Uuid tab_2_saved_guid = MakeUniqueGUID();
base::Uuid tab_1_saved_guid = base::Uuid::GenerateRandomV4();
base::Uuid tab_2_saved_guid = base::Uuid::GenerateRandomV4();
// create a group with a couple tabs
SavedTabGroup group = CreateDefaultEmptySavedTabGroup();
@ -338,7 +331,7 @@ TEST(SavedTabGroupTest, UpdateCreatorCacheGuid) {
}
TEST(SavedTabGroupTest, GetOriginatingTabGroupGuid) {
const base::Uuid kOriginatingTabGroupGuid = MakeUniqueGUID();
const base::Uuid kOriginatingTabGroupGuid = base::Uuid::GenerateRandomV4();
SavedTabGroup saved_group = CreateDefaultEmptySavedTabGroup();
saved_group.SetOriginatingTabGroupGuid(