2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-20 22:38:28 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <packager/media/base/widevine_key_source.h>
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2016-03-21 18:09:24 +00:00
|
|
|
#include <algorithm>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cinttypes>
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <iterator>
|
2016-03-21 18:09:24 +00:00
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
#include <absl/strings/str_format.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/key_fetcher.h>
|
|
|
|
#include <packager/media/base/protection_system_ids.h>
|
|
|
|
#include <packager/media/base/request_signer.h>
|
|
|
|
#include <packager/media/base/widevine_pssh_generator.h>
|
|
|
|
#include <packager/status/status_test_util.h>
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2014-10-03 05:17:54 +00:00
|
|
|
using ::testing::_;
|
2017-04-05 20:53:58 +00:00
|
|
|
using ::testing::Bool;
|
|
|
|
using ::testing::Combine;
|
2014-10-03 05:17:54 +00:00
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::InSequence;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::SetArgPointee;
|
|
|
|
using ::testing::StrEq;
|
2017-04-05 20:53:58 +00:00
|
|
|
using ::testing::Test;
|
|
|
|
using ::testing::Values;
|
|
|
|
using ::testing::WithParamInterface;
|
2014-10-03 05:17:54 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2017-04-05 20:53:58 +00:00
|
|
|
namespace media {
|
2014-02-20 22:38:28 +00:00
|
|
|
namespace {
|
2019-02-13 00:34:24 +00:00
|
|
|
const bool kClassic = true;
|
|
|
|
const bool kHasIv = true;
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kServerUrl[] = "http://www.foo.com/getcontentkey";
|
|
|
|
const char kContentId[] = "ContentFoo";
|
2014-05-08 22:58:07 +00:00
|
|
|
const char kPolicy[] = "PolicyFoo";
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kSignerName[] = "SignerFoo";
|
|
|
|
|
|
|
|
const char kMockSignature[] = "MockSignature";
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
// The license service may return an error indicating a transient error has
|
2014-02-20 22:38:28 +00:00
|
|
|
// just happened in the server, or other types of errors.
|
2014-08-20 23:51:15 +00:00
|
|
|
// WidevineKeySource will perform a number of retries on transient
|
2014-04-16 01:09:32 +00:00
|
|
|
// errors;
|
2014-08-20 23:51:15 +00:00
|
|
|
// WidevineKeySource does not know about other errors and retries are
|
2014-04-16 01:09:32 +00:00
|
|
|
// not performed.
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kLicenseStatusTransientError[] = "INTERNAL_ERROR";
|
|
|
|
const char kLicenseStatusUnknownError[] = "UNKNOWN_ERROR";
|
|
|
|
|
|
|
|
const char kExpectedRequestMessageFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"content_id":"%s","policy":"%s",)"
|
|
|
|
R"("tracks":[{"type":"SD"},{"type":"HD"},{"type":"UHD1"},)"
|
|
|
|
R"({"type":"UHD2"},{"type":"AUDIO"}],)"
|
|
|
|
R"("drm_types":["WIDEVINE"],"protection_scheme":"%s"})";
|
2018-05-11 00:19:28 +00:00
|
|
|
const char kExpectedRequestMessageFormatWithEntitlement[] =
|
|
|
|
R"({"content_id":"%s","policy":"%s",)"
|
|
|
|
R"("tracks":[{"type":"SD"},{"type":"HD"},{"type":"UHD1"},)"
|
|
|
|
R"({"type":"UHD2"},{"type":"AUDIO"}],)"
|
|
|
|
R"("drm_types":["WIDEVINE"],"protection_scheme":"%s",)"
|
|
|
|
R"("enable_entitlement_license":true})";
|
2016-01-05 23:17:32 +00:00
|
|
|
const char kExpectedRequestMessageWithAssetIdFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"tracks":[{"type":"SD"},{"type":"HD"},{"type":"UHD1"},)"
|
|
|
|
R"({"type":"UHD2"},{"type":"AUDIO"}],)"
|
|
|
|
R"("drm_types":["WIDEVINE"],"asset_id":%u})";
|
2016-01-05 23:17:32 +00:00
|
|
|
const char kExpectedRequestMessageWithPsshFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"tracks":[{"type":"SD"},{"type":"HD"},{"type":"UHD1"},)"
|
|
|
|
R"({"type":"UHD2"},{"type":"AUDIO"}],)"
|
|
|
|
R"("drm_types":["WIDEVINE"],"pssh_data":"%s"})";
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kExpectedSignedMessageFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"request":"%s","signature":"%s","signer":"%s"})";
|
|
|
|
const char kTrackFormat[] = R"({"type":"%s","key_id":"%s","key":"%s",)"
|
|
|
|
R"("pssh":[{"drm_type":"WIDEVINE","data":"%s"}]})";
|
2019-02-13 00:34:24 +00:00
|
|
|
const char kTrackFormatWithIv[] =
|
|
|
|
R"({"type":"%s","key_id":"%s","key":"%s","iv":"%s",)"
|
|
|
|
R"("pssh":[{"drm_type":"WIDEVINE","data":"%s"}]})";
|
2018-05-11 00:19:28 +00:00
|
|
|
const char kTrackFormatWithBoxes[] =
|
|
|
|
R"({"type":"%s","key_id":"%s","key":"%s",)"
|
|
|
|
R"("pssh":[{"drm_type":"WIDEVINE","data":"%s","boxes":"%s"}]})";
|
2018-05-04 21:31:33 +00:00
|
|
|
const char kClassicTrackFormat[] = R"({"type":"%s","key":"%s"})";
|
|
|
|
const char kLicenseResponseFormat[] = R"({"status":"%s","tracks":[%s]})";
|
|
|
|
const char kHttpResponseFormat[] = R"({"response":"%s"})";
|
2016-02-19 18:24:33 +00:00
|
|
|
const uint8_t kRequestPsshBox[] = {
|
2017-04-05 20:53:58 +00:00
|
|
|
0, 0, 0, 44, 'p', 's', 's', 'h', 0, 0, 0,
|
2016-02-19 18:24:33 +00:00
|
|
|
0, 0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6, 0x4a, 0xce, 0xa3, 0xc8,
|
2017-04-05 20:53:58 +00:00
|
|
|
0x27, 0xdc, 0xd5, 0x1d, 0x21, 0xed, 0, 0, 0, 12, 0x22,
|
|
|
|
0x0a, 'C', 'o', 'n', 't', 'e', 'n', 't', 'F', 'o', 'o'};
|
|
|
|
const char kRequestPsshData[] = {0x22, 0x0a, 'C', 'o', 'n', 't', 'e',
|
|
|
|
'n', 't', 'F', 'o', 'o', '\0'};
|
2016-02-19 18:24:33 +00:00
|
|
|
const uint8_t kRequestPsshDataFromKeyIds[] = {0x12, 0x06, 0x00, 0x01,
|
|
|
|
0x02, 0x03, 0x04, 0x05};
|
|
|
|
const uint8_t kRequestKeyId[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
|
2016-01-05 23:17:32 +00:00
|
|
|
// 32-bit with leading bit set, to verify that big uint32_t can be handled
|
|
|
|
// correctly.
|
|
|
|
const uint32_t kClassicAssetId = 0x80038cd9;
|
2017-04-04 19:43:41 +00:00
|
|
|
const uint8_t kClassicAssetIdBytes[] = {0x80, 0x03, 0x8c, 0xd9};
|
2014-02-20 22:38:28 +00:00
|
|
|
|
|
|
|
std::string Base64Encode(const std::string& input) {
|
|
|
|
std::string output;
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::Base64Escape(input, &output);
|
2014-02-20 22:38:28 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::string ToString(const std::vector<uint8_t> v) {
|
2014-02-20 22:38:28 +00:00
|
|
|
return std::string(v.begin(), v.end());
|
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
|
|
|
|
std::string GetMockKeyId(const std::string& track_type) {
|
2016-02-17 22:03:43 +00:00
|
|
|
// Key ID must be 16 characters.
|
|
|
|
std::string key_id = "MockKeyId" + track_type;
|
|
|
|
key_id.resize(16, '~');
|
|
|
|
return key_id;
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetMockKey(const std::string& track_type) {
|
2017-09-12 19:22:39 +00:00
|
|
|
// The key must be 16 characters, in case the key is needed to generate a
|
|
|
|
// PlayReady pssh.
|
|
|
|
std::string key = "MockKey" + track_type;
|
|
|
|
key.resize(16, '~');
|
|
|
|
return key;
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 00:34:24 +00:00
|
|
|
std::string GetMockIv(const std::string& track_type) {
|
|
|
|
// IV must be 16 characters.
|
|
|
|
std::string iv = "MockIv" + track_type;
|
|
|
|
iv.resize(16, '~');
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
std::string GetMockPsshData() {
|
|
|
|
return kRequestPsshData;
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
std::string GenerateMockLicenseResponseWithBoxes(const std::string& boxes) {
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
|
|
|
std::string tracks;
|
2019-02-13 00:34:24 +00:00
|
|
|
for (const std::string& track_type : kTrackTypes) {
|
2018-05-11 00:19:28 +00:00
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
2022-11-04 22:46:41 +00:00
|
|
|
tracks +=
|
|
|
|
absl::StrFormat(kTrackFormatWithBoxes, track_type.c_str(),
|
|
|
|
Base64Encode(GetMockKeyId(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockKey(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockPsshData()).c_str(), boxes.c_str());
|
2018-05-11 00:19:28 +00:00
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
return absl::StrFormat(kLicenseResponseFormat, "OK", tracks.c_str());
|
2018-05-11 00:19:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
std::string GenerateMockLicenseResponse() {
|
2016-11-11 23:17:17 +00:00
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
2014-04-15 22:18:26 +00:00
|
|
|
std::string tracks;
|
2019-02-13 00:34:24 +00:00
|
|
|
for (const std::string& track_type : kTrackTypes) {
|
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
2022-11-04 22:46:41 +00:00
|
|
|
tracks += absl::StrFormat(kTrackFormat, track_type.c_str(),
|
|
|
|
Base64Encode(GetMockKeyId(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockKey(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockPsshData()).c_str());
|
2019-02-13 00:34:24 +00:00
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
return absl::StrFormat(kLicenseResponseFormat, "OK", tracks.c_str());
|
2019-02-13 00:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GenerateMockLicenseResponseWithIv() {
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
|
|
|
std::string tracks;
|
|
|
|
for (const std::string& track_type : kTrackTypes) {
|
2014-04-15 22:18:26 +00:00
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
2022-11-04 22:46:41 +00:00
|
|
|
tracks += absl::StrFormat(kTrackFormatWithIv, track_type.c_str(),
|
|
|
|
Base64Encode(GetMockKeyId(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockKey(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockIv(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockPsshData()).c_str());
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
return absl::StrFormat(kLicenseResponseFormat, "OK", tracks.c_str());
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
std::string GenerateMockClassicLicenseResponse() {
|
2016-11-11 23:17:17 +00:00
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
2014-08-20 23:51:15 +00:00
|
|
|
std::string tracks;
|
2019-02-13 00:34:24 +00:00
|
|
|
for (const std::string& track_type : kTrackTypes) {
|
2014-08-20 23:51:15 +00:00
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
2022-11-04 22:46:41 +00:00
|
|
|
tracks += absl::StrFormat(kClassicTrackFormat, track_type.c_str(),
|
|
|
|
Base64Encode(GetMockKey(track_type)).c_str());
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
return absl::StrFormat(kLicenseResponseFormat, "OK", tracks.c_str());
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MockRequestSigner : public RequestSigner {
|
|
|
|
public:
|
|
|
|
explicit MockRequestSigner(const std::string& signer_name)
|
|
|
|
: RequestSigner(signer_name) {}
|
2015-07-22 23:40:45 +00:00
|
|
|
~MockRequestSigner() override {}
|
2014-02-20 22:38:28 +00:00
|
|
|
|
|
|
|
MOCK_METHOD2(GenerateSignature,
|
|
|
|
bool(const std::string& message, std::string* signature));
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MockRequestSigner);
|
|
|
|
};
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
class MockKeyFetcher : public KeyFetcher {
|
2014-02-20 22:38:28 +00:00
|
|
|
public:
|
2014-10-07 21:33:08 +00:00
|
|
|
MockKeyFetcher() : KeyFetcher() {}
|
2015-07-22 23:40:45 +00:00
|
|
|
~MockKeyFetcher() override {}
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
MOCK_METHOD3(FetchKeys,
|
|
|
|
Status(const std::string& service_address,
|
2014-02-20 22:38:28 +00:00
|
|
|
const std::string& data,
|
|
|
|
std::string* response));
|
|
|
|
|
|
|
|
private:
|
2014-10-07 21:33:08 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(MockKeyFetcher);
|
2014-02-20 22:38:28 +00:00
|
|
|
};
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
class WidevineKeySourceTest : public Test {
|
2014-02-20 22:38:28 +00:00
|
|
|
public:
|
2014-08-20 23:51:15 +00:00
|
|
|
WidevineKeySourceTest()
|
2014-02-20 22:38:28 +00:00
|
|
|
: mock_request_signer_(new MockRequestSigner(kSignerName)),
|
2014-10-07 21:33:08 +00:00
|
|
|
mock_key_fetcher_(new MockKeyFetcher()) {}
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2015-07-22 23:40:45 +00:00
|
|
|
void SetUp() override {
|
2014-09-30 21:52:21 +00:00
|
|
|
content_id_.assign(
|
|
|
|
reinterpret_cast<const uint8_t*>(kContentId),
|
|
|
|
reinterpret_cast<const uint8_t*>(kContentId) + strlen(kContentId));
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
protected:
|
2018-05-04 21:31:33 +00:00
|
|
|
std::string GetExpectedProtectionScheme() {
|
|
|
|
switch (protection_scheme_) {
|
|
|
|
case FOURCC_cenc:
|
|
|
|
return "CENC";
|
|
|
|
case FOURCC_cbcs:
|
|
|
|
case kAppleSampleAesProtectionScheme:
|
|
|
|
// Apple SAMPLE-AES is considered as a variation of cbcs.
|
|
|
|
return "CBCS";
|
|
|
|
case FOURCC_cbc1:
|
|
|
|
return "CBC1";
|
|
|
|
case FOURCC_cens:
|
|
|
|
return "CENS";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
2017-04-05 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
void CreateWidevineKeySource() {
|
2020-05-29 18:45:57 +00:00
|
|
|
ProtectionSystem protection_system = ProtectionSystem::kNone;
|
2018-08-10 20:31:17 +00:00
|
|
|
if (add_widevine_pssh_)
|
2020-05-29 18:45:57 +00:00
|
|
|
protection_system |= ProtectionSystem::kWidevine;
|
2017-09-12 19:22:39 +00:00
|
|
|
if (add_common_pssh_)
|
2020-05-29 18:45:57 +00:00
|
|
|
protection_system |= ProtectionSystem::kCommon;
|
2018-08-07 21:43:42 +00:00
|
|
|
widevine_key_source_.reset(new WidevineKeySource(
|
2020-05-29 18:45:57 +00:00
|
|
|
kServerUrl, protection_system, protection_scheme_));
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_key_fetcher(std::move(mock_key_fetcher_));
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 00:34:24 +00:00
|
|
|
void VerifyKeys(bool classic, bool has_iv) {
|
2014-06-18 19:23:34 +00:00
|
|
|
EncryptionKey encryption_key;
|
2017-06-13 21:54:12 +00:00
|
|
|
const std::string kStreamLabels[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
|
|
|
for (const std::string& stream_label : kStreamLabels) {
|
|
|
|
ASSERT_OK(widevine_key_source_->GetKey(stream_label, &encryption_key));
|
|
|
|
EXPECT_EQ(GetMockKey(stream_label), ToString(encryption_key.key));
|
2014-08-20 23:51:15 +00:00
|
|
|
if (!classic) {
|
2017-09-12 19:22:39 +00:00
|
|
|
size_t num_key_system_info =
|
2020-06-01 22:26:31 +00:00
|
|
|
add_widevine_pssh_ || !add_common_pssh_ ? 1 : 0;
|
2017-09-12 19:22:39 +00:00
|
|
|
ASSERT_EQ(num_key_system_info, encryption_key.key_system_info.size());
|
2017-06-13 21:54:12 +00:00
|
|
|
EXPECT_EQ(GetMockKeyId(stream_label), ToString(encryption_key.key_id));
|
2019-02-13 00:34:24 +00:00
|
|
|
if (has_iv)
|
|
|
|
EXPECT_EQ(GetMockIv(stream_label), ToString(encryption_key.iv));
|
|
|
|
else
|
|
|
|
EXPECT_TRUE(encryption_key.iv.empty());
|
2018-05-03 00:54:10 +00:00
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
auto key_system_info_iter = encryption_key.key_system_info.begin();
|
2016-03-21 18:09:24 +00:00
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
// Default to Widevine if neither are set.
|
|
|
|
if (add_widevine_pssh_ || !add_common_pssh_) {
|
|
|
|
const std::vector<uint8_t> widevine_system_id(
|
|
|
|
std::begin(kWidevineSystemId), std::end(kWidevineSystemId));
|
|
|
|
ASSERT_EQ(widevine_system_id, key_system_info_iter->system_id);
|
|
|
|
|
|
|
|
const std::vector<uint8_t>& pssh = key_system_info_iter->psshs;
|
|
|
|
std::unique_ptr<PsshBoxBuilder> pssh_builder =
|
|
|
|
PsshBoxBuilder::ParseFromBox(pssh.data(), pssh.size());
|
|
|
|
ASSERT_TRUE(pssh_builder);
|
|
|
|
EXPECT_EQ(GetMockPsshData(), ToString(pssh_builder->pssh_data()));
|
|
|
|
|
|
|
|
++key_system_info_iter;
|
2016-03-21 18:09:24 +00:00
|
|
|
}
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
2014-06-18 19:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MockRequestSigner> mock_request_signer_;
|
|
|
|
std::unique_ptr<MockKeyFetcher> mock_key_fetcher_;
|
|
|
|
std::unique_ptr<WidevineKeySource> widevine_key_source_;
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> content_id_;
|
2018-08-10 20:31:17 +00:00
|
|
|
bool add_widevine_pssh_ = false;
|
2017-04-05 20:53:58 +00:00
|
|
|
bool add_common_pssh_ = false;
|
|
|
|
FourCC protection_scheme_ = FOURCC_cenc;
|
2014-02-20 22:38:28 +00:00
|
|
|
|
|
|
|
private:
|
2014-08-20 23:51:15 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WidevineKeySourceTest);
|
2014-02-20 22:38:28 +00:00
|
|
|
};
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, GenerateSignatureFailure) {
|
2014-02-20 22:38:28 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(_, _))
|
|
|
|
.WillOnce(Return(false));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2014-05-08 23:34:45 +00:00
|
|
|
ASSERT_EQ(Status(error::INTERNAL_ERROR, "Signature generation failed."),
|
2014-08-20 23:51:15 +00:00
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, RetryOnHttpTimeout) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
|
|
|
|
// Retry is expected on HTTP timeout.
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(Return(Status(error::TIME_OUT, "")))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(!kClassic, !kHasIv);
|
2017-04-05 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WidevineKeySourceTest, RetryOnTransientError) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_license_status =
|
|
|
|
absl::StrFormat(kLicenseResponseFormat, kLicenseStatusTransientError, "");
|
|
|
|
std::string mock_response = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(mock_license_status).c_str());
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string expected_retried_response = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
|
|
|
|
// Retry is expected on transient error.
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(expected_retried_response),
|
|
|
|
Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(!kClassic, !kHasIv);
|
2017-04-05 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WidevineKeySourceTest, NoRetryOnUnknownError) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_license_status =
|
|
|
|
absl::StrFormat(kLicenseResponseFormat, kLicenseStatusUnknownError, "");
|
|
|
|
std::string mock_response = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(mock_license_status).c_str());
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
ASSERT_EQ(error::SERVER_ERROR,
|
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy).error_code());
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:34:24 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, CheckIv) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2019-02-13 00:34:24 +00:00
|
|
|
kHttpResponseFormat,
|
|
|
|
Base64Encode(GenerateMockLicenseResponseWithIv()).c_str());
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
|
|
|
VerifyKeys(!kClassic, kHasIv);
|
|
|
|
}
|
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, BoxesInResponse) {
|
|
|
|
const char kMockBoxes[] = "mock_pssh_boxes";
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2018-05-11 00:19:28 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponseWithBoxes(
|
|
|
|
Base64Encode(kMockBoxes)))
|
|
|
|
.c_str());
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
|
|
|
|
|
|
|
const char kHdStreamLabel[] = "HD";
|
|
|
|
EncryptionKey encryption_key;
|
|
|
|
ASSERT_OK(widevine_key_source_->GetKey(kHdStreamLabel, &encryption_key));
|
|
|
|
ASSERT_EQ(1u, encryption_key.key_system_info.size());
|
|
|
|
ASSERT_EQ(kMockBoxes, ToString(encryption_key.key_system_info.front().psshs));
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
class WidevineKeySourceParameterizedTest
|
|
|
|
: public WidevineKeySourceTest,
|
2022-11-04 22:46:41 +00:00
|
|
|
public WithParamInterface<std::tuple<bool, bool, FourCC>> {
|
2017-04-05 20:53:58 +00:00
|
|
|
public:
|
|
|
|
WidevineKeySourceParameterizedTest() {
|
2022-11-04 22:46:41 +00:00
|
|
|
add_widevine_pssh_ = std::get<0>(GetParam());
|
|
|
|
add_common_pssh_ = std::get<1>(GetParam());
|
|
|
|
protection_scheme_ = std::get<2>(GetParam());
|
2017-04-05 20:53:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
// Check whether expected request message and post data was generated and
|
|
|
|
// verify the correct behavior on http failure.
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, HttpFetchFailure) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string expected_message = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kExpectedRequestMessageFormat, Base64Encode(kContentId).c_str(), kPolicy,
|
2018-05-04 21:31:33 +00:00
|
|
|
GetExpectedProtectionScheme().c_str());
|
2016-01-05 23:17:32 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
2014-02-20 22:38:28 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string expected_post_data = absl::StrFormat(
|
|
|
|
kExpectedSignedMessageFormat, Base64Encode(expected_message).c_str(),
|
|
|
|
Base64Encode(kMockSignature).c_str(), kSignerName);
|
2014-02-20 22:38:28 +00:00
|
|
|
const Status kMockStatus = Status::UNKNOWN;
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_,
|
2014-10-09 23:33:56 +00:00
|
|
|
FetchKeys(StrEq(kServerUrl), expected_post_data, _))
|
2014-02-20 22:38:28 +00:00
|
|
|
.WillOnce(Return(kMockStatus));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2014-05-08 23:34:45 +00:00
|
|
|
ASSERT_EQ(kMockStatus,
|
2014-08-20 23:51:15 +00:00
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, LicenseStatusCencOK) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2014-08-20 23:51:15 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-08-20 23:51:15 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(!kClassic, !kHasIv);
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, LicenseStatusCencMalformedResponse) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2018-08-10 20:31:17 +00:00
|
|
|
kHttpResponseFormat, Base64Encode("malformed response").c_str());
|
2014-08-20 23:51:15 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-08-20 23:51:15 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_EQ(error::SERVER_ERROR,
|
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy)
|
|
|
|
.error_code());
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, LicenseStatusCencWithPsshBoxOK) {
|
2016-01-05 23:17:32 +00:00
|
|
|
std::string expected_message =
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::StrFormat(kExpectedRequestMessageWithPsshFormat,
|
|
|
|
Base64Encode(kRequestPsshData).c_str());
|
2016-01-05 23:17:32 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2014-04-15 22:18:26 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-04-24 16:59:07 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2018-08-10 20:31:17 +00:00
|
|
|
std::vector<uint8_t> pssh_box(std::begin(kRequestPsshBox),
|
|
|
|
std::end(kRequestPsshBox));
|
2017-04-04 19:43:41 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(EmeInitDataType::CENC, pssh_box));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(!kClassic, !kHasIv);
|
2016-02-19 18:24:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, LicenseStatusCencWithKeyIdsOK) {
|
2018-08-10 20:31:17 +00:00
|
|
|
std::string expected_pssh_data(std::begin(kRequestPsshDataFromKeyIds),
|
|
|
|
std::end(kRequestPsshDataFromKeyIds));
|
2016-02-19 18:24:33 +00:00
|
|
|
std::string expected_message =
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::StrFormat(kExpectedRequestMessageWithPsshFormat,
|
|
|
|
Base64Encode(expected_pssh_data).c_str());
|
2016-02-19 18:24:33 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2016-02-19 18:24:33 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2018-08-10 20:31:17 +00:00
|
|
|
std::vector<uint8_t> key_id(std::begin(kRequestKeyId),
|
|
|
|
std::end(kRequestKeyId));
|
2017-04-04 19:43:41 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(EmeInitDataType::WEBM, key_id));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(!kClassic, !kHasIv);
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, LicenseStatusClassicOK) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string expected_message = absl::StrFormat(
|
2016-01-05 23:17:32 +00:00
|
|
|
kExpectedRequestMessageWithAssetIdFormat, kClassicAssetId);
|
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
|
|
|
kHttpResponseFormat,
|
|
|
|
Base64Encode(GenerateMockClassicLicenseResponse()).c_str());
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-08-20 23:51:15 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2017-04-04 19:43:41 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(
|
|
|
|
EmeInitDataType::WIDEVINE_CLASSIC,
|
|
|
|
std::vector<uint8_t>(std::begin(kClassicAssetIdBytes),
|
|
|
|
std::end(kClassicAssetIdBytes))));
|
2019-02-13 00:34:24 +00:00
|
|
|
VerifyKeys(kClassic, !kHasIv);
|
2014-06-18 19:23:34 +00:00
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, VerifyEntitlementLicenseRequest) {
|
|
|
|
const std::string expected_message =
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::StrFormat(kExpectedRequestMessageFormatWithEntitlement,
|
|
|
|
Base64Encode(kContentId).c_str(), kPolicy,
|
|
|
|
GetExpectedProtectionScheme().c_str());
|
2018-05-11 00:19:28 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(Return(false));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
widevine_key_source_->set_enable_entitlement_license(true);
|
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
|
|
|
ASSERT_NOT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
|
|
|
}
|
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kCryptoPeriodRequestMessageFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"content_id":"%s","policy":"%s",)"
|
|
|
|
R"("tracks":[{"type":"SD"},{"type":"HD"},{"type":"UHD1"},)"
|
|
|
|
R"({"type":"UHD2"},{"type":"AUDIO"}],)"
|
|
|
|
R"("drm_types":["WIDEVINE"],)"
|
|
|
|
R"("first_crypto_period_index":%u,"crypto_period_count":%u,)"
|
2019-01-24 18:39:54 +00:00
|
|
|
R"("crypto_period_seconds":%u,)"
|
2018-05-04 21:31:33 +00:00
|
|
|
R"("protection_scheme":"%s"})";
|
2014-04-24 16:59:07 +00:00
|
|
|
|
|
|
|
const char kCryptoPeriodTrackFormat[] =
|
2018-05-04 21:31:33 +00:00
|
|
|
R"({"type":"%s","key_id":"%s","key":"%s",)"
|
|
|
|
R"("pssh":[{"drm_type":"WIDEVINE","data":""}], )"
|
|
|
|
R"("crypto_period_index":%u})";
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::string GetMockKey(const std::string& track_type, uint32_t index) {
|
2017-09-12 19:22:39 +00:00
|
|
|
// The key must be 16 characters, in case the key is needed to generate a
|
|
|
|
// PlayReady pssh.
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string key =
|
|
|
|
"MockKey" + track_type + "@" + absl::StrFormat("%" PRIu32, index);
|
2017-09-12 19:22:39 +00:00
|
|
|
key.resize(16, '~');
|
|
|
|
return key;
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 15:40:02 +00:00
|
|
|
std::string GenerateMockKeyRotationLicenseResponse(
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t initial_crypto_period_index,
|
|
|
|
uint32_t crypto_period_count) {
|
2016-11-11 23:17:17 +00:00
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
2014-04-24 16:59:07 +00:00
|
|
|
std::string tracks;
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t index = initial_crypto_period_index;
|
|
|
|
index < initial_crypto_period_index + crypto_period_count;
|
2014-04-24 16:59:07 +00:00
|
|
|
++index) {
|
2019-02-13 00:34:24 +00:00
|
|
|
for (const std::string& track_type : kTrackTypes) {
|
2014-04-24 16:59:07 +00:00
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
2022-11-04 22:46:41 +00:00
|
|
|
tracks += absl::StrFormat(
|
2019-02-13 00:34:24 +00:00
|
|
|
kCryptoPeriodTrackFormat, track_type.c_str(),
|
|
|
|
Base64Encode(GetMockKeyId(track_type)).c_str(),
|
|
|
|
Base64Encode(GetMockKey(track_type, index)).c_str(), index);
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
return absl::StrFormat(kLicenseResponseFormat, "OK", tracks.c_str());
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
TEST_P(WidevineKeySourceParameterizedTest, KeyRotationTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kFirstCryptoPeriodIndex = 8;
|
|
|
|
const uint32_t kCryptoPeriodCount = 10;
|
2019-01-24 18:39:54 +00:00
|
|
|
const uint32_t kCryptoPeriodSeconds = 100;
|
2014-04-24 16:59:07 +00:00
|
|
|
// Array of indexes to be checked.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kCryptoPeriodIndexes[] = {
|
2018-07-27 22:50:41 +00:00
|
|
|
kFirstCryptoPeriodIndex, 17, 37, 38, 36, 89};
|
|
|
|
// Derived from kCryptoPeriodIndexes: ceiling((89 - 8 ) / 10).
|
|
|
|
const uint32_t kCryptoIterations = 9;
|
2014-04-24 16:59:07 +00:00
|
|
|
|
|
|
|
// Generate expectations in sequence.
|
|
|
|
InSequence dummy;
|
2014-06-30 15:40:02 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
// Expecting a non-key rotation enabled request on FetchKeys().
|
2022-11-04 22:46:41 +00:00
|
|
|
{
|
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(_, _))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
std::string mock_response =
|
|
|
|
absl::StrFormat(kHttpResponseFormat,
|
|
|
|
Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
}
|
2014-06-30 15:40:02 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < kCryptoIterations; ++i) {
|
|
|
|
uint32_t first_crypto_period_index =
|
2014-06-30 15:40:02 +00:00
|
|
|
kFirstCryptoPeriodIndex - 1 + i * kCryptoPeriodCount;
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string expected_message = absl::StrFormat(
|
2017-04-05 20:53:58 +00:00
|
|
|
kCryptoPeriodRequestMessageFormat, Base64Encode(kContentId).c_str(),
|
2018-05-04 21:31:33 +00:00
|
|
|
kPolicy, first_crypto_period_index, kCryptoPeriodCount,
|
2019-01-24 18:39:54 +00:00
|
|
|
kCryptoPeriodSeconds, GetExpectedProtectionScheme().c_str());
|
2014-04-24 16:59:07 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(expected_message, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string mock_response = absl::StrFormat(
|
2014-04-24 16:59:07 +00:00
|
|
|
kHttpResponseFormat,
|
2014-06-30 15:40:02 +00:00
|
|
|
Base64Encode(GenerateMockKeyRotationLicenseResponse(
|
|
|
|
first_crypto_period_index, kCryptoPeriodCount))
|
|
|
|
.c_str());
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-04-24 16:59:07 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
}
|
2018-07-27 22:50:41 +00:00
|
|
|
// Fail future requests.
|
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(_, _))
|
|
|
|
.WillRepeatedly(Return(false));
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2016-08-17 17:41:40 +00:00
|
|
|
widevine_key_source_->set_signer(std::move(mock_request_signer_));
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(content_id_, kPolicy));
|
2014-04-24 16:59:07 +00:00
|
|
|
|
|
|
|
EncryptionKey encryption_key;
|
2017-06-13 21:54:12 +00:00
|
|
|
const std::string kStreamLabels[] = {"SD", "HD", "UHD1", "UHD2", "AUDIO"};
|
2022-11-04 22:46:41 +00:00
|
|
|
for (size_t i = 0; i < std::size(kCryptoPeriodIndexes); ++i) {
|
2017-06-13 21:54:12 +00:00
|
|
|
for (const std::string& stream_label : kStreamLabels) {
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->GetCryptoPeriodKey(
|
2019-01-24 18:39:54 +00:00
|
|
|
kCryptoPeriodIndexes[i], kCryptoPeriodSeconds, stream_label,
|
|
|
|
&encryption_key));
|
2017-06-13 21:54:12 +00:00
|
|
|
EXPECT_EQ(GetMockKey(stream_label, kCryptoPeriodIndexes[i]),
|
2014-04-24 16:59:07 +00:00
|
|
|
ToString(encryption_key.key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The old crypto period indexes should have been garbage collected.
|
2014-08-20 23:51:15 +00:00
|
|
|
Status status = widevine_key_source_->GetCryptoPeriodKey(
|
2019-01-24 18:39:54 +00:00
|
|
|
kFirstCryptoPeriodIndex, kCryptoPeriodSeconds, kStreamLabels[0],
|
|
|
|
&encryption_key);
|
2014-04-24 16:59:07 +00:00
|
|
|
EXPECT_EQ(error::INVALID_ARGUMENT, status.error_code());
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:09:24 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(WidevineKeySourceInstance,
|
2017-04-05 20:53:58 +00:00
|
|
|
WidevineKeySourceParameterizedTest,
|
|
|
|
Combine(Bool(),
|
2017-09-12 19:22:39 +00:00
|
|
|
Bool(),
|
2017-04-05 20:53:58 +00:00
|
|
|
Values(FOURCC_cenc,
|
|
|
|
FOURCC_cbcs,
|
|
|
|
FOURCC_cens,
|
|
|
|
FOURCC_cbc1,
|
|
|
|
kAppleSampleAesProtectionScheme)));
|
2016-03-21 18:09:24 +00:00
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|