Source release 16.2.0
This commit is contained in:
46
util/include/advance_iv_ctr.h
Normal file
46
util/include/advance_iv_ctr.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
#ifndef WVCDM_UTIL_ADVANCE_IV_CTR_H_
|
||||
#define WVCDM_UTIL_ADVANCE_IV_CTR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// Advance an IV according to ISO-CENC's CTR modes. The lower half of the IV is
|
||||
// split off and treated as an unsigned 64-bit integer, then incremented by the
|
||||
// number of complete crypto blocks decrypted. The resulting value is then
|
||||
// copied back into the IV over the previous lower half.
|
||||
inline void AdvanceIvCtr(uint8_t (*subsample_iv)[16], size_t bytes) {
|
||||
constexpr size_t kAesBlockSize = 16;
|
||||
constexpr size_t kIvSize = kAesBlockSize;
|
||||
constexpr size_t kCounterIndex = kIvSize / 2;
|
||||
constexpr size_t kCounterSize = kIvSize / 2;
|
||||
|
||||
uint64_t counter;
|
||||
|
||||
static_assert(
|
||||
sizeof(*subsample_iv) == kIvSize,
|
||||
"The subsample_iv field is no longer the length of an AES-128 IV.");
|
||||
static_assert(sizeof(counter) == kCounterSize,
|
||||
"A uint64_t failed to be half the size of an AES-128 IV.");
|
||||
|
||||
// Defensive copy because the elements of the array may not be properly
|
||||
// aligned
|
||||
memcpy(&counter, &(*subsample_iv)[kCounterIndex], kCounterSize);
|
||||
|
||||
const size_t increment =
|
||||
bytes / kAesBlockSize; // The truncation here is intentional
|
||||
counter = htonll64(ntohll64(counter) + increment);
|
||||
|
||||
memcpy(&(*subsample_iv)[kCounterIndex], &counter, kCounterSize);
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_ADVANCE_IV_CTR_H_
|
||||
20
util/include/arraysize.h
Normal file
20
util/include/arraysize.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// 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.
|
||||
|
||||
#ifndef WVCDM_UTIL_ARRAYSIZE_H_
|
||||
#define WVCDM_UTIL_ARRAYSIZE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// Returns the size of a fixed-length array.
|
||||
template <typename T, size_t N>
|
||||
constexpr size_t ArraySize(const T (&)[N]) {
|
||||
return N;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_ARRAYSIZE_H_
|
||||
117
util/include/cdm_random.h
Normal file
117
util/include/cdm_random.h
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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.
|
||||
#ifndef WVCDM_CORE_CDM_RANDOM_H_
|
||||
#define WVCDM_CORE_CDM_RANDOM_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// CdmRandomGenerator is a thread safe, pseudo-random number generator.
|
||||
// It's purpose is to simplified interface for C++11's <random> library.
|
||||
// Some of the methods use a "device specific" random seed, if the
|
||||
// compiler/device does not support device specific randomizers, then the
|
||||
// actual value supplied may not be random. The generator is designed to
|
||||
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
|
||||
// be used with standard library functions / class which are designed to
|
||||
// work with the standard library generators.
|
||||
class CdmRandomGenerator {
|
||||
public:
|
||||
// Result type of operator().
|
||||
using result_type = unsigned int;
|
||||
// Inclusive boundaries of operator().
|
||||
static constexpr unsigned int min() { return 0; }
|
||||
static constexpr unsigned int max() { return RAND_MAX; }
|
||||
|
||||
// The maximum number of bytes that can be generated at once for
|
||||
// `RandomData()`.
|
||||
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
|
||||
|
||||
// Initializes the pseudo-random generator with a value from a device
|
||||
// specific random number generator.
|
||||
CdmRandomGenerator();
|
||||
|
||||
// Initializes the pseudo-random generator with the specified seed value.
|
||||
explicit CdmRandomGenerator(unsigned int seed) : generator_(seed) {}
|
||||
|
||||
// All of these methods are thread-safe.
|
||||
|
||||
// Seeds the pseudo-random generator with a value from a device specific
|
||||
// random number generator.
|
||||
void Seed();
|
||||
|
||||
// Seeds the pseudo-random generator with the specified seed value.
|
||||
// This is somewhat similar to `srand()` from the C standard library;
|
||||
// except that the sequence generated from successive calls to `Rand()`
|
||||
// will not necessarily be the same as they would be from the
|
||||
// standard library `rand()`. This is due to the underlying pseudo-random
|
||||
// generator that is used.
|
||||
void Seed(unsigned int seed);
|
||||
|
||||
// Returns a pseudo-random integer.
|
||||
// This is similar to `rand()` from the C standard library. The integer
|
||||
// returned is in the range of [min(), max()].
|
||||
unsigned int Rand();
|
||||
|
||||
// Allows for RNG to be callable.
|
||||
unsigned int operator()() { return Rand(); }
|
||||
|
||||
// Returns a pseudo-random integer within the provided inclusive range.
|
||||
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
|
||||
uint64_t RandomInRange(uint64_t upper) { return RandomInRange(0, upper); }
|
||||
|
||||
// Returns a byte string containing randomized bytes of the specified
|
||||
// length.
|
||||
// If |length| is greater than |CdmRandomGenerator::kMaxRandomDataLength|,
|
||||
// then an error is logged and an empty string is returned.
|
||||
std::string RandomData(size_t length);
|
||||
|
||||
// Random true/false using Bernoulli distribution of equal probability.
|
||||
bool RandomBool();
|
||||
|
||||
private:
|
||||
// Mutex is used to lock the object, and allowing it to be used
|
||||
// concurrently in different threads.
|
||||
std::mutex generator_lock_;
|
||||
|
||||
// The `default_random_engine` depends on the compiler used and
|
||||
// potentially its version. This is important to know if you need to
|
||||
// create reproducible tests between platforms.
|
||||
std::default_random_engine generator_;
|
||||
};
|
||||
|
||||
// Provides a static interface to a process-wide instance of
|
||||
// CdmRandomGenerator.
|
||||
class CdmRandom {
|
||||
public:
|
||||
static unsigned int Rand() { return GetInstance()->Rand(); }
|
||||
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
|
||||
return GetInstance()->RandomInRange(lower, upper);
|
||||
}
|
||||
static uint64_t RandomInRange(uint64_t upper) {
|
||||
return GetInstance()->RandomInRange(upper);
|
||||
}
|
||||
|
||||
static std::string RandomData(size_t length) {
|
||||
return GetInstance()->RandomData(length);
|
||||
}
|
||||
|
||||
static bool RandomBool() { return GetInstance()->RandomBool(); }
|
||||
|
||||
private:
|
||||
// These are intended to be used by tests if needed.
|
||||
static void Seed(unsigned int seed) { GetInstance()->Seed(seed); }
|
||||
static void Seed() { GetInstance()->Seed(); }
|
||||
|
||||
// Returns the process-wide instance of CdmRandomGenerator.
|
||||
// It the global instance has not yet been created, then a new instance
|
||||
// is created using a device-specific random seed.
|
||||
static CdmRandomGenerator* GetInstance();
|
||||
};
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_CORE_CDM_RANDOM_H_
|
||||
@@ -11,7 +11,6 @@ namespace wvcdm {
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
|
||||
|
||||
@@ -33,22 +33,21 @@ extern LogPriority g_cutoff;
|
||||
// unit tests.
|
||||
CORE_UTIL_EXPORT void InitLogging();
|
||||
|
||||
CORE_UTIL_EXPORT void Log(
|
||||
const char* file, const char* function, int line, LogPriority level,
|
||||
const char* fmt, ...);
|
||||
CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line,
|
||||
LogPriority level, const char* fmt, ...);
|
||||
|
||||
// Log APIs
|
||||
#ifndef LOGE
|
||||
#define LOGE(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_ERROR, __VA_ARGS__)
|
||||
#define LOGW(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_WARN, __VA_ARGS__)
|
||||
#define LOGI(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_INFO, __VA_ARGS__)
|
||||
#define LOGD(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_DEBUG, __VA_ARGS__)
|
||||
#define LOGV(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_VERBOSE, __VA_ARGS__)
|
||||
# define LOGE(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_ERROR, __VA_ARGS__)
|
||||
# define LOGW(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_WARN, __VA_ARGS__)
|
||||
# define LOGI(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_INFO, __VA_ARGS__)
|
||||
# define LOGD(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_DEBUG, __VA_ARGS__)
|
||||
# define LOGV(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_VERBOSE, __VA_ARGS__)
|
||||
#endif
|
||||
} // namespace wvcdm
|
||||
|
||||
|
||||
@@ -10,24 +10,22 @@
|
||||
#include "util_common.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <wtypes.h>
|
||||
# include <BaseTsd.h>
|
||||
# include <winsock2.h> // For htonl and ntohl.
|
||||
# define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
# undef NO_ERROR
|
||||
# undef GetCurrentTime
|
||||
# undef DeleteFile
|
||||
# include <BaseTsd.h>
|
||||
# include <winsock2.h> // For htonl and ntohl.
|
||||
# include <wtypes.h>
|
||||
# define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
# undef NO_ERROR
|
||||
# undef GetCurrentTime
|
||||
# undef DeleteFile
|
||||
|
||||
using ssize_t = SSIZE_T;
|
||||
|
||||
inline void sleep(int seconds) {
|
||||
Sleep(seconds * 1000);
|
||||
}
|
||||
inline void sleep(int seconds) { Sleep(seconds * 1000); }
|
||||
CORE_UTIL_EXPORT int setenv(const char* key, const char* value, int overwrite);
|
||||
#else
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#endif // WVCDM_UTIL_PLATFORM_H_
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -35,6 +36,8 @@ CORE_UTIL_EXPORT std::string IntToString(int value);
|
||||
CORE_UTIL_EXPORT int64_t htonll64(int64_t x);
|
||||
CORE_UTIL_EXPORT inline int64_t ntohll64(int64_t x) { return htonll64(x); }
|
||||
CORE_UTIL_EXPORT std::string BytesToString(const uint8_t* bytes, unsigned size);
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
CORE_UTIL_EXPORT std::string EncodeUint32(unsigned int u);
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
#define WVCDM_UTIL_UTIL_COMMON_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __declspec(dllexport)
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __declspec(dllexport)
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT
|
||||
# endif
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // WVCDM_UTIL_UTIL_COMMON_H_
|
||||
|
||||
19
util/libcrypto_dependency.gypi
Normal file
19
util/libcrypto_dependency.gypi
Normal file
@@ -0,0 +1,19 @@
|
||||
# Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
|
||||
# source code may only be used and distributed under the Widevine Master
|
||||
# License Agreement.
|
||||
{
|
||||
'conditions': [
|
||||
[
|
||||
'privacy_crypto_impl=="openssl"', {
|
||||
'libraries': [
|
||||
'-lcrypto',
|
||||
],
|
||||
}, # privacy_crypto_impl=="openssl"
|
||||
'privacy_crypto_impl=="boringssl" or privacy_crypto_impl=="apple"', {
|
||||
'dependencies': [
|
||||
'<(boringssl_libcrypto_path)',
|
||||
], # dependencies
|
||||
}, # privacy_crypto_impl=="boringssl" or privacy_crypto_impl=="apple"
|
||||
],
|
||||
], # conditions
|
||||
}
|
||||
20
util/libssl_dependency.gypi
Normal file
20
util/libssl_dependency.gypi
Normal file
@@ -0,0 +1,20 @@
|
||||
# Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
|
||||
# source code may only be used and distributed under the Widevine Master
|
||||
# License Agreement.
|
||||
{
|
||||
'conditions': [
|
||||
[
|
||||
'privacy_crypto_impl=="openssl"', {
|
||||
'libraries': [
|
||||
'-lcrypto',
|
||||
'-lssl',
|
||||
],
|
||||
}, # privacy_crypto_impl=="openssl"
|
||||
'privacy_crypto_impl=="boringssl" or privacy_crypto_impl=="apple"', {
|
||||
'dependencies': [
|
||||
'<(boringssl_libssl_path)',
|
||||
], # dependencies
|
||||
}, # privacy_crypto_impl=="boringssl" or privacy_crypto_impl=="apple"
|
||||
],
|
||||
], # conditions
|
||||
}
|
||||
107
util/src/cdm_random.cpp
Normal file
107
util/src/cdm_random.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
// 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 "cdm_random.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
// This type alias is for convenience.
|
||||
using CdmRandomLock = std::unique_lock<std::mutex>;
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
// More information about C++11's random number generators can be found
|
||||
// from the introductory paper https://isocpp.org/files/papers/n3551.pdf
|
||||
|
||||
// Attemps to get random data in a device specific manner. If the device
|
||||
// does not support true random data, then a pseudo-random sequence might
|
||||
// be used instead. The exact behaviour depends on the compiler and
|
||||
// platform combination.
|
||||
unsigned int GetDeviceRandomSeed() {
|
||||
static std::random_device rdev;
|
||||
static std::mutex rdev_mutex;
|
||||
CdmRandomLock rdev_lock(rdev_mutex);
|
||||
return rdev();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// CdmRandomGenerator.
|
||||
|
||||
CdmRandomGenerator::CdmRandomGenerator() : generator_(GetDeviceRandomSeed()) {}
|
||||
|
||||
void CdmRandomGenerator::Seed() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
generator_.seed(GetDeviceRandomSeed());
|
||||
}
|
||||
|
||||
void CdmRandomGenerator::Seed(unsigned int s) {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
generator_.seed(s);
|
||||
}
|
||||
|
||||
unsigned int CdmRandomGenerator::Rand() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::uniform_int_distribution<unsigned int> dist(0, RAND_MAX);
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
uint64_t CdmRandomGenerator::RandomInRange(uint64_t lower, uint64_t upper) {
|
||||
if (lower == upper) {
|
||||
return lower;
|
||||
}
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
if (lower > upper) {
|
||||
LOGW(
|
||||
"Lower bound is larger than upper bound, swapping bounds: "
|
||||
"lower = %llu, upper = %llu",
|
||||
// Casting to insure this will work on 32-bit systems.
|
||||
static_cast<unsigned long long>(lower),
|
||||
static_cast<unsigned long long>(upper));
|
||||
std::swap(lower, upper);
|
||||
}
|
||||
std::uniform_int_distribution<uint64_t> dist(lower, upper);
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
std::string CdmRandomGenerator::RandomData(size_t length) {
|
||||
if (length > kMaxRandomDataLength) {
|
||||
LOGE("Maximum random data length exceeded: length = %zu, max_length = %zu",
|
||||
length, kMaxRandomDataLength);
|
||||
return std::string();
|
||||
}
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::uniform_int_distribution<uint8_t> dist; // Range of [0, 255].
|
||||
std::string random_data(length, '\0');
|
||||
std::generate(random_data.begin(), random_data.end(),
|
||||
[&]() { return dist(generator_); });
|
||||
return random_data;
|
||||
}
|
||||
|
||||
bool CdmRandomGenerator::RandomBool() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::bernoulli_distribution dist; // 50/50.
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
// CdmRandom.
|
||||
|
||||
// static
|
||||
CdmRandomGenerator* CdmRandom::GetInstance() {
|
||||
static std::mutex g_instance_lock;
|
||||
static CdmRandomGenerator* g_instance = nullptr;
|
||||
CdmRandomLock lock(g_instance_lock);
|
||||
if (g_instance == nullptr) {
|
||||
LOGV("Initalizing CDM random number generator");
|
||||
g_instance = new CdmRandomGenerator(GetDeviceRandomSeed());
|
||||
}
|
||||
return g_instance;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
@@ -6,7 +6,6 @@
|
||||
//
|
||||
#include <windows.h>
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
|
||||
LPVOID lpReserved) {
|
||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
|
||||
return TRUE;
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@@ -24,12 +25,11 @@ static const char kBase64Codes[] =
|
||||
// Gets the given (zero-indexed) bits [a, b) of |in|.
|
||||
#define GET_BITS(in, a, b) GET_LOW_BITS((in) >> (a), (b) - (a))
|
||||
// Calculates a/b using round-up division (only works for positive numbers).
|
||||
#define CEIL_DIVIDE(a, b) ((((a) - 1) / (b)) + 1)
|
||||
#define CEIL_DIVIDE(a, b) ((((a)-1) / (b)) + 1)
|
||||
|
||||
int DecodeBase64Char(char c) {
|
||||
const char* it = strchr(kBase64Codes, c);
|
||||
if (it == NULL)
|
||||
return -1;
|
||||
if (it == nullptr) return -1;
|
||||
return it - kBase64Codes;
|
||||
}
|
||||
|
||||
@@ -87,10 +87,12 @@ std::string a2bs_hex(const std::string& byte) {
|
||||
}
|
||||
|
||||
std::string b2a_hex(const std::vector<uint8_t>& byte) {
|
||||
return HexEncode(&byte[0], byte.size());
|
||||
if (byte.empty()) return "";
|
||||
return HexEncode(byte.data(), byte.size());
|
||||
}
|
||||
|
||||
std::string b2a_hex(const std::string& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return HexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
|
||||
byte.length());
|
||||
}
|
||||
@@ -121,8 +123,8 @@ std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
|
||||
if (i % 3 == 2) {
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 0, 6)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 0, 6)];
|
||||
temp = 0;
|
||||
}
|
||||
}
|
||||
@@ -135,7 +137,7 @@ std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
|
||||
} else if (bin_input.size() % 3 == 2) {
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = '=';
|
||||
}
|
||||
|
||||
@@ -208,8 +210,8 @@ std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
|
||||
|
||||
if (i % 4 == 3) {
|
||||
result[out_index++] = GET_BITS(temp, 16, 24);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
result[out_index++] = GET_BITS(temp, 0, 8);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
result[out_index++] = GET_BITS(temp, 0, 8);
|
||||
temp = 0;
|
||||
}
|
||||
}
|
||||
@@ -223,7 +225,7 @@ std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
|
||||
break;
|
||||
case 3:
|
||||
result[out_index++] = GET_BITS(temp, 16, 24);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
break;
|
||||
}
|
||||
result.resize(out_index);
|
||||
@@ -251,7 +253,7 @@ std::vector<uint8_t> Base64SafeDecode(const std::string& b64_input) {
|
||||
|
||||
std::string HexEncode(const uint8_t* in_buffer, unsigned int size) {
|
||||
static const char kHexChars[] = "0123456789ABCDEF";
|
||||
|
||||
if (size == 0) return "";
|
||||
// Each input byte creates two output hex characters.
|
||||
std::string out_buffer(size * 2, '\0');
|
||||
|
||||
@@ -298,4 +300,14 @@ std::string BytesToString(const uint8_t* bytes, unsigned size) {
|
||||
return std::string(char_bytes, char_bytes + size);
|
||||
}
|
||||
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
std::string EncodeUint32(unsigned int u) {
|
||||
std::string s;
|
||||
s.append(1, (u >> 24) & 0xFF);
|
||||
s.append(1, (u >> 16) & 0xFF);
|
||||
s.append(1, (u >> 8) & 0xFF);
|
||||
s.append(1, (u >> 0) & 0xFF);
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
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