Creating a new RNG and replacing rand().

[ Merge of http://go/wvgerrit/84607 ]
[ Merge of http://go/wvgerrit/84608 ]

The primary goal is to replace the use of `rand()` with the random
number generators provided with the C++11 standard.

This simplified generator wraps some of the technical aspects of the
<random> library and provides an interface for uniformly distributed
integers.

As part of the `rand()` purge in the CDM, all uses of the C random int
function in `core()` have been removed.  Places that previously used
`rand()` now use `CdmRandom` facilities.

Test: Linux unittest and Android unittest
Bug: 130680365
Change-Id: Ica383870536ed462dbb80e630c2d66845e38b937
This commit is contained in:
Alex Dale
2019-08-26 14:39:50 -07:00
parent 9da0617606
commit ee56d93454
12 changed files with 406 additions and 78 deletions

View File

@@ -11,6 +11,7 @@
#include <string>
#include "arraysize.h"
#include "cdm_random.h"
#include "file_store.h"
#include "properties.h"
#include "string_conversions.h"
@@ -2014,14 +2015,6 @@ class DeviceFilesTest : public ::testing::Test {
&device_base_path_));
}
std::string GenerateRandomData(uint32_t len) {
std::string data(len, 0);
for (size_t i = 0; i < len; i++) {
data[i] = rand() % 256;
}
return data;
}
size_t GetLicenseDataSize(LicenseInfo& data) {
CdmAppParameterMap app_parameters = GetAppParameters(data.app_parameters);
size_t app_parameters_len = 0;
@@ -2169,8 +2162,8 @@ MATCHER_P8(Contains, str1, str2, str3, str4, str5, str6, map7, str8, "") {
TEST_F(DeviceCertificateStoreTest, StoreCertificate) {
MockFileSystem file_system;
std::string certificate(GenerateRandomData(kCertificateLen));
std::string wrapped_private_key(GenerateRandomData(kWrappedKeyLen));
std::string certificate(CdmRandom::RandomData(kCertificateLen));
std::string wrapped_private_key(CdmRandom::RandomData(kWrappedKeyLen));
std::string device_certificate_path =
device_base_path_ + DeviceFiles::GetCertificateFileName();
@@ -2243,8 +2236,8 @@ TEST_F(DeviceCertificateTest, HasCertificate) {
TEST_P(DeviceFilesSecurityLevelTest, SecurityLevel) {
MockFileSystem file_system;
std::string certificate(GenerateRandomData(kCertificateLen));
std::string wrapped_private_key(GenerateRandomData(kWrappedKeyLen));
std::string certificate(CdmRandom::RandomData(kCertificateLen));
std::string wrapped_private_key(CdmRandom::RandomData(kWrappedKeyLen));
CdmSecurityLevel security_level = GetParam();
std::string device_base_path;

View File

@@ -5,6 +5,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "cdm_random.h"
#include "file_store.h"
#include "test_vectors.h"
@@ -30,14 +31,6 @@ class FileTest : public testing::Test {
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
}
std::string GenerateRandomData(uint32_t len) {
std::string data(len, 0);
for (size_t i = 0; i < len; i++) {
data[i] = rand() % 256;
}
return data;
}
FileSystem file_system;
};
@@ -104,7 +97,7 @@ TEST_F(FileTest, FileSize) {
std::string path = test_vectors::kTestDir + kTestFileName;
file_system.Remove(path);
std::string write_data = GenerateRandomData(600);
std::string write_data = CdmRandom::RandomData(600);
size_t write_data_size = write_data.size();
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
ASSERT_TRUE(file);
@@ -119,7 +112,7 @@ TEST_F(FileTest, WriteReadBinaryFile) {
std::string path = test_vectors::kTestDir + kTestFileName;
file_system.Remove(path);
std::string write_data = GenerateRandomData(600);
std::string write_data = CdmRandom::RandomData(600);
size_t write_data_size = write_data.size();
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
ASSERT_TRUE(file);

View File

@@ -10,6 +10,7 @@
#include <string>
#include "arraysize.h"
#include "cdm_random.h"
#include "crypto_session.h"
#include "device_files.h"
#include "file_store.h"
@@ -990,10 +991,9 @@ TEST_F(UsageTableHeaderTest,
// We try to load a usage entry from the first 9 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 1);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 1)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 2);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations
@@ -1025,10 +1025,8 @@ TEST_F(UsageTableHeaderTest,
// We try to load a usage entry from the first 8 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 2);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 2)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 3);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations
@@ -1060,10 +1058,8 @@ TEST_F(UsageTableHeaderTest, LoadEntry_LoadUsageEntryFailsThrice) {
// We try to load a usage entry from the first 7 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 3);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 3)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 4);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations