blob: 4eda7f68a374ce5ce60f1f891cd548293b76c01c [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
7#include <limits>
8
benchan57d121ac2014-09-05 05:08:329#include "base/strings/string_util.h"
[email protected]3469e7e2010-10-14 20:34:5910#include "testing/gtest/include/gtest/gtest.h"
11
12#if defined(OS_POSIX)
benchan87119862014-09-05 14:19:3913
14namespace {
15
16bool IsGUIDv4(const std::string& guid) {
17 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
18 // where y is one of [8, 9, A, B].
19 return base::IsValidGUID(guid) && guid[14] == '4' &&
20 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' ||
21 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b');
22}
23
24} // namespace
25
26
[email protected]3469e7e2010-10-14 20:34:5927TEST(GUIDTest, GUIDGeneratesAllZeroes) {
28 uint64 bytes[] = { 0, 0 };
[email protected]7e49ad32012-06-14 14:22:0729 std::string clientid = base::RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5930 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
31}
32
33TEST(GUIDTest, GUIDGeneratesCorrectly) {
34 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
[email protected]7e49ad32012-06-14 14:22:0735 std::string clientid = base::RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5936 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
37}
38#endif
39
40TEST(GUIDTest, GUIDCorrectlyFormatted) {
41 const int kIterations = 10;
42 for (int it = 0; it < kIterations; ++it) {
[email protected]7e49ad32012-06-14 14:22:0743 std::string guid = base::GenerateGUID();
44 EXPECT_TRUE(base::IsValidGUID(guid));
benchan57d121ac2014-09-05 05:08:3245 EXPECT_TRUE(base::IsValidGUID(base::StringToLowerASCII(guid)));
46 EXPECT_TRUE(base::IsValidGUID(StringToUpperASCII(guid)));
[email protected]3469e7e2010-10-14 20:34:5947 }
48}
49
50TEST(GUIDTest, GUIDBasicUniqueness) {
51 const int kIterations = 10;
52 for (int it = 0; it < kIterations; ++it) {
[email protected]7e49ad32012-06-14 14:22:0753 std::string guid1 = base::GenerateGUID();
54 std::string guid2 = base::GenerateGUID();
[email protected]3469e7e2010-10-14 20:34:5955 EXPECT_EQ(36U, guid1.length());
56 EXPECT_EQ(36U, guid2.length());
57 EXPECT_NE(guid1, guid2);
benchan87119862014-09-05 14:19:3958#if defined(OS_POSIX)
59 EXPECT_TRUE(IsGUIDv4(guid1));
60 EXPECT_TRUE(IsGUIDv4(guid2));
61#endif
[email protected]3469e7e2010-10-14 20:34:5962 }
63}