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
368 lines
12 KiB
C++
368 lines
12 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// 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 "test_vectors.h"
|
|
|
|
namespace wvutil {
|
|
|
|
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 kTestIdentifier1 = "some_identifier";
|
|
const std::string kTestIdentifier2 = "some_other_identifier";
|
|
const std::string kWildcard = "*";
|
|
const std::string kUnderscore = "_";
|
|
} // namespace
|
|
|
|
class FileTest : public testing::Test {
|
|
protected:
|
|
FileTest() {}
|
|
|
|
void TearDown() override { RemoveTestDir(); }
|
|
|
|
void RemoveTestDir() {
|
|
EXPECT_TRUE(file_system_.Remove(wvcdm::test_vectors::kTestDir));
|
|
}
|
|
|
|
FileSystem file_system_;
|
|
};
|
|
|
|
TEST_F(FileTest, FileExists) {
|
|
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(wvcdm::test_vectors::kTestDir));
|
|
EXPECT_FALSE(file_system_.Exists(wvcdm::test_vectors::kTestDir));
|
|
}
|
|
|
|
TEST_F(FileTest, OpenFile) {
|
|
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);
|
|
ASSERT_TRUE(file);
|
|
|
|
EXPECT_TRUE(file_system_.Exists(path));
|
|
}
|
|
|
|
TEST_F(FileTest, RemoveDirAndFile) {
|
|
std::string path = wvcdm::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(wvcdm::test_vectors::kTestDir));
|
|
EXPECT_FALSE(file_system_.Exists(path));
|
|
}
|
|
|
|
TEST_F(FileTest, RemoveWildcardFiles) {
|
|
std::string path1 = wvcdm::test_vectors::kTestDir + kTestFileName;
|
|
std::string path2 = wvcdm::test_vectors::kTestDir + kTestFileName2;
|
|
std::string wildcard_path =
|
|
wvcdm::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 = 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);
|
|
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 = 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);
|
|
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 = 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);
|
|
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());
|
|
}
|
|
|
|
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
|