| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/guid.h" | 5 #include "base/guid.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace base { |
| 13 |
| 12 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 bool IsGUIDv4(const std::string& guid) { | 18 bool IsGUIDv4(const std::string& guid) { |
| 17 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, | 19 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, |
| 18 // where y is one of [8, 9, A, B]. | 20 // where y is one of [8, 9, A, B]. |
| 19 return base::IsValidGUID(guid) && guid[14] == '4' && | 21 return IsValidGUID(guid) && guid[14] == '4' && |
| 20 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || | 22 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || |
| 21 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); | 23 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); |
| 22 } | 24 } |
| 23 | 25 |
| 24 } // namespace | 26 } // namespace |
| 25 | 27 |
| 26 | |
| 27 TEST(GUIDTest, GUIDGeneratesAllZeroes) { | 28 TEST(GUIDTest, GUIDGeneratesAllZeroes) { |
| 28 uint64 bytes[] = { 0, 0 }; | 29 uint64 bytes[] = { 0, 0 }; |
| 29 std::string clientid = base::RandomDataToGUIDString(bytes); | 30 std::string clientid = RandomDataToGUIDString(bytes); |
| 30 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); | 31 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); |
| 31 } | 32 } |
| 32 | 33 |
| 33 TEST(GUIDTest, GUIDGeneratesCorrectly) { | 34 TEST(GUIDTest, GUIDGeneratesCorrectly) { |
| 34 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; | 35 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; |
| 35 std::string clientid = base::RandomDataToGUIDString(bytes); | 36 std::string clientid = RandomDataToGUIDString(bytes); |
| 36 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); | 37 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); |
| 37 } | 38 } |
| 38 #endif | 39 #endif |
| 39 | 40 |
| 40 TEST(GUIDTest, GUIDCorrectlyFormatted) { | 41 TEST(GUIDTest, GUIDCorrectlyFormatted) { |
| 41 const int kIterations = 10; | 42 const int kIterations = 10; |
| 42 for (int it = 0; it < kIterations; ++it) { | 43 for (int it = 0; it < kIterations; ++it) { |
| 43 std::string guid = base::GenerateGUID(); | 44 std::string guid = GenerateGUID(); |
| 44 EXPECT_TRUE(base::IsValidGUID(guid)); | 45 EXPECT_TRUE(IsValidGUID(guid)); |
| 45 EXPECT_TRUE(base::IsValidGUID(base::StringToLowerASCII(guid))); | 46 EXPECT_TRUE(IsValidGUID(StringToLowerASCII(guid))); |
| 46 EXPECT_TRUE(base::IsValidGUID(StringToUpperASCII(guid))); | 47 EXPECT_TRUE(IsValidGUID(StringToUpperASCII(guid))); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 TEST(GUIDTest, GUIDBasicUniqueness) { | 51 TEST(GUIDTest, GUIDBasicUniqueness) { |
| 51 const int kIterations = 10; | 52 const int kIterations = 10; |
| 52 for (int it = 0; it < kIterations; ++it) { | 53 for (int it = 0; it < kIterations; ++it) { |
| 53 std::string guid1 = base::GenerateGUID(); | 54 std::string guid1 = GenerateGUID(); |
| 54 std::string guid2 = base::GenerateGUID(); | 55 std::string guid2 = GenerateGUID(); |
| 55 EXPECT_EQ(36U, guid1.length()); | 56 EXPECT_EQ(36U, guid1.length()); |
| 56 EXPECT_EQ(36U, guid2.length()); | 57 EXPECT_EQ(36U, guid2.length()); |
| 57 EXPECT_NE(guid1, guid2); | 58 EXPECT_NE(guid1, guid2); |
| 58 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
| 59 EXPECT_TRUE(IsGUIDv4(guid1)); | 60 EXPECT_TRUE(IsGUIDv4(guid1)); |
| 60 EXPECT_TRUE(IsGUIDv4(guid2)); | 61 EXPECT_TRUE(IsGUIDv4(guid2)); |
| 61 #endif | 62 #endif |
| 62 } | 63 } |
| 63 } | 64 } |
| 65 |
| 66 } // namespace base |
| OLD | NEW |