summaryrefslogtreecommitdiff
path: root/media/psshparser/gtest/TestPsshParser.cpp
blob: d504909057e2d4b88197c2cbfbcafa4200eaed29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include <algorithm>
#include <stdint.h>
#include <vector>

#include "psshparser/PsshParser.h"
#include "mozilla/ArrayUtils.h"

using namespace std;

// This is the CENC initData from Google's web-platform tests.
// https://github.com/w3c/web-platform-tests/blob/master/encrypted-media/Google/encrypted-media-utils.js#L50
const uint8_t gGoogleWPTCencInitData[] = {
  0x00, 0x00, 0x00, 0x00,                          // size = 0
  0x70, 0x73, 0x73, 0x68,                          // 'pssh'
  0x01,                                            // version = 1
  0x00, 0x00, 0x00,                                // flags
  0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,  // Common SystemID
  0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
  0x00, 0x00, 0x00, 0x01,                          // key count
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,  // key
  0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  0x00, 0x00, 0x00, 0x00                           // datasize
};

// 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, 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,
  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)
};

// Invalid box size, would overflow if used.
const uint8_t gOverflowBoxSize[] = {
  0xff, 0xff, 0xff, 0xff,                          // size = UINT32_MAX
};

// 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
  0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,  // Common SystemID
  0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
  0xff, 0xff, 0xff, 0xff,                          // key count = UINT32_MAX
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  // key
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff                           // datasize
};

// 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
  0x00, 0x00, 0x00,                                // flags
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  // Invalid SystemID
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  // Some data to pad out the box.
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};

const uint8_t gNonPSSHBoxZeroSize[] = {
  0x00, 0x00, 0x00, 0x00,                          // size = 0
  0xff, 0xff, 0xff, 0xff,                          // something other than 'pssh'
};

// Two lots of the google init data. To ensure we handle
// multiple boxes with size 0.
const uint8_t g2xGoogleWPTCencInitData[] = {
  0x00, 0x00, 0x00, 0x00,                          // size = 0
  0x70, 0x73, 0x73, 0x68,                          // 'pssh'
  0x01,                                            // version = 1
  0x00, 0x00, 0x00,                                // flags
  0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,  // Common SystemID
  0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
  0x00, 0x00, 0x00, 0x01,                          // key count
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,  // key
  0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  0x00, 0x00, 0x00, 0x00,                          // datasize

  0x00, 0x00, 0x00, 0x00,                          // size = 0
  0x70, 0x73, 0x73, 0x68,                          // 'pssh'
  0x01,                                            // version = 1
  0x00, 0x00, 0x00,                                // flags
  0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,  // Common SystemID
  0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
  0x00, 0x00, 0x00, 0x01,                          // key count
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,  // key
  0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  0x00, 0x00, 0x00, 0x00                           // datasize
};

TEST(PsshParser, ParseCencInitData) {
  std::vector<std::vector<uint8_t>> keyIds;
  bool rv;

  rv = ParseCENCInitData(gGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(gGoogleWPTCencInitData), keyIds);
  EXPECT_TRUE(rv);
  EXPECT_EQ(1u, keyIds.size());
  EXPECT_EQ(16u, keyIds[0].size());
  EXPECT_EQ(0, memcmp(&keyIds[0].front(), &gGoogleWPTCencInitData[32], 16));

  rv = ParseCENCInitData(gW3SpecExampleCencInitData, MOZ_ARRAY_LENGTH(gW3SpecExampleCencInitData), keyIds);
  EXPECT_TRUE(rv);
  EXPECT_EQ(2u, keyIds.size());
  EXPECT_EQ(16u, keyIds[0].size());
  EXPECT_EQ(0, memcmp(&keyIds[0].front(), &gW3SpecExampleCencInitData[32], 16));
  EXPECT_EQ(0, memcmp(&keyIds[1].front(), &gW3SpecExampleCencInitData[48], 16));

  rv = ParseCENCInitData(gOverflowBoxSize, MOZ_ARRAY_LENGTH(gOverflowBoxSize), keyIds);
  EXPECT_FALSE(rv);
  EXPECT_EQ(0u, keyIds.size());

  rv = ParseCENCInitData(gTooLargeKeyCountInitData, MOZ_ARRAY_LENGTH(gTooLargeKeyCountInitData), keyIds);
  EXPECT_FALSE(rv);
  EXPECT_EQ(0u, keyIds.size());

  rv = ParseCENCInitData(gNonCencInitData, MOZ_ARRAY_LENGTH(gNonCencInitData), keyIds);
  EXPECT_TRUE(rv);
  EXPECT_EQ(0u, keyIds.size());

  rv = ParseCENCInitData(gNonPSSHBoxZeroSize, MOZ_ARRAY_LENGTH(gNonPSSHBoxZeroSize), keyIds);
  EXPECT_FALSE(rv);
  EXPECT_EQ(0u, keyIds.size());

  rv = ParseCENCInitData(g2xGoogleWPTCencInitData, MOZ_ARRAY_LENGTH(g2xGoogleWPTCencInitData), keyIds);
  EXPECT_TRUE(rv);
  EXPECT_EQ(2u, keyIds.size());
  EXPECT_EQ(16u, keyIds[0].size());
  EXPECT_EQ(16u, keyIds[1].size());
  EXPECT_EQ(0, memcmp(&keyIds[0].front(), &g2xGoogleWPTCencInitData[32], 16));
  EXPECT_EQ(0, memcmp(&keyIds[1].front(), &g2xGoogleWPTCencInitData[84], 16));
}