1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "src/byte_parser.h"
9
10 #include "gtest/gtest.h"
11
12 #include "test_utils/element_parser_test.h"
13 #include "webm/status.h"
14
15 using webm::BinaryParser;
16 using webm::ElementParserTest;
17 using webm::kUnknownElementSize;
18 using webm::Status;
19 using webm::StringParser;
20
21 namespace {
22
23 class StringParserTest : public ElementParserTest<StringParser> {};
24
TEST_F(StringParserTest,StringInvalidSize)25 TEST_F(StringParserTest, StringInvalidSize) {
26 TestInit(kUnknownElementSize, Status::kInvalidElementSize);
27 }
28
TEST_F(StringParserTest,StringCustomDefault)29 TEST_F(StringParserTest, StringCustomDefault) {
30 ResetParser("foobar");
31
32 ParseAndVerify();
33
34 EXPECT_EQ("foobar", parser_.value());
35 }
36
TEST_F(StringParserTest,StringValidValue)37 TEST_F(StringParserTest, StringValidValue) {
38 ParseAndVerify();
39 EXPECT_EQ("", parser_.value());
40
41 SetReaderData({'!'});
42 ParseAndVerify();
43 EXPECT_EQ("!", parser_.value());
44
45 SetReaderData({'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'});
46 ParseAndVerify();
47 EXPECT_EQ("Hello, world", parser_.value());
48 }
49
TEST_F(StringParserTest,StringTrailingNulCharacters)50 TEST_F(StringParserTest, StringTrailingNulCharacters) {
51 // The trailing NUL characters should be trimmed.
52 SetReaderData({'H', 'i', '\0', '\0'});
53 ParseAndVerify();
54 EXPECT_EQ("Hi", parser_.value());
55
56 SetReaderData({'\0'});
57 ParseAndVerify();
58 EXPECT_EQ("", parser_.value());
59 }
60
TEST_F(StringParserTest,StringIncrementalParse)61 TEST_F(StringParserTest, StringIncrementalParse) {
62 SetReaderData({'M', 'a', 't', 'r', 'o', 's', 'k', 'a'});
63
64 IncrementalParseAndVerify();
65
66 EXPECT_EQ("Matroska", parser_.value());
67 }
68
69 class BinaryParserTest : public ElementParserTest<BinaryParser> {};
70
TEST_F(BinaryParserTest,BinaryInvalidSize)71 TEST_F(BinaryParserTest, BinaryInvalidSize) {
72 TestInit(kUnknownElementSize, Status::kInvalidElementSize);
73 }
74
TEST_F(BinaryParserTest,BinaryCustomDefault)75 TEST_F(BinaryParserTest, BinaryCustomDefault) {
76 std::vector<std::uint8_t> expected = {0x00, 0x02, 0x04, 0x06, 0x08};
77 ResetParser(expected);
78
79 ParseAndVerify();
80
81 EXPECT_EQ(expected, parser_.value());
82 }
83
TEST_F(BinaryParserTest,BinaryValidValue)84 TEST_F(BinaryParserTest, BinaryValidValue) {
85 std::vector<std::uint8_t> expected;
86
87 ParseAndVerify();
88 EXPECT_EQ(expected, parser_.value());
89
90 expected = {0x00};
91 SetReaderData(expected);
92 ParseAndVerify();
93 EXPECT_EQ(expected, parser_.value());
94
95 expected = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
96 SetReaderData(expected);
97 ParseAndVerify();
98 EXPECT_EQ(expected, parser_.value());
99
100 // Unlike StringParser, the BinaryParser should not trim trailing 0-bytes.
101 expected = {'H', 'i', '\0', '\0'};
102 SetReaderData(expected);
103 ParseAndVerify();
104 EXPECT_EQ(expected, parser_.value());
105 }
106
TEST_F(BinaryParserTest,BinaryIncrementalParse)107 TEST_F(BinaryParserTest, BinaryIncrementalParse) {
108 const std::vector<std::uint8_t> expected = {
109 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
110 SetReaderData(expected);
111
112 IncrementalParseAndVerify();
113
114 EXPECT_EQ(expected, parser_.value());
115 }
116
117 } // namespace
118