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