2014-02-20 22:38:28 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/base64.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
2014-10-07 21:33:08 +00:00
|
|
|
#include "packager/media/base/key_fetcher.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/request_signer.h"
|
2014-10-09 23:33:56 +00:00
|
|
|
#include "packager/media/base/test/status_test_util.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/widevine_key_source.h"
|
2014-02-20 22:38:28 +00:00
|
|
|
|
2014-10-03 05:17:54 +00:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::InSequence;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::SetArgPointee;
|
|
|
|
using ::testing::StrEq;
|
|
|
|
|
|
|
|
namespace edash_packager {
|
2014-02-20 22:38:28 +00:00
|
|
|
namespace {
|
|
|
|
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[] =
|
2014-05-08 22:58:07 +00:00
|
|
|
"{\"content_id\":\"%s\",\"drm_types\":[\"WIDEVINE\"],\"policy\":\"%s\","
|
2014-02-20 22:38:28 +00:00
|
|
|
"\"tracks\":[{\"type\":\"SD\"},{\"type\":\"HD\"},{\"type\":\"AUDIO\"}]}";
|
2016-01-05 23:17:32 +00:00
|
|
|
const char kExpectedRequestMessageWithAssetIdFormat[] =
|
|
|
|
"{\"asset_id\":%u,\"drm_types\":[\"WIDEVINE\"],"
|
|
|
|
"\"tracks\":[{\"type\":\"SD\"},{\"type\":\"HD\"},{\"type\":\"AUDIO\"}]}";
|
|
|
|
const char kExpectedRequestMessageWithPsshFormat[] =
|
|
|
|
"{\"drm_types\":[\"WIDEVINE\"],\"pssh_data\":\"%s\","
|
|
|
|
"\"tracks\":[{\"type\":\"SD\"},{\"type\":\"HD\"},{\"type\":\"AUDIO\"}]}";
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kExpectedSignedMessageFormat[] =
|
|
|
|
"{\"request\":\"%s\",\"signature\":\"%s\",\"signer\":\"%s\"}";
|
2014-04-15 22:18:26 +00:00
|
|
|
const char kTrackFormat[] =
|
|
|
|
"{\"type\":\"%s\",\"key_id\":\"%s\",\"key\":"
|
|
|
|
"\"%s\",\"pssh\":[{\"drm_type\":\"WIDEVINE\",\"data\":\"%s\"}]}";
|
2014-08-20 23:51:15 +00:00
|
|
|
const char kClassicTrackFormat[] = "{\"type\":\"%s\",\"key\":\"%s\"}";
|
2014-04-16 01:09:32 +00:00
|
|
|
const char kLicenseResponseFormat[] = "{\"status\":\"%s\",\"tracks\":[%s]}";
|
2014-02-20 22:38:28 +00:00
|
|
|
const char kHttpResponseFormat[] = "{\"response\":\"%s\"}";
|
2016-02-19 18:24:33 +00:00
|
|
|
const uint8_t kRequestPsshBox[] = {
|
|
|
|
0, 0, 0, 41, 'p', 's', 's', 'h', 0, 0, 0,
|
|
|
|
0, 0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6, 0x4a, 0xce, 0xa3, 0xc8,
|
|
|
|
0x27, 0xdc, 0xd5, 0x1d, 0x21, 0xed, 0, 0, 0, 0x09, 'P',
|
|
|
|
'S', 'S', 'H', ' ', 'd', 'a', 't', 'a'};
|
2014-08-20 23:51:15 +00:00
|
|
|
const char kRequestPsshData[] = "PSSH data";
|
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;
|
2014-02-20 22:38:28 +00:00
|
|
|
|
|
|
|
std::string Base64Encode(const std::string& input) {
|
|
|
|
std::string output;
|
|
|
|
base::Base64Encode(input, &output);
|
|
|
|
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) {
|
|
|
|
return "MockKey" + track_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetMockPsshData(const std::string& track_type) {
|
|
|
|
return "MockPsshData" + track_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GenerateMockLicenseResponse() {
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
|
|
|
|
std::string tracks;
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
|
|
|
tracks += base::StringPrintf(
|
|
|
|
kTrackFormat,
|
|
|
|
kTrackTypes[i].c_str(),
|
|
|
|
Base64Encode(GetMockKeyId(kTrackTypes[i])).c_str(),
|
|
|
|
Base64Encode(GetMockKey(kTrackTypes[i])).c_str(),
|
|
|
|
Base64Encode(GetMockPsshData(kTrackTypes[i])).c_str());
|
|
|
|
}
|
|
|
|
return base::StringPrintf(kLicenseResponseFormat, "OK", tracks.c_str());
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
std::string GenerateMockClassicLicenseResponse() {
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
|
|
|
|
std::string tracks;
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
|
|
|
tracks += base::StringPrintf(
|
|
|
|
kClassicTrackFormat,
|
|
|
|
kTrackTypes[i].c_str(),
|
|
|
|
Base64Encode(GetMockKey(kTrackTypes[i])).c_str());
|
|
|
|
}
|
|
|
|
return base::StringPrintf(kLicenseResponseFormat, "OK", tracks.c_str());
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
class WidevineKeySourceTest : public ::testing::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:
|
2014-10-15 00:47:25 +00:00
|
|
|
void CreateWidevineKeySource() {
|
|
|
|
widevine_key_source_.reset(new WidevineKeySource(kServerUrl));
|
2015-07-22 23:40:45 +00:00
|
|
|
widevine_key_source_->set_key_fetcher(mock_key_fetcher_.Pass());
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
void VerifyKeys(bool classic) {
|
2014-06-18 19:23:34 +00:00
|
|
|
EncryptionKey encryption_key;
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
|
|
|
|
for (size_t i = 0; i < arraysize(kTrackTypes); ++i) {
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->GetKey(
|
|
|
|
KeySource::GetTrackTypeFromString(kTrackTypes[i]),
|
2014-06-18 19:23:34 +00:00
|
|
|
&encryption_key));
|
|
|
|
EXPECT_EQ(GetMockKey(kTrackTypes[i]), ToString(encryption_key.key));
|
2014-08-20 23:51:15 +00:00
|
|
|
if (!classic) {
|
2016-02-17 22:03:43 +00:00
|
|
|
ASSERT_EQ(1u, encryption_key.key_system_info.size());
|
2014-08-20 23:51:15 +00:00
|
|
|
EXPECT_EQ(GetMockKeyId(kTrackTypes[i]),
|
|
|
|
ToString(encryption_key.key_id));
|
|
|
|
EXPECT_EQ(GetMockPsshData(kTrackTypes[i]),
|
2016-02-17 22:03:43 +00:00
|
|
|
ToString(encryption_key.key_system_info[0].pssh_data()));
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
2014-06-18 19:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-20 22:38:28 +00:00
|
|
|
scoped_ptr<MockRequestSigner> mock_request_signer_;
|
2014-10-07 21:33:08 +00:00
|
|
|
scoped_ptr<MockKeyFetcher> mock_key_fetcher_;
|
2014-08-20 23:51:15 +00:00
|
|
|
scoped_ptr<WidevineKeySource> widevine_key_source_;
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> content_id_;
|
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
|
|
|
};
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, GetTrackTypeFromString) {
|
|
|
|
EXPECT_EQ(KeySource::TRACK_TYPE_SD,
|
|
|
|
KeySource::GetTrackTypeFromString("SD"));
|
|
|
|
EXPECT_EQ(KeySource::TRACK_TYPE_HD,
|
|
|
|
KeySource::GetTrackTypeFromString("HD"));
|
|
|
|
EXPECT_EQ(KeySource::TRACK_TYPE_AUDIO,
|
|
|
|
KeySource::GetTrackTypeFromString("AUDIO"));
|
|
|
|
EXPECT_EQ(KeySource::TRACK_TYPE_UNKNOWN,
|
|
|
|
KeySource::GetTrackTypeFromString("FOO"));
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +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();
|
2015-07-22 23:40:45 +00:00
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether expected request message and post data was generated and
|
|
|
|
// verify the correct behavior on http failure.
|
2014-10-07 21:33:08 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, HttpFetchFailure) {
|
2014-02-20 22:38:28 +00:00
|
|
|
std::string expected_message = base::StringPrintf(
|
2014-05-08 22:58:07 +00:00
|
|
|
kExpectedRequestMessageFormat, Base64Encode(kContentId).c_str(), kPolicy);
|
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)));
|
|
|
|
|
|
|
|
std::string expected_post_data =
|
|
|
|
base::StringPrintf(kExpectedSignedMessageFormat,
|
|
|
|
Base64Encode(expected_message).c_str(),
|
|
|
|
Base64Encode(kMockSignature).c_str(),
|
|
|
|
kSignerName);
|
|
|
|
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();
|
2015-07-22 23:40:45 +00:00
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WidevineKeySourceTest, LicenseStatusCencOK) {
|
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
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));
|
|
|
|
VerifyKeys(false);
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, LicenseStatusCencNotOK) {
|
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
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();
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_EQ(error::SERVER_ERROR,
|
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy)
|
|
|
|
.error_code());
|
|
|
|
}
|
|
|
|
|
2016-02-19 18:24:33 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, LicenseStatusCencWithPsshBoxOK) {
|
2016-01-05 23:17:32 +00:00
|
|
|
std::string expected_message =
|
|
|
|
base::StringPrintf(kExpectedRequestMessageWithPsshFormat,
|
|
|
|
Base64Encode(kRequestPsshData).c_str());
|
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
std::string mock_response = base::StringPrintf(
|
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-01-05 23:17:32 +00:00
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
2016-02-19 18:24:33 +00:00
|
|
|
std::vector<uint8_t> pssh_box(kRequestPsshBox,
|
|
|
|
kRequestPsshBox + arraysize(kRequestPsshBox));
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(pssh_box));
|
|
|
|
VerifyKeys(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WidevineKeySourceTest, LicenseStatusCencWithKeyIdsOK) {
|
|
|
|
std::string expected_pssh_data(
|
|
|
|
kRequestPsshDataFromKeyIds,
|
|
|
|
kRequestPsshDataFromKeyIds + arraysize(kRequestPsshDataFromKeyIds));
|
|
|
|
std::string expected_message =
|
|
|
|
base::StringPrintf(kExpectedRequestMessageWithPsshFormat,
|
|
|
|
Base64Encode(expected_pssh_data).c_str());
|
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
|
|
|
CreateWidevineKeySource();
|
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
|
|
|
std::vector<std::vector<uint8_t>> key_ids;
|
|
|
|
key_ids.push_back(std::vector<uint8_t>(
|
|
|
|
kRequestKeyId, kRequestKeyId + arraysize(kRequestKeyId)));
|
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(key_ids));
|
2014-08-20 23:51:15 +00:00
|
|
|
VerifyKeys(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WidevineKeySourceTest, LicenseStatusClassicOK) {
|
2016-01-05 23:17:32 +00:00
|
|
|
std::string expected_message = base::StringPrintf(
|
|
|
|
kExpectedRequestMessageWithAssetIdFormat, kClassicAssetId);
|
|
|
|
EXPECT_CALL(*mock_request_signer_,
|
|
|
|
GenerateSignature(StrEq(expected_message), _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
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-01-05 23:17:32 +00:00
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->FetchKeys(kClassicAssetId));
|
|
|
|
VerifyKeys(true);
|
2014-06-18 19:23:34 +00:00
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, RetryOnHttpTimeout) {
|
2014-06-18 19:23:34 +00:00
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
|
|
|
|
|
|
|
// Retry is expected on HTTP timeout.
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-06-18 19:23:34 +00:00
|
|
|
.WillOnce(Return(Status(error::TIME_OUT, "")))
|
|
|
|
.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));
|
|
|
|
VerifyKeys(false);
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, RetryOnTransientError) {
|
2014-02-20 22:38:28 +00:00
|
|
|
std::string mock_license_status = base::StringPrintf(
|
2014-04-15 22:18:26 +00:00
|
|
|
kLicenseResponseFormat, kLicenseStatusTransientError, "");
|
2014-04-24 16:59:07 +00:00
|
|
|
std::string mock_response = base::StringPrintf(
|
2014-02-20 22:38:28 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(mock_license_status).c_str());
|
|
|
|
|
|
|
|
std::string expected_retried_response = base::StringPrintf(
|
2014-04-15 22:18:26 +00:00
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
2014-02-20 22:38:28 +00:00
|
|
|
|
|
|
|
// Retry is expected on transient error.
|
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
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(expected_retried_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));
|
|
|
|
VerifyKeys(false);
|
2014-02-20 22:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, NoRetryOnUnknownError) {
|
2014-02-20 22:38:28 +00:00
|
|
|
std::string mock_license_status = base::StringPrintf(
|
2014-04-15 22:18:26 +00:00
|
|
|
kLicenseResponseFormat, kLicenseStatusUnknownError, "");
|
2014-02-20 22:38:28 +00:00
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
kHttpResponseFormat, Base64Encode(mock_license_status).c_str());
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-02-20 22:38:28 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2014-05-08 23:34:45 +00:00
|
|
|
ASSERT_EQ(error::SERVER_ERROR,
|
2014-08-20 23:51:15 +00:00
|
|
|
widevine_key_source_->FetchKeys(content_id_, kPolicy).error_code());
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kCryptoPeriodRequestMessageFormat[] =
|
|
|
|
"{\"content_id\":\"%s\",\"crypto_period_count\":%u,\"drm_types\":["
|
2014-05-08 22:58:07 +00:00
|
|
|
"\"WIDEVINE\"],\"first_crypto_period_index\":%u,\"policy\":\"%s\","
|
2014-04-24 16:59:07 +00:00
|
|
|
"\"tracks\":[{\"type\":\"SD\"},{\"type\":\"HD\"},{\"type\":\"AUDIO\"}]}";
|
|
|
|
|
|
|
|
const char kCryptoPeriodTrackFormat[] =
|
2016-02-17 22:03:43 +00:00
|
|
|
"{\"type\":\"%s\",\"key_id\":\"%s\",\"key\":"
|
2014-04-24 16:59:07 +00:00
|
|
|
"\"%s\",\"pssh\":[{\"drm_type\":\"WIDEVINE\",\"data\":\"\"}], "
|
|
|
|
"\"crypto_period_index\":%u}";
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::string GetMockKey(const std::string& track_type, uint32_t index) {
|
2014-04-24 16:59:07 +00:00
|
|
|
return "MockKey" + track_type + "@" + base::UintToString(index);
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-04-24 16:59:07 +00:00
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
|
|
|
|
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) {
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
if (!tracks.empty())
|
|
|
|
tracks += ",";
|
|
|
|
tracks += base::StringPrintf(
|
|
|
|
kCryptoPeriodTrackFormat,
|
|
|
|
kTrackTypes[i].c_str(),
|
2016-02-17 22:03:43 +00:00
|
|
|
Base64Encode(GetMockKeyId(kTrackTypes[i])).c_str(),
|
2014-04-24 16:59:07 +00:00
|
|
|
Base64Encode(GetMockKey(kTrackTypes[i], index)).c_str(),
|
|
|
|
index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base::StringPrintf(kLicenseResponseFormat, "OK", tracks.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
TEST_F(WidevineKeySourceTest, KeyRotationTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kFirstCryptoPeriodIndex = 8;
|
|
|
|
const uint32_t kCryptoPeriodCount = 10;
|
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[] = {
|
|
|
|
kFirstCryptoPeriodIndex, 17, 37, 38, 36, 39};
|
2014-04-24 16:59:07 +00:00
|
|
|
// Derived from kCryptoPeriodIndexes: ceiling((39 - 8 ) / 10).
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t kCryptoIterations = 4;
|
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().
|
2014-06-30 15:40:02 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(_, _))
|
|
|
|
.WillOnce(Return(true));
|
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
kHttpResponseFormat, Base64Encode(GenerateMockLicenseResponse()).c_str());
|
2014-10-07 21:33:08 +00:00
|
|
|
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
|
2014-06-30 15:40:02 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
|
|
|
|
|
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;
|
2014-04-24 16:59:07 +00:00
|
|
|
std::string expected_message =
|
|
|
|
base::StringPrintf(kCryptoPeriodRequestMessageFormat,
|
|
|
|
Base64Encode(kContentId).c_str(),
|
|
|
|
kCryptoPeriodCount,
|
2014-05-08 22:58:07 +00:00
|
|
|
first_crypto_period_index,
|
|
|
|
kPolicy);
|
2014-04-24 16:59:07 +00:00
|
|
|
EXPECT_CALL(*mock_request_signer_, GenerateSignature(expected_message, _))
|
|
|
|
.WillOnce(DoAll(SetArgPointee<1>(kMockSignature), Return(true)));
|
|
|
|
|
|
|
|
std::string mock_response = base::StringPrintf(
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
CreateWidevineKeySource();
|
2015-07-22 23:40:45 +00:00
|
|
|
widevine_key_source_->set_signer(mock_request_signer_.Pass());
|
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;
|
|
|
|
for (size_t i = 0; i < arraysize(kCryptoPeriodIndexes); ++i) {
|
|
|
|
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
|
|
|
|
for (size_t j = 0; j < 3; ++j) {
|
2014-08-20 23:51:15 +00:00
|
|
|
ASSERT_OK(widevine_key_source_->GetCryptoPeriodKey(
|
2014-04-24 16:59:07 +00:00
|
|
|
kCryptoPeriodIndexes[i],
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::GetTrackTypeFromString(kTrackTypes[j]),
|
2014-04-24 16:59:07 +00:00
|
|
|
&encryption_key));
|
|
|
|
EXPECT_EQ(GetMockKey(kTrackTypes[j], kCryptoPeriodIndexes[i]),
|
|
|
|
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(
|
2014-04-24 16:59:07 +00:00
|
|
|
kFirstCryptoPeriodIndex,
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::TRACK_TYPE_SD,
|
2014-04-24 16:59:07 +00:00
|
|
|
&encryption_key);
|
|
|
|
EXPECT_EQ(error::INVALID_ARGUMENT, status.error_code());
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|