Bug 1308076 - Fixup PSSH parser gtests. r=jwwang

We're now obliged to be stricter, taking from the example of the Web Platform
Tests.

MozReview-Commit-ID: AJNDoRZ9BF8

--HG--
extra : source : 5180fdf042b03362b882ceb3a8ac0e664049bd64
This commit is contained in:
Chris Pearce 2016-10-07 21:12:18 +13:00
Родитель 3bb7ad5953
Коммит 591af22fc7
2 изменённых файлов: 34 добавлений и 48 удалений

Просмотреть файл

@ -112,7 +112,7 @@ const uint8_t gGoogleWPTCencInitData[] = {
// Example CENC initData from the EME spec format registry:
// https://w3c.github.io/encrypted-media/format-registry/initdata/cenc.html
const uint8_t gW3SpecExampleCencInitData[] = {
0x00, 0x00, 0x00, 0x4c, 0x70, 0x73, 0x73, 0x68, // BMFF box header (76 bytes, 'pssh')
0x00, 0x00, 0x00, 0x44, 0x70, 0x73, 0x73, 0x68, // BMFF box header (68 bytes, 'pssh')
0x01, 0x00, 0x00, 0x00, // Full box header (version = 1, flags = 0)
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, // SystemID
0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b,
@ -129,9 +129,9 @@ const uint8_t gOverflowBoxSize[] = {
0xff, 0xff, 0xff, 0xff, // size = UINT32_MAX
};
// Invalid box size, but retrievable data.
const uint8_t gMalformedCencInitData[] = {
0x00, 0x00, 0xff, 0xff, // size = too big a number
// Valid box size, but key count too large.
const uint8_t gTooLargeKeyCountInitData[] = {
0x00, 0x00, 0x00, 0x34, // size = too big a number
0x70, 0x73, 0x73, 0x68, // 'pssh'
0x01, // version = 1
0xff, 0xff, 0xff, // flags
@ -143,12 +143,9 @@ const uint8_t gMalformedCencInitData[] = {
0xff, 0xff, 0xff, 0xff // datasize
};
// Non PSSH box, followed by Non common SystemID PSSH, followed by common SystemID PSSH box.
const uint8_t gLeadingNonCommonCencInitData[] = {
0x00, 0x00, 0x00, 0x09, // size = 9
0xff, 0xff, 0xff, 0xff, // something other than 'pssh'
0xff, // odd number of bytes of garbage to throw off the parser
// Non common SystemID PSSH.
// No keys Ids can be extracted, but don't consider the box invalid.
const uint8_t gNonCencInitData[] = {
0x00, 0x00, 0x00, 0x5c, // size = 92
0x70, 0x73, 0x73, 0x68, // 'pssh'
0x01, // version = 1
@ -163,18 +160,6 @@ const uint8_t gLeadingNonCommonCencInitData[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
// gW3SpecExampleCencInitData
0x00, 0x00, 0x00, 0x4c, 0x70, 0x73, 0x73, 0x68, // BMFF box header (76 bytes, 'pssh')
0x01, 0x00, 0x00, 0x00, // Full box header (version = 1, flags = 0)
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, // SystemID
0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b,
0x00, 0x00, 0x00, 0x02, // KID_count (2)
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // First KID ("0123456789012345")
0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // Second KID ("ABCDEFGHIJKLMNOP")
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
0x00, 0x00, 0x00, 0x00 // Size of Data (0)
};
const uint8_t gNonPSSHBoxZeroSize[] = {
@ -210,46 +195,42 @@ const uint8_t g2xGoogleWPTCencInitData[] = {
TEST(ClearKey, ParseCencInitData) {
std::vector<std::vector<uint8_t>> keyIds;
bool rv;
ParseCENCInitData(gGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(gGoogleWPTCencInitData), keyIds);
rv = ParseCENCInitData(gGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(gGoogleWPTCencInitData), keyIds);
EXPECT_EQ(true, rv);
EXPECT_EQ(keyIds.size(), 1u);
EXPECT_EQ(keyIds[0].size(), 16u);
EXPECT_EQ(memcmp(&keyIds[0].front(), &gGoogleWPTCencInitData[32], 16), 0);
keyIds.clear();
ParseCENCInitData(gW3SpecExampleCencInitData, MOZ_ARRAY_LENGTH(gW3SpecExampleCencInitData), keyIds);
rv = ParseCENCInitData(gW3SpecExampleCencInitData, MOZ_ARRAY_LENGTH(gW3SpecExampleCencInitData), keyIds);
EXPECT_EQ(true, rv);
EXPECT_EQ(keyIds.size(), 2u);
EXPECT_EQ(keyIds[0].size(), 16u);
EXPECT_EQ(memcmp(&keyIds[0].front(), &gW3SpecExampleCencInitData[32], 16), 0);
EXPECT_EQ(memcmp(&keyIds[1].front(), &gW3SpecExampleCencInitData[48], 16), 0);
keyIds.clear();
ParseCENCInitData(gOverflowBoxSize, MOZ_ARRAY_LENGTH(gOverflowBoxSize), keyIds);
rv = ParseCENCInitData(gOverflowBoxSize, MOZ_ARRAY_LENGTH(gOverflowBoxSize), keyIds);
EXPECT_EQ(false, rv);
EXPECT_EQ(keyIds.size(), 0u);
keyIds.clear();
ParseCENCInitData(gMalformedCencInitData, MOZ_ARRAY_LENGTH(gMalformedCencInitData), keyIds);
EXPECT_EQ(keyIds.size(), 1u);
EXPECT_EQ(keyIds[0].size(), 16u);
EXPECT_EQ(memcmp(&keyIds[0].front(), &gMalformedCencInitData[32], 16), 0);
rv = ParseCENCInitData(gTooLargeKeyCountInitData, MOZ_ARRAY_LENGTH(gTooLargeKeyCountInitData), keyIds);
EXPECT_EQ(false, rv);
EXPECT_EQ(0u, keyIds.size());
keyIds.clear();
ParseCENCInitData(gLeadingNonCommonCencInitData, MOZ_ARRAY_LENGTH(gLeadingNonCommonCencInitData), keyIds);
EXPECT_EQ(keyIds.size(), 2u);
EXPECT_EQ(keyIds[0].size(), 16u);
EXPECT_EQ(memcmp(&keyIds[0].front(), &gW3SpecExampleCencInitData[32], 16), 0);
EXPECT_EQ(memcmp(&keyIds[1].front(), &gW3SpecExampleCencInitData[48], 16), 0);
rv = ParseCENCInitData(gNonCencInitData, MOZ_ARRAY_LENGTH(gNonCencInitData), keyIds);
EXPECT_EQ(true, rv);
EXPECT_EQ(0u, keyIds.size());
keyIds.clear();
ParseCENCInitData(gNonPSSHBoxZeroSize, MOZ_ARRAY_LENGTH(gNonPSSHBoxZeroSize), keyIds);
rv = ParseCENCInitData(gNonPSSHBoxZeroSize, MOZ_ARRAY_LENGTH(gNonPSSHBoxZeroSize), keyIds);
EXPECT_EQ(false, rv);
EXPECT_EQ(keyIds.size(), 0u);
keyIds.clear();
ParseCENCInitData(g2xGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(g2xGoogleWPTCencInitData), keyIds);
rv = ParseCENCInitData(g2xGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(g2xGoogleWPTCencInitData), keyIds);
EXPECT_EQ(true, rv);
EXPECT_EQ(keyIds.size(), 2u);
EXPECT_EQ(keyIds[0].size(), 16u);
EXPECT_EQ(keyIds[1].size(), 16u);
EXPECT_EQ(memcmp(&keyIds[0].front(), &g2xGoogleWPTCencInitData[32], 16), 0);
EXPECT_EQ(memcmp(&keyIds[1].front(), &g2xGoogleWPTCencInitData[84], 16), 0);
}

Просмотреть файл

@ -18,6 +18,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/Move.h"
#include <memory.h>
#include <algorithm>
#include <assert.h>
@ -112,6 +113,8 @@ ParseCENCInitData(const uint8_t* aInitData,
uint32_t aInitDataSize,
std::vector<std::vector<uint8_t>>& aOutKeyIds)
{
aOutKeyIds.clear();
std::vector<std::vector<uint8_t>> keyIds;
ByteReader reader(aInitData, aInitDataSize);
while (reader.CanRead32()) {
// Box size. For the common system Id, ignore this, as some useragents
@ -166,13 +169,14 @@ ParseCENCInitData(const uint8_t* aInitData,
}
uint32_t kidCount = reader.ReadU32();
if (kidCount * CENC_KEY_LEN > reader.Remaining()) {
// Not enough bytes remaining to read all keys.
return false;
}
for (uint32_t i = 0; i < kidCount; i++) {
if (reader.Remaining() < CENC_KEY_LEN) {
// Not enough remaining to read key.
return false;
}
const uint8_t* kid = reader.Read(CENC_KEY_LEN);
aOutKeyIds.push_back(std::vector<uint8_t>(kid, kid + CENC_KEY_LEN));
keyIds.push_back(std::vector<uint8_t>(kid, kid + CENC_KEY_LEN));
}
// Size of extra data. EME CENC format spec says datasize should
@ -188,5 +192,6 @@ ParseCENCInitData(const uint8_t* aInitData,
reader.Seek(end);
}
}
aOutKeyIds = mozilla::Move(keyIds);
return true;
}