blob: 1c5d393c4d827717581cf3cb68626d6d95f69784 [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
brettw7622fbed2015-06-09 20:20:1412namespace base {
13
[email protected]3469e7e2010-10-14 20:34:5914#if defined(OS_POSIX)
benchan87119862014-09-05 14:19:3915
16namespace {
17
18bool IsGUIDv4(const std::string& guid) {
19 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
20 // where y is one of [8, 9, A, B].
brettw7622fbed2015-06-09 20:20:1421 return IsValidGUID(guid) && guid[14] == '4' &&
benchan87119862014-09-05 14:19:3922 (guid[19] == '8' || guid[19] == '9' || guid[19] == 'A' ||
23 guid[19] == 'a' || guid[19] == 'B' || guid[19] == 'b');
24}
25
26} // namespace
27
[email protected]3469e7e2010-10-14 20:34:5928TEST(GUIDTest, GUIDGeneratesAllZeroes) {
29 uint64 bytes[] = { 0, 0 };
brettw7622fbed2015-06-09 20:20:1430 std::string clientid = RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5931 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
32}
33
34TEST(GUIDTest, GUIDGeneratesCorrectly) {
35 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
brettw7622fbed2015-06-09 20:20:1436 std::string clientid = RandomDataToGUIDString(bytes);
[email protected]3469e7e2010-10-14 20:34:5937 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
38}
39#endif
40
41TEST(GUIDTest, GUIDCorrectlyFormatted) {
42 const int kIterations = 10;
43 for (int it = 0; it < kIterations; ++it) {
brettw7622fbed2015-06-09 20:20:1444 std::string guid = GenerateGUID();
45 EXPECT_TRUE(IsValidGUID(guid));
brettwc15100c2015-08-06 22:54:1646 EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid)));
47 EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid)));
[email protected]3469e7e2010-10-14 20:34:5948 }
49}
50
51TEST(GUIDTest, GUIDBasicUniqueness) {
52 const int kIterations = 10;
53 for (int it = 0; it < kIterations; ++it) {
brettw7622fbed2015-06-09 20:20:1454 std::string guid1 = GenerateGUID();
55 std::string guid2 = GenerateGUID();
[email protected]3469e7e2010-10-14 20:34:5956 EXPECT_EQ(36U, guid1.length());
57 EXPECT_EQ(36U, guid2.length());
58 EXPECT_NE(guid1, guid2);
benchan87119862014-09-05 14:19:3959#if defined(OS_POSIX)
60 EXPECT_TRUE(IsGUIDv4(guid1));
61 EXPECT_TRUE(IsGUIDv4(guid2));
62#endif
[email protected]3469e7e2010-10-14 20:34:5963 }
64}
brettw7622fbed2015-06-09 20:20:1465
66} // namespace base