710 lines
24 KiB
C++
710 lines
24 KiB
C++
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
//
|
|
// This source file provides a basic set of unit tests for the Content
|
|
// Decryption Module (CDM). It exercises much of the API that will be
|
|
// required by the host application to get the license and keys for
|
|
// rendering protected content.
|
|
|
|
#include "cdm_test_config.h"
|
|
#include "test_host_1.h"
|
|
#include "test_util.h"
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "clock.h"
|
|
#include "config_test_env.h"
|
|
#include "content_decryption_module.h"
|
|
#include "device_cert.h"
|
|
#include "license_request.h"
|
|
#include "log.h"
|
|
#include "properties.h"
|
|
#include "scoped_ptr.h"
|
|
#include "string_conversions.h"
|
|
#include "url_request.h"
|
|
|
|
static const int kTestPolicyRenewalDelaySeconds = 180;
|
|
static const int kDelayWaitToForRenewalMessageSeconds = 2;
|
|
static const int kHttpOk = 200;
|
|
static const int kHttpBadGateway = 502;
|
|
static const int kNumRetries = 5;
|
|
static const int kRetryBaseDelaySeconds = 3;
|
|
|
|
namespace {
|
|
|
|
// Default key system identifier.
|
|
const char kKeySystemWidevine[] = "com.widevine.alpha";
|
|
|
|
// Default mime type for key request generation.
|
|
const char kMimeType[] = "video/mp4";
|
|
|
|
// Key ID of key used to encrypt the test content.
|
|
// This is used to look up the content key.
|
|
const std::vector<uint8_t> kTestKeyId =
|
|
wvcdm::a2b_hex("371ea35e1a985d75d198a7f41020dc23");
|
|
|
|
// Dummy encrypted data.
|
|
const std::vector<uint8_t> kInputVector1 = wvcdm::a2b_hex(
|
|
"64ab17b3e3dfab47245c7cce4543d4fc7a26dcf248f19f9b59f3c92601440b36"
|
|
"17c8ed0c96c656549e461f38708cd47a434066f8df28ccc28b79252eee3f9c2d"
|
|
"7f6c68ebe40141fe818fe082ca523c03d69ddaf183a93c022327fedc5582c5ab"
|
|
"ca9d342b71263a67f9cb2336f12108aaaef464f17177e44e9b0c4e56e61da53c"
|
|
"2150b4405cc82d994dfd9bf4087c761956d6688a9705db4cf350381085f383c4"
|
|
"9666d4aed135c519c1f0b5cba06e287feea96ea367bf54e7368dcf998276c6e4"
|
|
"6497e0c50e20fef74e42cb518fe7f22ef27202428688f86404e8278587017012"
|
|
"c1d65537c6cbd7dde04aae338d68115a9f430afc100ab83cdadf45dca39db685");
|
|
const std::vector<uint8_t> kIv1 =
|
|
wvcdm::a2b_hex("f6f4b1e600a5b67813ed2bded913ba9f");
|
|
|
|
// Expected output for kInputVector1.
|
|
const std::vector<uint8_t> kOutputVector1 = wvcdm::a2b_hex(
|
|
"217ce9bde99bd91e9733a1a00b9b557ac3a433dc92633546156817fae26b6e1c"
|
|
"942ac20a89ff79f4c2f25fba99d6a44618a8c0420b27d54e3da17b77c9d43cca"
|
|
"595d259a1e4a8b6d7744cd98c5d3f921adc252eb7d8af6b916044b676a574747"
|
|
"8df21fdc42f166880d97a2225cd5c9ea5e7b752f4cf81bbdbe98e542ee10e1c6"
|
|
"ad868a6ac55c10d564fc23b8acff407daaf4ed2743520e02cda9680d9ea88e91"
|
|
"029359c4cf5906b6ab5bf60fbb3f1a1c7c59acfc7e4fb4ad8e623c04d503a3dd"
|
|
"4884604c8da8a53ce33db9ff8f1c5bb6bb97f37b39906bf41596555c1bcce9ed"
|
|
"08a899cd760ff0899a1170c2f224b9c52997a0785b7fe170805fd3e8b1127659");
|
|
|
|
// Dummy encrypted data. This is a combination of clear and encrypted data.
|
|
const std::vector<uint8_t> kInputVector2 = wvcdm::a2b_hex(
|
|
// subsample 0
|
|
"abcdef"
|
|
"53cc758763904ea5870458e6b23d36db1e6d7f7aaa2f3eeebb5393a7264991e7"
|
|
"ce4f57b198326e1a208a821799b2a29c90567ab57321b06e51fc20dc9bc5fc55"
|
|
"10720a8bb1f5e002c3e50ff70d2d806a9432cad237050d09581f5b0d59b00090"
|
|
"b3ad69b4087f5a155b17e13c44d33fa007475d207fc4ac2ef3b571ecb9"
|
|
// subsample 1
|
|
"0123456789"
|
|
"f3c852"
|
|
"ce00dc4806f0c6856ae1732e20308096478e1d822d75c2bb768119565d3bd6e6"
|
|
"901e36164f4802355ee758fc46ef6cf5f852dd5256c7b1e5f96d29"
|
|
// subsample 2
|
|
"deadbeefbaadf00d"
|
|
"3b20525d5e"
|
|
"78b8e5aa344d5c4e425e67ddf889ea7c4bb1d49af67eba67718b765e0a940402"
|
|
"8d306f4ce693ad6dc0a931d507fa14fff4d293d4170280b3e0fca2d628f722e8"
|
|
);
|
|
const std::vector<uint8_t> kIv2 =
|
|
wvcdm::a2b_hex("6ba18dd40f49da7f64c368e4db43fc88");
|
|
|
|
// Expected output for kInputVector2.
|
|
const std::vector<uint8_t> kOutputVector2 = wvcdm::a2b_hex(
|
|
// subsample 0
|
|
"abcdef"
|
|
"52e65334501acadf78e2b26460def3ac973771ed7c64001a2e82917342a7eab3"
|
|
"047f5e85449692fae8f677be425a47bdea850df5a3ffff17043afb1f2b437ab2"
|
|
"b1d5e0784c4ed8f97fc24b8f565e85ed63fb7d1365980d9aea7b8b58f488f83c"
|
|
"1ce80b6096c60f3b113c988ff185b26e798da8fc6f327e4ff00e4b3fbf"
|
|
// subsample 1
|
|
"0123456789"
|
|
"b1ed0a"
|
|
"a054bce40ccb0ebc70b181d1a12055f46ac55e29c7c2473a29d2a366d240ec48"
|
|
"7cede274f012813a877f99159e7062b6a37cfc9327a7bc2195814e"
|
|
// subsample 2
|
|
"deadbeefbaadf00d"
|
|
"653b818d1d"
|
|
"4ab9a9128361d8ca6a9d2766df5c096ee29f4f5204febdf217a94a5b560cd692"
|
|
"cc36d3e071df789fdeac2fb7ec6dcd7af94bb1f85c22025b25e702e38212b927"
|
|
);
|
|
|
|
// Dummy encrypted data. This will be decrypted with a data_offset
|
|
// instead of subsamples.
|
|
const std::vector<uint8_t> kInputVector3 = wvcdm::a2b_hex(
|
|
"64ab17b3e3dfab47245c7cce4543d4fc7a26dcf248f19f9b59f3c92601440b36"
|
|
"17c8ed0c96c656549e461f38708cd47a434066f8df28ccc28b79252eee3f9c2d"
|
|
"7f6c68ebe40141fe818fe082ca523c03d69ddaf183a93c022327fedc5582c5ab"
|
|
"ca9d342b71263a67f9cb2336f12108aaaef464f17177e44e9b0c4e56e61da53c"
|
|
"2150b4405cc82d994dfd9bf4087c761956d6688a9705db4cf350381085f383c4"
|
|
"9666d4aed135c519c1f0b5cba06e287feea96ea367bf54e7368dcf998276c6e4"
|
|
"6497e0c50e20fef74e42cb518fe7f22ef27202428688f86404e8278587017012"
|
|
"c1d65537c6cbd7dde04aae338d68115a9f430afc100ab83cdadf45dca39db685");
|
|
const std::vector<uint8_t> kIv3 =
|
|
wvcdm::a2b_hex("f6f4b1e600a5b67813ed2bded913ba9f");
|
|
|
|
// The data_offset for kInputVector3.
|
|
const uint32_t kInputOffset3 = 9;
|
|
|
|
// Expected output for kInputVector3 offset by kInputOffset3.
|
|
const std::vector<uint8_t> kOutputVector3 = wvcdm::a2b_hex(
|
|
"19ab304b49908e2395b32f26bf471adf4a4bc92f9e999cca8476d24a257931b4"
|
|
"c5fd177693ed55e31cd2b85dc196b2b722cd8854eb9334f3dab0b5bd26aa5e66"
|
|
"a9d1cfbba877c9456b11dc99a6bdc7015ca1544f7ce66171a8179eca19efe515"
|
|
"8c4c1d0612dff64100387065da108fdbfcc14738202ac3d27520eb48c020ddb7"
|
|
"714dca22e5e2241aff6932dba1587a97ac1a952827d411d8582dfecc2e9e1494"
|
|
"644046ca7044bc41c3c5e0a3a405d5551f3f5bdd6f36042e2f0f3693778b9277"
|
|
"6ed8d106647a7539df7d30288803cd9ca1c274bebe688151c72b451f571a441f"
|
|
"83d0ff77d8d57dcb395122e175f4944569917627d6c3dc");
|
|
|
|
void* GetCdmHost(int host_interface_version, void* user_data) {
|
|
if (host_interface_version != cdm::Host_1::kVersion) return NULL;
|
|
return user_data;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace wvcdm {
|
|
|
|
class CdmApi1Test : public testing::Test {
|
|
public:
|
|
CdmApi1Test() : cdm_(NULL) {}
|
|
~CdmApi1Test() {}
|
|
|
|
protected:
|
|
virtual void SetUp() {
|
|
// Create the Host.
|
|
host_.reset(new TestHost_1());
|
|
|
|
// Set various parameters that the CDM will query.
|
|
host_->SetPlatformString("SecurityLevel", "L1");
|
|
host_->SetPlatformString("PrivacyOn", "False");
|
|
std::string cert(reinterpret_cast<const char*>(kDeviceCert),
|
|
sizeof(kDeviceCert));
|
|
host_->SetPlatformString("DeviceCertificate", cert);
|
|
|
|
// Initialize the CDM module before creating a CDM instance.
|
|
InitializeCdmModule();
|
|
|
|
// Create the CDM.
|
|
cdm_ =
|
|
reinterpret_cast<cdm::ContentDecryptionModule_1*>(::CreateCdmInstance(
|
|
cdm::ContentDecryptionModule_1::kVersion, kKeySystemWidevine,
|
|
strlen(kKeySystemWidevine), GetCdmHost, host_.get()));
|
|
|
|
// Tell the Host about the CDM.
|
|
host_->SetCdmPtr(cdm_);
|
|
}
|
|
|
|
cdm::Status GenerateKeyRequest(const std::string& init_data) {
|
|
cdm::Status status = cdm_->GenerateKeyRequest(
|
|
kMimeType, strlen(kMimeType),
|
|
(const uint8_t*)init_data.data(), init_data.length());
|
|
return status;
|
|
}
|
|
|
|
cdm::Status GenerateKeyRequestWithMimeType(const std::string& mime_type) {
|
|
cdm::Status status = cdm_->GenerateKeyRequest(
|
|
mime_type.c_str(), mime_type.length(),
|
|
(const uint8_t*)g_key_id.data(), g_key_id.length());
|
|
return status;
|
|
}
|
|
|
|
// posts a request and extracts the drm message from the response
|
|
std::string GetKeyRequestResponse(const TestHost_1::KeyMessage& key_msg) {
|
|
std::string url;
|
|
if (key_msg.default_url.empty()) {
|
|
url = g_license_server + g_client_auth;
|
|
} else {
|
|
// Note that the client auth string is not appended when the CDM tells
|
|
// us what URL to use.
|
|
url = key_msg.default_url;
|
|
}
|
|
|
|
int status_code;
|
|
std::string response;
|
|
UrlRequest url_request(url);
|
|
|
|
for (int retries = 0; retries < kNumRetries; ++retries) {
|
|
EXPECT_TRUE(url_request.is_connected());
|
|
if (!url_request.is_connected()) {
|
|
return "";
|
|
}
|
|
|
|
url_request.PostRequest(key_msg.message);
|
|
int resp_bytes = url_request.GetResponse(&response);
|
|
|
|
status_code = url_request.GetStatusCode(response);
|
|
// Sometimes, the server returns "HTTP 502 bad gateway".
|
|
// If we treat this as a non-fatal error, we reduce test flakiness.
|
|
if (status_code != kHttpBadGateway) {
|
|
// Move on with normal processing.
|
|
break;
|
|
}
|
|
|
|
// Reconnect to the server and try again. Since the server's 502
|
|
// response could indicate a temporary failure due to load, we use
|
|
// an exponential backoff. Each time we reconnect, we delay by
|
|
// exactly twice as long as the last time. This is a simplified
|
|
// version of the delay strategy recommended by Google in the
|
|
// internal document "Rate Limiting in Google Applications" under
|
|
// the heading "Settings for client exponential backoff". We do
|
|
// not bother to fuzz the delay, since unit tests are not running
|
|
// simultaneously in large numbers like real clients would be.
|
|
LOGE("Bad gateway, retrying.");
|
|
sleep(kRetryBaseDelaySeconds << retries);
|
|
url_request.Reconnect();
|
|
}
|
|
|
|
// Some license servers return 400 for invalid message, some
|
|
// return 500; treat anything other than 200 as an invalid message.
|
|
EXPECT_EQ(kHttpOk, status_code);
|
|
|
|
if (status_code != kHttpOk) {
|
|
return "";
|
|
} else {
|
|
std::string drm_msg;
|
|
LicenseRequest lic_request;
|
|
lic_request.GetDrmMessage(response, drm_msg);
|
|
LOGV("drm msg: %u bytes\n%s", drm_msg.size(),
|
|
HexEncode(reinterpret_cast<const uint8_t*>(drm_msg.data()),
|
|
drm_msg.size()).c_str());
|
|
return drm_msg;
|
|
}
|
|
}
|
|
|
|
void ProcessKeyResponse() {
|
|
TestHost_1::KeyMessage key_msg = host_->GetLastKeyMessage();
|
|
ASSERT_FALSE(key_msg.message.empty());
|
|
EXPECT_TRUE(key_msg.default_url.empty());
|
|
std::string drm_msg = GetKeyRequestResponse(key_msg);
|
|
EXPECT_EQ(cdm::kSuccess, AddKey(key_msg.session_id, drm_msg));
|
|
}
|
|
|
|
void ProcessKeyRenewalResponse() {
|
|
TestHost_1::KeyMessage key_msg = host_->GetLastKeyMessage();
|
|
ASSERT_FALSE(key_msg.message.empty());
|
|
EXPECT_FALSE(key_msg.default_url.empty());
|
|
std::string drm_msg = GetKeyRequestResponse(key_msg);
|
|
EXPECT_EQ(cdm::kSuccess, AddKey(key_msg.session_id, drm_msg));
|
|
}
|
|
|
|
void CloseSession(const std::string& session_id) {
|
|
cdm::Status status =
|
|
cdm_->CloseSession(session_id.data(), session_id.length());
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
}
|
|
|
|
cdm::Status AddKey(const std::string& session_id,
|
|
const std::string& drm_msg) {
|
|
cdm::Status status =
|
|
cdm_->AddKey(session_id.data(), session_id.size(),
|
|
(const uint8_t*)drm_msg.data(), drm_msg.size(), NULL, 0);
|
|
return status;
|
|
}
|
|
|
|
cdm::InputBuffer BuildInputBuffer(const std::vector<uint8_t>& encrypted,
|
|
const std::vector<uint8_t>& iv) {
|
|
cdm::InputBuffer buf;
|
|
buf.data = &encrypted[0];
|
|
buf.data_size = encrypted.size();
|
|
buf.key_id = &kTestKeyId[0];
|
|
buf.key_id_size = kTestKeyId.size();
|
|
buf.iv = &iv[0];
|
|
buf.iv_size = iv.size();
|
|
buf.data_offset = 0;
|
|
buf.timestamp = 10;
|
|
return buf;
|
|
}
|
|
|
|
cdm::InputBuffer BuildInputBuffer(
|
|
const std::vector<uint8_t>& encrypted,
|
|
const std::vector<uint8_t>& iv,
|
|
const std::vector<cdm::SubsampleEntry>& sub) {
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv);
|
|
buf.subsamples = &sub[0];
|
|
buf.num_subsamples = sub.size();
|
|
return buf;
|
|
}
|
|
|
|
cdm::InputBuffer BuildInputBuffer(
|
|
const std::vector<uint8_t>& encrypted,
|
|
const std::vector<uint8_t>& iv,
|
|
const uint32_t offset) {
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv);
|
|
buf.data_offset = offset;
|
|
return buf;
|
|
}
|
|
|
|
std::vector<cdm::SubsampleEntry> BuildMultipleSubsamples() {
|
|
std::vector<cdm::SubsampleEntry> sub;
|
|
sub.push_back(cdm::SubsampleEntry(3, 125));
|
|
sub.push_back(cdm::SubsampleEntry(5, 62));
|
|
sub.push_back(cdm::SubsampleEntry(8, 69));
|
|
return sub;
|
|
}
|
|
|
|
std::vector<cdm::SubsampleEntry> BuildSingleSubsample(size_t size) {
|
|
std::vector<cdm::SubsampleEntry> sub;
|
|
sub.push_back(cdm::SubsampleEntry(0, size));
|
|
return sub;
|
|
}
|
|
|
|
cdm::ContentDecryptionModule_1* cdm_; // owned by host_
|
|
wvcdm::scoped_ptr<TestHost_1> host_;
|
|
};
|
|
|
|
namespace {
|
|
|
|
class DummyCDM : public cdm::ContentDecryptionModule_1 {
|
|
public:
|
|
DummyCDM() : timer_fired_(false), last_context_(NULL) {}
|
|
|
|
virtual cdm::Status GenerateKeyRequest(const char*, int, const uint8_t*,
|
|
int) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual cdm::Status AddKey(const char*, int, const uint8_t*, int,
|
|
const uint8_t*, int) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual bool IsKeyValid(const uint8_t*, int) OVERRIDE { return false; }
|
|
|
|
virtual cdm::Status CloseSession(const char*, int) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual void TimerExpired(void* context) OVERRIDE {
|
|
timer_fired_ = true;
|
|
last_context_ = context;
|
|
}
|
|
|
|
virtual cdm::Status Decrypt(const cdm::InputBuffer&,
|
|
cdm::DecryptedBlock*) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual cdm::Status DecryptDecodeAndRenderFrame(
|
|
const cdm::InputBuffer&) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual cdm::Status DecryptDecodeAndRenderSamples(
|
|
const cdm::InputBuffer&) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual void Destroy() OVERRIDE { delete this; }
|
|
|
|
virtual cdm::Status GetProvisioningRequest(std::string*,
|
|
std::string*) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
virtual cdm::Status HandleProvisioningResponse(std::string&) OVERRIDE {
|
|
return cdm::kSessionError;
|
|
}
|
|
|
|
bool TimerFired() const { return timer_fired_; }
|
|
|
|
void* LastTimerContext() const { return last_context_; }
|
|
|
|
void ResetTimerStatus() {
|
|
timer_fired_ = false;
|
|
last_context_ = NULL;
|
|
}
|
|
|
|
private:
|
|
bool timer_fired_;
|
|
void* last_context_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_F(CdmApi1Test, TestHostTimer) {
|
|
// Validate that the TestHost timers are processed in the correct order.
|
|
// To do this, we replace the cdm with a dummy that only tracks timers.
|
|
DummyCDM* cdm = new DummyCDM();
|
|
|
|
// The old CDM is destroyed by SetCdmPtr.
|
|
cdm_ = cdm;
|
|
host_->SetCdmPtr(cdm);
|
|
|
|
const double kTimerDelaySeconds = 1.0;
|
|
const int64_t kTimerDelayMs = kTimerDelaySeconds * 1000;
|
|
void* kCtx1 = reinterpret_cast<void*>(0x1);
|
|
void* kCtx2 = reinterpret_cast<void*>(0x2);
|
|
|
|
host_->SetTimer(kTimerDelayMs * 1, kCtx1);
|
|
host_->SetTimer(kTimerDelayMs * 2, kCtx2);
|
|
|
|
host_->FastForwardTime(kTimerDelaySeconds);
|
|
EXPECT_TRUE(cdm->TimerFired());
|
|
EXPECT_EQ(kCtx1, cdm->LastTimerContext());
|
|
cdm->ResetTimerStatus();
|
|
|
|
host_->FastForwardTime(kTimerDelaySeconds);
|
|
EXPECT_TRUE(cdm->TimerFired());
|
|
EXPECT_EQ(kCtx2, cdm->LastTimerContext());
|
|
cdm->ResetTimerStatus();
|
|
|
|
host_->FastForwardTime(kTimerDelaySeconds);
|
|
EXPECT_FALSE(cdm->TimerFired());
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, DeviceCertificateTest) {
|
|
if (Properties::use_certificates_as_identification()) {
|
|
// Clear any existing device cert.
|
|
host_->SetPlatformString("DeviceCertificate", "");
|
|
|
|
ASSERT_EQ(cdm::kNeedsDeviceCertificate, GenerateKeyRequest(g_key_id));
|
|
|
|
// The Host must handle the certificate provisioning request.
|
|
std::string server_url;
|
|
std::string request;
|
|
cdm::Status status = cdm_->GetProvisioningRequest(&request, &server_url);
|
|
ASSERT_EQ(cdm::kSuccess, status);
|
|
|
|
UrlRequest url_request(server_url);
|
|
url_request.PostCertRequestInQueryString(request);
|
|
|
|
std::string message;
|
|
bool ok = url_request.GetResponse(&message);
|
|
ASSERT_TRUE(ok);
|
|
|
|
status = cdm_->HandleProvisioningResponse(message);
|
|
ASSERT_EQ(cdm::kSuccess, status);
|
|
|
|
// Now we are provisioned, so GKR should succeed.
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
} else {
|
|
LOGI(
|
|
"Skipping CdmApi1Test::DeviceCertificateTest because this platform "
|
|
"does not support device certificates.");
|
|
}
|
|
}
|
|
|
|
// Note that these tests, BaseMessageTest, NormalDecryption and TimeTest,
|
|
// are dependent on getting back a license from the license server where the
|
|
// url for the license server is defined in the conf_test_env.cpp. If these
|
|
// tests fail immediately, verify that the license server URL is correct
|
|
// and works in your test environment.
|
|
|
|
TEST_F(CdmApi1Test, BaseMessageTest) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, NormalDecryption) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
// Level 1 / Level 2 payload comes back in the cpu memory as cleartext.
|
|
std::vector<uint8_t> encrypted = kInputVector1;
|
|
std::vector<uint8_t> iv = kIv1;
|
|
std::vector<uint8_t> expected = kOutputVector1;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildSingleSubsample(encrypted.size());
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub);
|
|
|
|
TestDecryptedBlock output;
|
|
cdm::Status status = cdm_->Decrypt(buf, &output);
|
|
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
EXPECT_EQ(
|
|
0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size));
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, NormalSubSampleDecryptionWithSubsampleInfo) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
// Level 1 / Level 2 payload comes back in the cpu memory as cleartext.
|
|
std::vector<uint8_t> encrypted = kInputVector2;
|
|
std::vector<uint8_t> iv = kIv2;
|
|
std::vector<uint8_t> expected = kOutputVector2;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildMultipleSubsamples();
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub);
|
|
|
|
TestDecryptedBlock output;
|
|
cdm::Status status = cdm_->Decrypt(buf, &output);
|
|
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
EXPECT_EQ(
|
|
0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size));
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, NormalSubSampleDecryptionWithMissingSubsampleInfo) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
std::vector<uint8_t> encrypted = kInputVector2;
|
|
std::vector<uint8_t> iv = kIv2;
|
|
std::vector<uint8_t> expected = kOutputVector2;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildMultipleSubsamples();
|
|
|
|
// Don't add these subsamples yet!
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv);
|
|
buf.num_subsamples = sub.size();
|
|
|
|
TestDecryptedBlock output;
|
|
|
|
cdm::Status status = cdm_->Decrypt(buf, &output);
|
|
EXPECT_EQ(cdm::kDecryptError, status);
|
|
|
|
// Add the subsamples pointer and expect success.
|
|
buf.subsamples = &sub[0];
|
|
status = cdm_->Decrypt(buf, &output);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
EXPECT_EQ(
|
|
0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size));
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, DecryptWithDataOffset) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
std::vector<uint8_t> encrypted = kInputVector3;
|
|
std::vector<uint8_t> iv = kIv3;
|
|
std::vector<uint8_t> expected = kOutputVector3;
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, kInputOffset3);
|
|
|
|
TestDecryptedBlock output;
|
|
cdm::Status status = cdm_->Decrypt(buf, &output);
|
|
cdm::Buffer *output_buf = output.DecryptedBuffer();
|
|
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
EXPECT_EQ(buf.data_size - buf.data_offset, output_buf->Size());
|
|
EXPECT_EQ(0, memcmp(output.DecryptedBuffer()->Data(), &expected[0],
|
|
output_buf->Size()));
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, DecryptReturnsSizedBuffer) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
std::vector<uint8_t> encrypted = kInputVector1;
|
|
std::vector<uint8_t> iv = kIv1;
|
|
std::vector<uint8_t> expected = kOutputVector1;
|
|
|
|
std::vector<cdm::SubsampleEntry> sub = BuildSingleSubsample(encrypted.size());
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub);
|
|
|
|
TestDecryptedBlock output;
|
|
cdm::Status status = cdm_->Decrypt(buf, &output);
|
|
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
cdm::Buffer* buffer = output.DecryptedBuffer();
|
|
EXPECT_NE((void*)NULL, buffer);
|
|
if (buffer) {
|
|
EXPECT_EQ(expected.size(), output.DecryptedBuffer()->Size());
|
|
}
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, TimeTest) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
// We expect that by the time we've added a key, the CDM has set a timer.
|
|
// Otherwise, it couldn't correctly handle renewal.
|
|
EXPECT_NE(0, host_->NumTimers());
|
|
host_->FastForwardTime(kTestPolicyRenewalDelaySeconds +
|
|
kDelayWaitToForRenewalMessageSeconds);
|
|
|
|
// When the timer expired, we should have sent a renewal, so we can
|
|
// add this renewed key now, assuming things are working as expected.
|
|
ProcessKeyRenewalResponse();
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, SecureDecryptionLevel1) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
// Level 1 passes encrypted payload straight through. By calling the
|
|
// CDM's DecryptDecodeAndRenderSamples, and/or DecryptDecodeAndRenderFrame,
|
|
// OEMCrypto_DecryptCTR will be told to use Direct Rendering.
|
|
std::vector<uint8_t> encrypted = kInputVector1;
|
|
std::vector<uint8_t> iv = kIv1;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildSingleSubsample(encrypted.size());
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub);
|
|
|
|
cdm::Status status;
|
|
|
|
status = cdm_->DecryptDecodeAndRenderSamples(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
status = cdm_->DecryptDecodeAndRenderFrame(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, SecureDecryptionLevel1WithSubsampleInfo) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
// Level 1 passes encrypted payload straight through. By calling the
|
|
// CDM's DecryptDecodeAndRenderSamples, and/or DecryptDecodeAndRenderFrame,
|
|
// OEMCrypto_DecryptCTR will be told to use Direct Rendering.
|
|
std::vector<uint8_t> encrypted = kInputVector2;
|
|
std::vector<uint8_t> iv = kIv2;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildMultipleSubsamples();
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub);
|
|
|
|
cdm::Status status;
|
|
|
|
status = cdm_->DecryptDecodeAndRenderSamples(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
status = cdm_->DecryptDecodeAndRenderFrame(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, SecureDecryptionLevel1WithMissingSubsampleInfo) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
ProcessKeyResponse();
|
|
|
|
std::vector<uint8_t> encrypted = kInputVector2;
|
|
std::vector<uint8_t> iv = kIv2;
|
|
std::vector<cdm::SubsampleEntry> sub = BuildMultipleSubsamples();
|
|
|
|
// Don't add these subsamples yet!
|
|
cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv);
|
|
buf.num_subsamples = sub.size();
|
|
|
|
cdm::Status status;
|
|
|
|
status = cdm_->DecryptDecodeAndRenderSamples(buf);
|
|
EXPECT_EQ(cdm::kDecryptError, status);
|
|
status = cdm_->DecryptDecodeAndRenderFrame(buf);
|
|
EXPECT_EQ(cdm::kDecryptError, status);
|
|
|
|
// Add the subsamples pointer and expect success.
|
|
buf.subsamples = &sub[0];
|
|
status = cdm_->DecryptDecodeAndRenderSamples(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
status = cdm_->DecryptDecodeAndRenderFrame(buf);
|
|
EXPECT_EQ(cdm::kSuccess, status);
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, GenerateKeyRequestFailureSendsKeyError) {
|
|
// Pass a bogus key id and expect failure.
|
|
EXPECT_EQ(cdm::kSessionError, GenerateKeyRequest(""));
|
|
// Expect the CDM to pass a key error back to the host.
|
|
EXPECT_EQ(1, host_->KeyErrorsSize());
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, AddKeyFailureSendsKeyError) {
|
|
EXPECT_EQ(cdm::kSuccess, GenerateKeyRequest(g_key_id));
|
|
|
|
// Get the message and response.
|
|
TestHost_1::KeyMessage key_msg = host_->GetLastKeyMessage();
|
|
EXPECT_TRUE(key_msg.default_url.empty());
|
|
std::string drm_msg = GetKeyRequestResponse(key_msg);
|
|
|
|
// Call AddKey with a bad session id and expect failure.
|
|
EXPECT_EQ(cdm::kSessionError, AddKey("BLAH", drm_msg));
|
|
|
|
// Expect the CDM to pass a key error back to the host.
|
|
EXPECT_EQ(1, host_->KeyErrorsSize());
|
|
|
|
// Call AddKey with a bad license and expect failure.
|
|
EXPECT_EQ(cdm::kSessionError, AddKey(key_msg.session_id, "BLAH"));
|
|
|
|
// Expect the CDM to pass one more key error back to the host.
|
|
EXPECT_EQ(2, host_->KeyErrorsSize());
|
|
}
|
|
|
|
TEST_F(CdmApi1Test, MimeTypeMatters) {
|
|
cdm::Status status;
|
|
|
|
status = GenerateKeyRequestWithMimeType("video/mp4");
|
|
ASSERT_EQ(cdm::kSuccess, status);
|
|
|
|
status = GenerateKeyRequestWithMimeType("video/webm");
|
|
ASSERT_EQ(cdm::kSuccess, status);
|
|
|
|
status = GenerateKeyRequestWithMimeType("video/blah");
|
|
ASSERT_EQ(cdm::kSessionError, status);
|
|
}
|
|
|
|
} // namespace wvcdm
|