1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "crypto/random.h" 6 7 #include <stddef.h> 8 9 #include <algorithm> 10 #include <string> 11 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 // Basic functionality tests. Does NOT test the security of the random data. 15 16 // Ensures we don't have all trivial data, i.e. that the data is indeed random. 17 // Currently, that means the bytes cannot be all the same (e.g. all zeros). IsTrivial(base::span<const uint8_t> bytes)18bool IsTrivial(base::span<const uint8_t> bytes) { 19 const uint8_t first_byte = bytes.front(); 20 return std::ranges::all_of(bytes, 21 [=](uint8_t byte) { return byte == first_byte; }); 22 } 23 TEST(RandBytes,RandBytes)24TEST(RandBytes, RandBytes) { 25 std::array<uint8_t, 16> bytes; 26 crypto::RandBytes(bytes); 27 EXPECT_FALSE(IsTrivial(bytes)); 28 } 29 TEST(RandBytes,RandBytesAsVector)30TEST(RandBytes, RandBytesAsVector) { 31 std::vector<uint8_t> vector = crypto::RandBytesAsVector(16); 32 EXPECT_FALSE(IsTrivial(vector)); 33 } 34