blob: e4903f58694b83613b8d4c9748e7d57839d1981f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 <string>
12
jbauchcb560652016-08-04 05:20:32 -070013#include "webrtc/base/buffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include "webrtc/base/gunit.h"
15#include "webrtc/base/helpers.h"
16#include "webrtc/base/ssladapter.h"
17
18namespace rtc {
19
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +000020class RandomTest : public testing::Test {};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22TEST_F(RandomTest, TestCreateRandomId) {
23 CreateRandomId();
24}
25
26TEST_F(RandomTest, TestCreateRandomDouble) {
27 for (int i = 0; i < 100; ++i) {
28 double r = CreateRandomDouble();
29 EXPECT_GE(r, 0.0);
30 EXPECT_LT(r, 1.0);
31 }
32}
33
34TEST_F(RandomTest, TestCreateNonZeroRandomId) {
35 EXPECT_NE(0U, CreateRandomNonZeroId());
36}
37
38TEST_F(RandomTest, TestCreateRandomString) {
39 std::string random = CreateRandomString(256);
40 EXPECT_EQ(256U, random.size());
41 std::string random2;
42 EXPECT_TRUE(CreateRandomString(256, &random2));
43 EXPECT_NE(random, random2);
44 EXPECT_EQ(256U, random2.size());
45}
46
jbauchcb560652016-08-04 05:20:32 -070047TEST_F(RandomTest, TestCreateRandomData) {
48 static size_t kRandomDataLength = 32;
49 std::string random1;
50 std::string random2;
51 EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random1));
52 EXPECT_EQ(kRandomDataLength, random1.size());
53 EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random2));
54 EXPECT_EQ(kRandomDataLength, random2.size());
55 EXPECT_NE(0, memcmp(random1.data(), random2.data(), kRandomDataLength));
56}
57
deadbeeffac06552015-11-25 11:26:01 -080058TEST_F(RandomTest, TestCreateRandomUuid) {
59 std::string random = CreateRandomUuid();
60 EXPECT_EQ(36U, random.size());
61}
62
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063TEST_F(RandomTest, TestCreateRandomForTest) {
64 // Make sure we get the output we expect.
65 SetRandomTestMode(true);
66 EXPECT_EQ(2154761789U, CreateRandomId());
67 EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
deadbeeffac06552015-11-25 11:26:01 -080068 EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
jbauchcb560652016-08-04 05:20:32 -070069 static size_t kRandomDataLength = 32;
70 std::string random;
71 EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
72 EXPECT_EQ(kRandomDataLength, random.size());
73 Buffer expected("\xbd\x52\x2a\x4b\x97\x93\x2f\x1c"
74 "\xc4\x72\xab\xa2\x88\x68\x3e\xcc"
75 "\xa3\x8d\xaf\x13\x3b\xbc\x83\xbb"
76 "\x16\xf1\xcf\x56\x0c\xf5\x4a\x8b", kRandomDataLength);
77 EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
79 // Reset and make sure we get the same output.
80 SetRandomTestMode(true);
81 EXPECT_EQ(2154761789U, CreateRandomId());
82 EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
deadbeeffac06552015-11-25 11:26:01 -080083 EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
jbauchcb560652016-08-04 05:20:32 -070084 EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
85 EXPECT_EQ(kRandomDataLength, random.size());
86 EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
88 // Test different character sets.
89 SetRandomTestMode(true);
90 std::string str;
91 EXPECT_TRUE(CreateRandomString(16, "a", &str));
92 EXPECT_EQ("aaaaaaaaaaaaaaaa", str);
93 EXPECT_TRUE(CreateRandomString(16, "abc", &str));
94 EXPECT_EQ("acbccaaaabbaacbb", str);
95
96 // Turn off test mode for other tests.
97 SetRandomTestMode(false);
98}
99
100} // namespace rtc