blob: b6d976d8e0d3f7d85f921080d0b23fb0846f5794 [file] [log] [blame]
[email protected]7e49ad32012-06-14 14:22:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]3469e7e2010-10-14 20:34:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7e49ad32012-06-14 14:22:075#include "base/guid.h"
[email protected]3469e7e2010-10-14 20:34:596
avi9b6f42932015-12-26 22:15:147#include <stdint.h>
8
[email protected]3469e7e2010-10-14 20:34:599#include <limits>
10
benchan57d121ac2014-09-05 05:08:3211#include "base/strings/string_util.h"
avi9b6f42932015-12-26 22:15:1412#include "build/build_config.h"
[email protected]3469e7e2010-10-14 20:34:5913#include "testing/gtest/include/gtest/gtest.h"
14
brettw7622fbed2015-06-09 20:20:1415namespace base {
16
[email protected]3469e7e2010-10-14 20:34:5917#if defined(OS_POSIX)
benchan87119862014-09-05 14:19:3918
19namespace {
20
21bool IsGUIDv4(const std::string& guid) {
22 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
23 // where y is one of [8, 9, A, B].
brettw7622fbed2015-06-09 20:20:1424 return IsValidGUID(guid) && guid[14] == '4' &&
benchan87119862014-09-05 14:19:3925 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' ||
26 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b');
27}
28
29} // namespace
30
[email protected]3469e7e2010-10-14 20:34:5931TEST(GUIDTest, GUIDGeneratesAllZeroes) {
avi9b6f42932015-12-26 22:15:1432 uint64_t bytes[] = {0, 0};
brettw7622fbed2015-06-09 20:20:1433 std::string clientid = RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5934 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
35}
36
37TEST(GUIDTest, GUIDGeneratesCorrectly) {
avi9b6f42932015-12-26 22:15:1438 uint64_t bytes[] = {0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL};
brettw7622fbed2015-06-09 20:20:1439 std::string clientid = RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5940 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
41}
42#endif
43
44TEST(GUIDTest, GUIDCorrectlyFormatted) {
45 const int kIterations = 10;
46 for (int it = 0; it < kIterations; ++it) {
brettw7622fbed2015-06-09 20:20:1447 std::string guid = GenerateGUID();
48 EXPECT_TRUE(IsValidGUID(guid));
brettwc15100c2015-08-06 22:54:1649 EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid)));
50 EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid)));
[email protected]3469e7e2010-10-14 20:34:5951 }
52}
53
54TEST(GUIDTest, GUIDBasicUniqueness) {
55 const int kIterations = 10;
56 for (int it = 0; it < kIterations; ++it) {
brettw7622fbed2015-06-09 20:20:1457 std::string guid1 = GenerateGUID();
58 std::string guid2 = GenerateGUID();
[email protected]3469e7e2010-10-14 20:34:5959 EXPECT_EQ(36U, guid1.length());
60 EXPECT_EQ(36U, guid2.length());
61 EXPECT_NE(guid1, guid2);
benchan87119862014-09-05 14:19:3962#if defined(OS_POSIX)
63 EXPECT_TRUE(IsGUIDv4(guid1));
64 EXPECT_TRUE(IsGUIDv4(guid2));
65#endif
[email protected]3469e7e2010-10-14 20:34:5966 }
67}
brettw7622fbed2015-06-09 20:20:1468
69} // namespace base