Home | History | Annotate | Download | only in base
      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 
     13 #include "webrtc/base/gunit.h"
     14 #include "webrtc/base/helpers.h"
     15 #include "webrtc/base/ssladapter.h"
     16 
     17 namespace rtc {
     18 
     19 class RandomTest : public testing::Test {
     20  public:
     21   static void SetUpTestCase() {
     22     rtc::InitializeSSL();
     23   }
     24 
     25   static void TearDownTestCase() {
     26     rtc::CleanupSSL();
     27   }
     28 };
     29 
     30 TEST_F(RandomTest, TestCreateRandomId) {
     31   CreateRandomId();
     32 }
     33 
     34 TEST_F(RandomTest, TestCreateRandomDouble) {
     35   for (int i = 0; i < 100; ++i) {
     36     double r = CreateRandomDouble();
     37     EXPECT_GE(r, 0.0);
     38     EXPECT_LT(r, 1.0);
     39   }
     40 }
     41 
     42 TEST_F(RandomTest, TestCreateNonZeroRandomId) {
     43   EXPECT_NE(0U, CreateRandomNonZeroId());
     44 }
     45 
     46 TEST_F(RandomTest, TestCreateRandomString) {
     47   std::string random = CreateRandomString(256);
     48   EXPECT_EQ(256U, random.size());
     49   std::string random2;
     50   EXPECT_TRUE(CreateRandomString(256, &random2));
     51   EXPECT_NE(random, random2);
     52   EXPECT_EQ(256U, random2.size());
     53 }
     54 
     55 TEST_F(RandomTest, TestCreateRandomForTest) {
     56   // Make sure we get the output we expect.
     57   SetRandomTestMode(true);
     58   EXPECT_EQ(2154761789U, CreateRandomId());
     59   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
     60 
     61   // Reset and make sure we get the same output.
     62   SetRandomTestMode(true);
     63   EXPECT_EQ(2154761789U, CreateRandomId());
     64   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16));
     65 
     66   // Test different character sets.
     67   SetRandomTestMode(true);
     68   std::string str;
     69   EXPECT_TRUE(CreateRandomString(16, "a", &str));
     70   EXPECT_EQ("aaaaaaaaaaaaaaaa", str);
     71   EXPECT_TRUE(CreateRandomString(16, "abc", &str));
     72   EXPECT_EQ("acbccaaaabbaacbb", str);
     73 
     74   // Turn off test mode for other tests.
     75   SetRandomTestMode(false);
     76 }
     77 
     78 }  // namespace rtc
     79