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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include "src/master_parser.h"
#include <cstdint>
#include <memory>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "src/byte_parser.h"
#include "test_utils/element_parser_test.h"
#include "webm/element.h"
#include "webm/id.h"
#include "webm/status.h"
using testing::_;
using testing::DoAll;
using testing::InSequence;
using testing::NotNull;
using testing::Return;
using testing::SetArgPointee;
using webm::Action;
using webm::BinaryParser;
using webm::ElementMetadata;
using webm::ElementParser;
using webm::ElementParserTest;
using webm::Id;
using webm::kUnknownElementSize;
using webm::LimitedReader;
using webm::MasterParser;
using webm::Status;
namespace {
// Simple helper method that just takes an Id and ElementParser* and returns
// them in a std::pair<Id, std::unique_ptr<ElementParser>>. Provided just for
// simplifying some statements.
std::pair<Id, std::unique_ptr<ElementParser>> ParserForId(
Id id, ElementParser* parser) {
return {id, std::unique_ptr<ElementParser>(parser)};
}
class MasterParserTest : public ElementParserTest<MasterParser> {};
// Errors parsing an ID should be returned to the caller.
TEST_F(MasterParserTest, BadId) {
SetReaderData({
0x00, // Invalid ID.
0x80, // ID = 0x80 (unknown).
0x80, // Size = 0.
});
EXPECT_CALL(callback_, OnElementBegin(_, _)).Times(0);
ParseAndExpectResult(Status::kInvalidElementId);
}
// Errors from a child parser's Init should be returned to the caller.
TEST_F(MasterParserTest, ChildInitFails) {
SetReaderData({
0xEC, // ID = 0xEC (Void).
0xFF, // Size = unknown.
});
const ElementMetadata metadata = {Id::kVoid, 2, kUnknownElementSize, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
ParseAndExpectResult(Status::kInvalidElementSize);
}
// Indefinite unknown children should result in an error.
TEST_F(MasterParserTest, IndefiniteUnknownChild) {
SetReaderData({
0x80, // ID = 0x80 (unknown).
0xFF, // Size = unknown.
0x00, 0x00, // Body.
});
EXPECT_CALL(callback_, OnElementBegin(_, _)).Times(0);
ParseAndExpectResult(Status::kIndefiniteUnknownElement);
}
// Child elements that overflow the master element's size should be detected.
TEST_F(MasterParserTest, ChildOverflow) {
SetReaderData({
0xEC, // ID = 0xEC (Void).
0x82, // Size = 2.
0x00, 0x00, // Body.
});
EXPECT_CALL(callback_, OnElementBegin(_, _)).Times(0);
EXPECT_CALL(callback_, OnVoid(_, _, _)).Times(0);
ParseAndExpectResult(Status::kElementOverflow, reader_.size() - 1);
}
// Child elements with an unknown size can't be naively checked to see if they
// overflow the master element's size. Make sure the overflow is still detected.
TEST_F(MasterParserTest, ChildOverflowWithUnknownSize) {
SetReaderData({
0xA1, // ID = 0xA1 (Block) (master).
0xFF, // Size = unknown.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x12, // Body.
});
{
InSequence dummy;
ElementMetadata metadata = {Id::kBlock, 2, kUnknownElementSize, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
}
ResetParser(ParserForId(Id::kBlock, new MasterParser));
ParseAndExpectResult(Status::kElementOverflow, 4);
}
// An element with an unknown size should be terminated by its parents bounds.
TEST_F(MasterParserTest, ChildWithUnknownSizeBoundedByParentSize) {
SetReaderData({
0xA1, // ID = 0xA1 (Block) (master).
0xFF, // Size = unknown.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x12, // Body.
0x00, // Invalid ID. This should not be read.
});
{
InSequence dummy;
ElementMetadata metadata = {Id::kBlock, 2, kUnknownElementSize, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
metadata = {Id::kVoid, 2, 1, 2};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
}
ResetParser(ParserForId(Id::kBlock, new MasterParser));
ParseAndVerify(reader_.size() - 1);
}
// An empty master element is okay.
TEST_F(MasterParserTest, Empty) {
EXPECT_CALL(callback_, OnElementBegin(_, _)).Times(0);
ParseAndVerify();
}
TEST_F(MasterParserTest, DefaultActionIsRead) {
SetReaderData({
0xEC, // ID = 0xEC (Void).
0x80, // Size = 0.
});
{
InSequence dummy;
const ElementMetadata metadata = {Id::kVoid, 2, 0, 0};
// This intentionally does not set the action and relies on the parser using
// a default action value of kRead.
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull()))
.WillOnce(Return(Status(Status::kOkCompleted)));
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
}
ParseAndVerify();
}
// Unrecognized children should be skipped over.
TEST_F(MasterParserTest, UnknownChildren) {
SetReaderData({
0x40, 0x00, // ID = 0x4000 (unknown).
0x80, // Size = 0.
0x80, // ID = 0x80 (unknown).
0x40, 0x00, // Size = 0.
});
EXPECT_CALL(callback_, OnVoid(_, _, _)).Times(0);
{
InSequence dummy;
ElementMetadata metadata = {static_cast<Id>(0x4000), 3, 0, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
metadata = {static_cast<Id>(0x80), 3, 0, 3};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
}
ParseAndVerify();
}
// A master element with unknown size is terminated by the first element that is
// not a valid child.
TEST_F(MasterParserTest, UnknownSize) {
SetReaderData({
// Void elements may appear anywhere in a master element and should not
// terminate the parse for a master element with an unknown size. In other
// words, they're always valid children.
0xEC, // ID = 0xEC (Void).
0x81, // Size = 1.
0x00, // Body.
// This element marks the end for the parser since this is the first
// unrecognized element. The ID and size should be read (which the parser
// uses to determine the end has been reached), but nothing more.
0x80, // ID = 0x80 (unknown).
0x81, // Size = 1.
0x12, // Body.
});
{
InSequence dummy;
const ElementMetadata metadata = {Id::kVoid, 2, 1, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
}
ParseAndVerify(kUnknownElementSize);
EXPECT_EQ(static_cast<std::uint64_t>(5), reader_.Position());
}
// Consecutive elements with unknown size should parse without issues, despite
// the internal parsers having to read ahead into the next (non-child) element.
TEST_F(MasterParserTest, MultipleUnknownChildSize) {
SetReaderData({
0xA1, // ID = 0xA1 (Block) (master).
0xFF, // Size = unknown.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x12, // Body.
0xA1, // ID = 0xA1 (Block) (master).
0xFF, // Size = unknown.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x13, // Body.
});
{
InSequence dummy;
ElementMetadata metadata = {Id::kBlock, 2, kUnknownElementSize, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
metadata = {Id::kVoid, 2, 1, 2};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
metadata = {Id::kBlock, 2, kUnknownElementSize, 5};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
metadata = {Id::kVoid, 2, 1, 7};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
}
ResetParser(ParserForId(Id::kBlock, new MasterParser));
ParseAndVerify();
}
// Reaching the end of the file while reading an element with unknown size
// should return Status::kOkCompleted instead of Status::kEndOfFile.
TEST_F(MasterParserTest, UnknownSizeToFileEnd) {
SetReaderData({
0xEC, // ID = 0xEC (Void).
0x81, // Size = 1.
0x00, // Body.
});
{
InSequence dummy;
const ElementMetadata metadata = {Id::kVoid, 2, 1, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(1);
}
ParseAndVerify();
}
// Parsing one byte at a time is okay.
TEST_F(MasterParserTest, IncrementalParse) {
SetReaderData({
0x1A, 0x45, 0xDF, 0xA3, // ID = 0x1A45DFA3 (EBML).
0x08, 0x00, 0x00, 0x00, 0x06, // Size = 6.
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // Body.
});
const ElementMetadata metadata = {Id::kEbml, 9, 6, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
BinaryParser* binary_parser = new BinaryParser;
ResetParser(ParserForId(Id::kEbml, binary_parser));
IncrementalParseAndVerify();
std::vector<std::uint8_t> expected = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
EXPECT_EQ(expected, binary_parser->value());
}
// Alternating actions between skip and read is okay. The parser should remember
// the requested action between repeated calls to Feed.
TEST_F(MasterParserTest, IncrementalSkipThenReadThenSkip) {
SetReaderData({
0xA1, // ID = 0xA1 (Block) (master).
0x83, // Size = 3.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x12, // Body.
0xA1, // ID = 0xA1 (Block) (master).
0x83, // Size = 3.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x13, // Body.
0xA1, // ID = 0xA1 (Block) (master).
0xFF, // Size = unknown.
0xEC, // ID = 0xEC (Void) (child).
0x81, // Size = 1.
0x14, // Body.
});
{
InSequence dummy;
ElementMetadata metadata = {Id::kBlock, 2, 3, 0};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull()))
.WillOnce(Return(Status(Status::kOkPartial)))
.WillOnce(DoAll(SetArgPointee<1>(Action::kSkip),
Return(Status(Status::kOkCompleted))));
metadata = {Id::kBlock, 2, 3, 5};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
metadata = {Id::kVoid, 2, 1, 7};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull())).Times(1);
// Expect to get called twice because we'll cap the LimitedReader to 1-byte
// reads. The first attempt to read will fail because we'll have already
// reached the 1-byte max.
EXPECT_CALL(callback_, OnVoid(metadata, NotNull(), NotNull())).Times(2);
metadata = {Id::kBlock, 2, kUnknownElementSize, 10};
EXPECT_CALL(callback_, OnElementBegin(metadata, NotNull()))
.WillOnce(DoAll(SetArgPointee<1>(Action::kSkip),
Return(Status(Status::kOkCompleted))));
}
ResetParser(ParserForId(Id::kBlock, new MasterParser));
IncrementalParseAndVerify();
}
} // namespace
|