diff --git a/libwvdrmengine/cdm/core/src/device_files.cpp b/libwvdrmengine/cdm/core/src/device_files.cpp index 7c21b8e4..807681fb 100644 --- a/libwvdrmengine/cdm/core/src/device_files.cpp +++ b/libwvdrmengine/cdm/core/src/device_files.cpp @@ -71,8 +71,6 @@ using video_widevine_client::sdk:: namespace { -const char kAtscCertificateFileName[] = "atsccert.bin"; -const char kCertificateFileName[] = "cert.bin"; const char kHlsAttributesFileNameExt[] = ".hal"; const char kUsageInfoFileNamePrefix[] = "usage"; const char kUsageInfoFileNameExt[] = ".bin"; diff --git a/libwvdrmengine/cdm/util/include/file_store.h b/libwvdrmengine/cdm/util/include/file_store.h index 94dded6a..a2310e9e 100644 --- a/libwvdrmengine/cdm/util/include/file_store.h +++ b/libwvdrmengine/cdm/util/include/file_store.h @@ -18,6 +18,13 @@ namespace wvcdm { +static const std::string kAtscCertificateFileName = "atsccert.bin"; +static const std::string kCertificateFileName = "cert1.bin"; +static const std::string kCertificateFileNameExt = ".bin"; +static const std::string kCertificateFileNamePrefix = "cert1_"; +static const std::string kLegacyCertificateFileName = "cert.bin"; +static const std::string kLegacyCertificateFileNamePrefix = "cert"; + // File class. The implementation is platform dependent. class CORE_UTIL_EXPORT File { public: diff --git a/libwvdrmengine/cdm/util/src/file_store.cpp b/libwvdrmengine/cdm/util/src/file_store.cpp index b3aa7bb8..76d24c4f 100644 --- a/libwvdrmengine/cdm/util/src/file_store.cpp +++ b/libwvdrmengine/cdm/util/src/file_store.cpp @@ -29,9 +29,6 @@ namespace wvcdm { namespace { -const char kCertificateFileNamePrefix[] = "cert"; -const char kCertificateFileNameExt[] = ".bin"; -const char kCertificateFileName[] = "cert.bin"; std::string GetFileNameSafeHash(const std::string& input) { std::vector hash(MD5_DIGEST_LENGTH); @@ -54,6 +51,10 @@ std::string GetFileNameForIdentifier(const std::string path, if (file_name == kCertificateFileName && !identifier.empty()) { const std::string hash = GetFileNameSafeHash(identifier); file_name = kCertificateFileNamePrefix + hash + kCertificateFileNameExt; + } else if (file_name == kLegacyCertificateFileName && !identifier.empty()) { + const std::string hash = GetFileNameSafeHash(identifier); + file_name = + kLegacyCertificateFileNamePrefix + hash + kCertificateFileNameExt; } if (dir_path.empty()) diff --git a/libwvdrmengine/cdm/util/test/file_store_unittest.cpp b/libwvdrmengine/cdm/util/test/file_store_unittest.cpp index bae0aea0..64b7d8df 100644 --- a/libwvdrmengine/cdm/util/test/file_store_unittest.cpp +++ b/libwvdrmengine/cdm/util/test/file_store_unittest.cpp @@ -18,7 +18,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 { @@ -175,4 +178,182 @@ TEST_F(FileTest, ListFiles) { EXPECT_EQ(0u, names.size()); } +TEST_F(FileTest, CreateGlobalCertificates) { + // Clear directory + std::vector names; + std::string path_dir = 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 = test_vectors::kTestDir + kCertificateFileName; + std::string legacy_certificate_path = + test_vectors::kTestDir + kLegacyCertificateFileName; + + std::unique_ptr 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 names; + std::string path_dir = 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 = test_vectors::kTestDir + kCertificateFileName; + std::string legacy_certificate_path = + test_vectors::kTestDir + kLegacyCertificateFileName; + + // Create Global certificates + std::unique_ptr 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 names; + std::string path_dir = 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 = test_vectors::kTestDir + kCertificateFileName; + std::string legacy_certificate_path = + test_vectors::kTestDir + kLegacyCertificateFileName; + + // Create Global certificates + std::unique_ptr 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(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(test_vectors::kTestDir + names[i])); + } else { + EXPECT_FALSE(file_system_.Exists(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(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(test_vectors::kTestDir + names[i])); + } +} + } // namespace wvcdm