Add back Initialize which starts key fetching thread

The key production thread was started in constructor. The default http
fetcher might have already been used to fetch keys before it is modified
by set_http_fetcher. This could lead to flaky unittest.

Change-Id: I977b6450862d87ffeb5cb219bcd46b33d877e550
This commit is contained in:
KongQun Yang 2014-05-08 16:34:45 -07:00 committed by Kongqun Yang
parent cdf0aa178d
commit 1899d5c3b0
4 changed files with 54 additions and 13 deletions

View File

@ -66,12 +66,20 @@ scoped_ptr<EncryptionKeySource> CreateEncryptionKeySource() {
} }
} }
encryption_key_source.reset(new WidevineEncryptionKeySource( scoped_ptr<WidevineEncryptionKeySource> widevine_encryption_key_source(
new WidevineEncryptionKeySource(
FLAGS_key_server_url, FLAGS_key_server_url,
FLAGS_content_id, FLAGS_content_id,
FLAGS_policy, FLAGS_policy,
signer.Pass(), signer.Pass(),
FLAGS_crypto_period_duration == 0 ? kDisableKeyRotation : 0)); FLAGS_crypto_period_duration == 0 ? kDisableKeyRotation : 0));
Status status = widevine_encryption_key_source->Initialize();
if (!status.ok()) {
LOG(ERROR) << "Widevine encryption key source failed to initialize: "
<< status.ToString();
return scoped_ptr<EncryptionKeySource>();
}
encryption_key_source = widevine_encryption_key_source.Pass();
} else if (FLAGS_enable_fixed_key_encryption) { } else if (FLAGS_enable_fixed_key_encryption) {
encryption_key_source = EncryptionKeySource::CreateFromHexStrings( encryption_key_source = EncryptionKeySource::CreateFromHexStrings(
FLAGS_key_id, FLAGS_key, FLAGS_pssh, ""); FLAGS_key_id, FLAGS_key, FLAGS_pssh, "");

View File

@ -144,16 +144,28 @@ WidevineEncryptionKeySource::WidevineEncryptionKeySource(
key_pool_(kDefaultCryptoPeriodCount, key_pool_(kDefaultCryptoPeriodCount,
key_rotation_enabled_ ? first_crypto_period_index : 0) { key_rotation_enabled_ ? first_crypto_period_index : 0) {
DCHECK(signer_); DCHECK(signer_);
key_production_thread_.Start();
} }
WidevineEncryptionKeySource::~WidevineEncryptionKeySource() { WidevineEncryptionKeySource::~WidevineEncryptionKeySource() {
key_pool_.Stop(); key_pool_.Stop();
if (key_production_thread_.HasBeenStarted())
key_production_thread_.Join(); key_production_thread_.Join();
} }
Status WidevineEncryptionKeySource::Initialize() {
DCHECK(!key_production_thread_.HasBeenStarted());
key_production_thread_.Start();
// Perform a GetKey request to find out common encryption request status.
// It also primes the key_pool if successful.
return key_rotation_enabled_ ? GetCryptoPeriodKey(first_crypto_period_index_,
TRACK_TYPE_SD, NULL)
: GetKey(TRACK_TYPE_SD, NULL);
}
Status WidevineEncryptionKeySource::GetKey(TrackType track_type, Status WidevineEncryptionKeySource::GetKey(TrackType track_type,
EncryptionKey* key) { EncryptionKey* key) {
DCHECK(key_production_thread_.HasBeenStarted());
DCHECK(!key_rotation_enabled_); DCHECK(!key_rotation_enabled_);
return GetKeyInternal(0u, track_type, key); return GetKeyInternal(0u, track_type, key);
} }
@ -162,6 +174,7 @@ Status WidevineEncryptionKeySource::GetCryptoPeriodKey(
uint32 crypto_period_index, uint32 crypto_period_index,
TrackType track_type, TrackType track_type,
EncryptionKey* key) { EncryptionKey* key) {
DCHECK(key_production_thread_.HasBeenStarted());
DCHECK(key_rotation_enabled_); DCHECK(key_rotation_enabled_);
return GetKeyInternal(crypto_period_index, track_type, key); return GetKeyInternal(crypto_period_index, track_type, key);
} }
@ -195,6 +208,7 @@ Status WidevineEncryptionKeySource::GetKeyInternal(
return Status(error::INTERNAL_ERROR, return Status(error::INTERNAL_ERROR,
"Cannot find key of type " + TrackTypeToString(track_type)); "Cannot find key of type " + TrackTypeToString(track_type));
} }
if (key)
*key = *encryption_key_map[track_type]; *key = *encryption_key_map[track_type];
return Status::OK; return Status::OK;
} }

View File

@ -39,6 +39,11 @@ class WidevineEncryptionKeySource : public EncryptionKeySource {
int first_crypto_period_index); int first_crypto_period_index);
virtual ~WidevineEncryptionKeySource(); virtual ~WidevineEncryptionKeySource();
/// Initialize the key source. Must be called before calling GetKey or
/// GetCryptoPeriodKey.
/// @return OK on success, an error status otherwise.
Status Initialize();
/// @name EncryptionKeySource implementation overrides. /// @name EncryptionKeySource implementation overrides.
/// @{ /// @{
virtual Status GetKey(TrackType track_type, EncryptionKey* key) OVERRIDE; virtual Status GetKey(TrackType track_type, EncryptionKey* key) OVERRIDE;

View File

@ -167,6 +167,10 @@ TEST_F(WidevineEncryptionKeySourceTest, GenerateSignatureFailure) {
.WillOnce(Return(false)); .WillOnce(Return(false));
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
ASSERT_EQ(Status(error::INTERNAL_ERROR, "Signature generation failed."),
widevine_encryption_key_source_->Initialize());
// GetKey should return the same failure.
EncryptionKey encryption_key; EncryptionKey encryption_key;
ASSERT_EQ(Status(error::INTERNAL_ERROR, "Signature generation failed."), ASSERT_EQ(Status(error::INTERNAL_ERROR, "Signature generation failed."),
widevine_encryption_key_source_->GetKey( widevine_encryption_key_source_->GetKey(
@ -191,6 +195,10 @@ TEST_F(WidevineEncryptionKeySourceTest, HttpPostFailure) {
.WillOnce(Return(kMockStatus)); .WillOnce(Return(kMockStatus));
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
ASSERT_EQ(kMockStatus,
widevine_encryption_key_source_->Initialize());
// GetKey should return the same failure.
EncryptionKey encryption_key; EncryptionKey encryption_key;
ASSERT_EQ(kMockStatus, ASSERT_EQ(kMockStatus,
widevine_encryption_key_source_->GetKey( widevine_encryption_key_source_->GetKey(
@ -208,6 +216,7 @@ TEST_F(WidevineEncryptionKeySourceTest, LicenseStatusOK) {
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK))); .WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
ASSERT_OK(widevine_encryption_key_source_->Initialize());
EncryptionKey encryption_key; EncryptionKey encryption_key;
const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"}; const std::string kTrackTypes[] = {"SD", "HD", "AUDIO"};
@ -241,6 +250,8 @@ TEST_F(WidevineEncryptionKeySourceTest, RetryOnTransientError) {
Return(Status::OK))); Return(Status::OK)));
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
ASSERT_OK(widevine_encryption_key_source_->Initialize());
EncryptionKey encryption_key; EncryptionKey encryption_key;
ASSERT_OK(widevine_encryption_key_source_->GetKey( ASSERT_OK(widevine_encryption_key_source_->GetKey(
EncryptionKeySource::TRACK_TYPE_SD, &encryption_key)); EncryptionKeySource::TRACK_TYPE_SD, &encryption_key));
@ -263,10 +274,8 @@ TEST_F(WidevineEncryptionKeySourceTest, NoRetryOnUnknownError) {
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK))); .WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
EncryptionKey encryption_key; ASSERT_EQ(error::SERVER_ERROR,
Status status = widevine_encryption_key_source_->GetKey( widevine_encryption_key_source_->Initialize().error_code());
EncryptionKeySource::TRACK_TYPE_SD, &encryption_key);
ASSERT_EQ(error::SERVER_ERROR, status.error_code());
} }
namespace { namespace {
@ -339,6 +348,7 @@ TEST_F(WidevineEncryptionKeySourceTest, KeyRotationTest) {
} }
CreateWidevineEncryptionKeySource(kFirstCryptoPeriodIndex); CreateWidevineEncryptionKeySource(kFirstCryptoPeriodIndex);
ASSERT_OK(widevine_encryption_key_source_->Initialize());
EncryptionKey encryption_key; EncryptionKey encryption_key;
@ -375,6 +385,8 @@ class WidevineEncryptionKeySourceDeathTest
TEST_F(WidevineEncryptionKeySourceDeathTest, TEST_F(WidevineEncryptionKeySourceDeathTest,
GetCryptoPeriodKeyOnNonKeyRotationSource) { GetCryptoPeriodKeyOnNonKeyRotationSource) {
CreateWidevineEncryptionKeySource(kDisableKeyRotation); CreateWidevineEncryptionKeySource(kDisableKeyRotation);
widevine_encryption_key_source_->Initialize();
EncryptionKey encryption_key; EncryptionKey encryption_key;
EXPECT_DEBUG_DEATH( EXPECT_DEBUG_DEATH(
widevine_encryption_key_source_->GetCryptoPeriodKey( widevine_encryption_key_source_->GetCryptoPeriodKey(
@ -384,6 +396,8 @@ TEST_F(WidevineEncryptionKeySourceDeathTest,
TEST_F(WidevineEncryptionKeySourceDeathTest, GetKeyOnKeyRotationSource) { TEST_F(WidevineEncryptionKeySourceDeathTest, GetKeyOnKeyRotationSource) {
CreateWidevineEncryptionKeySource(0); CreateWidevineEncryptionKeySource(0);
widevine_encryption_key_source_->Initialize();
EncryptionKey encryption_key; EncryptionKey encryption_key;
EXPECT_DEBUG_DEATH(widevine_encryption_key_source_->GetKey( EXPECT_DEBUG_DEATH(widevine_encryption_key_source_->GetKey(
EncryptionKeySource::TRACK_TYPE_SD, &encryption_key), EncryptionKeySource::TRACK_TYPE_SD, &encryption_key),