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/guid.h | 29 | ||||
-rw-r--r-- | chrome/browser/guid_posix.cc | 28 | ||||
-rw-r--r-- | chrome/browser/guid_unittest.cc | 51 | ||||
-rw-r--r-- | chrome/browser/guid_win.cc | 38 | ||||
-rw-r--r-- | chrome/browser/metrics/metrics_service.cc | 4 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 3 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
11 files changed, 152 insertions, 91 deletions
diff --git a/base/rand_util.h b/base/rand_util.h index d298134..699fc7f 100644 --- a/base/rand_util.h +++ b/base/rand_util.h @@ -6,8 +6,6 @@ #define BASE_RAND_UTIL_H_ #pragma once -#include <string> - #include "base/basictypes.h" namespace base { @@ -28,9 +26,6 @@ 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 6a0a203..43dfd1e 100644 --- a/base/rand_util_posix.cc +++ b/base/rand_util_posix.cc @@ -12,7 +12,6 @@ #include "base/file_util.h" #include "base/lazy_instance.h" #include "base/logging.h" -#include "base/stringprintf.h" namespace { @@ -55,22 +54,6 @@ 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 112099f..cbc338a 100644 --- a/base/rand_util_unittest.cc +++ b/base/rand_util_unittest.cc @@ -35,50 +35,3 @@ 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 5437177..ec0411e 100644 --- a/base/rand_util_win.cc +++ b/base/rand_util_win.cc @@ -6,13 +6,8 @@ #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 { @@ -32,19 +27,4 @@ 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/guid.h b/chrome/browser/guid.h new file mode 100644 index 0000000..bc47ed8 --- /dev/null +++ b/chrome/browser/guid.h @@ -0,0 +1,29 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_GUID_H_ +#define CHROME_BROWSER_GUID_H_ +#pragma once + +#include <string> + +#include "base/basictypes.h" +#include "build/build_config.h" + +namespace guid { + +// Generate a 128-bit random GUID of the form: "%08X-%04X-%04X-%04X-%012llX". +// If GUID generation fails an empty string is returned. +// The POSIX implementation uses psuedo random number generation to create +// the GUID. The Windows implementation uses system services. +std::string GenerateGUID(); + +#if defined(OS_POSIX) +// For unit testing purposes only. Do not use outside of tests. +std::string RandomDataToGUIDString(const uint64 bytes[2]); +#endif + +} // namespace guid + +#endif // CHROME_BROWSER_GUID_H_ diff --git a/chrome/browser/guid_posix.cc b/chrome/browser/guid_posix.cc new file mode 100644 index 0000000..48d7198 --- /dev/null +++ b/chrome/browser/guid_posix.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2008 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 "chrome/browser/guid.h" + +#include "base/rand_util.h" +#include "base/stringprintf.h" + +namespace guid { + +std::string GenerateGUID() { + uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() }; + return RandomDataToGUIDString(sixteen_bytes); +} + +// TODO(cmasone): Once we're comfortable this works, migrate Windows code to +// use this as well. +std::string RandomDataToGUIDString(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); +} + +} // namespace guid diff --git a/chrome/browser/guid_unittest.cc b/chrome/browser/guid_unittest.cc new file mode 100644 index 0000000..ebbce8f --- /dev/null +++ b/chrome/browser/guid_unittest.cc @@ -0,0 +1,51 @@ +// Copyright (c) 2008 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 "chrome/browser/guid.h" + +#include <limits> + +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_POSIX) +TEST(GUIDTest, GUIDGeneratesAllZeroes) { + uint64 bytes[] = { 0, 0 }; + std::string clientid = guid::RandomDataToGUIDString(bytes); + EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); +} + +TEST(GUIDTest, GUIDGeneratesCorrectly) { + uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; + std::string clientid = guid::RandomDataToGUIDString(bytes); + EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); +} +#endif + +TEST(GUIDTest, GUIDCorrectlyFormatted) { + const int kIterations = 10; + for (int it = 0; it < kIterations; ++it) { + std::string guid = guid::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(GUIDTest, GUIDBasicUniqueness) { + const int kIterations = 10; + for (int it = 0; it < kIterations; ++it) { + std::string guid1 = guid::GenerateGUID(); + std::string guid2 = guid::GenerateGUID(); + EXPECT_EQ(36U, guid1.length()); + EXPECT_EQ(36U, guid2.length()); + EXPECT_NE(guid1, guid2); + } +} diff --git a/chrome/browser/guid_win.cc b/chrome/browser/guid_win.cc new file mode 100644 index 0000000..115e806 --- /dev/null +++ b/chrome/browser/guid_win.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2008 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 "chrome/browser/guid.h" + +#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 guid { + +std::string GenerateGUID() { + const int kGUIDSize = 39; + + GUID guid; + HRESULT guid_result = CoCreateGuid(&guid); + DCHECK(SUCCEEDED(guid_result)); + if (!SUCCEEDED(guid_result)) + return std::string(); + + std::wstring guid_string; + int result = StringFromGUID2(guid, + WriteInto(&guid_string, kGUIDSize), kGUIDSize); + DCHECK(result == kGUIDSize); + if (result != kGUIDSize) + return std::string(); + + return WideToUTF8(guid_string.substr(1, guid_string.length() - 2)); +} + +} // namespace guid diff --git a/chrome/browser/metrics/metrics_service.cc b/chrome/browser/metrics/metrics_service.cc index 2687eba..37b79b7 100644 --- a/chrome/browser/metrics/metrics_service.cc +++ b/chrome/browser/metrics/metrics_service.cc @@ -162,7 +162,6 @@ #include "base/command_line.h" #include "base/md5.h" #include "base/metrics/histogram.h" -#include "base/rand_util.h" #include "base/string_number_conversions.h" #include "base/thread.h" #include "base/utf_string_conversions.h" @@ -170,6 +169,7 @@ #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/guid.h" #include "chrome/browser/load_notification_details.h" #include "chrome/browser/memory_details.h" #include "chrome/browser/metrics/histogram_synchronizer.h" @@ -808,7 +808,7 @@ void MetricsService::OnInitTaskComplete( } std::string MetricsService::GenerateClientID() { - return base::GenerateGUID(); + return guid::GenerateGUID(); } //------------------------------------------------------------------------------ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 50cac0e..950e9a5 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1947,6 +1947,9 @@ 'browser/gtk/update_recommended_dialog.h', 'browser/gtk/view_id_util.cc', 'browser/gtk/view_id_util.h', + 'browser/guid.h', + 'browser/guid_posix.cc', + 'browser/guid_win.cc', 'browser/hang_monitor/hung_plugin_action.cc', 'browser/hang_monitor/hung_plugin_action.h', 'browser/hang_monitor/hung_window_detector.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index a9a8fc2..2bf6aa8 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1298,6 +1298,7 @@ 'browser/gtk/reload_button_gtk_unittest.cc', 'browser/gtk/status_icons/status_tray_gtk_unittest.cc', 'browser/gtk/tabs/tab_renderer_gtk_unittest.cc', + 'browser/guid_unittest.cc', 'browser/history/expire_history_backend_unittest.cc', 'browser/history/history_backend_unittest.cc', 'browser/history/history_querying_unittest.cc', |