Make Chromoting browser tests pass again
[chromium-blink-merge.git] / media / cdm / cenc_utils_unittest.cc
blobae758178370f56b03972b34cf5d27f25e51491c3
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cdm/cenc_utils.h"
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace media {
12 const uint8 kKey1Data[] = {
13 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
14 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03
16 const uint8 kKey2Data[] = {
17 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
18 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
20 const uint8 kKey3Data[] = {
21 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
22 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
24 const uint8 kKey4Data[] = {
25 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
26 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
29 class CencUtilsTest : public testing::Test {
30 public:
31 CencUtilsTest()
32 : key1_(kKey1Data, kKey1Data + arraysize(kKey1Data)),
33 key2_(kKey2Data, kKey2Data + arraysize(kKey2Data)),
34 key3_(kKey3Data, kKey3Data + arraysize(kKey3Data)),
35 key4_(kKey4Data, kKey4Data + arraysize(kKey4Data)) {}
37 protected:
38 // Initialize the start of the 'pssh' box (up to key_count)
39 void InitializePSSHBox(std::vector<uint8>* box, uint8 size, uint8 version) {
40 DCHECK(box->size() == 0);
42 box->reserve(size);
43 // Add size.
44 box->push_back(0);
45 box->push_back(0);
46 box->push_back(0);
47 box->push_back(size);
48 // Add 'pssh'.
49 box->push_back('p');
50 box->push_back('s');
51 box->push_back('s');
52 box->push_back('h');
53 // Add version.
54 box->push_back(version);
55 // Add flags.
56 box->push_back(0);
57 box->push_back(0);
58 box->push_back(0);
59 // Add Clear Key SystemID.
60 box->push_back(0x10);
61 box->push_back(0x77);
62 box->push_back(0xEF);
63 box->push_back(0xEC);
64 box->push_back(0xC0);
65 box->push_back(0xB2);
66 box->push_back(0x4D);
67 box->push_back(0x02);
68 box->push_back(0xAC);
69 box->push_back(0xE3);
70 box->push_back(0x3C);
71 box->push_back(0x1E);
72 box->push_back(0x52);
73 box->push_back(0xE2);
74 box->push_back(0xFB);
75 box->push_back(0x4B);
78 std::vector<uint8> MakePSSHBox(uint8 version) {
79 std::vector<uint8> box;
80 uint8 size = (version == 0) ? 32 : 36;
81 InitializePSSHBox(&box, size, version);
82 if (version > 0) {
83 // Add key_count (= 0).
84 box.push_back(0);
85 box.push_back(0);
86 box.push_back(0);
87 box.push_back(0);
89 // Add data_size (= 0).
90 box.push_back(0);
91 box.push_back(0);
92 box.push_back(0);
93 box.push_back(0);
94 return box;
97 std::vector<uint8> MakePSSHBox(uint8 version,
98 const std::vector<uint8>& key1) {
99 DCHECK(version > 0);
100 DCHECK(key1.size() == 16);
102 std::vector<uint8> box;
103 uint8 size = 52;
104 InitializePSSHBox(&box, size, version);
106 // Add key_count (= 1).
107 box.push_back(0);
108 box.push_back(0);
109 box.push_back(0);
110 box.push_back(1);
112 // Add key1.
113 for (int i = 0; i < 16; ++i)
114 box.push_back(key1[i]);
116 // Add data_size (= 0).
117 box.push_back(0);
118 box.push_back(0);
119 box.push_back(0);
120 box.push_back(0);
121 return box;
124 std::vector<uint8> MakePSSHBox(uint8 version,
125 const std::vector<uint8>& key1,
126 const std::vector<uint8>& key2) {
127 DCHECK(version > 0);
128 DCHECK(key1.size() == 16);
129 DCHECK(key2.size() == 16);
131 std::vector<uint8> box;
132 uint8 size = 68;
133 InitializePSSHBox(&box, size, version);
135 // Add key_count (= 2).
136 box.push_back(0);
137 box.push_back(0);
138 box.push_back(0);
139 box.push_back(2);
141 // Add key1.
142 for (int i = 0; i < 16; ++i)
143 box.push_back(key1[i]);
145 // Add key2.
146 for (int i = 0; i < 16; ++i)
147 box.push_back(key2[i]);
149 // Add data_size (= 0).
150 box.push_back(0);
151 box.push_back(0);
152 box.push_back(0);
153 box.push_back(0);
154 return box;
157 const std::vector<uint8>& Key1() { return key1_; }
158 const std::vector<uint8>& Key2() { return key2_; }
159 const std::vector<uint8>& Key3() { return key3_; }
160 const std::vector<uint8>& Key4() { return key4_; }
162 private:
163 std::vector<uint8> key1_;
164 std::vector<uint8> key2_;
165 std::vector<uint8> key3_;
166 std::vector<uint8> key4_;
169 TEST_F(CencUtilsTest, EmptyPSSH) {
170 std::vector<std::vector<uint8>> key_ids;
171 EXPECT_TRUE(GetKeyIdsForCommonSystemId(nullptr, 0, &key_ids));
172 EXPECT_EQ(0u, key_ids.size());
175 TEST_F(CencUtilsTest, PSSHVersion0) {
176 std::vector<uint8> box = MakePSSHBox(0);
177 std::vector<std::vector<uint8>> key_ids;
178 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
179 EXPECT_EQ(0u, key_ids.size());
182 TEST_F(CencUtilsTest, PSSHVersion1WithNoKeys) {
183 std::vector<uint8> box = MakePSSHBox(1);
184 std::vector<std::vector<uint8>> key_ids;
185 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
186 EXPECT_EQ(0u, key_ids.size());
189 TEST_F(CencUtilsTest, PSSHVersion1WithOneKey) {
190 std::vector<uint8> box = MakePSSHBox(1, Key1());
191 std::vector<std::vector<uint8>> key_ids;
192 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
193 EXPECT_EQ(1u, key_ids.size());
194 EXPECT_EQ(key_ids[0], Key1());
197 TEST_F(CencUtilsTest, PSSHVersion1WithTwoKeys) {
198 std::vector<uint8> box = MakePSSHBox(1, Key1(), Key2());
199 std::vector<std::vector<uint8>> key_ids;
200 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
201 EXPECT_EQ(2u, key_ids.size());
202 EXPECT_EQ(key_ids[0], Key1());
203 EXPECT_EQ(key_ids[1], Key2());
206 TEST_F(CencUtilsTest, PSSHVersion0Plus1) {
207 std::vector<uint8> box0 = MakePSSHBox(0);
208 std::vector<uint8> box1 = MakePSSHBox(1, Key1());
210 // Concatentate box1 into box0.
211 for (const auto& value : box1)
212 box0.push_back(value);
214 std::vector<std::vector<uint8>> key_ids;
215 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box0[0], box0.size(), &key_ids));
216 EXPECT_EQ(1u, key_ids.size());
217 EXPECT_EQ(key_ids[0], Key1());
220 TEST_F(CencUtilsTest, PSSHVersion1Plus0) {
221 std::vector<uint8> box0 = MakePSSHBox(0);
222 std::vector<uint8> box1 = MakePSSHBox(1, Key1());
224 // Concatentate box0 into box1.
225 for (const auto& value : box0)
226 box1.push_back(value);
228 std::vector<std::vector<uint8>> key_ids;
229 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box1[0], box1.size(), &key_ids));
230 EXPECT_EQ(1u, key_ids.size());
231 EXPECT_EQ(key_ids[0], Key1());
234 TEST_F(CencUtilsTest, MultiplePSSHVersion1) {
235 std::vector<uint8> box = MakePSSHBox(1, Key1(), Key2());
236 std::vector<uint8> box1 = MakePSSHBox(1, Key3());
237 std::vector<uint8> box2 = MakePSSHBox(1, Key4());
239 // Concatentate box1 into box.
240 for (const auto& value : box1)
241 box.push_back(value);
242 // Concatentate box2 into box.
243 for (const auto& value : box2)
244 box.push_back(value);
246 std::vector<std::vector<uint8>> key_ids;
247 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
248 EXPECT_EQ(4u, key_ids.size());
249 EXPECT_EQ(key_ids[0], Key1());
250 EXPECT_EQ(key_ids[1], Key2());
251 EXPECT_EQ(key_ids[2], Key3());
252 EXPECT_EQ(key_ids[3], Key4());
255 TEST_F(CencUtilsTest, InvalidPSSH) {
256 std::vector<uint8> box = MakePSSHBox(1, Key1(), Key2());
257 std::vector<std::vector<uint8>> key_ids;
258 for (uint32 i = 1; i < box.size(); ++i) {
259 // Modify size of data passed to be less than real size.
260 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box[0], i, &key_ids));
261 // Modify starting point.
262 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box[i], box.size() - i, &key_ids));
266 TEST_F(CencUtilsTest, InvalidSystemID) {
267 std::vector<uint8> box = MakePSSHBox(1, Key1(), Key2());
269 // Modify the System ID.
270 ++box[20];
272 std::vector<std::vector<uint8>> key_ids;
273 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
274 EXPECT_EQ(0u, key_ids.size());
277 TEST_F(CencUtilsTest, InvalidFlags) {
278 std::vector<uint8> box = MakePSSHBox(1, Key1(), Key2());
280 // Modify flags.
281 box[10] = 3;
283 std::vector<std::vector<uint8>> key_ids;
284 // TODO(jrummell): This should fail as the 'pssh' box is skipped.
285 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box[0], box.size(), &key_ids));
286 EXPECT_EQ(0u, key_ids.size());
289 TEST_F(CencUtilsTest, LongSize) {
290 const uint8 data[] = {
291 0x00, 0x00, 0x00, 0x01, // size = 1
292 0x70, 0x73, 0x73, 0x68, // 'pssh'
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize
294 0x01, // version
295 0x00, 0x00, 0x00, // flags
296 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
297 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
298 0x00, 0x00, 0x00, 0x02, // key count
299 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
300 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
301 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
302 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
303 0x00, 0x00, 0x00, 0x00 // datasize
306 std::vector<std::vector<uint8>> key_ids;
307 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data, arraysize(data), &key_ids));
308 EXPECT_EQ(2u, key_ids.size());
311 TEST_F(CencUtilsTest, NoSize) {
312 const uint8 data[] = {
313 0x00, 0x00, 0x00, 0x00, // size = 0
314 0x70, 0x73, 0x73, 0x68, // 'pssh'
315 0x01, // version
316 0x00, 0x00, 0x00, // flags
317 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
318 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
319 0x00, 0x00, 0x00, 0x02, // key count
320 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
321 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
322 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
323 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
324 0x00, 0x00, 0x00, 0x00 // datasize
327 std::vector<std::vector<uint8>> key_ids;
328 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data, arraysize(data), &key_ids));
329 EXPECT_EQ(2u, key_ids.size());
332 TEST_F(CencUtilsTest, HugeSize) {
333 const uint8 data[] = {
334 0x00, 0x00, 0x00, 0x01, // size = 1
335 0x70, 0x73, 0x73, 0x68, // 'pssh'
336 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // longsize = big
337 0x01, // version
338 0x00, 0x00, 0x00, // flags
339 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
340 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
341 0x00, 0x00, 0x00, 0x02, // key count
342 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
343 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
344 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
345 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
346 0x00, 0x00, 0x00, 0x00 // datasize
349 std::vector<std::vector<uint8>> key_ids;
350 EXPECT_FALSE(GetKeyIdsForCommonSystemId(data, arraysize(data), &key_ids));
353 } // namespace media