Source release 16.2.0
This commit is contained in:
124
util/test/base64_test.cpp
Normal file
124
util/test/base64_test.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
|
||||
// Test vectors as suggested by http://tools.ietf.org/html/rfc4648#section-10
|
||||
const std::string kNullString("");
|
||||
const std::string kf("f");
|
||||
const std::string kfo("fo");
|
||||
const std::string kfoo("foo");
|
||||
const std::string kfoob("foob");
|
||||
const std::string kfooba("fooba");
|
||||
const std::string kfoobar("foobar");
|
||||
const std::string kfB64("Zg==");
|
||||
const std::string kfoB64("Zm8=");
|
||||
const std::string kfooB64("Zm9v");
|
||||
const std::string kfoobB64("Zm9vYg==");
|
||||
const std::string kfoobaB64("Zm9vYmE=");
|
||||
const std::string kfoobarB64("Zm9vYmFy");
|
||||
|
||||
// Arbitrary clear test vectors
|
||||
const std::string kMultipleOf24BitsData("Good day!");
|
||||
const std::string kOneByteOverData("Hello Friend!");
|
||||
const std::string kTwoBytesOverData("Hello Friend!!");
|
||||
const std::string kTestData =
|
||||
"\030\361\\\366\267> \331\210\360\\-\311:\324\256\376"
|
||||
"\261\234\241\326d\326\177\346\346\223\333Y\305\214\330";
|
||||
|
||||
// Arbitrary encoded test vectors
|
||||
const std::string kMultipleOf24BitsB64Data("R29vZCBkYXkh");
|
||||
const std::string kOneByteOverB64Data("SGVsbG8gRnJpZW5kIQ==");
|
||||
const std::string kTwoBytesOverB64Data("SGVsbG8gRnJpZW5kISE=");
|
||||
const std::string kB64TestData = "GPFc9rc+INmI8FwtyTrUrv6xnKHWZNZ/5uaT21nFjNg=";
|
||||
|
||||
const std::pair<const std::string*, const std::string*> kBase64TestVectors[] = {
|
||||
make_pair(&kNullString, &kNullString),
|
||||
make_pair(&kf, &kfB64),
|
||||
make_pair(&kfo, &kfoB64),
|
||||
make_pair(&kfoo, &kfooB64),
|
||||
make_pair(&kfoob, &kfoobB64),
|
||||
make_pair(&kfooba, &kfoobaB64),
|
||||
make_pair(&kfoobar, &kfoobarB64),
|
||||
make_pair(&kMultipleOf24BitsData, &kMultipleOf24BitsB64Data),
|
||||
make_pair(&kOneByteOverData, &kOneByteOverB64Data),
|
||||
make_pair(&kTwoBytesOverData, &kTwoBytesOverB64Data),
|
||||
make_pair(&kTestData, &kB64TestData)};
|
||||
|
||||
const std::string kBase64ErrorVectors[] = {"Foo$sa", "Foo\x99\x23\xfa\02",
|
||||
"Foo==Foo", "FooBa"};
|
||||
|
||||
std::string ConvertToBase64WebSafe(const std::string& std_base64_string) {
|
||||
std::string str(std_base64_string);
|
||||
for (size_t i = 0; i < str.size(); ++i) {
|
||||
if (str[i] == '+')
|
||||
str[i] = '-';
|
||||
else if (str[i] == '/')
|
||||
str[i] = '_';
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class Base64EncodeDecodeTest
|
||||
: public ::testing::TestWithParam<
|
||||
std::pair<const std::string*, const std::string*> > {};
|
||||
|
||||
TEST_P(Base64EncodeDecodeTest, EncodeDecodeTest) {
|
||||
std::pair<const std::string*, const std::string*> values = GetParam();
|
||||
std::vector<uint8_t> decoded_vector = Base64Decode(values.second->data());
|
||||
std::string decoded_string(decoded_vector.begin(), decoded_vector.end());
|
||||
EXPECT_STREQ(values.first->data(), decoded_string.data());
|
||||
std::string b64_string = Base64Encode(decoded_vector);
|
||||
EXPECT_STREQ(values.second->data(), b64_string.data());
|
||||
}
|
||||
|
||||
TEST_P(Base64EncodeDecodeTest, WebSafeEncodeDecodeTest) {
|
||||
std::pair<const std::string*, const std::string*> values = GetParam();
|
||||
std::string encoded_string = ConvertToBase64WebSafe(*(values.second));
|
||||
std::vector<uint8_t> decoded_vector = Base64SafeDecode(encoded_string);
|
||||
std::string decoded_string(decoded_vector.begin(), decoded_vector.end());
|
||||
EXPECT_STREQ(values.first->data(), decoded_string.data());
|
||||
std::string b64_string = Base64SafeEncode(decoded_vector);
|
||||
EXPECT_STREQ(encoded_string.data(), b64_string.data());
|
||||
}
|
||||
|
||||
class Base64ErrorDecodeTest : public ::testing::TestWithParam<std::string> {};
|
||||
|
||||
TEST_P(Base64ErrorDecodeTest, EncoderErrors) {
|
||||
std::vector<uint8_t> result = Base64Decode(GetParam());
|
||||
EXPECT_EQ(0u, result.size());
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(ExecutesBase64Test, Base64EncodeDecodeTest,
|
||||
::testing::ValuesIn(kBase64TestVectors));
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(ExecutesBase64Test, Base64ErrorDecodeTest,
|
||||
::testing::ValuesIn(kBase64ErrorVectors));
|
||||
|
||||
class HtoNLL64Test : public ::testing::Test {};
|
||||
|
||||
TEST_F(HtoNLL64Test, PositiveNumber) {
|
||||
uint8_t data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
int64_t* network_byte_order = reinterpret_cast<int64_t*>(data);
|
||||
int64_t host_byte_order = htonll64(*network_byte_order);
|
||||
EXPECT_EQ(0x0102030405060708, host_byte_order);
|
||||
}
|
||||
TEST_F(HtoNLL64Test, NegativeNumber) {
|
||||
uint8_t data[8] = {0xfe, 2, 3, 4, 5, 6, 7, 8};
|
||||
int64_t* network_byte_order = reinterpret_cast<int64_t*>(data);
|
||||
int64_t host_byte_order = htonll64(*network_byte_order);
|
||||
EXPECT_EQ(-0x01FdFcFbFaF9F8F8, host_byte_order);
|
||||
}
|
||||
} // namespace wvcdm
|
||||
187
util/test/cdm_random_unittest.cpp
Normal file
187
util/test/cdm_random_unittest.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cdm_random.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
// Random data vector lengths.
|
||||
constexpr size_t kVectorLength = 1024;
|
||||
constexpr size_t kMaxRandomDataLength =
|
||||
CdmRandomGenerator::kMaxRandomDataLength;
|
||||
constexpr size_t kAboveMaxRandomDataLength = std::numeric_limits<size_t>::max();
|
||||
|
||||
constexpr size_t kRandomTrialCount = 100;
|
||||
constexpr size_t kThreadCount = 16;
|
||||
constexpr unsigned int kSeeds[] = {0, 1337, 1565904109, 776964657};
|
||||
|
||||
class CdmRandomGeneratorTest : public testing::TestWithParam<unsigned int> {};
|
||||
} // namespace
|
||||
|
||||
// Checks that the class CdmRandomGenerator meets the requirements of
|
||||
// UniformRandomBitGenerator.
|
||||
// ref: https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator
|
||||
TEST(CdmRandomGeneratorTest, UniformRandomBitGeneratorRequirements) {
|
||||
// Let G represent class CdmRandomGenerator, and g represent an instance
|
||||
// of CdmRandomGenerator.
|
||||
// 1) G::result_type is an unsigned integer (unspecified precision).
|
||||
EXPECT_TRUE(std::is_integral<CdmRandomGenerator::result_type>::value);
|
||||
EXPECT_TRUE(std::is_unsigned<CdmRandomGenerator::result_type>::value);
|
||||
// 2&3 a) G::min() and G::max() have the result type of G::result_type.
|
||||
EXPECT_TRUE((std::is_same<CdmRandomGenerator::result_type,
|
||||
decltype(CdmRandomGenerator::min())>::value));
|
||||
EXPECT_TRUE((std::is_same<CdmRandomGenerator::result_type,
|
||||
decltype(CdmRandomGenerator::max())>::value));
|
||||
// 2&3 b) G::min() is strictly less than G::max().
|
||||
EXPECT_LT(CdmRandomGenerator::min(), CdmRandomGenerator::max());
|
||||
// 4 a) g() have the result type of G::result_type.
|
||||
CdmRandomGenerator g;
|
||||
EXPECT_TRUE(
|
||||
(std::is_same<CdmRandomGenerator::result_type, decltype(g())>::value));
|
||||
// 4 b) g() is within [G::min() G::max()]
|
||||
std::vector<CdmRandomGenerator::result_type> values;
|
||||
for (size_t i = 0; i < kRandomTrialCount; ++i) {
|
||||
CdmRandomGenerator::result_type x = g();
|
||||
EXPECT_LE(CdmRandomGenerator::min(), x);
|
||||
EXPECT_GE(CdmRandomGenerator::max(), x);
|
||||
values.push_back(x);
|
||||
}
|
||||
|
||||
// Verify compilation.
|
||||
|
||||
// std::shuffle(RandomIt first, RandomIt last, URBG&& g) requires the
|
||||
// class URBG to meet "UniformRandomBitGenerator" requirements. This
|
||||
// will fail to compile if the requirements are not met.
|
||||
std::shuffle(values.begin(), values.end(), CdmRandomGenerator());
|
||||
}
|
||||
|
||||
TEST_P(CdmRandomGeneratorTest, AllMethods) {
|
||||
const unsigned int seed = GetParam();
|
||||
CdmRandomGenerator rng;
|
||||
rng.Seed();
|
||||
rng.Seed(seed);
|
||||
|
||||
rng.Rand();
|
||||
rng();
|
||||
|
||||
rng.RandomInRange(1234, 1000000);
|
||||
rng.RandomInRange(1000000);
|
||||
|
||||
rng.RandomData(kVectorLength);
|
||||
|
||||
rng.RandomBool();
|
||||
}
|
||||
|
||||
TEST_P(CdmRandomGeneratorTest, RandomInRange) {
|
||||
const unsigned int seed = GetParam();
|
||||
CdmRandomGenerator rng(seed);
|
||||
|
||||
for (size_t i = 0; i < kRandomTrialCount; ++i) {
|
||||
const int rand_int = rng.Rand();
|
||||
EXPECT_GE(rand_int, 0);
|
||||
EXPECT_LE(rand_int, RAND_MAX);
|
||||
}
|
||||
|
||||
// Range size of 1.
|
||||
const uint64_t rand_u64_1 = rng.RandomInRange(100, 100);
|
||||
EXPECT_EQ(rand_u64_1, 100ul);
|
||||
|
||||
// Range size of 2.
|
||||
const uint64_t rand_u64_2 = rng.RandomInRange(1234, 1235);
|
||||
EXPECT_GE(rand_u64_2, 1234ul);
|
||||
EXPECT_LE(rand_u64_2, 1235ul);
|
||||
|
||||
// Small range.
|
||||
const uint64_t rand_u64_3 = rng.RandomInRange(10);
|
||||
EXPECT_LE(rand_u64_3, 10ul);
|
||||
|
||||
// Max range, mainly checking that nothing crashes.
|
||||
rng.RandomInRange(0, std::numeric_limits<uint64_t>::max());
|
||||
|
||||
// Invalid range representation. Should swap the bounds.
|
||||
const uint64_t rand_u64_4 = rng.RandomInRange(1235, 1234);
|
||||
EXPECT_GE(rand_u64_4, 1234ul);
|
||||
EXPECT_LE(rand_u64_4, 1235ul);
|
||||
}
|
||||
|
||||
TEST_P(CdmRandomGeneratorTest, RandomDataLength) {
|
||||
const unsigned int seed = GetParam();
|
||||
CdmRandomGenerator rng(seed);
|
||||
|
||||
const std::string empty_data = rng.RandomData(0);
|
||||
EXPECT_EQ(empty_data.size(), 0ul);
|
||||
|
||||
const std::string data = rng.RandomData(kVectorLength);
|
||||
EXPECT_EQ(data.size(), kVectorLength);
|
||||
|
||||
const std::string max_data = rng.RandomData(kMaxRandomDataLength);
|
||||
EXPECT_EQ(max_data.size(), kMaxRandomDataLength);
|
||||
|
||||
// Requesting data above the maximum length will result in an error,
|
||||
// returning an empty string.
|
||||
const std::string error_data = rng.RandomData(kAboveMaxRandomDataLength);
|
||||
EXPECT_EQ(error_data.size(), 0ul);
|
||||
}
|
||||
|
||||
TEST_P(CdmRandomGeneratorTest, Reproducibility) {
|
||||
const unsigned int seed = GetParam();
|
||||
CdmRandomGenerator rng(seed);
|
||||
const std::string random_data_1 = rng.RandomData(kVectorLength);
|
||||
// Reset generator.
|
||||
rng.Seed(seed);
|
||||
const std::string random_data_2 = rng.RandomData(kVectorLength);
|
||||
EXPECT_EQ(random_data_1, random_data_2);
|
||||
}
|
||||
|
||||
TEST_P(CdmRandomGeneratorTest, ThreadSafety) {
|
||||
const unsigned int seed = GetParam();
|
||||
CdmRandomGenerator rng(seed);
|
||||
bool barrier = true;
|
||||
|
||||
auto thread_job = [&]() {
|
||||
while (barrier) {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
||||
}
|
||||
for (size_t i = 0; i < kRandomTrialCount; ++i) {
|
||||
rng.Rand();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (size_t i = 0; i < kThreadCount; ++i) {
|
||||
threads.push_back(std::thread(thread_job));
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
barrier = false;
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(VariousSeeds, CdmRandomGeneratorTest,
|
||||
testing::ValuesIn(kSeeds));
|
||||
|
||||
TEST(CdmRandomTest, AllMethods) {
|
||||
CdmRandom::Rand();
|
||||
CdmRandom::RandomInRange(1234, 1000000);
|
||||
CdmRandom::RandomInRange(1000000);
|
||||
CdmRandom::RandomData(kVectorLength);
|
||||
CdmRandom::RandomBool();
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
178
util/test/file_store_unittest.cpp
Normal file
178
util/test/file_store_unittest.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cdm_random.h"
|
||||
#include "file_store.h"
|
||||
#include "test_vectors.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
const std::string kTestDirName = "test_dir";
|
||||
const std::string kTestFileName = "test.txt";
|
||||
const std::string kTestFileName2 = "test2.txt";
|
||||
const std::string kTestFileName3 = "test3.other";
|
||||
const std::string kTestFileNameExt = ".txt";
|
||||
const std::string kTestFileNameExt3 = ".other";
|
||||
const std::string kWildcard = "*";
|
||||
} // namespace
|
||||
|
||||
class FileTest : public testing::Test {
|
||||
protected:
|
||||
FileTest() {}
|
||||
|
||||
void TearDown() override { RemoveTestDir(); }
|
||||
|
||||
void RemoveTestDir() {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
FileSystem file_system;
|
||||
};
|
||||
|
||||
TEST_F(FileTest, FileExists) {
|
||||
EXPECT_TRUE(file_system.Exists(test_vectors::kExistentFile));
|
||||
EXPECT_TRUE(file_system.Exists(test_vectors::kExistentDir));
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kNonExistentFile));
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kNonExistentDir));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveDir) {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, OpenFile) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
EXPECT_TRUE(file_system.Remove(path));
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveDirAndFile) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
EXPECT_TRUE(file_system.Remove(path));
|
||||
EXPECT_FALSE(file_system.Exists(path));
|
||||
|
||||
file = file_system.Open(path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
RemoveTestDir();
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kTestDir));
|
||||
EXPECT_FALSE(file_system.Exists(path));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveWildcardFiles) {
|
||||
std::string path1 = test_vectors::kTestDir + kTestFileName;
|
||||
std::string path2 = test_vectors::kTestDir + kTestFileName2;
|
||||
std::string wildcard_path =
|
||||
test_vectors::kTestDir + kWildcard + kTestFileNameExt;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path1, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path2, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path1));
|
||||
EXPECT_TRUE(file_system.Exists(path2));
|
||||
EXPECT_TRUE(file_system.Remove(wildcard_path));
|
||||
EXPECT_FALSE(file_system.Exists(path1));
|
||||
EXPECT_FALSE(file_system.Exists(path2));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, FileSize) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
file_system.Remove(path);
|
||||
|
||||
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);
|
||||
EXPECT_EQ(file->Write(write_data.data(), write_data_size), write_data_size);
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
|
||||
EXPECT_EQ(static_cast<ssize_t>(write_data_size), file_system.FileSize(path));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, WriteReadBinaryFile) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
file_system.Remove(path);
|
||||
|
||||
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);
|
||||
EXPECT_EQ(file->Write(write_data.data(), write_data_size), write_data_size);
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
|
||||
std::string read_data;
|
||||
read_data.resize(file_system.FileSize(path));
|
||||
size_t read_data_size = read_data.size();
|
||||
file = file_system.Open(path, FileSystem::kReadOnly);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_EQ(file->Read(&read_data[0], read_data_size), read_data_size);
|
||||
EXPECT_EQ(write_data, read_data);
|
||||
}
|
||||
|
||||
TEST_F(FileTest, ListFiles) {
|
||||
std::vector<std::string> names;
|
||||
|
||||
std::string not_path("zzz");
|
||||
std::string path1 = test_vectors::kTestDir + kTestFileName;
|
||||
std::string path2 = test_vectors::kTestDir + kTestFileName2;
|
||||
std::string path3 = test_vectors::kTestDir + kTestFileName3;
|
||||
std::string path_dir = test_vectors::kTestDir;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path1, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path2, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path3, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path1));
|
||||
EXPECT_TRUE(file_system.Exists(path2));
|
||||
EXPECT_TRUE(file_system.Exists(path3));
|
||||
|
||||
// Ask for non-existent path.
|
||||
EXPECT_FALSE(file_system.List(not_path, &names));
|
||||
|
||||
// Valid path, but no way to return names.
|
||||
EXPECT_FALSE(file_system.List(path_dir, nullptr));
|
||||
|
||||
// Valid path, valid return.
|
||||
EXPECT_TRUE(file_system.List(path_dir, &names));
|
||||
|
||||
// Should find three files. Order not important.
|
||||
EXPECT_EQ(3u, names.size());
|
||||
EXPECT_THAT(names, ::testing::UnorderedElementsAre(
|
||||
kTestFileName, kTestFileName2, kTestFileName3));
|
||||
|
||||
std::string wild_card_path = path_dir + kWildcard + kTestFileNameExt;
|
||||
EXPECT_TRUE(file_system.Remove(wild_card_path));
|
||||
EXPECT_TRUE(file_system.List(path_dir, &names));
|
||||
|
||||
EXPECT_EQ(1u, names.size());
|
||||
EXPECT_TRUE(names[0].compare(kTestFileName3) == 0);
|
||||
|
||||
std::string wild_card_path2 = path_dir + kWildcard + kTestFileNameExt3;
|
||||
EXPECT_TRUE(file_system.Remove(wild_card_path2));
|
||||
EXPECT_TRUE(file_system.List(path_dir, &names));
|
||||
|
||||
EXPECT_EQ(0u, names.size());
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
40
util/test/test_clock.cpp
Normal file
40
util/test/test_clock.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2019 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Clock - A fake clock just for running tests.
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "test_sleep.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
// A fake clock that only advances when TestSleep::Sleep is called.
|
||||
class FakeClock : public wvcdm::TestSleep::CallBack {
|
||||
public:
|
||||
FakeClock() {
|
||||
auto now = std::chrono::steady_clock().now();
|
||||
now_ = now.time_since_epoch() / std::chrono::milliseconds(1);
|
||||
TestSleep::set_callback(this);
|
||||
}
|
||||
~FakeClock() { TestSleep::set_callback(nullptr); }
|
||||
void ElapseTime(int64_t milliseconds) { now_ += milliseconds; }
|
||||
|
||||
int64_t now() const { return now_; }
|
||||
|
||||
private:
|
||||
int64_t now_;
|
||||
};
|
||||
|
||||
FakeClock* g_fake_clock = nullptr;
|
||||
} // namespace
|
||||
|
||||
// On devices running a fake OEMCrypto, we can use a fake sleep and fake time.
|
||||
int64_t Clock::GetCurrentTime() {
|
||||
if (g_fake_clock == nullptr) g_fake_clock = new FakeClock();
|
||||
return g_fake_clock->now() / 1000;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
44
util/test/test_sleep.cpp
Normal file
44
util/test/test_sleep.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include "test_sleep.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
bool TestSleep::real_sleep_ = true;
|
||||
TestSleep::CallBack* TestSleep::callback_ = nullptr;
|
||||
|
||||
void TestSleep::Sleep(unsigned int seconds) {
|
||||
int64_t milliseconds = 1000 * seconds;
|
||||
if (real_sleep_) {
|
||||
// This next bit of logic is to avoid slow drift apart of the real clock and
|
||||
// the fake clock. We compute how far from the real clock has advanced in
|
||||
// total since the start, and then compare to a running total of sleep
|
||||
// calls. We sleep for approximately x second, and then advance the clock by
|
||||
// the amount of time that has actually passed.
|
||||
static auto start_real = std::chrono::steady_clock().now();
|
||||
static int64_t fake_clock = 0;
|
||||
sleep(seconds);
|
||||
auto now_real = std::chrono::steady_clock().now();
|
||||
int64_t total_real = (now_real - start_real) / std::chrono::milliseconds(1);
|
||||
// We want to advance the fake clock by the difference between the real
|
||||
// clock, and the previous value on the fake clock.
|
||||
milliseconds = total_real - fake_clock;
|
||||
fake_clock += milliseconds;
|
||||
}
|
||||
if (callback_ != nullptr) callback_->ElapseTime(milliseconds);
|
||||
}
|
||||
|
||||
void TestSleep::SyncFakeClock() {
|
||||
// Syncing can be done by sleeping 0 seconds.
|
||||
Sleep(0);
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
49
util/test/test_sleep.h
Normal file
49
util/test/test_sleep.h
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
//
|
||||
// TestSleep - Controls sleep and clock adjustment during tests.
|
||||
//
|
||||
#ifndef WVCDM_UTIL_TEST_SLEEP_H_
|
||||
#define WVCDM_UTIL_TEST_SLEEP_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
class TestSleep {
|
||||
public:
|
||||
// The callback is called when the clock should be advanced.
|
||||
class CallBack {
|
||||
public:
|
||||
virtual void ElapseTime(int64_t milliseconds) = 0;
|
||||
|
||||
protected:
|
||||
virtual ~CallBack(){};
|
||||
};
|
||||
|
||||
// If real_sleep_ is true, then this sleeps for |seconds| of time.
|
||||
// If the callback exists, this calls the callback.
|
||||
static void Sleep(unsigned int seconds);
|
||||
|
||||
// If we are using a real clock and a fake clock, then the real clock advances
|
||||
// a little while we are doing work, but the fake one only advances when we
|
||||
// sleep. This function advances the fake clock to be in sync with the real
|
||||
// clock. This function should be called to prevent a slow flaky test from
|
||||
// failing due to this drift.
|
||||
static void SyncFakeClock();
|
||||
|
||||
static void set_real_sleep(bool real_sleep) { real_sleep_ = real_sleep; }
|
||||
|
||||
static void set_callback(CallBack* callback) { callback_ = callback; }
|
||||
|
||||
private:
|
||||
// Controls if the test sleep should use real sleep.
|
||||
static bool real_sleep_;
|
||||
// Called when the clock should advance.
|
||||
static CallBack* callback_;
|
||||
};
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_TEST_SLEEP_H_
|
||||
Reference in New Issue
Block a user