1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "examples/file_reader.h"
16
17 #include <cstdint>
18 #include <cstdio>
19 #include <memory>
20 #include <vector>
21
22 #include "examples/file_reader_interface.h"
23 #include "examples/file_reader_test_common.h"
24 #include "gtest/gtest.h"
25 #include "tests/utils.h"
26
27 namespace libgav1 {
28 namespace {
29
30 // For use with tests that expect Open() failure to distinguish failure due to
31 // the file contents versus failure due to a missing file.
FileCanBeRead(const std::string & filename)32 bool FileCanBeRead(const std::string& filename) {
33 FILE* const file = fopen(filename.c_str(), "r");
34 if (file != nullptr) {
35 fclose(file);
36 return true;
37 }
38 return false;
39 }
40
TEST(FileReaderTest,FailOpen)41 TEST(FileReaderTest, FailOpen) {
42 EXPECT_EQ(FileReader::Open(""), nullptr);
43 const std::string filename =
44 test_utils::GetTestInputFilePath("ivf-signature-only");
45 SCOPED_TRACE("Filename: " + filename);
46 EXPECT_TRUE(FileCanBeRead(filename));
47 EXPECT_EQ(FileReader::Open(filename), nullptr);
48 }
49
TEST(FileReaderTest,Open)50 TEST(FileReaderTest, Open) {
51 const std::string filenames[] = {
52 test_utils::GetTestInputFilePath("five-frames.ivf"),
53 test_utils::GetTestInputFilePath("ivf-header-and-truncated-frame-header"),
54 test_utils::GetTestInputFilePath("ivf-header-only"),
55 test_utils::GetTestInputFilePath("one-frame-truncated.ivf"),
56 test_utils::GetTestInputFilePath("one-frame.ivf"),
57 };
58 for (const auto& filename : filenames) {
59 EXPECT_NE(FileReader::Open(filename), nullptr) << "Filename: " << filename;
60 }
61 }
62
TEST_P(FileReaderFailTest,FailRead)63 TEST_P(FileReaderFailTest, FailRead) {
64 ASSERT_FALSE(reader_->ReadTemporalUnit(&tu_data_, nullptr));
65 }
66
TEST_P(FileReaderErrorTolerant,ReadThroughEndOfFile)67 TEST_P(FileReaderErrorTolerant, ReadThroughEndOfFile) {
68 while (!reader_->IsEndOfFile()) {
69 tu_data_.clear();
70 ASSERT_TRUE(reader_->ReadTemporalUnit(&tu_data_, nullptr));
71 ASSERT_GT(tu_data_.size(), 0);
72 }
73 }
74
TEST_P(FileReaderTestNoTimeStamps,ReadThroughEndOfFile)75 TEST_P(FileReaderTestNoTimeStamps, ReadThroughEndOfFile) {
76 while (!reader_->IsEndOfFile()) {
77 tu_data_.clear();
78 ASSERT_TRUE(reader_->ReadTemporalUnit(&tu_data_, nullptr));
79 }
80 }
81
TEST_P(FileReaderTestWithTimeStamps,ReadThroughEndOfFile)82 TEST_P(FileReaderTestWithTimeStamps, ReadThroughEndOfFile) {
83 int64_t timestamp = 0;
84 while (!reader_->IsEndOfFile()) {
85 tu_data_.clear();
86 ASSERT_TRUE(reader_->ReadTemporalUnit(&tu_data_, ×tamp));
87 if (!tu_data_.empty()) {
88 last_timestamp_ = timestamp;
89 }
90 }
91 ASSERT_TRUE(tu_data_.empty());
92 ASSERT_EQ(last_timestamp_, expected_last_timestamp_);
93 }
94
95 INSTANTIATE_TEST_SUITE_P(
96 FailRead, FileReaderFailTest,
97 testing::Values(
98 FileReaderTestParameters(FileReader::Open,
99 "ivf-header-and-truncated-frame-header"),
100 FileReaderTestParameters(FileReader::Open, "one-frame-truncated.ivf")));
101
102 INSTANTIATE_TEST_SUITE_P(ReadThroughEndOfFile, FileReaderErrorTolerant,
103 testing::Values(FileReaderTestParameters(
104 FileReader::Open, "one-frame-truncated.ivf")));
105
106 INSTANTIATE_TEST_SUITE_P(
107 ReadThroughEndOfFile, FileReaderTestNoTimeStamps,
108 testing::Values(FileReaderTestParameters(FileReader::Open, "one-frame.ivf"),
109 FileReaderTestParameters(FileReader::Open,
110 "one-frame-large-timestamp.ivf"),
111 FileReaderTestParameters(FileReader::Open,
112 "five-frames.ivf")));
113
114 INSTANTIATE_TEST_SUITE_P(
115 ReadThroughEndOfFile, FileReaderTestWithTimeStamps,
116 testing::Values(
117 FileReaderTestWithTimeStampsParameters(FileReader::Open,
118 "one-frame.ivf", 0),
119 FileReaderTestWithTimeStampsParameters(FileReader::Open,
120 "one-frame-large-timestamp.ivf",
121 4294967296),
122 FileReaderTestWithTimeStampsParameters(FileReader::Open,
123 "five-frames.ivf", 4)));
124
125 } // namespace
126 } // namespace libgav1
127