Initial v17 Release
Headers and Unit tests have been updated to match the v17 spec. Documentation can be found here: https://developers.devsite.corp.google.com/widevine/drm/client/oemcrypto/v17
This commit is contained in:
@@ -2,14 +2,14 @@
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "log.h"
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -55,8 +55,14 @@ const std::pair<const std::string*, const std::string*> kBase64TestVectors[] = {
|
||||
make_pair(&kTwoBytesOverData, &kTwoBytesOverB64Data),
|
||||
make_pair(&kTestData, &kB64TestData)};
|
||||
|
||||
const std::string kBase64ErrorVectors[] = {"Foo$sa", "Foo\x99\x23\xfa\02",
|
||||
"Foo==Foo", "FooBa"};
|
||||
// Arbitrary invalid base64 test vectors
|
||||
const std::string kBase64ErrorVectors[] = {"Foo$sa",
|
||||
"Foo\x99\x23\xfa\02",
|
||||
"Foo==Foo",
|
||||
"FooBa",
|
||||
"SGVsbG8sIFdvcmxkI===",
|
||||
"SGVsbG8sIFdvcmxkI======",
|
||||
"SGVsbG8sIFdvcmxkIQp=="};
|
||||
|
||||
std::string ConvertToBase64WebSafe(const std::string& std_base64_string) {
|
||||
std::string str(std_base64_string);
|
||||
@@ -77,35 +83,97 @@ class Base64EncodeDecodeTest
|
||||
|
||||
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());
|
||||
const std::string& plain_text_string = *(values.first);
|
||||
const std::string& expected_encoded = *(values.second);
|
||||
|
||||
// Encode from string.
|
||||
const std::string b64_string_encoded = Base64Encode(plain_text_string);
|
||||
EXPECT_EQ(b64_string_encoded, expected_encoded);
|
||||
|
||||
// Encode from vector.
|
||||
const std::vector<uint8_t> plain_text_vector(plain_text_string.begin(),
|
||||
plain_text_string.end());
|
||||
const std::string b64_vector_encoded = Base64Encode(plain_text_vector);
|
||||
EXPECT_EQ(b64_vector_encoded, expected_encoded);
|
||||
|
||||
// Decode from string.
|
||||
const std::vector<uint8_t> decoded_vector = Base64Decode(expected_encoded);
|
||||
EXPECT_EQ(decoded_vector, plain_text_vector);
|
||||
}
|
||||
|
||||
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());
|
||||
const std::string& plain_text_string = *(values.first);
|
||||
const std::string& expected_encoded =
|
||||
ConvertToBase64WebSafe(*(values.second));
|
||||
|
||||
// Encode from string.
|
||||
const std::string b64_string_encoded = Base64SafeEncode(plain_text_string);
|
||||
EXPECT_EQ(b64_string_encoded, expected_encoded);
|
||||
|
||||
// Encode from vector.
|
||||
const std::vector<uint8_t> plain_text_vector(plain_text_string.begin(),
|
||||
plain_text_string.end());
|
||||
const std::string b64_vector_encoded = Base64SafeEncode(plain_text_vector);
|
||||
EXPECT_EQ(b64_vector_encoded, expected_encoded);
|
||||
|
||||
// Decode from string.
|
||||
const std::vector<uint8_t> decoded_vector =
|
||||
Base64SafeDecode(expected_encoded);
|
||||
EXPECT_EQ(decoded_vector, plain_text_vector);
|
||||
}
|
||||
|
||||
TEST_P(Base64EncodeDecodeTest, WebSafeEncodeNoPad) {
|
||||
std::pair<const std::string*, const std::string*> values = GetParam();
|
||||
const std::string& plain_text_string = *(values.first);
|
||||
const std::string& padded_encoded = ConvertToBase64WebSafe(*(values.second));
|
||||
|
||||
// Encode from string.
|
||||
const std::string b64_string_encoded =
|
||||
Base64SafeEncodeNoPad(plain_text_string);
|
||||
|
||||
// If input is empty, output will be empty.
|
||||
if (plain_text_string.empty()) {
|
||||
EXPECT_TRUE(b64_string_encoded.empty());
|
||||
return;
|
||||
}
|
||||
|
||||
if (padded_encoded.back() == '=') {
|
||||
// If padding is present in the regular encoding, then it should be
|
||||
// striped from the result.
|
||||
EXPECT_NE(b64_string_encoded.back(), '=');
|
||||
const std::string expected_encoded =
|
||||
padded_encoded.substr(0, b64_string_encoded.size());
|
||||
EXPECT_EQ(b64_string_encoded, expected_encoded);
|
||||
} else {
|
||||
// If no padding is present, then results should be equal.
|
||||
EXPECT_EQ(b64_string_encoded, padded_encoded);
|
||||
}
|
||||
|
||||
// Encode from vector.
|
||||
const std::vector<uint8_t> plain_text_vector(plain_text_string.begin(),
|
||||
plain_text_string.end());
|
||||
const std::string b64_vector_encoded =
|
||||
Base64SafeEncodeNoPad(plain_text_vector);
|
||||
// Assuming the above has passed, the results should be the same as
|
||||
// a result encoded from a string.
|
||||
EXPECT_EQ(b64_vector_encoded, b64_string_encoded);
|
||||
}
|
||||
|
||||
class Base64ErrorDecodeTest : public ::testing::TestWithParam<std::string> {};
|
||||
|
||||
TEST_P(Base64ErrorDecodeTest, EncoderErrors) {
|
||||
std::vector<uint8_t> result = Base64Decode(GetParam());
|
||||
EXPECT_EQ(0u, result.size());
|
||||
const std::vector<uint8_t> standard_result = Base64Decode(GetParam());
|
||||
EXPECT_TRUE(standard_result.empty());
|
||||
const std::vector<uint8_t> safe_result = Base64SafeDecode(GetParam());
|
||||
EXPECT_TRUE(safe_result.empty());
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(ExecutesBase64Test, Base64EncodeDecodeTest,
|
||||
::testing::ValuesIn(kBase64TestVectors));
|
||||
INSTANTIATE_TEST_SUITE_P(ExecutesBase64Test, Base64EncodeDecodeTest,
|
||||
::testing::ValuesIn(kBase64TestVectors));
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(ExecutesBase64Test, Base64ErrorDecodeTest,
|
||||
::testing::ValuesIn(kBase64ErrorVectors));
|
||||
INSTANTIATE_TEST_SUITE_P(ExecutesBase64Test, Base64ErrorDecodeTest,
|
||||
::testing::ValuesIn(kBase64ErrorVectors));
|
||||
|
||||
class HtoNLL64Test : public ::testing::Test {};
|
||||
|
||||
@@ -121,4 +189,4 @@ TEST_F(HtoNLL64Test, NegativeNumber) {
|
||||
int64_t host_byte_order = htonll64(*network_byte_order);
|
||||
EXPECT_EQ(-0x01FdFcFbFaF9F8F8, host_byte_order);
|
||||
}
|
||||
} // namespace wvcdm
|
||||
} // namespace wvutil
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "cdm_random.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -12,12 +16,7 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cdm_random.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
namespace {
|
||||
// Random data vector lengths.
|
||||
@@ -173,8 +172,8 @@ TEST_P(CdmRandomGeneratorTest, ThreadSafety) {
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(VariousSeeds, CdmRandomGeneratorTest,
|
||||
testing::ValuesIn(kSeeds));
|
||||
INSTANTIATE_TEST_SUITE_P(VariousSeeds, CdmRandomGeneratorTest,
|
||||
testing::ValuesIn(kSeeds));
|
||||
|
||||
TEST(CdmRandomTest, AllMethods) {
|
||||
CdmRandom::Rand();
|
||||
@@ -184,4 +183,4 @@ TEST(CdmRandomTest, AllMethods) {
|
||||
CdmRandom::RandomBool();
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
} // namespace wvutil
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "file_store.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cdm_random.h"
|
||||
#include "file_store.h"
|
||||
#include "test_vectors.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
namespace {
|
||||
const std::string kTestDirName = "test_dir";
|
||||
@@ -18,7 +19,10 @@ 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 kTestIdentifier1 = "some_identifier";
|
||||
const std::string kTestIdentifier2 = "some_other_identifier";
|
||||
const std::string kWildcard = "*";
|
||||
const std::string kUnderscore = "_";
|
||||
} // namespace
|
||||
|
||||
class FileTest : public testing::Test {
|
||||
@@ -28,100 +32,100 @@ class FileTest : public testing::Test {
|
||||
void TearDown() override { RemoveTestDir(); }
|
||||
|
||||
void RemoveTestDir() {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
EXPECT_TRUE(file_system_.Remove(wvcdm::test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
FileSystem file_system;
|
||||
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));
|
||||
EXPECT_TRUE(file_system_.Exists(wvcdm::test_vectors::kExistentFile));
|
||||
EXPECT_TRUE(file_system_.Exists(wvcdm::test_vectors::kExistentDir));
|
||||
EXPECT_FALSE(file_system_.Exists(wvcdm::test_vectors::kNonExistentFile));
|
||||
EXPECT_FALSE(file_system_.Exists(wvcdm::test_vectors::kNonExistentDir));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveDir) {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kTestDir));
|
||||
EXPECT_TRUE(file_system_.Remove(wvcdm::test_vectors::kTestDir));
|
||||
EXPECT_FALSE(file_system_.Exists(wvcdm::test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, OpenFile) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
EXPECT_TRUE(file_system.Remove(path));
|
||||
std::string path = wvcdm::test_vectors::kTestDir + kTestFileName;
|
||||
EXPECT_TRUE(file_system_.Remove(path));
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
|
||||
std::unique_ptr<File> file = file_system_.Open(path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
EXPECT_TRUE(file_system_.Exists(path));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveDirAndFile) {
|
||||
std::string path = test_vectors::kTestDir + kTestFileName;
|
||||
std::string path = wvcdm::test_vectors::kTestDir + kTestFileName;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
|
||||
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));
|
||||
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);
|
||||
file = file_system_.Open(path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
|
||||
EXPECT_TRUE(file_system.Exists(path));
|
||||
EXPECT_TRUE(file_system_.Exists(path));
|
||||
RemoveTestDir();
|
||||
EXPECT_FALSE(file_system.Exists(test_vectors::kTestDir));
|
||||
EXPECT_FALSE(file_system.Exists(path));
|
||||
EXPECT_FALSE(file_system_.Exists(wvcdm::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 path1 = wvcdm::test_vectors::kTestDir + kTestFileName;
|
||||
std::string path2 = wvcdm::test_vectors::kTestDir + kTestFileName2;
|
||||
std::string wildcard_path =
|
||||
test_vectors::kTestDir + kWildcard + kTestFileNameExt;
|
||||
wvcdm::test_vectors::kTestDir + kWildcard + kTestFileNameExt;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path1, FileSystem::kCreate);
|
||||
std::unique_ptr<File> file = file_system_.Open(path1, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path2, FileSystem::kCreate);
|
||||
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));
|
||||
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 path = wvcdm::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);
|
||||
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_TRUE(file_system_.Exists(path));
|
||||
|
||||
EXPECT_EQ(static_cast<ssize_t>(write_data_size), file_system.FileSize(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 path = wvcdm::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);
|
||||
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_TRUE(file_system_.Exists(path));
|
||||
|
||||
std::string read_data;
|
||||
read_data.resize(file_system.FileSize(path));
|
||||
read_data.resize(file_system_.FileSize(path));
|
||||
size_t read_data_size = read_data.size();
|
||||
file = file_system.Open(path, FileSystem::kReadOnly);
|
||||
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);
|
||||
@@ -131,30 +135,30 @@ 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::string path1 = wvcdm::test_vectors::kTestDir + kTestFileName;
|
||||
std::string path2 = wvcdm::test_vectors::kTestDir + kTestFileName2;
|
||||
std::string path3 = wvcdm::test_vectors::kTestDir + kTestFileName3;
|
||||
std::string path_dir = wvcdm::test_vectors::kTestDir;
|
||||
|
||||
std::unique_ptr<File> file = file_system.Open(path1, FileSystem::kCreate);
|
||||
std::unique_ptr<File> file = file_system_.Open(path1, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path2, FileSystem::kCreate);
|
||||
file = file_system_.Open(path2, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system.Open(path3, FileSystem::kCreate);
|
||||
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));
|
||||
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));
|
||||
EXPECT_FALSE(file_system_.List(not_path, &names));
|
||||
|
||||
// Valid path, but no way to return names.
|
||||
EXPECT_FALSE(file_system.List(path_dir, nullptr));
|
||||
EXPECT_FALSE(file_system_.List(path_dir, nullptr));
|
||||
|
||||
// Valid path, valid return.
|
||||
EXPECT_TRUE(file_system.List(path_dir, &names));
|
||||
EXPECT_TRUE(file_system_.List(path_dir, &names));
|
||||
|
||||
// Should find three files. Order not important.
|
||||
EXPECT_EQ(3u, names.size());
|
||||
@@ -162,17 +166,202 @@ TEST_F(FileTest, ListFiles) {
|
||||
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_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_TRUE(file_system_.Remove(wild_card_path2));
|
||||
EXPECT_TRUE(file_system_.List(path_dir, &names));
|
||||
|
||||
EXPECT_EQ(0u, names.size());
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
TEST_F(FileTest, CreateGlobalCertificates) {
|
||||
// Clear directory
|
||||
std::vector<std::string> names;
|
||||
std::string path_dir = wvcdm::test_vectors::kTestDir;
|
||||
std::string wild_card_path = path_dir + kWildcard;
|
||||
file_system_.Remove(wild_card_path);
|
||||
if (file_system_.List(path_dir, &names)) {
|
||||
EXPECT_EQ(0u, names.size());
|
||||
}
|
||||
|
||||
// Create certificates and verify that they exist
|
||||
std::string certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kCertificateFileName;
|
||||
std::string legacy_certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kLegacyCertificateFileName;
|
||||
|
||||
std::unique_ptr<File> file =
|
||||
file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(file_system_.IsGlobal());
|
||||
|
||||
EXPECT_TRUE(file_system_.Exists(certificate_path));
|
||||
EXPECT_TRUE(file_system_.Exists(legacy_certificate_path));
|
||||
|
||||
EXPECT_TRUE(file_system_.List(path_dir, &names));
|
||||
|
||||
// Should find two files. Order not important.
|
||||
EXPECT_EQ(2u, names.size());
|
||||
EXPECT_THAT(names, ::testing::UnorderedElementsAre(
|
||||
kCertificateFileName, kLegacyCertificateFileName));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, CreateCertificates) {
|
||||
// Clear directory
|
||||
std::vector<std::string> names;
|
||||
std::string path_dir = wvcdm::test_vectors::kTestDir;
|
||||
std::string wild_card_path = path_dir + kWildcard;
|
||||
file_system_.Remove(wild_card_path);
|
||||
if (file_system_.List(path_dir, &names)) {
|
||||
EXPECT_EQ(0u, names.size());
|
||||
}
|
||||
|
||||
std::string certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kCertificateFileName;
|
||||
std::string legacy_certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kLegacyCertificateFileName;
|
||||
|
||||
// Create Global certificates
|
||||
std::unique_ptr<File> file =
|
||||
file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(file_system_.IsGlobal());
|
||||
|
||||
// Create certificates with first identifier
|
||||
file_system_.set_identifier(kTestIdentifier1);
|
||||
file = file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(!file_system_.IsGlobal());
|
||||
|
||||
// Create certificates with second identifier
|
||||
file_system_.set_identifier(kTestIdentifier2);
|
||||
file = file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(!file_system_.IsGlobal());
|
||||
|
||||
EXPECT_TRUE(file_system_.Exists(certificate_path));
|
||||
EXPECT_TRUE(file_system_.Exists(legacy_certificate_path));
|
||||
|
||||
EXPECT_TRUE(file_system_.List(path_dir, &names));
|
||||
|
||||
// Should find six files. Order not important.
|
||||
bool is_global_certificate_present = false;
|
||||
bool is_global_legacy_certificate_present = false;
|
||||
size_t certificate_count = 0;
|
||||
size_t legacy_certificate_count = 0;
|
||||
EXPECT_EQ(6u, names.size());
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
if (names[i].size() > kCertificateFileName.size()) {
|
||||
if (names[i].compare(0, kCertificateFileNamePrefix.size(),
|
||||
kCertificateFileNamePrefix) == 0)
|
||||
++certificate_count;
|
||||
else if (names[i].compare(0, kLegacyCertificateFileNamePrefix.size(),
|
||||
kLegacyCertificateFileNamePrefix) == 0)
|
||||
++legacy_certificate_count;
|
||||
} else if (names[i].compare(kCertificateFileName) == 0) {
|
||||
is_global_certificate_present = true;
|
||||
} else if (names[i].compare(kLegacyCertificateFileName) == 0) {
|
||||
is_global_legacy_certificate_present = true;
|
||||
} else {
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(2, certificate_count);
|
||||
EXPECT_EQ(2, legacy_certificate_count);
|
||||
EXPECT_TRUE(is_global_certificate_present);
|
||||
EXPECT_TRUE(is_global_legacy_certificate_present);
|
||||
}
|
||||
|
||||
TEST_F(FileTest, RemoveCertificates) {
|
||||
// Clear directory
|
||||
std::vector<std::string> names;
|
||||
std::string path_dir = wvcdm::test_vectors::kTestDir;
|
||||
std::string wild_card_path = path_dir + kWildcard;
|
||||
file_system_.Remove(wild_card_path);
|
||||
if (file_system_.List(path_dir, &names)) {
|
||||
EXPECT_EQ(0u, names.size());
|
||||
}
|
||||
|
||||
std::string certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kCertificateFileName;
|
||||
std::string legacy_certificate_path =
|
||||
wvcdm::test_vectors::kTestDir + kLegacyCertificateFileName;
|
||||
|
||||
// Create Global certificates
|
||||
std::unique_ptr<File> file =
|
||||
file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(file_system_.IsGlobal());
|
||||
|
||||
// Create certificates with first identifier
|
||||
file_system_.set_identifier(kTestIdentifier1);
|
||||
file = file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(!file_system_.IsGlobal());
|
||||
|
||||
// Create certificates with second identifier
|
||||
file_system_.set_identifier(kTestIdentifier2);
|
||||
file = file_system_.Open(certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
file = file_system_.Open(legacy_certificate_path, FileSystem::kCreate);
|
||||
ASSERT_TRUE(file);
|
||||
EXPECT_TRUE(!file_system_.IsGlobal());
|
||||
|
||||
EXPECT_TRUE(file_system_.Exists(certificate_path));
|
||||
EXPECT_TRUE(file_system_.Exists(legacy_certificate_path));
|
||||
|
||||
EXPECT_TRUE(file_system_.List(path_dir, &names));
|
||||
|
||||
EXPECT_EQ(6u, names.size());
|
||||
|
||||
// Remove all even number listed files
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
if (i % 2 == 0) {
|
||||
EXPECT_TRUE(
|
||||
file_system_.Remove(wvcdm::test_vectors::kTestDir + names[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that they have been removed
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
if (i % 2 == 1) {
|
||||
EXPECT_TRUE(
|
||||
file_system_.Exists(wvcdm::test_vectors::kTestDir + names[i]));
|
||||
} else {
|
||||
EXPECT_FALSE(
|
||||
file_system_.Exists(wvcdm::test_vectors::kTestDir + names[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all odd number listed files
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
if (i % 2 == 1) {
|
||||
EXPECT_TRUE(
|
||||
file_system_.Remove(wvcdm::test_vectors::kTestDir + names[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that all have been removed
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
EXPECT_FALSE(file_system_.Exists(wvcdm::test_vectors::kTestDir + names[i]));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
@@ -4,20 +4,19 @@
|
||||
//
|
||||
// Clock - A fake clock just for running tests.
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "clock.h"
|
||||
#include "test_sleep.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
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();
|
||||
auto now = std::chrono::system_clock().now();
|
||||
now_ = now.time_since_epoch() / std::chrono::milliseconds(1);
|
||||
TestSleep::set_callback(this);
|
||||
}
|
||||
@@ -40,4 +39,4 @@ int64_t Clock::GetCurrentTime() {
|
||||
return g_fake_clock->now() / 1000;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
} // namespace wvutil
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
# include <TargetConditionals.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
@@ -19,7 +22,7 @@
|
||||
#include "clock.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
bool TestSleep::real_sleep_ = true;
|
||||
TestSleep::CallBack* TestSleep::callback_ = nullptr;
|
||||
@@ -73,10 +76,13 @@ bool TestSleep::RollbackSystemTime(int seconds) {
|
||||
file_time.dwHighDateTime = long_time >> 32;
|
||||
if (!FileTimeToSystemTime(&file_time, &time)) return false;
|
||||
if (!SetSystemTime(&time)) return false;
|
||||
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
|
||||
LOGE("iOS time rollback: cannot set system time.");
|
||||
return false;
|
||||
#else
|
||||
auto time = std::chrono::system_clock::now();
|
||||
auto modified_time = time - std::chrono::seconds(seconds);
|
||||
;
|
||||
|
||||
timespec time_spec;
|
||||
time_spec.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
modified_time.time_since_epoch())
|
||||
@@ -140,6 +146,9 @@ bool TestSleep::CanChangeSystemTime() {
|
||||
}
|
||||
LOGE("Win32 time rollback: cannot set system time.");
|
||||
return false;
|
||||
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
|
||||
LOGE("iOS time rollback: cannot set system time.");
|
||||
return false;
|
||||
#else
|
||||
// Otherwise, the test needs to be run as root.
|
||||
const uid_t uid = getuid();
|
||||
@@ -148,4 +157,4 @@ bool TestSleep::CanChangeSystemTime() {
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
} // namespace wvcdm
|
||||
} // namespace wvutil
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace wvutil {
|
||||
|
||||
class TestSleep {
|
||||
public:
|
||||
@@ -70,6 +70,6 @@ class TestSleep {
|
||||
static int total_clock_rollback_seconds_;
|
||||
};
|
||||
|
||||
} // namespace wvcdm
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WVCDM_UTIL_TEST_SLEEP_H_
|
||||
|
||||
Reference in New Issue
Block a user