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/recursive_parser.h"
9
10 #include <cstdint>
11
12 #include "gtest/gtest.h"
13
14 #include "src/byte_parser.h"
15 #include "src/element_parser.h"
16 #include "test_utils/element_parser_test.h"
17 #include "webm/element.h"
18 #include "webm/status.h"
19
20 using webm::Callback;
21 using webm::ElementMetadata;
22 using webm::ElementParser;
23 using webm::ElementParserTest;
24 using webm::Reader;
25 using webm::RecursiveParser;
26 using webm::Status;
27 using webm::StringParser;
28
29 namespace {
30
31 class FailParser : public ElementParser {
32 public:
FailParser(std::size_t)33 explicit FailParser(std::size_t /* max_recursion_depth */) { ADD_FAILURE(); }
34
Init(const ElementMetadata &,std::uint64_t)35 Status Init(const ElementMetadata& /* metadata */,
36 std::uint64_t /* max_size */) override {
37 ADD_FAILURE();
38 return Status(Status::kInvalidElementSize);
39 }
40
Feed(Callback *,Reader *,std::uint64_t * num_bytes_read)41 Status Feed(Callback* /* callback */, Reader* /* reader */,
42 std::uint64_t* num_bytes_read) override {
43 ADD_FAILURE();
44 *num_bytes_read = 0;
45 return Status(Status::kInvalidElementSize);
46 }
47
value() const48 int value() const {
49 ADD_FAILURE();
50 return 0;
51 }
52
mutable_value()53 int* mutable_value() {
54 ADD_FAILURE();
55 return nullptr;
56 }
57 };
58
59 class StringParserWrapper : public StringParser {
60 public:
StringParserWrapper(std::size_t max_recursion_depth)61 explicit StringParserWrapper(std::size_t max_recursion_depth) {
62 EXPECT_EQ(max_recursion_depth, 24);
63 }
64 };
65
66 class RecursiveFailParserTest
67 : public ElementParserTest<RecursiveParser<FailParser>> {};
68
TEST_F(RecursiveFailParserTest,NoConstruction)69 TEST_F(RecursiveFailParserTest, NoConstruction) {
70 RecursiveParser<FailParser> parser;
71 }
72
73 class RecursiveStringParserTest
74 : public ElementParserTest<RecursiveParser<StringParserWrapper>> {};
75
TEST_F(RecursiveStringParserTest,ParsesOkay)76 TEST_F(RecursiveStringParserTest, ParsesOkay) {
77 ParseAndVerify();
78 EXPECT_EQ("", parser_.value());
79
80 SetReaderData({0x48, 0x69}); // "Hi".
81 ParseAndVerify();
82 EXPECT_EQ("Hi", parser_.value());
83 }
84
TEST_F(RecursiveStringParserTest,ExceedMaxRecursionDepth)85 TEST_F(RecursiveStringParserTest, ExceedMaxRecursionDepth) {
86 ResetParser(0);
87 TestInit(0, Status::kExceededRecursionDepthLimit);
88 }
89
90 } // namespace
91