diff options
-rw-r--r-- | base/rand_util.h | 5 | ||||
-rw-r--r-- | base/rand_util_posix.cc | 17 | ||||
-rw-r--r-- | base/rand_util_unittest.cc | 47 | ||||
-rw-r--r-- | base/rand_util_win.cc | 20 | ||||
-rw-r--r-- | chrome/browser/metrics/metrics_service.cc | 41 | ||||
-rw-r--r-- | chrome/browser/metrics/metrics_service.h | 6 | ||||
-rw-r--r-- | chrome/browser/metrics/metrics_service_unittest.cc | 22 |
7 files changed, 96 insertions, 62 deletions
diff --git a/base/rand_util.h b/base/rand_util.h index 699fc7f..d298134 100644 --- a/base/rand_util.h +++ b/base/rand_util.h @@ -6,6 +6,8 @@ #define BASE_RAND_UTIL_H_ #pragma once +#include <string> + #include "base/basictypes.h" namespace base { @@ -26,6 +28,9 @@ uint64 RandGenerator(uint64 max); // Returns a random double in range [0, 1). Thread-safe. double RandDouble(); +// Generate a 128-bit random GUID of the form: "%08X-%04X-%04X-%04X-%012llX". +std::string GenerateGUID(); + } // namespace base #endif // BASE_RAND_UTIL_H_ diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc index 43dfd1e..6a0a203 100644 --- a/base/rand_util_posix.cc +++ b/base/rand_util_posix.cc @@ -12,6 +12,7 @@ #include "base/file_util.h" #include "base/lazy_instance.h" #include "base/logging.h" +#include "base/stringprintf.h" namespace { @@ -54,6 +55,22 @@ uint64 RandUint64() { return number; } +// TODO(cmasone): Once we're comfortable this works, migrate Windows code to +// use this as well. +std::string RandomBytesToGUIDString(const uint64 bytes[2]) { + return StringPrintf("%08X-%04X-%04X-%04X-%012llX", + static_cast<unsigned int>(bytes[0] >> 32), + static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff), + static_cast<unsigned int>(bytes[0] & 0x0000ffff), + static_cast<unsigned int>(bytes[1] >> 48), + bytes[1] & 0x0000ffffffffffffULL); +} + +std::string GenerateGUID() { + uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() }; + return RandomBytesToGUIDString(sixteen_bytes); +} + } // namespace base int GetUrandomFD(void) { diff --git a/base/rand_util_unittest.cc b/base/rand_util_unittest.cc index cbc338a..112099f 100644 --- a/base/rand_util_unittest.cc +++ b/base/rand_util_unittest.cc @@ -35,3 +35,50 @@ TEST(RandUtilTest, RandGeneratorForRandomShuffle) { EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), std::numeric_limits<int64>::max()); } + +#if defined(OS_POSIX) +// For unit testing purposes only. Do not use outside of tests. +namespace base { +extern std::string RandomBytesToGUIDString(const uint64 bytes[2]); +} // base + +TEST(RandUtilTest, GUIDGeneratesAllZeroes) { + uint64 bytes[] = { 0, 0 }; + std::string clientid = base::RandomBytesToGUIDString(bytes); + EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); +} + +TEST(RandUtilTest, GUIDGeneratesCorrectly) { + uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; + std::string clientid = base::RandomBytesToGUIDString(bytes); + EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); +} +#endif + +TEST(RandUtilTest, GUIDCorrectlyFormatted) { + const int kIterations = 10; + for (int it = 0; it < kIterations; ++it) { + std::string guid = base::GenerateGUID(); + EXPECT_EQ(36U, guid.length()); + std::string hexchars = "0123456789ABCDEF"; + for (uint32 i = 0; i < guid.length(); ++i) { + char current = guid.at(i); + if (i == 8 || i == 13 || i == 18 || i == 23) { + EXPECT_EQ('-', current); + } else { + EXPECT_TRUE(std::string::npos != hexchars.find(current)); + } + } + } +} + +TEST(RandUtilTest, GUIDBasicUniqueness) { + const int kIterations = 10; + for (int it = 0; it < kIterations; ++it) { + std::string guid1 = base::GenerateGUID(); + std::string guid2 = base::GenerateGUID(); + EXPECT_EQ(36U, guid1.length()); + EXPECT_EQ(36U, guid2.length()); + EXPECT_NE(guid1, guid2); + } +} diff --git a/base/rand_util_win.cc b/base/rand_util_win.cc index ec0411e..5437177 100644 --- a/base/rand_util_win.cc +++ b/base/rand_util_win.cc @@ -6,8 +6,13 @@ #include <stdlib.h> +#include <objbase.h> +#include <windows.h> + #include "base/basictypes.h" #include "base/logging.h" +#include "base/string_util.h" +#include "base/utf_string_conversions.h" namespace { @@ -27,4 +32,19 @@ uint64 RandUint64() { return (static_cast<uint64>(first_half) << 32) + second_half; } +std::string GenerateGUID() { + const int kGUIDSize = 39; + + GUID guid; + HRESULT guid_result = CoCreateGuid(&guid); + DCHECK(SUCCEEDED(guid_result)); + + std::wstring guid_string; + int result = StringFromGUID2(guid, + WriteInto(&guid_string, kGUIDSize), kGUIDSize); + DCHECK(result == kGUIDSize); + + return WideToUTF8(guid_string.substr(1, guid_string.length() - 2)); +} + } // namespace base diff --git a/chrome/browser/metrics/metrics_service.cc b/chrome/browser/metrics/metrics_service.cc index b6d4c3f..e4c9a12 100644 --- a/chrome/browser/metrics/metrics_service.cc +++ b/chrome/browser/metrics/metrics_service.cc @@ -158,15 +158,11 @@ #include "chrome/browser/metrics/metrics_service.h" -#if defined(OS_WIN) -#include <windows.h> -#include <objbase.h> -#endif - #include "base/base64.h" #include "base/command_line.h" #include "base/histogram.h" #include "base/md5.h" +#include "base/rand_util.h" #include "base/string_number_conversions.h" #include "base/thread.h" #include "base/utf_string_conversions.h" @@ -192,10 +188,6 @@ #include "webkit/glue/plugins/webplugininfo.h" #include "libxml/xmlwriter.h" -#if !defined(OS_WIN) -#include "base/rand_util.h" -#endif - // TODO(port): port browser_distribution.h. #if !defined(OS_POSIX) #include "chrome/installer/util/browser_distribution.h" @@ -816,38 +808,9 @@ void MetricsService::OnInitTaskComplete( } std::string MetricsService::GenerateClientID() { -#if defined(OS_WIN) - const int kGUIDSize = 39; - - GUID guid; - HRESULT guid_result = CoCreateGuid(&guid); - DCHECK(SUCCEEDED(guid_result)); - - std::wstring guid_string; - int result = StringFromGUID2(guid, - WriteInto(&guid_string, kGUIDSize), kGUIDSize); - DCHECK(result == kGUIDSize); - - return WideToUTF8(guid_string.substr(1, guid_string.length() - 2)); -#else - uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() }; - return RandomBytesToGUIDString(sixteen_bytes); -#endif + return base::GenerateGUID(); } -#if defined(OS_POSIX) -// TODO(cmasone): Once we're comfortable this works, migrate Windows code to -// use this as well. -std::string MetricsService::RandomBytesToGUIDString(const uint64 bytes[2]) { - return StringPrintf("%08X-%04X-%04X-%04X-%012llX", - static_cast<unsigned int>(bytes[0] >> 32), - static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff), - static_cast<unsigned int>(bytes[0] & 0x0000ffff), - static_cast<unsigned int>(bytes[1] >> 48), - bytes[1] & 0x0000ffffffffffffULL); -} -#endif - //------------------------------------------------------------------------------ // State save methods diff --git a/chrome/browser/metrics/metrics_service.h b/chrome/browser/metrics/metrics_service.h index 650935a..f1cfbb2 100644 --- a/chrome/browser/metrics/metrics_service.h +++ b/chrome/browser/metrics/metrics_service.h @@ -192,12 +192,6 @@ class MetricsService : public NotificationObserver, // Generates a new client ID to use to identify self to metrics server. static std::string GenerateClientID(); -#if defined(OS_POSIX) - // Generates a new client ID to use to identify self to metrics server, - // given 128 bits of randomness. - static std::string RandomBytesToGUIDString(const uint64 bytes[2]); -#endif - // Schedule the next save of LocalState information. This is called // automatically by the task that performs each save to schedule the next one. void ScheduleNextStateSave(); diff --git a/chrome/browser/metrics/metrics_service_unittest.cc b/chrome/browser/metrics/metrics_service_unittest.cc index a5ec080..9e7b463 100644 --- a/chrome/browser/metrics/metrics_service_unittest.cc +++ b/chrome/browser/metrics/metrics_service_unittest.cc @@ -13,18 +13,12 @@ #include "testing/gtest/include/gtest/gtest.h" -#if defined(OS_POSIX) && defined(LINUX2) -TEST(MetricsServiceTest, ClientIdGeneratesAllZeroes) { - uint64 bytes[] = { 0, 0 }; - std::string clientid = MetricsService::RandomBytesToGUIDString(bytes); - EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); -} -TEST(MetricsServiceTest, ClientIdGeneratesCorrectly) { - uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; - std::string clientid = MetricsService::RandomBytesToGUIDString(bytes); - EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); -} +class MetricsServiceTest : public ::testing::Test { +}; + +static const size_t kMaxLocalListSize = 3; +// Ensure the ClientId is formatted as expected. TEST(MetricsServiceTest, ClientIdCorrectlyFormatted) { std::string clientid = MetricsService::GenerateClientID(); EXPECT_EQ(36U, clientid.length()); @@ -38,12 +32,6 @@ TEST(MetricsServiceTest, ClientIdCorrectlyFormatted) { } } } -#endif - -class MetricsServiceTest : public ::testing::Test { -}; - -static const size_t kMaxLocalListSize = 3; // Store and retrieve empty list. TEST(MetricsServiceTest, EmptyLogList) { |