• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARSER_TEST_H_
9 #define TEST_UTILS_PARSER_TEST_H_
10 
11 #include <cstdint>
12 #include <new>
13 #include <vector>
14 
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 
18 #include "test_utils/limited_reader.h"
19 #include "test_utils/mock_callback.h"
20 #include "webm/buffer_reader.h"
21 #include "webm/reader.h"
22 #include "webm/status.h"
23 
24 namespace webm {
25 
26 // Base class for unit tests that test an instance of the Parser inteface. The
27 // template parameter T is the parser class being tested.
28 template <typename T>
29 class ParserTest : public testing::Test {
30  public:
31   // Sets the reader's internal buffer to the given buffer.
SetReaderData(std::vector<std::uint8_t> data)32   virtual void SetReaderData(std::vector<std::uint8_t> data) {
33     reader_ = BufferReader(std::move(data));
34   }
35 
36   // Destroys and recreates the parser, forwarding the arguments to the
37   // constructor. This is primarily useful for tests that require the parser to
38   // have different constructor parameters.
39   template <typename... Args>
ResetParser(Args &&...args)40   void ResetParser(Args&&... args) {
41     parser_.~T();
42     new (&parser_) T(std::forward<Args>(args)...);
43   }
44 
45   // Calls Feed() on the parser, making sure it completes successfully and reads
46   // size number of bytes.
ParseAndVerify(std::uint64_t size)47   virtual void ParseAndVerify(std::uint64_t size) {
48     std::uint64_t num_bytes_read = 0;
49     const Status status = parser_.Feed(&callback_, &reader_, &num_bytes_read);
50     ASSERT_EQ(Status::kOkCompleted, status.code);
51     ASSERT_EQ(size, num_bytes_read);
52   }
53 
54   // Calls Feed() on the parser, making sure it completes successfully and reads
55   // all the data available in the reader.
ParseAndVerify()56   virtual void ParseAndVerify() { ParseAndVerify(reader_.size()); }
57 
58   // Similar to ParseAndVerify(), but instead artificially limits the reader to
59   // providing one byte per call to Feed(). If Feed() returns
60   // Status::kWouldBlock or Status::kOkPartial, Feed() will be called again
61   // (feeding it another byte).
IncrementalParseAndVerify()62   virtual void IncrementalParseAndVerify() {
63     const std::uint64_t expected_num_bytes_read = reader_.size();
64 
65     webm::LimitedReader limited_reader(
66         std::unique_ptr<webm::Reader>(new BufferReader(std::move(reader_))));
67 
68     Status status;
69     std::uint64_t num_bytes_read = 0;
70     do {
71       limited_reader.set_total_read_skip_limit(1);
72       std::uint64_t local_num_bytes_read = 0;
73       status = parser_.Feed(&callback_, &limited_reader, &local_num_bytes_read);
74       num_bytes_read += local_num_bytes_read;
75       const std::uint64_t kMinBytesRead = 1;
76       ASSERT_GE(kMinBytesRead, local_num_bytes_read);
77     } while (status.code == Status::kWouldBlock ||
78              status.code == Status::kOkPartial);
79 
80     ASSERT_EQ(Status::kOkCompleted, status.code);
81     ASSERT_EQ(expected_num_bytes_read, num_bytes_read);
82   }
83 
84   // Calls Feed() on the parser, making sure it returns the expected status
85   // code.
ParseAndExpectResult(Status::Code expected)86   virtual void ParseAndExpectResult(Status::Code expected) {
87     std::uint64_t num_bytes_read = 0;
88     const Status status = parser_.Feed(&callback_, &reader_, &num_bytes_read);
89     ASSERT_EQ(expected, status.code);
90   }
91 
92  protected:
93   // These members are protected (not private) so unit tests have access to
94   // them. This is intentional.
95 
96   // The parser that the unit tests will be testing.
97   T parser_;
98 
99   // The callback that is used during parsing.
100   testing::NiceMock<MockCallback> callback_;
101 
102   // The reader used for feeding data into the parser.
103   BufferReader reader_;
104 };
105 
106 }  // namespace webm
107 
108 #endif  // TEST_UTILS_PARSER_TEST_H_
109