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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
|
// 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/block_parser.h"
#include <cstdint>
#include <memory>
#include <type_traits>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "src/parser_utils.h"
#include "test_utils/element_parser_test.h"
#include "webm/id.h"
#include "webm/status.h"
using testing::_;
using testing::Between;
using testing::DoAll;
using testing::Exactly;
using testing::InSequence;
using testing::Invoke;
using testing::NotNull;
using testing::Return;
using testing::SetArgPointee;
using webm::Action;
using webm::Block;
using webm::BlockParser;
using webm::ElementParserTest;
using webm::FrameMetadata;
using webm::Id;
using webm::kUnknownElementSize;
using webm::Lacing;
using webm::ReadByte;
using webm::Reader;
using webm::SimpleBlock;
using webm::SimpleBlockParser;
using webm::Status;
namespace {
// Represents a single block and its expected parsing results for use in tests.
struct TestData {
// Block data.
std::vector<std::uint8_t> data;
// Expected results.
std::uint64_t expected_track_number;
std::int16_t expected_timecode;
Lacing expected_lacing;
bool expected_is_visible;
bool expected_is_key_frame;
bool expected_is_discardable;
int expected_num_frames;
std::uint64_t expected_frame_start_position;
std::vector<std::uint64_t> expected_frame_sizes;
};
// Test data for an EBML-laced block containing only one frame.
const TestData ebml_lacing_one_frame = {
// Data.
{
0x81, // Track number = 1.
0x00,
0x00, // Timecode = 0.
0x86, // Flags = key_frame | ebml_lacing.
0x00, // Lace count - 1 = 0 (1 frame).
// Lace data (1 frame).
// Frame 0.
0x00,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kEbml, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Test data for a Xiph-laced block containing only one frame.
const TestData xiph_lacing_one_frame = {
// Data.
{
0x81, // Track number = 1.
0x00,
0x00, // Timecode = 0.
0x82, // Flags = key_frame | xiph_lacing.
0x00, // Lace count - 1 = 0 (1 frame).
// Lace data (1 frame).
// Frame 0.
0x00,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kXiph, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Test data for a fixed-laced block containing only one frame.
const TestData fixed_lacing_one_frame = {
// Data.
{
0x81, // Track number = 1.
0x00,
0x00, // Timecode = 0.
0x84, // Flags = key_frame | fixed_lacing.
0x00, // Lace count - 1 = 0 (1 frame).
// Lace data (1 frame).
0x00,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kFixed, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Test data for an EBML-laced block.
// clang-format off
const TestData ebml_lacing = {
// Data.
{
0x81, // Track number = 1.
0x00, 0x00, // Timecode = 0.
0x86, // Flags = key_frame | ebml_lacing.
0x05, // Lace count - 1 = 5 (6 frames).
// Lace data (6 frames).
0xFF, // Lace 0 size = 127.
0x5F, 0x81, // Lace 1 size = 1.
0xC0, // Lace 2 size = 2.
0xFF, // Lace 3 size = 66.
0x81, // Lace 4 size = 4.
// Lace 5 size inferred to be 5.
// Lace data (6 frames).
// Frame 0.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Frame 1.
0x01,
// Frame 2.
0x02, 0x02,
// Frame 3.
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
// Frame 4.
0x04, 0x04, 0x04, 0x04,
// Frame 5.
0x05, 0x05, 0x05, 0x05, 0x05,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kEbml, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
6, // expected_num_frames
11, // expected_frame_start_position
// expected_frame_sizes
{127, 1, 2, 66, 4, 5},
};
// Test data for a Xiph-laced block.
const TestData xiph_lacing = {
// Data.
{
0x81, // Track number = 1.
0x00, 0x00, // Timecode = 0.
0x82, // Flags = key_frame | xiph_lacing.
0x03, // Lace count - 1 = 3 (4 frames).
// Lace sizes.
0xFF, 0xFF, 0x00, // Lace 0 size = 510.
0xFF, 0x01, // Lace 1 size = 256.
0x02, // Lace 2 size = 2.
// Lace 3 size inferred to be 3.
// Lace data (4 frames).
// Frame 0.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// Frame 1.
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01,
// Frame 2.
0x02, 0x02,
// Frame 3.
0x03, 0x03, 0x03,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kXiph, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
4, // expected_num_frames
11, // expected_frame_start_position
// expected_frame_sizes
{510, 256, 2, 3},
};
// clang-format on
// Test data for a fixed-laced block.
const TestData fixed_lacing = {
// Data.
{
0x81, // Track number = 1.
0x00, 0x00, // Timecode = 0.
0x84, // Flags = key_frame | fixed_lacing.
0x03, // Lace count - 1 = 3 (4 frames).
// Lace data (4 frames).
0x00, 0x00, // Frame 0.
0x01, 0x01, // Frame 1.
0x02, 0x02, // Frame 2.
0x03, 0x03, // Frame 3.
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kFixed, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
4, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{2, 2, 2, 2},
};
// Test data for a block that has no lacing.
const TestData no_lacing = {
// Data.
{
0x40, 0x01, // Track number = 1.
0x00, 0x00, // Timecode = 0.
0x80, // Flags = key_frame.
// Lace data (1 frame).
0x00, 0x00, 0x00, // Frame 0.
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kNone, // expected_lacing
true, // expected_is_visible
true, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{3},
};
// Test data for a block that has no flags set in the header.
const TestData no_flags = {
// Data.
{
0x81, // Track number = 1.
0x00,
0x00, // Timecode = 0.
0x00, // Flags = 0.
// Lace data (1 frame).
0x00,
},
// Expected results.
1, // expected_track_number
0, // expected_timecode
Lacing::kNone, // expected_lacing
true, // expected_is_visible
false, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
4, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Test data for a block that has all Block (ID = Id::kBlock (0xA1)) flags set
// in the header (and no other flags).
const TestData block_flags = {
// Data.
{
0x82, // Track number = 2.
0xFE,
0xDC, // Timecode = -292.
0x08, // Flags = invisible.
// Lace data (1 frame).
0x00,
},
// Expected results.
2, // expected_track_number
-292, // expected_timecode
Lacing::kNone, // expected_lacing
false, // expected_is_visible
false, // expected_is_key_frame
false, // expected_is_discardable
1, // expected_num_frames
4, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Test data for a block that has all SimpleBlock (ID = Id::kSimpleBlock (0xA3))
// flags set in the header.
const TestData simple_block_flags = {
// Data.
{
0x41,
0x23, // Track number = 291.
0x12,
0x34, // Timecode = 4660.
0x89, // Flags = key_frame | invisible | discardable.
// Lace data (1 frame).
0x00,
},
// Expected results.
291, // expected_track_number
4660, // expected_timecode
Lacing::kNone, // expected_lacing
false, // expected_is_visible
true, // expected_is_key_frame
true, // expected_is_discardable
1, // expected_num_frames
5, // expected_frame_start_position
// expected_frame_sizes
{1},
};
// Checks that the Block matches the expected results in the TestData.
void ValidateBlock(const TestData& test_data, const Block& actual) {
EXPECT_EQ(test_data.expected_track_number, actual.track_number);
EXPECT_EQ(test_data.expected_timecode, actual.timecode);
EXPECT_EQ(test_data.expected_is_visible, actual.is_visible);
EXPECT_EQ(test_data.expected_lacing, actual.lacing);
EXPECT_EQ(test_data.expected_num_frames, actual.num_frames);
}
// Checks that the SimpleBlock matches the expected results in the TestData.
void ValidateBlock(const TestData& test_data, const SimpleBlock& actual) {
ValidateBlock(test_data, static_cast<const Block&>(actual));
EXPECT_EQ(test_data.expected_is_key_frame, actual.is_key_frame);
EXPECT_EQ(test_data.expected_is_discardable, actual.is_discardable);
}
// Constructs a SimpleBlock populated with the expected results for this test.
SimpleBlock ExpectedSimpleBlock(const TestData& test_data) {
SimpleBlock expected;
expected.track_number = test_data.expected_track_number;
expected.timecode = test_data.expected_timecode;
expected.lacing = test_data.expected_lacing;
expected.is_visible = test_data.expected_is_visible;
expected.is_key_frame = test_data.expected_is_key_frame;
expected.is_discardable = test_data.expected_is_discardable;
expected.num_frames = test_data.expected_num_frames;
return expected;
}
// Simple functor that can be used for Callback::OnFrame() that will validate
// the frame metadata and frame byte values.
struct FrameHandler {
// The expected value for the frame metadata.
FrameMetadata expected_metadata;
// The expected value for each byte in the frame.
std::uint8_t expected_frame_byte_value;
// Can be used for Callback::OnFrame() to consume data from the reader and
// validate the results.
Status operator()(const FrameMetadata& metadata, Reader* reader,
std::uint64_t* bytes_remaining) const {
EXPECT_EQ(expected_metadata, metadata);
std::uint8_t frame_byte_value;
Status status;
do {
status = ReadByte(reader, &frame_byte_value);
if (!status.completed_ok()) {
break;
}
EXPECT_EQ(expected_frame_byte_value, frame_byte_value);
--*bytes_remaining;
} while (*bytes_remaining > 0);
return status;
}
};
template <typename T, Id id>
class BasicBlockParserTest : public ElementParserTest<T, id> {
public:
// Sets expectations for a normal (i.e. successful parse) test.
void SetExpectations(const TestData& test_data, bool incremental,
bool set_action) {
InSequence dummy;
const SimpleBlock expected_simple_block = ExpectedSimpleBlock(test_data);
const Block expected_block = ExpectedSimpleBlock(test_data);
FrameMetadata metadata = FirstFrameMetadata(test_data);
if (std::is_same<T, SimpleBlockParser>::value) {
auto& expectation = EXPECT_CALL(
callback_, OnSimpleBlockBegin(metadata.parent_element,
expected_simple_block, NotNull()));
if (set_action) {
expectation.Times(1);
} else {
expectation.WillOnce(Return(Status(Status::kOkCompleted)));
}
EXPECT_CALL(callback_, OnBlockBegin(_, _, _)).Times(0);
} else {
auto& expectation = EXPECT_CALL(
callback_,
OnBlockBegin(metadata.parent_element, expected_block, NotNull()));
if (set_action) {
expectation.Times(1);
} else {
expectation.WillOnce(Return(Status(Status::kOkCompleted)));
}
EXPECT_CALL(callback_, OnSimpleBlockBegin(_, _, _)).Times(0);
}
std::uint8_t expected_frame_byte_value = 0;
for (const std::uint64_t frame_size : test_data.expected_frame_sizes) {
metadata.size = frame_size;
const FrameHandler frame_handler = {metadata, expected_frame_byte_value};
// Incremental parsing will call OnFrame once for every byte, plus
// maybe one more time if the first call reads zero bytes (if the reader
// is blocked).
const int this_frame_size = static_cast<int>(frame_size);
EXPECT_CALL(callback_, OnFrame(metadata, NotNull(), NotNull()))
.Times(incremental ? Between(this_frame_size, this_frame_size + 1)
: Exactly(1))
.WillRepeatedly(Invoke(frame_handler));
metadata.position += metadata.size;
++expected_frame_byte_value;
}
if (std::is_same<T, SimpleBlockParser>::value) {
EXPECT_CALL(callback_, OnSimpleBlockEnd(metadata.parent_element,
expected_simple_block))
.Times(1);
EXPECT_CALL(callback_, OnBlockEnd(_, _)).Times(0);
} else {
EXPECT_CALL(callback_,
OnBlockEnd(metadata.parent_element, expected_block))
.Times(1);
EXPECT_CALL(callback_, OnSimpleBlockEnd(_, _)).Times(0);
}
}
// Runs a single test using the provided test data.
void RunTest(const TestData& test_data) {
SetReaderData(test_data.data);
SetExpectations(test_data, false, true);
ParseAndVerify();
ValidateBlock(test_data, parser_.value());
}
// Same as RunTest(), except it forces parsers to parse one byte at a time.
void RunIncrementalTest(const TestData& test_data) {
SetReaderData(test_data.data);
SetExpectations(test_data, true, true);
IncrementalParseAndVerify();
ValidateBlock(test_data, parser_.value());
}
// Tests invalid element sizes.
void TestInvalidElementSize() {
TestInit(0, Status::kInvalidElementSize);
TestInit(4, Status::kInvalidElementSize);
TestInit(kUnknownElementSize, Status::kInvalidElementSize);
}
// Tests invalid blocks by feeding only the header of the block into the
// parser.
void TestInvalidHeaderOnly(const TestData& test_data) {
EXPECT_CALL(callback_, OnFrame(_, _, _)).Times(0);
EXPECT_CALL(callback_, OnBlockEnd(_, _)).Times(0);
EXPECT_CALL(callback_, OnSimpleBlockEnd(_, _)).Times(0);
SetReaderData(test_data.data);
ParseAndExpectResult(Status::kInvalidElementValue,
test_data.expected_frame_start_position);
}
// Tests an invalid fixed-lace block that has inconsistent frame sizes.
void TestInvalidFixedLaceSizes() {
SetReaderData({
0x81, // Track number = 1.
0x00, 0x00, // Timecode = 0.
0x84, // Flags = key_frame | fixed_lacing.
0x01, // Lace count - 1 = 1 (2 frames).
// Lace data (2 frames).
0x00, 0x00, // Frame 0.
0x01, // Frame 1 (invalid: inconsistent frame size).
});
EXPECT_CALL(callback_, OnFrame(_, _, _)).Times(0);
EXPECT_CALL(callback_, OnBlockEnd(_, _)).Times(0);
EXPECT_CALL(callback_, OnSimpleBlockEnd(_, _)).Times(0);
ParseAndExpectResult(Status::kInvalidElementValue);
}
// Tests setting the action to Action::kSkip in Callback::OnSimpleBlockBegin
// for the SimpleBlockParser.
void TestSimpleBlockSkip(const TestData& test_data) {
SetReaderData(test_data.data);
const SimpleBlock expected_simple_block = ExpectedSimpleBlock(test_data);
const FrameMetadata metadata = FirstFrameMetadata(test_data);
EXPECT_CALL(callback_, OnSimpleBlockBegin(metadata.parent_element,
expected_simple_block, NotNull()))
.WillOnce(Return(Status(Status::kOkPartial)))
.WillOnce(DoAll(SetArgPointee<2>(Action::kSkip),
Return(Status(Status::kOkCompleted))));
EXPECT_CALL(callback_, OnFrame(_, _, _)).Times(0);
EXPECT_CALL(callback_, OnBlockEnd(_, _)).Times(0);
EXPECT_CALL(callback_, OnSimpleBlockEnd(_, _)).Times(0);
IncrementalParseAndVerify();
}
protected:
using ElementParserTest<T, id>::callback_;
using ElementParserTest<T, id>::IncrementalParseAndVerify;
using ElementParserTest<T, id>::metadata_;
using ElementParserTest<T, id>::ParseAndExpectResult;
using ElementParserTest<T, id>::ParseAndVerify;
using ElementParserTest<T, id>::parser_;
using ElementParserTest<T, id>::SetReaderData;
using ElementParserTest<T, id>::TestInit;
private:
// Gets the FrameMetadata for the very first frame in the test data.
FrameMetadata FirstFrameMetadata(const TestData& test_data) {
FrameMetadata metadata;
metadata.parent_element = metadata_;
metadata.parent_element.size = test_data.data.size();
metadata.position = test_data.expected_frame_start_position +
metadata.parent_element.position +
metadata.parent_element.header_size;
metadata.size = test_data.expected_frame_sizes[0];
return metadata;
}
};
class BlockParserTest : public BasicBlockParserTest<BlockParser, Id::kBlock> {};
TEST_F(BlockParserTest, InvalidElementSize) { TestInvalidElementSize(); }
TEST_F(BlockParserTest, InvalidHeaderOnlyNoLacing) {
TestInvalidHeaderOnly(no_lacing);
}
TEST_F(BlockParserTest, InvalidHeaderOnlyFixedLacing) {
TestInvalidHeaderOnly(fixed_lacing);
}
TEST_F(BlockParserTest, InvalidFixedLaceSizes) { TestInvalidFixedLaceSizes(); }
TEST_F(BlockParserTest, BlockNoFlags) { RunTest(no_flags); }
TEST_F(BlockParserTest, BlockFlags) { RunTest(block_flags); }
TEST_F(BlockParserTest, EbmlLacingOneFrame) { RunTest(ebml_lacing_one_frame); }
TEST_F(BlockParserTest, EbmlLacing) { RunTest(ebml_lacing); }
TEST_F(BlockParserTest, XiphLacingOneFrame) { RunTest(xiph_lacing_one_frame); }
TEST_F(BlockParserTest, XiphLacing) { RunTest(xiph_lacing); }
TEST_F(BlockParserTest, FixedLacingOneFrame) {
RunTest(fixed_lacing_one_frame);
}
TEST_F(BlockParserTest, FixedLacing) { RunTest(fixed_lacing); }
TEST_F(BlockParserTest, NoLacing) { RunTest(no_lacing); }
TEST_F(BlockParserTest, BlockWithPositionAndHeaderSize) {
metadata_.position = 15;
metadata_.header_size = 3;
RunTest(no_lacing);
}
TEST_F(BlockParserTest, IncrementalBlockFlags) {
RunIncrementalTest(block_flags);
}
TEST_F(BlockParserTest, IncrementalEbmlLacingOneFrame) {
RunIncrementalTest(ebml_lacing_one_frame);
}
TEST_F(BlockParserTest, IncrementalEbmlLacing) {
RunIncrementalTest(ebml_lacing);
}
TEST_F(BlockParserTest, IncrementalXiphLacingOneFrame) {
RunIncrementalTest(xiph_lacing_one_frame);
}
TEST_F(BlockParserTest, IncrementalXiphLacing) {
RunIncrementalTest(xiph_lacing);
}
TEST_F(BlockParserTest, IncrementalFixedLacingOneFrame) {
RunIncrementalTest(fixed_lacing_one_frame);
}
TEST_F(BlockParserTest, IncrementalFixedLacing) {
RunIncrementalTest(fixed_lacing);
}
TEST_F(BlockParserTest, DefaultActionIsRead) {
SetReaderData(fixed_lacing_one_frame.data);
SetExpectations(fixed_lacing_one_frame, false, false);
ParseAndVerify();
ValidateBlock(fixed_lacing_one_frame, parser_.value());
}
TEST_F(BlockParserTest, IncrementalNoLacing) { RunIncrementalTest(no_lacing); }
class SimpleBlockParserTest
: public BasicBlockParserTest<SimpleBlockParser, Id::kSimpleBlock> {};
TEST_F(SimpleBlockParserTest, InvalidElementSize) { TestInvalidElementSize(); }
TEST_F(SimpleBlockParserTest, InvalidHeaderOnlyNoLacing) {
TestInvalidHeaderOnly(no_lacing);
}
TEST_F(SimpleBlockParserTest, InvalidHeaderOnlyFixedLacing) {
TestInvalidHeaderOnly(fixed_lacing);
}
TEST_F(SimpleBlockParserTest, InvalidFixedLaceSizes) {
TestInvalidFixedLaceSizes();
}
TEST_F(SimpleBlockParserTest, SimpleBlockSkip) {
TestSimpleBlockSkip(no_flags);
}
TEST_F(SimpleBlockParserTest, SimpleBlockNoFlags) { RunTest(no_flags); }
TEST_F(SimpleBlockParserTest, SimpleBlockFlags) { RunTest(simple_block_flags); }
TEST_F(SimpleBlockParserTest, EbmlLacingOneFrame) {
RunTest(ebml_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, EbmlLacing) { RunTest(ebml_lacing); }
TEST_F(SimpleBlockParserTest, XiphLacingOneFrame) {
RunTest(xiph_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, XiphLacing) { RunTest(xiph_lacing); }
TEST_F(SimpleBlockParserTest, FixedLacingOneFrame) {
RunTest(fixed_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, FixedLacing) { RunTest(fixed_lacing); }
TEST_F(SimpleBlockParserTest, NoLacing) { RunTest(no_lacing); }
TEST_F(BlockParserTest, SimpleBlockWithPositionAndHeaderSize) {
metadata_.position = 16;
metadata_.header_size = 4;
RunTest(no_lacing);
}
TEST_F(SimpleBlockParserTest, IncrementalSimpleBlockFlags) {
RunIncrementalTest(simple_block_flags);
}
TEST_F(SimpleBlockParserTest, IncrementalEbmlLacingOneFrame) {
RunIncrementalTest(ebml_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, IncrementalEbmlLacing) {
RunIncrementalTest(ebml_lacing);
}
TEST_F(SimpleBlockParserTest, IncrementalXiphLacingOneFrame) {
RunIncrementalTest(xiph_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, IncrementalXiphLacing) {
RunIncrementalTest(xiph_lacing);
}
TEST_F(SimpleBlockParserTest, IncrementalFixedLacingOneFrame) {
RunIncrementalTest(fixed_lacing_one_frame);
}
TEST_F(SimpleBlockParserTest, IncrementalFixedLacing) {
RunIncrementalTest(fixed_lacing);
}
TEST_F(SimpleBlockParserTest, IncrementalNoLacing) {
RunIncrementalTest(no_lacing);
}
TEST_F(SimpleBlockParserTest, DefaultActionIsRead) {
SetReaderData(fixed_lacing_one_frame.data);
SetExpectations(fixed_lacing_one_frame, false, false);
ParseAndVerify();
ValidateBlock(fixed_lacing_one_frame, parser_.value());
}
} // namespace
|