diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-15 23:38:25 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-15 23:38:25 +0000 |
commit | 3eb0d8f7bea3e4e54836fa506f4034bcbd3ef4c2 (patch) | |
tree | bb7afc78bd7ebb9fea5ed3ed459880511501fb2a /chrome/common | |
parent | 9a7ed36f8ad5d33725064021464180a808a3a5d2 (diff) | |
download | chromium_src-3eb0d8f7bea3e4e54836fa506f4034bcbd3ef4c2.zip chromium_src-3eb0d8f7bea3e4e54836fa506f4034bcbd3ef4c2.tar.gz chromium_src-3eb0d8f7bea3e4e54836fa506f4034bcbd3ef4c2.tar.bz2 |
Move GUID utils to src/chrome/common.
BUG=None
TEST=compiles,unittests
Review URL: http://codereview.chromium.org/5849001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69347 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/guid.cc | 32 | ||||
-rw-r--r-- | chrome/common/guid.h | 32 | ||||
-rw-r--r-- | chrome/common/guid_posix.cc | 28 | ||||
-rw-r--r-- | chrome/common/guid_unittest.cc | 42 | ||||
-rw-r--r-- | chrome/common/guid_win.cc | 38 |
5 files changed, 172 insertions, 0 deletions
diff --git a/chrome/common/guid.cc b/chrome/common/guid.cc new file mode 100644 index 0000000..a6b4b09 --- /dev/null +++ b/chrome/common/guid.cc @@ -0,0 +1,32 @@ +// 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. + +#include "chrome/common/guid.h" + +#include "base/rand_util.h" +#include "base/stringprintf.h" + +namespace guid { + +bool IsValidGUID(const std::string& guid) { + const size_t kGUIDLength = 36U; + if (guid.length() != kGUIDLength) + return false; + + std::string hexchars = "0123456789ABCDEF"; + for (uint32 i = 0; i < guid.length(); ++i) { + char current = guid[i]; + if (i == 8 || i == 13 || i == 18 || i == 23) { + if (current != '-') + return false; + } else { + if (hexchars.find(current) == std::string::npos) + return false; + } + } + + return true; +} + +} // namespace guid diff --git a/chrome/common/guid.h b/chrome/common/guid.h new file mode 100644 index 0000000..9121c9e --- /dev/null +++ b/chrome/common/guid.h @@ -0,0 +1,32 @@ +// 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_COMMON_GUID_H_ +#define CHROME_COMMON_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(); + +// Returns true if the input string conforms to the GUID format. +bool IsValidGUID(const std::string& guid); + +#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_COMMON_GUID_H_ diff --git a/chrome/common/guid_posix.cc b/chrome/common/guid_posix.cc new file mode 100644 index 0000000..f21788b --- /dev/null +++ b/chrome/common/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/common/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/common/guid_unittest.cc b/chrome/common/guid_unittest.cc new file mode 100644 index 0000000..628bafc --- /dev/null +++ b/chrome/common/guid_unittest.cc @@ -0,0 +1,42 @@ +// 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. + +#include "chrome/common/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_TRUE(guid::IsValidGUID(guid)); + } +} + +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/common/guid_win.cc b/chrome/common/guid_win.cc new file mode 100644 index 0000000..0d95cd6 --- /dev/null +++ b/chrome/common/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/common/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 |