diff options
author | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-13 23:49:25 +0000 |
---|---|---|
committer | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-13 23:49:25 +0000 |
commit | f2c859a17e291cd481f89b55b79ac5450a81fcd0 (patch) | |
tree | fe38ce3e7712809d56967dc00316d85077ccc496 /base/rand_util_unittest.cc | |
parent | c010c40a4aca2cb281c2e7a3c75cec24d2558661 (diff) | |
download | chromium_src-f2c859a17e291cd481f89b55b79ac5450a81fcd0.zip chromium_src-f2c859a17e291cd481f89b55b79ac5450a81fcd0.tar.gz chromium_src-f2c859a17e291cd481f89b55b79ac5450a81fcd0.tar.bz2 |
Factoring GUID generation from metrics to base
Factors GUID generation into base/rand_util. The Autofill feature is in need of this utility so am factoring GUID generation out of metrics and moving to the commons.
BUG=58813
TEST=RandUtilTest.GUIDGeneratesAllZeroes, RandUtilTest.GUIDGeneratesCorrectly, RandUtilTest.GUIDCorrectlyFormatted, MetricsServiceTest.ClientIdCorrectlyFormatted
Review URL: http://codereview.chromium.org/3800001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62480 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/rand_util_unittest.cc')
-rw-r--r-- | base/rand_util_unittest.cc | 47 |
1 files changed, 47 insertions, 0 deletions
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); + } +} |