henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 <string> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/buffer.h" |
| 14 | #include "rtc_base/gunit.h" |
| 15 | #include "rtc_base/helpers.h" |
| 16 | #include "rtc_base/ssladapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 20 | class RandomTest : public testing::Test {}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | |
| 22 | TEST_F(RandomTest, TestCreateRandomId) { |
| 23 | CreateRandomId(); |
| 24 | } |
| 25 | |
| 26 | TEST_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 | |
| 34 | TEST_F(RandomTest, TestCreateNonZeroRandomId) { |
| 35 | EXPECT_NE(0U, CreateRandomNonZeroId()); |
| 36 | } |
| 37 | |
| 38 | TEST_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 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 47 | TEST_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 | |
jbauch | e3eff7c | 2016-08-08 17:13:33 -0700 | [diff] [blame] | 58 | TEST_F(RandomTest, TestCreateRandomStringEvenlyDivideTable) { |
| 59 | static std::string kUnbiasedTable("01234567"); |
| 60 | std::string random; |
| 61 | EXPECT_TRUE(CreateRandomString(256, kUnbiasedTable, &random)); |
| 62 | EXPECT_EQ(256U, random.size()); |
| 63 | |
| 64 | static std::string kBiasedTable("0123456789"); |
| 65 | EXPECT_FALSE(CreateRandomString(256, kBiasedTable, &random)); |
| 66 | EXPECT_EQ(0U, random.size()); |
| 67 | } |
| 68 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 69 | TEST_F(RandomTest, TestCreateRandomUuid) { |
| 70 | std::string random = CreateRandomUuid(); |
| 71 | EXPECT_EQ(36U, random.size()); |
| 72 | } |
| 73 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | TEST_F(RandomTest, TestCreateRandomForTest) { |
| 75 | // Make sure we get the output we expect. |
| 76 | SetRandomTestMode(true); |
| 77 | EXPECT_EQ(2154761789U, CreateRandomId()); |
| 78 | EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 79 | EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 80 | static size_t kRandomDataLength = 32; |
| 81 | std::string random; |
| 82 | EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random)); |
| 83 | EXPECT_EQ(kRandomDataLength, random.size()); |
| 84 | Buffer expected("\xbd\x52\x2a\x4b\x97\x93\x2f\x1c" |
| 85 | "\xc4\x72\xab\xa2\x88\x68\x3e\xcc" |
| 86 | "\xa3\x8d\xaf\x13\x3b\xbc\x83\xbb" |
| 87 | "\x16\xf1\xcf\x56\x0c\xf5\x4a\x8b", kRandomDataLength); |
| 88 | EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | |
| 90 | // Reset and make sure we get the same output. |
| 91 | SetRandomTestMode(true); |
| 92 | EXPECT_EQ(2154761789U, CreateRandomId()); |
| 93 | EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 94 | EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random)); |
| 96 | EXPECT_EQ(kRandomDataLength, random.size()); |
| 97 | EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | |
| 99 | // Test different character sets. |
| 100 | SetRandomTestMode(true); |
| 101 | std::string str; |
| 102 | EXPECT_TRUE(CreateRandomString(16, "a", &str)); |
| 103 | EXPECT_EQ("aaaaaaaaaaaaaaaa", str); |
jbauch | e3eff7c | 2016-08-08 17:13:33 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(CreateRandomString(16, "abcd", &str)); |
| 105 | EXPECT_EQ("dbaaabdaccbcabbd", str); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | |
| 107 | // Turn off test mode for other tests. |
| 108 | SetRandomTestMode(false); |
| 109 | } |
| 110 | |
| 111 | } // namespace rtc |