| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 #if defined(OS_POSIX) | |
| 18 | |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 bool IsGUIDv4(const std::string& guid) { | 19 bool IsGUIDv4(const std::string& guid) { |
| 22 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, | 20 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, |
| 23 // where y is one of [8, 9, A, B]. | 21 // where y is one of [8, 9, A, B]. |
| 24 return IsValidGUID(guid) && guid[14] == '4' && | 22 return IsValidGUID(guid) && guid[14] == '4' && |
| 25 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || | 23 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || |
| 26 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); | 24 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); |
| 27 } | 25 } |
| 28 | 26 |
| 29 } // namespace | 27 } // namespace |
| 30 | 28 |
| 31 TEST(GUIDTest, GUIDGeneratesAllZeroes) { | 29 TEST(GUIDTest, GUIDGeneratesAllZeroes) { |
| 32 uint64_t bytes[] = {0, 0}; | 30 uint64_t bytes[] = {0, 0}; |
| 33 std::string clientid = RandomDataToGUIDString(bytes); | 31 std::string clientid = RandomDataToGUIDString(bytes); |
| 34 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); | 32 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); |
| 35 } | 33 } |
| 36 | 34 |
| 37 TEST(GUIDTest, GUIDGeneratesCorrectly) { | 35 TEST(GUIDTest, GUIDGeneratesCorrectly) { |
| 38 uint64_t bytes[] = {0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL}; | 36 uint64_t bytes[] = {0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL}; |
| 39 std::string clientid = RandomDataToGUIDString(bytes); | 37 std::string clientid = RandomDataToGUIDString(bytes); |
| 40 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); | 38 EXPECT_EQ("01234567-89ab-cdef-fedc-ba9876543210", clientid); |
| 41 } | 39 } |
| 42 #endif | |
| 43 | 40 |
| 44 TEST(GUIDTest, GUIDCorrectlyFormatted) { | 41 TEST(GUIDTest, GUIDCorrectlyFormatted) { |
| 45 const int kIterations = 10; | 42 const int kIterations = 10; |
| 46 for (int it = 0; it < kIterations; ++it) { | 43 for (int it = 0; it < kIterations; ++it) { |
| 47 std::string guid = GenerateGUID(); | 44 std::string guid = GenerateGUID(); |
| 48 EXPECT_TRUE(IsValidGUID(guid)); | 45 EXPECT_TRUE(IsValidGUID(guid)); |
| 46 EXPECT_TRUE(IsValidGUIDOutputString(guid)); |
| 49 EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid))); | 47 EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid))); |
| 50 EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid))); | 48 EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid))); |
| 51 } | 49 } |
| 52 } | 50 } |
| 53 | 51 |
| 54 TEST(GUIDTest, GUIDBasicUniqueness) { | 52 TEST(GUIDTest, GUIDBasicUniqueness) { |
| 55 const int kIterations = 10; | 53 const int kIterations = 10; |
| 56 for (int it = 0; it < kIterations; ++it) { | 54 for (int it = 0; it < kIterations; ++it) { |
| 57 std::string guid1 = GenerateGUID(); | 55 std::string guid1 = GenerateGUID(); |
| 58 std::string guid2 = GenerateGUID(); | 56 std::string guid2 = GenerateGUID(); |
| 59 EXPECT_EQ(36U, guid1.length()); | 57 EXPECT_EQ(36U, guid1.length()); |
| 60 EXPECT_EQ(36U, guid2.length()); | 58 EXPECT_EQ(36U, guid2.length()); |
| 61 EXPECT_NE(guid1, guid2); | 59 EXPECT_NE(guid1, guid2); |
| 62 #if defined(OS_POSIX) | |
| 63 EXPECT_TRUE(IsGUIDv4(guid1)); | 60 EXPECT_TRUE(IsGUIDv4(guid1)); |
| 64 EXPECT_TRUE(IsGUIDv4(guid2)); | 61 EXPECT_TRUE(IsGUIDv4(guid2)); |
| 65 #endif | |
| 66 } | 62 } |
| 67 } | 63 } |
| 68 | 64 |
| 69 } // namespace base | 65 } // namespace base |
| OLD | NEW |