random_unittest.cc 897 Bytes
Newer Older
1 2 3 4 5 6
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "crypto/random.h"

7 8
#include <stddef.h>

9 10
#include <string>

11
#include "base/strings/string_util.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include "testing/gtest/include/gtest/gtest.h"

// Basic functionality tests. Does NOT test the security of the random data.

// Ensures we don't have all trivial data, i.e. that the data is indeed random.
// Currently, that means the bytes cannot be all the same (e.g. all zeros).
bool IsTrivial(const std::string& bytes) {
  for (size_t i = 0; i < bytes.size(); i++) {
    if (bytes[i] != bytes[0]) {
      return false;
    }
  }
  return true;
}

TEST(RandBytes, RandBytes) {
  std::string bytes(16, '\0');
29
  crypto::RandBytes(base::WriteInto(&bytes, bytes.size()), bytes.size());
30 31
  EXPECT_TRUE(!IsTrivial(bytes));
}