Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / webm / webm_content_encodings_client_unittest.cc
blob22049ebfce74ec0bf1faaa84132342de180a3a7f
1 // Copyright 2014 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 "base/bind.h"
6 #include "media/formats/webm/webm_constants.h"
7 #include "media/formats/webm/webm_content_encodings_client.h"
8 #include "media/formats/webm/webm_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace media {
13 class WebMContentEncodingsClientTest : public testing::Test {
14 public:
15 WebMContentEncodingsClientTest()
16 : client_(new MediaLog()), parser_(kWebMIdContentEncodings, &client_) {}
18 void ParseAndExpectToFail(const uint8* buf, int size) {
19 int result = parser_.Parse(buf, size);
20 EXPECT_EQ(-1, result);
23 protected:
24 WebMContentEncodingsClient client_;
25 WebMListParser parser_;
28 TEST_F(WebMContentEncodingsClientTest, EmptyContentEncodings) {
29 const uint8 kContentEncodings[] = {
30 0x6D, 0x80, 0x80, // ContentEncodings (size = 0)
32 int size = sizeof(kContentEncodings);
33 ParseAndExpectToFail(kContentEncodings, size);
36 TEST_F(WebMContentEncodingsClientTest, EmptyContentEncoding) {
37 const uint8 kContentEncodings[] = {
38 0x6D, 0x80, 0x83, // ContentEncodings (size = 3)
39 0x63, 0x40, 0x80, // ContentEncoding (size = 0)
41 int size = sizeof(kContentEncodings);
42 ParseAndExpectToFail(kContentEncodings, size);
45 TEST_F(WebMContentEncodingsClientTest, SingleContentEncoding) {
46 const uint8 kContentEncodings[] = {
47 0x6D, 0x80, 0xA1, // ContentEncodings (size = 33)
48 0x62, 0x40, 0x9e, // ContentEncoding (size = 30)
49 0x50, 0x31, 0x81, 0x00, // ContentEncodingOrder (size = 1)
50 0x50, 0x32, 0x81, 0x01, // ContentEncodingScope (size = 1)
51 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
52 0x50, 0x35, 0x8F, // ContentEncryption (size = 15)
53 0x47, 0xE1, 0x81, 0x05, // ContentEncAlgo (size = 1)
54 0x47, 0xE2, 0x88, // ContentEncKeyID (size = 8)
55 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
57 int size = sizeof(kContentEncodings);
59 int result = parser_.Parse(kContentEncodings, size);
60 ASSERT_EQ(size, result);
61 const ContentEncodings& content_encodings = client_.content_encodings();
63 ASSERT_EQ(1u, content_encodings.size());
64 ASSERT_TRUE(content_encodings[0]);
65 EXPECT_EQ(0, content_encodings[0]->order());
66 EXPECT_EQ(ContentEncoding::kScopeAllFrameContents,
67 content_encodings[0]->scope());
68 EXPECT_EQ(ContentEncoding::kTypeEncryption, content_encodings[0]->type());
69 EXPECT_EQ(ContentEncoding::kEncAlgoAes,
70 content_encodings[0]->encryption_algo());
71 EXPECT_EQ(8u, content_encodings[0]->encryption_key_id().size());
74 TEST_F(WebMContentEncodingsClientTest, MultipleContentEncoding) {
75 const uint8 kContentEncodings[] = {
76 0x6D, 0x80, 0xC2, // ContentEncodings (size = 66)
77 0x62, 0x40, 0x9e, // ContentEncoding (size = 30)
78 0x50, 0x31, 0x81, 0x00, // ContentEncodingOrder (size = 1)
79 0x50, 0x32, 0x81, 0x03, // ContentEncodingScope (size = 1)
80 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
81 0x50, 0x35, 0x8F, // ContentEncryption (size = 15)
82 0x47, 0xE1, 0x81, 0x05, // ContentEncAlgo (size = 1)
83 0x47, 0xE2, 0x88, // ContentEncKeyID (size = 8)
84 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
85 0x62, 0x40, 0x9e, // ContentEncoding (size = 30)
86 0x50, 0x31, 0x81, 0x01, // ContentEncodingOrder (size = 1)
87 0x50, 0x32, 0x81, 0x03, // ContentEncodingScope (size = 1)
88 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
89 0x50, 0x35, 0x8F, // ContentEncryption (size = 15)
90 0x47, 0xE1, 0x81, 0x01, // ContentEncAlgo (size = 1)
91 0x47, 0xE2, 0x88, // ContentEncKeyID (size = 8)
92 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
94 int size = sizeof(kContentEncodings);
96 int result = parser_.Parse(kContentEncodings, size);
97 ASSERT_EQ(size, result);
98 const ContentEncodings& content_encodings = client_.content_encodings();
99 ASSERT_EQ(2u, content_encodings.size());
101 for (int i = 0; i < 2; ++i) {
102 ASSERT_TRUE(content_encodings[i]);
103 EXPECT_EQ(i, content_encodings[i]->order());
104 EXPECT_EQ(ContentEncoding::kScopeAllFrameContents |
105 ContentEncoding::kScopeTrackPrivateData,
106 content_encodings[i]->scope());
107 EXPECT_EQ(ContentEncoding::kTypeEncryption, content_encodings[i]->type());
108 EXPECT_EQ(!i ? ContentEncoding::kEncAlgoAes : ContentEncoding::kEncAlgoDes,
109 content_encodings[i]->encryption_algo());
110 EXPECT_EQ(8u, content_encodings[i]->encryption_key_id().size());
114 TEST_F(WebMContentEncodingsClientTest, DefaultValues) {
115 const uint8 kContentEncodings[] = {
116 0x6D, 0x80, 0x8A, // ContentEncodings (size = 10)
117 0x62, 0x40, 0x87, // ContentEncoding (size = 7)
118 // ContentEncodingOrder missing
119 // ContentEncodingScope missing
120 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
121 0x50, 0x35, 0x80, // ContentEncryption (size = 0)
122 // ContentEncAlgo missing
124 int size = sizeof(kContentEncodings);
126 int result = parser_.Parse(kContentEncodings, size);
127 ASSERT_EQ(size, result);
128 const ContentEncodings& content_encodings = client_.content_encodings();
130 ASSERT_EQ(1u, content_encodings.size());
131 ASSERT_TRUE(content_encodings[0]);
132 EXPECT_EQ(0, content_encodings[0]->order());
133 EXPECT_EQ(ContentEncoding::kScopeAllFrameContents,
134 content_encodings[0]->scope());
135 EXPECT_EQ(ContentEncoding::kTypeEncryption, content_encodings[0]->type());
136 EXPECT_EQ(ContentEncoding::kEncAlgoNotEncrypted,
137 content_encodings[0]->encryption_algo());
138 EXPECT_TRUE(content_encodings[0]->encryption_key_id().empty());
141 TEST_F(WebMContentEncodingsClientTest, ContentEncodingsClientReuse) {
142 const uint8 kContentEncodings[] = {
143 0x6D, 0x80, 0xA1, // ContentEncodings (size = 33)
144 0x62, 0x40, 0x9e, // ContentEncoding (size = 30)
145 0x50, 0x31, 0x81, 0x00, // ContentEncodingOrder (size = 1)
146 0x50, 0x32, 0x81, 0x01, // ContentEncodingScope (size = 1)
147 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
148 0x50, 0x35, 0x8F, // ContentEncryption (size = 15)
149 0x47, 0xE1, 0x81, 0x05, // ContentEncAlgo (size = 1)
150 0x47, 0xE2, 0x88, // ContentEncKeyID (size = 8)
151 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
153 int size = sizeof(kContentEncodings);
155 // Parse for the first time.
156 int result = parser_.Parse(kContentEncodings, size);
157 ASSERT_EQ(size, result);
159 // Parse again.
160 parser_.Reset();
161 result = parser_.Parse(kContentEncodings, size);
162 ASSERT_EQ(size, result);
163 const ContentEncodings& content_encodings = client_.content_encodings();
165 ASSERT_EQ(1u, content_encodings.size());
166 ASSERT_TRUE(content_encodings[0]);
167 EXPECT_EQ(0, content_encodings[0]->order());
168 EXPECT_EQ(ContentEncoding::kScopeAllFrameContents,
169 content_encodings[0]->scope());
170 EXPECT_EQ(ContentEncoding::kTypeEncryption, content_encodings[0]->type());
171 EXPECT_EQ(ContentEncoding::kEncAlgoAes,
172 content_encodings[0]->encryption_algo());
173 EXPECT_EQ(8u, content_encodings[0]->encryption_key_id().size());
176 TEST_F(WebMContentEncodingsClientTest, InvalidContentEncodingOrder) {
177 const uint8 kContentEncodings[] = {
178 0x6D, 0x80, 0x8E, // ContentEncodings (size = 14)
179 0x62, 0x40, 0x8B, // ContentEncoding (size = 11)
180 0x50, 0x31, 0x81, 0xEE, // ContentEncodingOrder (size = 1), invalid
181 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
182 0x50, 0x35, 0x80, // ContentEncryption (size = 0)
184 int size = sizeof(kContentEncodings);
185 ParseAndExpectToFail(kContentEncodings, size);
188 TEST_F(WebMContentEncodingsClientTest, InvalidContentEncodingScope) {
189 const uint8 kContentEncodings[] = {
190 0x6D, 0x80, 0x8E, // ContentEncodings (size = 14)
191 0x62, 0x40, 0x8B, // ContentEncoding (size = 11)
192 0x50, 0x32, 0x81, 0xEE, // ContentEncodingScope (size = 1), invalid
193 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
194 0x50, 0x35, 0x80, // ContentEncryption (size = 0)
196 int size = sizeof(kContentEncodings);
197 ParseAndExpectToFail(kContentEncodings, size);
200 TEST_F(WebMContentEncodingsClientTest, InvalidContentEncodingType) {
201 const uint8 kContentEncodings[] = {
202 0x6D, 0x80, 0x8E, // ContentEncodings (size = 14)
203 0x62, 0x40, 0x8B, // ContentEncoding (size = 11)
204 0x50, 0x33, 0x81, 0x00, // ContentEncodingType (size = 1), invalid
205 0x50, 0x35, 0x80, // ContentEncryption (size = 0)
207 int size = sizeof(kContentEncodings);
208 ParseAndExpectToFail(kContentEncodings, size);
211 // ContentEncodingType is encryption but no ContentEncryption present.
212 TEST_F(WebMContentEncodingsClientTest, MissingContentEncryption) {
213 const uint8 kContentEncodings[] = {
214 0x6D, 0x80, 0x87, // ContentEncodings (size = 7)
215 0x62, 0x40, 0x84, // ContentEncoding (size = 4)
216 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
217 // ContentEncryption missing
219 int size = sizeof(kContentEncodings);
220 ParseAndExpectToFail(kContentEncodings, size);
223 TEST_F(WebMContentEncodingsClientTest, InvalidContentEncAlgo) {
224 const uint8 kContentEncodings[] = {
225 0x6D, 0x80, 0x99, // ContentEncodings (size = 25)
226 0x62, 0x40, 0x96, // ContentEncoding (size = 22)
227 0x50, 0x33, 0x81, 0x01, // ContentEncodingType (size = 1)
228 0x50, 0x35, 0x8F, // ContentEncryption (size = 15)
229 0x47, 0xE1, 0x81, 0xEE, // ContentEncAlgo (size = 1), invalid
230 0x47, 0xE2, 0x88, // ContentEncKeyID (size = 8)
231 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
233 int size = sizeof(kContentEncodings);
234 ParseAndExpectToFail(kContentEncodings, size);
237 } // namespace media