guid_unittest.cc 1.21 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "butil/guid.h"
gejun's avatar
gejun committed
6 7 8 9 10 11 12 13

#include <limits>

#include <gtest/gtest.h>

#if defined(OS_POSIX)
TEST(GUIDTest, GUIDGeneratesAllZeroes) {
  uint64_t bytes[] = { 0, 0 };
14
  std::string clientid = butil::RandomDataToGUIDString(bytes);
gejun's avatar
gejun committed
15 16 17 18 19
  EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
}

TEST(GUIDTest, GUIDGeneratesCorrectly) {
  uint64_t bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
20
  std::string clientid = butil::RandomDataToGUIDString(bytes);
gejun's avatar
gejun committed
21 22 23 24 25 26 27
  EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
}
#endif

TEST(GUIDTest, GUIDCorrectlyFormatted) {
  const int kIterations = 10;
  for (int it = 0; it < kIterations; ++it) {
28 29
    std::string guid = butil::GenerateGUID();
    EXPECT_TRUE(butil::IsValidGUID(guid));
gejun's avatar
gejun committed
30 31 32 33 34 35
  }
}

TEST(GUIDTest, GUIDBasicUniqueness) {
  const int kIterations = 10;
  for (int it = 0; it < kIterations; ++it) {
36 37
    std::string guid1 = butil::GenerateGUID();
    std::string guid2 = butil::GenerateGUID();
gejun's avatar
gejun committed
38 39 40 41 42
    EXPECT_EQ(36U, guid1.length());
    EXPECT_EQ(36U, guid2.length());
    EXPECT_NE(guid1, guid2);
  }
}