OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "media/webm/cluster_builder.h" |
| 6 #include "media/webm/webm_constants.h" |
| 7 #include "media/webm/webm_parser.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 using ::testing::InSequence; |
| 12 using ::testing::Return; |
| 13 using ::testing::StrictMock; |
| 14 using ::testing::_; |
| 15 |
| 16 namespace media { |
| 17 |
| 18 class MockWebMParserClient : public WebMParserClient { |
| 19 public: |
| 20 virtual ~MockWebMParserClient() {} |
| 21 |
| 22 // WebMParserClient methods. |
| 23 MOCK_METHOD1(OnListStart, bool(int)); |
| 24 MOCK_METHOD1(OnListEnd, bool(int)); |
| 25 MOCK_METHOD2(OnUInt, bool(int, int64)); |
| 26 MOCK_METHOD2(OnFloat, bool(int, double)); |
| 27 MOCK_METHOD3(OnBinary, bool(int, const uint8*, int)); |
| 28 MOCK_METHOD2(OnString, bool(int, const std::string&)); |
| 29 MOCK_METHOD5(OnSimpleBlock, bool(int, int, int, const uint8*, int)); |
| 30 }; |
| 31 |
| 32 class WebMParserTest : public testing::Test { |
| 33 protected: |
| 34 StrictMock<MockWebMParserClient> client_; |
| 35 }; |
| 36 |
| 37 struct SimpleBlockInfo { |
| 38 int track_num; |
| 39 int timestamp; |
| 40 }; |
| 41 |
| 42 static void AddSimpleBlock(ClusterBuilder* cb, int track_num, |
| 43 int64 timecode) { |
| 44 uint8 data[] = { 0x00 }; |
| 45 cb->AddSimpleBlock(track_num, timecode, 0, data, sizeof(data)); |
| 46 } |
| 47 |
| 48 static Cluster* CreateCluster(int timecode, |
| 49 const SimpleBlockInfo* block_info, |
| 50 int block_count) { |
| 51 ClusterBuilder cb; |
| 52 cb.SetClusterTimecode(0); |
| 53 |
| 54 for (int i = 0; i < block_count; i++) |
| 55 AddSimpleBlock(&cb, block_info[i].track_num, block_info[i].timestamp); |
| 56 |
| 57 return cb.Finish(); |
| 58 } |
| 59 |
| 60 static void CreateClusterExpectations(int timecode, |
| 61 const SimpleBlockInfo* block_info, |
| 62 int block_count, |
| 63 MockWebMParserClient* client) { |
| 64 |
| 65 InSequence s; |
| 66 EXPECT_CALL(*client, OnListStart(kWebMIdCluster)).WillOnce(Return(true)); |
| 67 EXPECT_CALL(*client, OnUInt(kWebMIdTimecode, 0)).WillOnce(Return(true)); |
| 68 |
| 69 for (int i = 0; i < block_count; i++) { |
| 70 EXPECT_CALL(*client, OnSimpleBlock(block_info[i].track_num, |
| 71 block_info[i].timestamp, |
| 72 _, _, _)) |
| 73 .WillOnce(Return(true)); |
| 74 } |
| 75 |
| 76 EXPECT_CALL(*client, OnListEnd(kWebMIdCluster)).WillOnce(Return(true)); |
| 77 } |
| 78 |
| 79 TEST_F(WebMParserTest, EmptyCluster) { |
| 80 const uint8 kEmptyCluster[] = { |
| 81 0x1F, 0x43, 0xB6, 0x75, 0x80 // CLUSTER (size = 0) |
| 82 }; |
| 83 int size = sizeof(kEmptyCluster); |
| 84 |
| 85 InSequence s; |
| 86 EXPECT_CALL(client_, OnListStart(kWebMIdCluster)).WillOnce(Return(true)); |
| 87 EXPECT_CALL(client_, OnListEnd(kWebMIdCluster)).WillOnce(Return(true)); |
| 88 |
| 89 WebMListParser parser(kWebMIdCluster); |
| 90 int result = parser.Parse(kEmptyCluster, size, &client_); |
| 91 EXPECT_EQ(size, result); |
| 92 EXPECT_TRUE(parser.IsParsingComplete()); |
| 93 } |
| 94 |
| 95 TEST_F(WebMParserTest, EmptyClusterInSegment) { |
| 96 const uint8 kBuffer[] = { |
| 97 0x18, 0x53, 0x80, 0x67, 0x85, // SEGMENT (size = 5) |
| 98 0x1F, 0x43, 0xB6, 0x75, 0x80, // CLUSTER (size = 0) |
| 99 }; |
| 100 int size = sizeof(kBuffer); |
| 101 |
| 102 InSequence s; |
| 103 EXPECT_CALL(client_, OnListStart(kWebMIdSegment)).WillOnce(Return(true)); |
| 104 EXPECT_CALL(client_, OnListStart(kWebMIdCluster)).WillOnce(Return(true)); |
| 105 EXPECT_CALL(client_, OnListEnd(kWebMIdCluster)).WillOnce(Return(true)); |
| 106 EXPECT_CALL(client_, OnListEnd(kWebMIdSegment)).WillOnce(Return(true)); |
| 107 |
| 108 WebMListParser parser(kWebMIdSegment); |
| 109 int result = parser.Parse(kBuffer, size, &client_); |
| 110 EXPECT_EQ(size, result); |
| 111 EXPECT_TRUE(parser.IsParsingComplete()); |
| 112 } |
| 113 |
| 114 // Test the case where a non-list child element has a size |
| 115 // that is beyond the end of the parent. |
| 116 TEST_F(WebMParserTest, ChildNonListLargerThanParent) { |
| 117 const uint8 kBuffer[] = { |
| 118 0x1F, 0x43, 0xB6, 0x75, 0x81, // CLUSTER (size = 1) |
| 119 0xE7, 0x81, 0x01, // Timecode (size=1, value=1) |
| 120 }; |
| 121 int size = sizeof(kBuffer); |
| 122 |
| 123 InSequence s; |
| 124 EXPECT_CALL(client_, OnListStart(kWebMIdCluster)).WillOnce(Return(true)); |
| 125 |
| 126 WebMListParser parser(kWebMIdCluster); |
| 127 int result = parser.Parse(kBuffer, size, &client_); |
| 128 EXPECT_EQ(-1, result); |
| 129 EXPECT_FALSE(parser.IsParsingComplete()); |
| 130 } |
| 131 |
| 132 // Test the case where a list child element has a size |
| 133 // that is beyond the end of the parent. |
| 134 TEST_F(WebMParserTest, ChildListLargerThanParent) { |
| 135 const uint8 kBuffer[] = { |
| 136 0x18, 0x53, 0x80, 0x67, 0x85, // SEGMENT (size = 5) |
| 137 0x1F, 0x43, 0xB6, 0x75, 0x81, 0x11 // CLUSTER (size = 1) |
| 138 }; |
| 139 int size = sizeof(kBuffer); |
| 140 |
| 141 InSequence s; |
| 142 EXPECT_CALL(client_, OnListStart(kWebMIdSegment)).WillOnce(Return(true)); |
| 143 |
| 144 WebMListParser parser(kWebMIdSegment); |
| 145 int result = parser.Parse(kBuffer, size, &client_); |
| 146 EXPECT_EQ(-1, result); |
| 147 EXPECT_FALSE(parser.IsParsingComplete()); |
| 148 } |
| 149 |
| 150 // Expecting to parse a Cluster, but get a Segment. |
| 151 TEST_F(WebMParserTest, ListIdDoesNotMatch) { |
| 152 const uint8 kBuffer[] = { |
| 153 0x18, 0x53, 0x80, 0x67, 0x80, // SEGMENT (size = 0) |
| 154 }; |
| 155 int size = sizeof(kBuffer); |
| 156 |
| 157 WebMListParser parser(kWebMIdCluster); |
| 158 int result = parser.Parse(kBuffer, size, &client_); |
| 159 EXPECT_EQ(-1, result); |
| 160 EXPECT_FALSE(parser.IsParsingComplete()); |
| 161 } |
| 162 |
| 163 TEST_F(WebMParserTest, InvalidElementInList) { |
| 164 const uint8 kBuffer[] = { |
| 165 0x18, 0x53, 0x80, 0x67, 0x82, // SEGMENT (size = 2) |
| 166 0xAE, 0x80, // TrackEntry (size = 0) |
| 167 }; |
| 168 int size = sizeof(kBuffer); |
| 169 |
| 170 InSequence s; |
| 171 EXPECT_CALL(client_, OnListStart(kWebMIdSegment)).WillOnce(Return(true)); |
| 172 |
| 173 WebMListParser parser(kWebMIdSegment); |
| 174 int result = parser.Parse(kBuffer, size, &client_); |
| 175 EXPECT_EQ(-1, result); |
| 176 EXPECT_FALSE(parser.IsParsingComplete()); |
| 177 } |
| 178 |
| 179 TEST_F(WebMParserTest, VoidAndCRC32InList) { |
| 180 const uint8 kBuffer[] = { |
| 181 0x18, 0x53, 0x80, 0x67, 0x99, // SEGMENT (size = 25) |
| 182 0xEC, 0x83, 0x00, 0x00, 0x00, // Void (size = 3) |
| 183 0xBF, 0x83, 0x00, 0x00, 0x00, // CRC32 (size = 3) |
| 184 0x1F, 0x43, 0xB6, 0x75, 0x8A, // CLUSTER (size = 10) |
| 185 0xEC, 0x83, 0x00, 0x00, 0x00, // Void (size = 3) |
| 186 0xBF, 0x83, 0x00, 0x00, 0x00, // CRC32 (size = 3) |
| 187 }; |
| 188 int size = sizeof(kBuffer); |
| 189 |
| 190 InSequence s; |
| 191 EXPECT_CALL(client_, OnListStart(kWebMIdSegment)).WillOnce(Return(true)); |
| 192 EXPECT_CALL(client_, OnListStart(kWebMIdCluster)).WillOnce(Return(true)); |
| 193 EXPECT_CALL(client_, OnListEnd(kWebMIdCluster)).WillOnce(Return(true)); |
| 194 EXPECT_CALL(client_, OnListEnd(kWebMIdSegment)).WillOnce(Return(true)); |
| 195 |
| 196 WebMListParser parser(kWebMIdSegment); |
| 197 int result = parser.Parse(kBuffer, size, &client_); |
| 198 EXPECT_EQ(size, result); |
| 199 EXPECT_TRUE(parser.IsParsingComplete()); |
| 200 } |
| 201 |
| 202 |
| 203 TEST_F(WebMParserTest, ParseListElementWithSingleCall) { |
| 204 const SimpleBlockInfo kBlockInfo[] = { |
| 205 { 0, 1 }, |
| 206 { 1, 2 }, |
| 207 { 0, 3 }, |
| 208 { 0, 4 }, |
| 209 { 1, 4 }, |
| 210 }; |
| 211 int block_count = arraysize(kBlockInfo); |
| 212 |
| 213 scoped_ptr<Cluster> cluster(CreateCluster(0, kBlockInfo, block_count)); |
| 214 CreateClusterExpectations(0, kBlockInfo, block_count, &client_); |
| 215 |
| 216 WebMListParser parser(kWebMIdCluster); |
| 217 int result = parser.Parse(cluster->data(), cluster->size(), &client_); |
| 218 EXPECT_EQ(cluster->size(), result); |
| 219 EXPECT_TRUE(parser.IsParsingComplete()); |
| 220 } |
| 221 |
| 222 TEST_F(WebMParserTest, ParseListElementWithMultipleCalls) { |
| 223 const SimpleBlockInfo kBlockInfo[] = { |
| 224 { 0, 1 }, |
| 225 { 1, 2 }, |
| 226 { 0, 3 }, |
| 227 { 0, 4 }, |
| 228 { 1, 4 }, |
| 229 }; |
| 230 int block_count = arraysize(kBlockInfo); |
| 231 |
| 232 scoped_ptr<Cluster> cluster(CreateCluster(0, kBlockInfo, block_count)); |
| 233 CreateClusterExpectations(0, kBlockInfo, block_count, &client_); |
| 234 |
| 235 const uint8* data = cluster->data(); |
| 236 int size = cluster->size(); |
| 237 int default_parse_size = 3; |
| 238 WebMListParser parser(kWebMIdCluster); |
| 239 int parse_size = std::min(default_parse_size, size); |
| 240 |
| 241 while (size > 0) { |
| 242 int result = parser.Parse(data, parse_size, &client_); |
| 243 EXPECT_GE(result, 0); |
| 244 EXPECT_LE(result, parse_size); |
| 245 |
| 246 if (result == 0) { |
| 247 // The parser needs more data so increase the parse_size a little. |
| 248 EXPECT_FALSE(parser.IsParsingComplete()); |
| 249 parse_size += default_parse_size; |
| 250 parse_size = std::min(parse_size, size); |
| 251 continue; |
| 252 } |
| 253 |
| 254 parse_size = default_parse_size; |
| 255 |
| 256 data += result; |
| 257 size -= result; |
| 258 |
| 259 EXPECT_EQ((size == 0), parser.IsParsingComplete()); |
| 260 } |
| 261 EXPECT_TRUE(parser.IsParsingComplete()); |
| 262 } |
| 263 |
| 264 } // namespace media |
OLD | NEW |