Bug 1870246 - Part 1: Update test262. r=spidermonkey-reviewers,mgaudet
[gecko.git] / media / psshparser / gtest / TestPsshParser.cpp
blob2027090a1fb20f811649737acdd24bf2131d97f9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "gtest/gtest.h"
8 #include <algorithm>
9 #include <stdint.h>
10 #include <vector>
12 #include "psshparser/PsshParser.h"
13 #include "mozilla/ArrayUtils.h"
15 using namespace std;
17 // This is the CENC initData from Google's web-platform tests.
18 // https://github.com/w3c/web-platform-tests/blob/master/encrypted-media/Google/encrypted-media-utils.js#L50
19 const uint8_t gGoogleWPTCencInitData[] = {
20 // clang-format off
21 0x00, 0x00, 0x00, 0x00, // size = 0
22 0x70, 0x73, 0x73, 0x68, // 'pssh'
23 0x01, // version = 1
24 0x00, 0x00, 0x00, // flags
25 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID
26 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
27 0x00, 0x00, 0x00, 0x01, // key count
28 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // key
29 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
30 0x00, 0x00, 0x00, 0x00 // datasize
31 // clang-format on
34 // Example CENC initData from the EME spec format registry:
35 // https://w3c.github.io/encrypted-media/format-registry/initdata/cenc.html
36 const uint8_t gW3SpecExampleCencInitData[] = {
37 // clang-format off
38 0x00, 0x00, 0x00, 0x44, 0x70, 0x73, 0x73, 0x68, // BMFF box header (68 bytes, 'pssh')
39 0x01, 0x00, 0x00, 0x00, // Full box header (version = 1, flags = 0)
40 0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, // SystemID
41 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b,
42 0x00, 0x00, 0x00, 0x02, // KID_count (2)
43 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // First KID ("0123456789012345")
44 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
45 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // Second KID ("ABCDEFGHIJKLMNOP")
46 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
47 0x00, 0x00, 0x00, 0x00 // Size of Data (0)
48 // clang-format on
51 // Invalid box size, would overflow if used.
52 const uint8_t gOverflowBoxSize[] = {
53 0xff, 0xff, 0xff, 0xff, // size = UINT32_MAX
56 // Valid box size, but key count too large.
57 const uint8_t gTooLargeKeyCountInitData[] = {
58 // clang-format off
59 0x00, 0x00, 0x00, 0x34, // size = too big a number
60 0x70, 0x73, 0x73, 0x68, // 'pssh'
61 0x01, // version = 1
62 0xff, 0xff, 0xff, // flags
63 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID
64 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
65 0xff, 0xff, 0xff, 0xff, // key count = UINT32_MAX
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // key
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff // datasize
69 // clang-format on
72 // Non common SystemID PSSH.
73 // No keys Ids can be extracted, but don't consider the box invalid.
74 const uint8_t gNonCencInitData[] = {
75 // clang-format off
76 0x00, 0x00, 0x00, 0x5c, // size = 92
77 0x70, 0x73, 0x73, 0x68, // 'pssh'
78 0x01, // version = 1
79 0x00, 0x00, 0x00, // flags
80 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Invalid SystemID
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Some data to pad out the box.
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 // clang-format on
93 const uint8_t gNonPSSHBoxZeroSize[] = {
94 // clang-format off
95 0x00, 0x00, 0x00, 0x00, // size = 0
96 0xff, 0xff, 0xff, 0xff, // something other than 'pssh'
97 // clang-format on
100 // Two lots of the google init data. To ensure we handle
101 // multiple boxes with size 0.
102 const uint8_t g2xGoogleWPTCencInitData[] = {
103 // clang-format off
104 0x00, 0x00, 0x00, 0x00, // size = 0
105 0x70, 0x73, 0x73, 0x68, // 'pssh'
106 0x01, // version = 1
107 0x00, 0x00, 0x00, // flags
108 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID
109 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
110 0x00, 0x00, 0x00, 0x01, // key count
111 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // key
112 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
113 0x00, 0x00, 0x00, 0x00, // datasize
115 0x00, 0x00, 0x00, 0x00, // size = 0
116 0x70, 0x73, 0x73, 0x68, // 'pssh'
117 0x01, // version = 1
118 0x00, 0x00, 0x00, // flags
119 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID
120 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
121 0x00, 0x00, 0x00, 0x01, // key count
122 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // key
123 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
124 0x00, 0x00, 0x00, 0x00 // datasize
125 // clang-format on
128 TEST(PsshParser, ParseCencInitData)
130 std::vector<std::vector<uint8_t>> keyIds;
131 bool rv;
133 rv = ParseCENCInitData(gGoogleWPTCencInitData,
134 MOZ_ARRAY_LENGTH(gGoogleWPTCencInitData), keyIds);
135 EXPECT_TRUE(rv);
136 EXPECT_EQ(1u, keyIds.size());
137 EXPECT_EQ(16u, keyIds[0].size());
138 EXPECT_EQ(0, memcmp(&keyIds[0].front(), &gGoogleWPTCencInitData[32], 16));
140 rv = ParseCENCInitData(gW3SpecExampleCencInitData,
141 MOZ_ARRAY_LENGTH(gW3SpecExampleCencInitData), keyIds);
142 EXPECT_TRUE(rv);
143 EXPECT_EQ(2u, keyIds.size());
144 EXPECT_EQ(16u, keyIds[0].size());
145 EXPECT_EQ(0, memcmp(&keyIds[0].front(), &gW3SpecExampleCencInitData[32], 16));
146 EXPECT_EQ(0, memcmp(&keyIds[1].front(), &gW3SpecExampleCencInitData[48], 16));
148 rv = ParseCENCInitData(gOverflowBoxSize, MOZ_ARRAY_LENGTH(gOverflowBoxSize),
149 keyIds);
150 EXPECT_FALSE(rv);
151 EXPECT_EQ(0u, keyIds.size());
153 rv = ParseCENCInitData(gTooLargeKeyCountInitData,
154 MOZ_ARRAY_LENGTH(gTooLargeKeyCountInitData), keyIds);
155 EXPECT_FALSE(rv);
156 EXPECT_EQ(0u, keyIds.size());
158 rv = ParseCENCInitData(gNonCencInitData, MOZ_ARRAY_LENGTH(gNonCencInitData),
159 keyIds);
160 EXPECT_TRUE(rv);
161 EXPECT_EQ(0u, keyIds.size());
163 rv = ParseCENCInitData(gNonPSSHBoxZeroSize,
164 MOZ_ARRAY_LENGTH(gNonPSSHBoxZeroSize), keyIds);
165 EXPECT_FALSE(rv);
166 EXPECT_EQ(0u, keyIds.size());
168 rv = ParseCENCInitData(g2xGoogleWPTCencInitData,
169 MOZ_ARRAY_LENGTH(g2xGoogleWPTCencInitData), keyIds);
170 EXPECT_TRUE(rv);
171 EXPECT_EQ(2u, keyIds.size());
172 EXPECT_EQ(16u, keyIds[0].size());
173 EXPECT_EQ(16u, keyIds[1].size());
174 EXPECT_EQ(0, memcmp(&keyIds[0].front(), &g2xGoogleWPTCencInitData[32], 16));
175 EXPECT_EQ(0, memcmp(&keyIds[1].front(), &g2xGoogleWPTCencInitData[84], 16));