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
|
// 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/byte_parser.h"
#include "gtest/gtest.h"
#include "test_utils/element_parser_test.h"
#include "webm/status.h"
using webm::BinaryParser;
using webm::ElementParserTest;
using webm::kUnknownElementSize;
using webm::Status;
using webm::StringParser;
namespace {
class StringParserTest : public ElementParserTest<StringParser> {};
TEST_F(StringParserTest, StringInvalidSize) {
TestInit(kUnknownElementSize, Status::kInvalidElementSize);
}
TEST_F(StringParserTest, StringCustomDefault) {
ResetParser("foobar");
ParseAndVerify();
EXPECT_EQ("foobar", parser_.value());
}
TEST_F(StringParserTest, StringValidValue) {
ParseAndVerify();
EXPECT_EQ("", parser_.value());
SetReaderData({'!'});
ParseAndVerify();
EXPECT_EQ("!", parser_.value());
SetReaderData({'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'});
ParseAndVerify();
EXPECT_EQ("Hello, world", parser_.value());
}
TEST_F(StringParserTest, StringTrailingNulCharacters) {
// The trailing NUL characters should be trimmed.
SetReaderData({'H', 'i', '\0', '\0'});
ParseAndVerify();
EXPECT_EQ("Hi", parser_.value());
SetReaderData({'\0'});
ParseAndVerify();
EXPECT_EQ("", parser_.value());
}
TEST_F(StringParserTest, StringIncrementalParse) {
SetReaderData({'M', 'a', 't', 'r', 'o', 's', 'k', 'a'});
IncrementalParseAndVerify();
EXPECT_EQ("Matroska", parser_.value());
}
class BinaryParserTest : public ElementParserTest<BinaryParser> {};
TEST_F(BinaryParserTest, BinaryInvalidSize) {
TestInit(kUnknownElementSize, Status::kInvalidElementSize);
}
TEST_F(BinaryParserTest, BinaryCustomDefault) {
std::vector<std::uint8_t> expected = {0x00, 0x02, 0x04, 0x06, 0x08};
ResetParser(expected);
ParseAndVerify();
EXPECT_EQ(expected, parser_.value());
}
TEST_F(BinaryParserTest, BinaryValidValue) {
std::vector<std::uint8_t> expected;
ParseAndVerify();
EXPECT_EQ(expected, parser_.value());
expected = {0x00};
SetReaderData(expected);
ParseAndVerify();
EXPECT_EQ(expected, parser_.value());
expected = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
SetReaderData(expected);
ParseAndVerify();
EXPECT_EQ(expected, parser_.value());
// Unlike StringParser, the BinaryParser should not trim trailing 0-bytes.
expected = {'H', 'i', '\0', '\0'};
SetReaderData(expected);
ParseAndVerify();
EXPECT_EQ(expected, parser_.value());
}
TEST_F(BinaryParserTest, BinaryIncrementalParse) {
const std::vector<std::uint8_t> expected = {
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
SetReaderData(expected);
IncrementalParseAndVerify();
EXPECT_EQ(expected, parser_.value());
}
} // namespace
|