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 #ifndef TEST_UTILS_ELEMENT_PARSER_TEST_H_
9 #define TEST_UTILS_ELEMENT_PARSER_TEST_H_
10 
11 #include <cstdint>
12 #include <vector>
13 
14 #include "gtest/gtest.h"
15 
16 #include "test_utils/limited_reader.h"
17 #include "test_utils/parser_test.h"
18 #include "webm/buffer_reader.h"
19 #include "webm/reader.h"
20 #include "webm/status.h"
21 
22 namespace webm {
23 
24 // Base class for unit tests that test an instance of the ElementParser
25 // inteface. The template parameter T is the parser class being tested, and the
26 // optional id is the element ID associated with elements from the parser.
27 template <typename T, Id id = static_cast<Id>(0)>
28 class ElementParserTest : public ParserTest<T> {
29  public:
30   // Sets the reader's internal buffer to the given buffer and metadata_ to
31   // data.size().
SetReaderData(std::vector<std::uint8_t> data)32   void SetReaderData(std::vector<std::uint8_t> data) override {
33     metadata_.size = data.size();
34     ParserTest<T>::SetReaderData(std::move(data));
35   }
36 
37   // Sets metadata_.size to size and then calls Init() on the parser, ensuring
38   // that it returns the expected status code.
TestInit(std::uint64_t size,Status::Code expected)39   void TestInit(std::uint64_t size, Status::Code expected) {
40     metadata_.size = size;
41     const Status status = parser_.Init(metadata_, metadata_.size);
42     ASSERT_EQ(expected, status.code);
43   }
44 
45   // Similar to the base class implementation, but with the difference that
46   // Init() is also called (after setting metadata_.size to size).
ParseAndVerify(std::uint64_t size)47   void ParseAndVerify(std::uint64_t size) override {
48     TestInit(size, Status::kOkCompleted);
49 
50     std::uint64_t num_bytes_read = 0;
51     const Status status = parser_.Feed(&callback_, &reader_, &num_bytes_read);
52     ASSERT_EQ(Status::kOkCompleted, status.code);
53 
54     if (size != kUnknownElementSize) {
55       ASSERT_EQ(size, num_bytes_read);
56     }
57   }
58 
ParseAndVerify()59   void ParseAndVerify() override { ParseAndVerify(metadata_.size); }
60 
IncrementalParseAndVerify()61   void IncrementalParseAndVerify() override {
62     TestInit(metadata_.size, Status::kOkCompleted);
63 
64     webm::LimitedReader limited_reader(
65         std::unique_ptr<webm::Reader>(new BufferReader(std::move(reader_))));
66 
67     Status status;
68     std::uint64_t num_bytes_read = 0;
69     do {
70       limited_reader.set_total_read_skip_limit(1);
71       std::uint64_t local_num_bytes_read = 0;
72       status = parser_.Feed(&callback_, &limited_reader, &local_num_bytes_read);
73       num_bytes_read += local_num_bytes_read;
74       ASSERT_GE(static_cast<std::uint64_t>(1), local_num_bytes_read);
75     } while (status.code == Status::kWouldBlock ||
76              status.code == Status::kOkPartial);
77 
78     ASSERT_EQ(Status::kOkCompleted, status.code);
79 
80     if (metadata_.size != kUnknownElementSize) {
81       ASSERT_EQ(metadata_.size, num_bytes_read);
82     }
83   }
84 
85   // Initializes the parser (after setting metadata_.size to size), ensures it
86   // succeeds, and then calls Feed() on the parser, making sure it returns the
87   // expected status code.
ParseAndExpectResult(Status::Code expected,std::uint64_t size)88   void ParseAndExpectResult(Status::Code expected, std::uint64_t size) {
89     TestInit(size, Status::kOkCompleted);
90 
91     std::uint64_t num_bytes_read = 0;
92     const Status status = parser_.Feed(&callback_, &reader_, &num_bytes_read);
93     ASSERT_EQ(expected, status.code);
94   }
95 
96   // Initializes the parser, ensures it succeeds, and then calls Feed() on the
97   // parser, making sure it returns the expected status code.
ParseAndExpectResult(Status::Code expected)98   void ParseAndExpectResult(Status::Code expected) override {
99     ParseAndExpectResult(expected, metadata_.size);
100   }
101 
102  protected:
103   using ParserTest<T>::callback_;
104   using ParserTest<T>::parser_;
105   using ParserTest<T>::reader_;
106 
107   // Element metadata associated with the element parsed by parser_. This is
108   // passed to Init() when initializing the parser.
109   ElementMetadata metadata_ = {id, 0, 0, 0};
110 };
111 
112 }  // namespace webm
113 
114 #endif  // TEST_UTILS_ELEMENT_PARSER_TEST_H_
115