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/parser_utils.h"
9 
10 #include <cstdint>
11 #include <limits>
12 #include <memory>
13 
14 #include "gtest/gtest.h"
15 
16 #include "test_utils/limited_reader.h"
17 #include "webm/buffer_reader.h"
18 #include "webm/reader.h"
19 #include "webm/status.h"
20 
21 using webm::BufferReader;
22 using webm::LimitedReader;
23 using webm::Reader;
24 using webm::Status;
25 
26 namespace {
27 
28 class ParserUtilsTest : public testing::Test {};
29 
TEST_F(ParserUtilsTest,ReadByte)30 TEST_F(ParserUtilsTest, ReadByte) {
31   LimitedReader reader(
32       std::unique_ptr<Reader>(new BufferReader({0x12, 0x34, 0x56, 0x78})));
33 
34   Status status;
35   std::uint8_t byte;
36 
37   status = ReadByte(&reader, &byte);
38   EXPECT_EQ(Status::kOkCompleted, status.code);
39   EXPECT_EQ(0x12, byte);
40 
41   status = ReadByte(&reader, &byte);
42   EXPECT_EQ(Status::kOkCompleted, status.code);
43   EXPECT_EQ(0x34, byte);
44 
45   status = ReadByte(&reader, &byte);
46   EXPECT_EQ(Status::kOkCompleted, status.code);
47   EXPECT_EQ(0x56, byte);
48 
49   reader.set_total_read_limit(0);
50   status = ReadByte(&reader, &byte);
51   EXPECT_EQ(Status::kWouldBlock, status.code);
52   EXPECT_EQ(0x56, byte);
53 
54   reader.set_total_read_limit(std::numeric_limits<std::size_t>::max());
55   status = ReadByte(&reader, &byte);
56   EXPECT_EQ(Status::kOkCompleted, status.code);
57   EXPECT_EQ(0x78, byte);
58 
59   status = ReadByte(&reader, &byte);
60   EXPECT_EQ(Status::kEndOfFile, status.code);
61   EXPECT_EQ(0x78, byte);
62 }
63 
TEST_F(ParserUtilsTest,AccumulateIntegerBytes)64 TEST_F(ParserUtilsTest, AccumulateIntegerBytes) {
65   LimitedReader reader(
66       std::unique_ptr<Reader>(new BufferReader({0x12, 0x34, 0x56, 0x78})));
67 
68   std::uint32_t integer = 0;
69   Status status;
70   std::uint64_t num_bytes_actually_read;
71 
72   reader.set_total_read_limit(1);
73   status =
74       AccumulateIntegerBytes(4, &reader, &integer, &num_bytes_actually_read);
75   EXPECT_EQ(Status::kWouldBlock, status.code);
76   EXPECT_EQ(static_cast<std::uint64_t>(1), num_bytes_actually_read);
77   EXPECT_EQ(static_cast<std::uint32_t>(0x12), integer);
78 
79   reader.set_total_read_limit(std::numeric_limits<std::size_t>::max());
80   status =
81       AccumulateIntegerBytes(3, &reader, &integer, &num_bytes_actually_read);
82   EXPECT_EQ(Status::kOkCompleted, status.code);
83   EXPECT_EQ(static_cast<std::uint64_t>(3), num_bytes_actually_read);
84   EXPECT_EQ(static_cast<std::uint32_t>(0x12345678), integer);
85 
86   // Make sure calling with num_bytes_remaining == 0 is a no-op.
87   status =
88       AccumulateIntegerBytes(0, &reader, &integer, &num_bytes_actually_read);
89   EXPECT_EQ(Status::kOkCompleted, status.code);
90   EXPECT_EQ(static_cast<std::uint64_t>(0), num_bytes_actually_read);
91   EXPECT_EQ(static_cast<std::uint32_t>(0x12345678), integer);
92 
93   status =
94       AccumulateIntegerBytes(4, &reader, &integer, &num_bytes_actually_read);
95   EXPECT_EQ(Status::kEndOfFile, status.code);
96   EXPECT_EQ(static_cast<std::uint64_t>(0), num_bytes_actually_read);
97   EXPECT_EQ(static_cast<std::uint32_t>(0x12345678), integer);
98 }
99 
100 }  // namespace
101