Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: base/guid_unittest.cc

Issue 541633005: Make GUID generation on POSIX compliant with GUID v4 format. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/guid_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/guid.h" 5 #include "base/guid.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 #if defined(OS_POSIX) 12 #if defined(OS_POSIX)
13
14 namespace {
15
16 bool 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
13 TEST(GUIDTest, GUIDGeneratesAllZeroes) { 27 TEST(GUIDTest, GUIDGeneratesAllZeroes) {
14 uint64 bytes[] = { 0, 0 }; 28 uint64 bytes[] = { 0, 0 };
15 std::string clientid = base::RandomDataToGUIDString(bytes); 29 std::string clientid = base::RandomDataToGUIDString(bytes);
16 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); 30 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
17 } 31 }
18 32
19 TEST(GUIDTest, GUIDGeneratesCorrectly) { 33 TEST(GUIDTest, GUIDGeneratesCorrectly) {
20 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; 34 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
21 std::string clientid = base::RandomDataToGUIDString(bytes); 35 std::string clientid = base::RandomDataToGUIDString(bytes);
22 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); 36 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
(...skipping 11 matching lines...) Expand all
34 } 48 }
35 49
36 TEST(GUIDTest, GUIDBasicUniqueness) { 50 TEST(GUIDTest, GUIDBasicUniqueness) {
37 const int kIterations = 10; 51 const int kIterations = 10;
38 for (int it = 0; it < kIterations; ++it) { 52 for (int it = 0; it < kIterations; ++it) {
39 std::string guid1 = base::GenerateGUID(); 53 std::string guid1 = base::GenerateGUID();
40 std::string guid2 = base::GenerateGUID(); 54 std::string guid2 = base::GenerateGUID();
41 EXPECT_EQ(36U, guid1.length()); 55 EXPECT_EQ(36U, guid1.length());
42 EXPECT_EQ(36U, guid2.length()); 56 EXPECT_EQ(36U, guid2.length());
43 EXPECT_NE(guid1, guid2); 57 EXPECT_NE(guid1, guid2);
58 #if defined(OS_POSIX)
59 EXPECT_TRUE(IsGUIDv4(guid1));
60 EXPECT_TRUE(IsGUIDv4(guid2));
61 #endif
44 } 62 }
45 } 63 }
OLDNEW
« no previous file with comments | « base/guid_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698