OEMCrypto v16.1 Documentation and Headers
This commit contains the updated v16.1 documentation dated Nov 12th, as well has the headers and update ODK library. Unit tests and reference code is partially implemented, but not yet complete.
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
|
||||
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 2013 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 2018 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