[email protected] | 7e49ad3 | 2012-06-14 14:22:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7e49ad3 | 2012-06-14 14:22:07 | [diff] [blame] | 5 | #include "base/guid.h" |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 9 | #include <limits> |
| 10 | |
benchan | 57d121ac | 2014-09-05 05:08:32 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 15 | namespace base { |
| 16 | |
benchan | 8711986 | 2014-09-05 14:19:39 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | bool IsGUIDv4(const std::string& guid) { |
| 20 | // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, |
| 21 | // where y is one of [8, 9, A, B]. |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 22 | return IsValidGUID(guid) && guid[14] == '4' && |
benchan | 8711986 | 2014-09-05 14:19:39 | [diff] [blame] | 23 | (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' || |
| 24 | guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b'); |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 29 | TEST(GUIDTest, GUIDGeneratesAllZeroes) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 30 | uint64_t bytes[] = {0, 0}; |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 31 | std::string clientid = RandomDataToGUIDString(bytes); |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 32 | EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); |
| 33 | } |
| 34 | |
| 35 | TEST(GUIDTest, GUIDGeneratesCorrectly) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 36 | uint64_t bytes[] = {0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL}; |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 37 | std::string clientid = RandomDataToGUIDString(bytes); |
kinuko | 4ad1f55 | 2016-04-27 11:00:20 | [diff] [blame^] | 38 | EXPECT_EQ("01234567-89ab-cdef-fedc-ba9876543210", clientid); |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 39 | } |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 40 | |
| 41 | TEST(GUIDTest, GUIDCorrectlyFormatted) { |
| 42 | const int kIterations = 10; |
| 43 | for (int it = 0; it < kIterations; ++it) { |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 44 | std::string guid = GenerateGUID(); |
| 45 | EXPECT_TRUE(IsValidGUID(guid)); |
kinuko | 4ad1f55 | 2016-04-27 11:00:20 | [diff] [blame^] | 46 | EXPECT_TRUE(IsValidGUIDOutputString(guid)); |
brettw | c15100c | 2015-08-06 22:54:16 | [diff] [blame] | 47 | EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid))); |
| 48 | EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid))); |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | TEST(GUIDTest, GUIDBasicUniqueness) { |
| 53 | const int kIterations = 10; |
| 54 | for (int it = 0; it < kIterations; ++it) { |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 55 | std::string guid1 = GenerateGUID(); |
| 56 | std::string guid2 = GenerateGUID(); |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 57 | EXPECT_EQ(36U, guid1.length()); |
| 58 | EXPECT_EQ(36U, guid2.length()); |
| 59 | EXPECT_NE(guid1, guid2); |
benchan | 8711986 | 2014-09-05 14:19:39 | [diff] [blame] | 60 | EXPECT_TRUE(IsGUIDv4(guid1)); |
| 61 | EXPECT_TRUE(IsGUIDv4(guid2)); |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 62 | } |
| 63 | } |
brettw | 7622fbed | 2015-06-09 20:20:14 | [diff] [blame] | 64 | |
| 65 | } // namespace base |