Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/guid.h" |
| 6 | |
| 7 | #include <limits> |
| 8 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | #if defined(OS_POSIX) |
| 16 | |
| 17 | namespace { |
| 18 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 19 | template <typename Char> |
| 20 | inline bool IsHexDigit(Char c) { |
| 21 | return (c >= '0' && c <= '9') || |
| 22 | (c >= 'A' && c <= 'F') || |
| 23 | (c >= 'a' && c <= 'f'); |
| 24 | } |
| 25 | |
| 26 | bool IsValidGUID(const std::string& guid) { |
| 27 | const size_t kGUIDLength = 36U; |
| 28 | if (guid.length() != kGUIDLength) |
| 29 | return false; |
| 30 | |
| 31 | for (size_t i = 0; i < guid.length(); ++i) { |
| 32 | char current = guid[i]; |
| 33 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 34 | if (current != '-') |
| 35 | return false; |
| 36 | } else { |
| 37 | if (!IsHexDigit(current)) |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 45 | bool IsGUIDv4(const std::string& guid) { |
| 46 | // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, |
| 47 | // where y is one of [8, 9, A, B]. |
| 48 | return IsValidGUID(guid) && guid[14] == '4' && |
| 49 | (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || |
| 50 | guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | TEST(GUIDTest, GUIDGeneratesAllZeroes) { |
| 56 | uint64 bytes[] = { 0, 0 }; |
| 57 | std::string clientid = RandomDataToGUIDString(bytes); |
| 58 | EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); |
| 59 | } |
| 60 | |
| 61 | TEST(GUIDTest, GUIDGeneratesCorrectly) { |
| 62 | uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; |
| 63 | std::string clientid = RandomDataToGUIDString(bytes); |
| 64 | EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | TEST(GUIDTest, GUIDCorrectlyFormatted) { |
| 69 | const int kIterations = 10; |
| 70 | for (int it = 0; it < kIterations; ++it) { |
| 71 | std::string guid = GenerateGUID(); |
| 72 | EXPECT_TRUE(IsValidGUID(guid)); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | TEST(GUIDTest, GUIDBasicUniqueness) { |
| 77 | const int kIterations = 10; |
| 78 | for (int it = 0; it < kIterations; ++it) { |
| 79 | std::string guid1 = GenerateGUID(); |
| 80 | std::string guid2 = GenerateGUID(); |
| 81 | EXPECT_EQ(36U, guid1.length()); |
| 82 | EXPECT_EQ(36U, guid2.length()); |
| 83 | EXPECT_NE(guid1, guid2); |
| 84 | #if defined(OS_POSIX) |
| 85 | EXPECT_TRUE(IsGUIDv4(guid1)); |
| 86 | EXPECT_TRUE(IsGUIDv4(guid2)); |
| 87 | #endif |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace base |