You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/helpers.h"
  11. #include <string.h>
  12. #include <string>
  13. #include "rtc_base/buffer.h"
  14. #include "test/gtest.h"
  15. namespace rtc {
  16. class RandomTest : public ::testing::Test {};
  17. TEST_F(RandomTest, TestCreateRandomId) {
  18. CreateRandomId();
  19. }
  20. TEST_F(RandomTest, TestCreateRandomDouble) {
  21. for (int i = 0; i < 100; ++i) {
  22. double r = CreateRandomDouble();
  23. EXPECT_GE(r, 0.0);
  24. EXPECT_LT(r, 1.0);
  25. }
  26. }
  27. TEST_F(RandomTest, TestCreateNonZeroRandomId) {
  28. EXPECT_NE(0U, CreateRandomNonZeroId());
  29. }
  30. TEST_F(RandomTest, TestCreateRandomString) {
  31. std::string random = CreateRandomString(256);
  32. EXPECT_EQ(256U, random.size());
  33. std::string random2;
  34. EXPECT_TRUE(CreateRandomString(256, &random2));
  35. EXPECT_NE(random, random2);
  36. EXPECT_EQ(256U, random2.size());
  37. }
  38. TEST_F(RandomTest, TestCreateRandomData) {
  39. static size_t kRandomDataLength = 32;
  40. std::string random1;
  41. std::string random2;
  42. EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random1));
  43. EXPECT_EQ(kRandomDataLength, random1.size());
  44. EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random2));
  45. EXPECT_EQ(kRandomDataLength, random2.size());
  46. EXPECT_NE(0, memcmp(random1.data(), random2.data(), kRandomDataLength));
  47. }
  48. TEST_F(RandomTest, TestCreateRandomStringEvenlyDivideTable) {
  49. static std::string kUnbiasedTable("01234567");
  50. std::string random;
  51. EXPECT_TRUE(CreateRandomString(256, kUnbiasedTable, &random));
  52. EXPECT_EQ(256U, random.size());
  53. static std::string kBiasedTable("0123456789");
  54. EXPECT_FALSE(CreateRandomString(256, kBiasedTable, &random));
  55. EXPECT_EQ(0U, random.size());
  56. }
  57. TEST_F(RandomTest, TestCreateRandomUuid) {
  58. std::string random = CreateRandomUuid();
  59. EXPECT_EQ(36U, random.size());
  60. }
  61. TEST_F(RandomTest, TestCreateRandomForTest) {
  62. // Make sure we get the output we expect.
  63. SetRandomTestMode(true);
  64. EXPECT_EQ(2154761789U, CreateRandomId());
  65. EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
  66. EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
  67. static size_t kRandomDataLength = 32;
  68. std::string random;
  69. EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
  70. EXPECT_EQ(kRandomDataLength, random.size());
  71. Buffer expected(
  72. "\xbd\x52\x2a\x4b\x97\x93\x2f\x1c"
  73. "\xc4\x72\xab\xa2\x88\x68\x3e\xcc"
  74. "\xa3\x8d\xaf\x13\x3b\xbc\x83\xbb"
  75. "\x16\xf1\xcf\x56\x0c\xf5\x4a\x8b",
  76. kRandomDataLength);
  77. EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
  78. // Reset and make sure we get the same output.
  79. SetRandomTestMode(true);
  80. EXPECT_EQ(2154761789U, CreateRandomId());
  81. EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
  82. EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid());
  83. EXPECT_TRUE(CreateRandomData(kRandomDataLength, &random));
  84. EXPECT_EQ(kRandomDataLength, random.size());
  85. EXPECT_EQ(0, memcmp(expected.data(), random.data(), kRandomDataLength));
  86. // Test different character sets.
  87. SetRandomTestMode(true);
  88. std::string str;
  89. EXPECT_TRUE(CreateRandomString(16, "a", &str));
  90. EXPECT_EQ("aaaaaaaaaaaaaaaa", str);
  91. EXPECT_TRUE(CreateRandomString(16, "abcd", &str));
  92. EXPECT_EQ("dbaaabdaccbcabbd", str);
  93. // Turn off test mode for other tests.
  94. SetRandomTestMode(false);
  95. }
  96. } // namespace rtc