2016-02-19 20:23:37 +00:00
|
|
|
// Copyright 2016 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
|
|
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
2017-10-17 23:03:08 +00:00
|
|
|
#include "packager/media/base/raw_key_source.h"
|
2017-06-29 22:23:53 +00:00
|
|
|
#include "packager/status_test_util.h"
|
2016-02-19 20:23:37 +00:00
|
|
|
|
|
|
|
#define EXPECT_HEX_EQ(expected, actual) \
|
|
|
|
do { \
|
|
|
|
std::vector<uint8_t> decoded_; \
|
|
|
|
ASSERT_TRUE(base::HexStringToBytes((expected), &decoded_)); \
|
|
|
|
EXPECT_EQ(decoded_, (actual)); \
|
|
|
|
} while (false)
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-02-19 20:23:37 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
namespace {
|
2017-09-20 22:49:00 +00:00
|
|
|
const char kKeyIdHex[] = "0101020305080d1522375990e9000000";
|
|
|
|
const char kKeyHex[] = "00100100200300500801302103405500";
|
|
|
|
const char kKeyId2Hex[] = "1111121315180d1522375990e9000000";
|
|
|
|
const char kKey2Hex[] = "10201110300300500801302103405500";
|
2016-02-19 20:23:37 +00:00
|
|
|
const char kIvHex[] = "000102030405060708090a0b0c0d0e0f";
|
2017-09-20 22:49:00 +00:00
|
|
|
// PSSH boxes generated manually according to PSSH box syntax specified in
|
|
|
|
// ISO/IEC 23001-7:2016 8.1.2.
|
2016-02-19 20:23:37 +00:00
|
|
|
const char kPsshBox1Hex[] =
|
2017-09-20 22:49:00 +00:00
|
|
|
"000000427073736801000000"
|
|
|
|
"020305070b0d1113171d1f25292b2f35"
|
|
|
|
"00000001"
|
|
|
|
"0101020305080d1522375990e9000000"
|
|
|
|
"0000000e"
|
|
|
|
"6544617368207061636b61676572";
|
2016-02-19 20:23:37 +00:00
|
|
|
const char kPsshBox2Hex[] =
|
2017-09-20 22:49:00 +00:00
|
|
|
"0000002e7073736800000000"
|
|
|
|
"bbbbbbbbbaaaaaaaaaaaaddddddddddd"
|
|
|
|
"0000000e"
|
|
|
|
"fffffffff000000000000ddddddd";
|
2016-02-19 20:23:37 +00:00
|
|
|
const char kDefaultPsshBoxHex[] =
|
2017-09-20 22:49:00 +00:00
|
|
|
"000000447073736801000000"
|
|
|
|
"1077efecc0b24d02ace33c1e52e2fb4b"
|
|
|
|
"00000002"
|
|
|
|
"1111121315180d1522375990e9000000"
|
|
|
|
"0101020305080d1522375990e9000000"
|
|
|
|
"00000000";
|
|
|
|
const char kDrmLabel[] = "SomeDrmLabel";
|
|
|
|
const char kAnotherDrmLabel[] = "AnotherDrmLabel";
|
|
|
|
const char kEmptyDrmLabel[] = "";
|
2017-06-14 23:18:16 +00:00
|
|
|
|
2018-01-17 18:53:58 +00:00
|
|
|
const int kNoProtectionSystemFlag = NO_PROTECTION_SYSTEM_FLAG;
|
|
|
|
|
2017-06-14 23:18:16 +00:00
|
|
|
std::vector<uint8_t> HexStringToVector(const std::string& str) {
|
|
|
|
std::vector<uint8_t> vec;
|
|
|
|
CHECK(base::HexStringToBytes(str, &vec));
|
|
|
|
return vec;
|
|
|
|
}
|
2017-09-20 22:49:00 +00:00
|
|
|
} // namespace
|
2016-02-19 20:23:37 +00:00
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
TEST(RawKeySourceTest, Success) {
|
2017-09-20 22:49:00 +00:00
|
|
|
RawKeyParams raw_key_params;
|
|
|
|
raw_key_params.key_map[kDrmLabel].key_id = HexStringToVector(kKeyIdHex);
|
|
|
|
raw_key_params.key_map[kDrmLabel].key = HexStringToVector(kKeyHex);
|
|
|
|
raw_key_params.key_map[kEmptyDrmLabel].key_id = HexStringToVector(kKeyId2Hex);
|
|
|
|
raw_key_params.key_map[kEmptyDrmLabel].key = HexStringToVector(kKey2Hex);
|
|
|
|
raw_key_params.iv = HexStringToVector(kIvHex);
|
|
|
|
raw_key_params.pssh =
|
|
|
|
HexStringToVector(std::string(kPsshBox1Hex) + kPsshBox2Hex);
|
2017-10-17 23:03:08 +00:00
|
|
|
std::unique_ptr<RawKeySource> key_source =
|
2018-01-17 18:53:58 +00:00
|
|
|
RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
|
2016-02-19 20:23:37 +00:00
|
|
|
ASSERT_NE(nullptr, key_source);
|
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
EncryptionKey key_from_drm_label;
|
|
|
|
ASSERT_OK(key_source->GetKey(kDrmLabel, &key_from_drm_label));
|
|
|
|
EXPECT_HEX_EQ(kKeyIdHex, key_from_drm_label.key_id);
|
|
|
|
EXPECT_HEX_EQ(kKeyHex, key_from_drm_label.key);
|
|
|
|
EXPECT_HEX_EQ(kIvHex, key_from_drm_label.iv);
|
|
|
|
ASSERT_EQ(2u, key_from_drm_label.key_system_info.size());
|
|
|
|
EXPECT_HEX_EQ(kPsshBox1Hex,
|
|
|
|
key_from_drm_label.key_system_info[0].CreateBox());
|
|
|
|
EXPECT_HEX_EQ(kPsshBox2Hex,
|
|
|
|
key_from_drm_label.key_system_info[1].CreateBox());
|
|
|
|
|
|
|
|
// Using Key ID.
|
|
|
|
EncryptionKey key_from_key_id;
|
|
|
|
ASSERT_OK(key_source->GetKey(HexStringToVector(kKeyIdHex), &key_from_key_id));
|
|
|
|
EXPECT_EQ(key_from_key_id.key_id, key_from_drm_label.key_id);
|
|
|
|
|
|
|
|
// |kAnotherDrmLabel| is not present in the key source, but |kEmptyDrmLabel|
|
|
|
|
// is in the key source, the associated key for |kEmptyDrmLabel| will be
|
|
|
|
// returned.
|
|
|
|
ASSERT_OK(key_source->GetKey(kAnotherDrmLabel, &key_from_drm_label));
|
|
|
|
EXPECT_HEX_EQ(kKeyId2Hex, key_from_drm_label.key_id);
|
|
|
|
EXPECT_HEX_EQ(kKey2Hex, key_from_drm_label.key);
|
|
|
|
EXPECT_HEX_EQ(kIvHex, key_from_drm_label.iv);
|
|
|
|
ASSERT_EQ(2u, key_from_drm_label.key_system_info.size());
|
|
|
|
EXPECT_HEX_EQ(kPsshBox1Hex,
|
|
|
|
key_from_drm_label.key_system_info[0].CreateBox());
|
|
|
|
EXPECT_HEX_EQ(kPsshBox2Hex,
|
|
|
|
key_from_drm_label.key_system_info[1].CreateBox());
|
2016-02-19 20:23:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
TEST(RawKeySourceTest, EmptyPssh) {
|
2017-09-20 22:49:00 +00:00
|
|
|
RawKeyParams raw_key_params;
|
|
|
|
raw_key_params.key_map[kDrmLabel].key_id = HexStringToVector(kKeyIdHex);
|
|
|
|
raw_key_params.key_map[kDrmLabel].key = HexStringToVector(kKeyHex);
|
|
|
|
raw_key_params.key_map[kAnotherDrmLabel].key_id =
|
|
|
|
HexStringToVector(kKeyId2Hex);
|
|
|
|
raw_key_params.key_map[kAnotherDrmLabel].key = HexStringToVector(kKey2Hex);
|
|
|
|
raw_key_params.iv = HexStringToVector(kIvHex);
|
2017-10-17 23:03:08 +00:00
|
|
|
std::unique_ptr<RawKeySource> key_source =
|
2018-01-17 18:53:58 +00:00
|
|
|
RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
|
2016-02-19 20:23:37 +00:00
|
|
|
ASSERT_NE(nullptr, key_source);
|
|
|
|
|
|
|
|
EncryptionKey key;
|
2017-09-20 22:49:00 +00:00
|
|
|
ASSERT_OK(key_source->GetKey(kDrmLabel, &key));
|
2016-02-19 20:23:37 +00:00
|
|
|
EXPECT_HEX_EQ(kKeyIdHex, key.key_id);
|
|
|
|
EXPECT_HEX_EQ(kKeyHex, key.key);
|
|
|
|
EXPECT_HEX_EQ(kIvHex, key.iv);
|
|
|
|
ASSERT_EQ(1u, key.key_system_info.size());
|
|
|
|
EXPECT_HEX_EQ(kDefaultPsshBoxHex, key.key_system_info[0].CreateBox());
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:03:08 +00:00
|
|
|
TEST(RawKeySourceTest, Failure) {
|
2016-02-19 20:23:37 +00:00
|
|
|
// Invalid key id size.
|
2017-09-20 22:49:00 +00:00
|
|
|
RawKeyParams raw_key_params;
|
|
|
|
raw_key_params.key_map[kEmptyDrmLabel].key_id =
|
|
|
|
HexStringToVector("000102030405");
|
|
|
|
raw_key_params.key_map[kEmptyDrmLabel].key = HexStringToVector(kKeyHex);
|
|
|
|
raw_key_params.pssh = HexStringToVector(kPsshBox1Hex);
|
|
|
|
raw_key_params.iv = HexStringToVector(kIvHex);
|
2017-10-17 23:03:08 +00:00
|
|
|
std::unique_ptr<RawKeySource> key_source =
|
2018-01-17 18:53:58 +00:00
|
|
|
RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
|
2016-02-19 20:23:37 +00:00
|
|
|
EXPECT_EQ(nullptr, key_source);
|
|
|
|
|
|
|
|
// Invalid pssh box.
|
2017-09-20 22:49:00 +00:00
|
|
|
raw_key_params.key_map[kEmptyDrmLabel].key_id = HexStringToVector(kKeyIdHex);
|
|
|
|
raw_key_params.pssh = HexStringToVector("000102030405");
|
2018-01-17 18:53:58 +00:00
|
|
|
key_source = RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
|
2016-02-19 20:23:37 +00:00
|
|
|
EXPECT_EQ(nullptr, key_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|