1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/helpers.h"
12 
13 #include <string.h>
14 
15 #include <string>
16 
17 #include "rtc_base/buffer.h"
18 #include "test/gtest.h"
19 
20 namespace rtc {
21 
22 class RandomTest : public ::testing::Test {};
23 
TEST_F(RandomTest,TestCreateRandomId)24 TEST_F(RandomTest, TestCreateRandomId) {
25   CreateRandomId();
26 }
27 
TEST_F(RandomTest,TestCreateRandomDouble)28 TEST_F(RandomTest, TestCreateRandomDouble) {
29   for (int i = 0; i < 100; ++i) {
30     double r = CreateRandomDouble();
31     EXPECT_GE(r, 0.0);
32     EXPECT_LT(r, 1.0);
33   }
34 }
35 
TEST_F(RandomTest,TestCreateNonZeroRandomId)36 TEST_F(RandomTest, TestCreateNonZeroRandomId) {
37   EXPECT_NE(0U, CreateRandomNonZeroId());
38 }
39 
TEST_F(RandomTest,TestCreateRandomString)40 TEST_F(RandomTest, TestCreateRandomString) {
41   std::string random = CreateRandomString(256);
42   EXPECT_EQ(256U, random.size());
43   std::string random2;
44   EXPECT_TRUE(CreateRandomString(256, &random2));
45   EXPECT_NE(random, random2);
46   EXPECT_EQ(256U, random2.size());
47 }
48 
TEST_F(RandomTest,TestCreateRandomData)49 TEST_F(RandomTest, TestCreateRandomData) {
50   static size_t kRandomDataLength = 32;
51   std::string random1;
52   std::string random2;
53   EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random1));
54   EXPECT_EQ(kRandomDataLength, random1.size());
55   EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random2));
56   EXPECT_EQ(kRandomDataLength, random2.size());
57   EXPECT_NE(0, memcmp(random1.data(), random2.data(), kRandomDataLength));
58 }
59 
TEST_F(RandomTest,TestCreateRandomStringEvenlyDivideTable)60 TEST_F(RandomTest, TestCreateRandomStringEvenlyDivideTable) {
61   static std::string kUnbiasedTable("01234567");
62   std::string random;
63   EXPECT_TRUE(CreateRandomString(256, kUnbiasedTable, &random));
64   EXPECT_EQ(256U, random.size());
65 
66   static std::string kBiasedTable("0123456789");
67   EXPECT_FALSE(CreateRandomString(256, kBiasedTable, &random));
68   EXPECT_EQ(0U, random.size());
69 }
70 
TEST_F(RandomTest,TestCreateRandomUuid)71 TEST_F(RandomTest, TestCreateRandomUuid) {
72   std::string random = CreateRandomUuid();
73   EXPECT_EQ(36U, random.size());
74 }
75 
TEST_F(RandomTest,TestCreateRandomForTest)76 TEST_F(RandomTest, TestCreateRandomForTest) {
77   // Make sure we get the output we expect.
78   SetRandomTestMode(true);
79   EXPECT_EQ(2154761789U, CreateRandomId());
80   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
81   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
82   static size_t kRandomDataLength = 32;
83   std::string random;
84   EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
85   EXPECT_EQ(kRandomDataLength, random.size());
86   Buffer expected(
87       "\xbd\x52\x2a\x4b\x97\x93\x2f\x1c"
88       "\xc4\x72\xab\xa2\x88\x68\x3e\xcc"
89       "\xa3\x8d\xaf\x13\x3b\xbc\x83\xbb"
90       "\x16\xf1\xcf\x56\x0c\xf5\x4a\x8b",
91       kRandomDataLength);
92   EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
93 
94   // Reset and make sure we get the same output.
95   SetRandomTestMode(true);
96   EXPECT_EQ(2154761789U, CreateRandomId());
97   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
98   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
99   EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
100   EXPECT_EQ(kRandomDataLength, random.size());
101   EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
102 
103   // Test different character sets.
104   SetRandomTestMode(true);
105   std::string str;
106   EXPECT_TRUE(CreateRandomString(16, "a", &str));
107   EXPECT_EQ("aaaaaaaaaaaaaaaa", str);
108   EXPECT_TRUE(CreateRandomString(16, "abcd", &str));
109   EXPECT_EQ("dbaaabdaccbcabbd", str);
110 
111   // Turn off test mode for other tests.
112   SetRandomTestMode(false);
113 }
114 
115 }  // namespace rtc
116