xref: /aosp_15_r20/external/libaom/test/invalid_file_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <cstdio>
13 #include <ostream>
14 #include <string>
15 
16 #include "gtest/gtest.h"
17 #include "test/codec_factory.h"
18 #include "test/ivf_video_source.h"
19 #include "test/util.h"
20 #include "test/video_source.h"
21 
22 namespace {
23 
24 struct DecodeParam {
25   int threads;
26   const char *filename;
27   const char *res_filename;  // If nullptr, the result filename is
28                              // filename + ".res".
29 };
30 
31 // Constructs result file name.
GetResFilename(const DecodeParam & param)32 std::string GetResFilename(const DecodeParam &param) {
33   if (param.res_filename != nullptr) return param.res_filename;
34   const std::string filename = param.filename;
35   return filename + ".res";
36 }
37 
operator <<(std::ostream & os,const DecodeParam & dp)38 std::ostream &operator<<(std::ostream &os, const DecodeParam &dp) {
39   return os << "threads: " << dp.threads << " file: " << dp.filename
40             << " result file: " << GetResFilename(dp);
41 }
42 
43 class InvalidFileTest : public ::libaom_test::DecoderTest,
44                         public ::libaom_test::CodecTestWithParam<DecodeParam> {
45  protected:
InvalidFileTest()46   InvalidFileTest() : DecoderTest(GET_PARAM(0)), res_file_(nullptr) {}
47 
~InvalidFileTest()48   ~InvalidFileTest() override {
49     if (res_file_ != nullptr) fclose(res_file_);
50   }
51 
OpenResFile(const std::string & res_file_name)52   void OpenResFile(const std::string &res_file_name) {
53     res_file_ = libaom_test::OpenTestDataFile(res_file_name);
54     ASSERT_NE(res_file_, nullptr)
55         << "Result file open failed. Filename: " << res_file_name;
56   }
57 
DecompressedFrameHook(const aom_image_t & img,const unsigned int)58   void DecompressedFrameHook(const aom_image_t &img,
59                              const unsigned int /*frame_number*/) override {
60     EXPECT_NE(img.fb_priv, nullptr);
61   }
62 
HandleDecodeResult(const aom_codec_err_t res_dec,const libaom_test::CompressedVideoSource & video,libaom_test::Decoder * decoder)63   bool HandleDecodeResult(const aom_codec_err_t res_dec,
64                           const libaom_test::CompressedVideoSource &video,
65                           libaom_test::Decoder *decoder) override {
66     EXPECT_NE(res_file_, nullptr);
67     int expected_res_dec = -1;
68 
69     // Read integer result.
70     const int res = fscanf(res_file_, "%d", &expected_res_dec);
71     EXPECT_NE(res, EOF) << "Read result data failed";
72 
73     if (expected_res_dec != -1) {
74       // Check results match.
75       const DecodeParam input = GET_PARAM(1);
76       if (input.threads > 1) {
77         // The serial decode check is too strict for tile-threaded decoding as
78         // there is no guarantee on the decode order nor which specific error
79         // will take precedence. Currently a tile-level error is not forwarded
80         // so the frame will simply be marked corrupt.
81         EXPECT_TRUE(res_dec == expected_res_dec ||
82                     res_dec == AOM_CODEC_CORRUPT_FRAME)
83             << "Results don't match: frame number = " << video.frame_number()
84             << ". (" << decoder->DecodeError()
85             << "). Expected: " << expected_res_dec << " or "
86             << AOM_CODEC_CORRUPT_FRAME;
87       } else {
88         EXPECT_EQ(expected_res_dec, res_dec)
89             << "Results don't match: frame number = " << video.frame_number()
90             << ". (" << decoder->DecodeError() << ")";
91       }
92     }
93 
94     return !HasFailure();
95   }
96 
HandlePeekResult(libaom_test::Decoder * const,libaom_test::CompressedVideoSource *,const aom_codec_err_t)97   void HandlePeekResult(libaom_test::Decoder *const /*decoder*/,
98                         libaom_test::CompressedVideoSource * /*video*/,
99                         const aom_codec_err_t /*res_peek*/) override {}
100 
RunTest()101   void RunTest() {
102     const DecodeParam input = GET_PARAM(1);
103     aom_codec_dec_cfg_t cfg = { 0, 0, 0, !FORCE_HIGHBITDEPTH_DECODING };
104     cfg.threads = input.threads;
105     libaom_test::IVFVideoSource decode_video(input.filename);
106     decode_video.Init();
107 
108     // The result file holds a list of expected integer results, one for each
109     // decoded frame.  Any result that doesn't match the file's list will
110     // cause a test failure.
111     const std::string res_filename = GetResFilename(input);
112     OpenResFile(res_filename);
113 
114     ASSERT_NO_FATAL_FAILURE(RunLoop(&decode_video, cfg));
115   }
116 
117  private:
118   FILE *res_file_;
119 };
120 
TEST_P(InvalidFileTest,ReturnCode)121 TEST_P(InvalidFileTest, ReturnCode) { RunTest(); }
122 
123 // If res_filename (the third field) is nullptr, then the result filename is
124 // filename + ".res" by default. Set res_filename to a string if the result
125 // filename differs from the default.
126 const DecodeParam kAV1InvalidFileTests[] = {
127   // { threads, filename, res_filename }
128   { 1, "invalid-bug-1814.ivf", nullptr },
129   { 1, "invalid-chromium-906381.ivf", nullptr },
130   { 1, "invalid-google-142530197.ivf", nullptr },
131   { 1, "invalid-google-142530197-1.ivf", nullptr },
132   { 4, "invalid-oss-fuzz-9463.ivf", "invalid-oss-fuzz-9463.ivf.res.2" },
133   { 1, "invalid-oss-fuzz-9720.ivf", nullptr },
134   { 1, "invalid-oss-fuzz-10389.ivf", "invalid-oss-fuzz-10389.ivf.res.4" },
135 #if !defined(CHROMIUM) && !CONFIG_SIZE_LIMIT ||         \
136     (CONFIG_SIZE_LIMIT && DECODE_WIDTH_LIMIT >= 5120 && \
137      DECODE_HEIGHT_LIMIT >= 180)
138   { 1, "invalid-oss-fuzz-11523.ivf", "invalid-oss-fuzz-11523.ivf.res.2" },
139 #endif
140   { 4, "invalid-oss-fuzz-15363.ivf", nullptr },
141   { 1, "invalid-oss-fuzz-16437.ivf", "invalid-oss-fuzz-16437.ivf.res.2" },
142 #if CONFIG_MAX_DECODE_PROFILE >= 1
143   { 1, "invalid-oss-fuzz-24706.ivf", nullptr },
144 #endif
145 #if CONFIG_AV1_HIGHBITDEPTH
146   // These test vectors contain 10-bit or 12-bit video.
147   { 1, "invalid-oss-fuzz-9288.ivf", nullptr },
148   { 1, "invalid-oss-fuzz-9482.ivf", nullptr },
149   { 1, "invalid-oss-fuzz-10061.ivf", nullptr },
150   { 1, "invalid-oss-fuzz-10117-mc-buf-use-highbd.ivf", nullptr },
151   { 1, "invalid-oss-fuzz-10227.ivf", nullptr },
152   { 4, "invalid-oss-fuzz-10555.ivf", nullptr },
153   { 1, "invalid-oss-fuzz-10705.ivf", nullptr },
154 #if CONFIG_CWG_C013
155   { 1, "invalid-oss-fuzz-10723.ivf", "invalid-oss-fuzz-10723.ivf.res.3" },
156 #else
157   { 1, "invalid-oss-fuzz-10723.ivf", "invalid-oss-fuzz-10723.ivf.res.2" },
158 #endif
159   { 1, "invalid-oss-fuzz-10779.ivf", nullptr },
160   { 1, "invalid-oss-fuzz-11477.ivf", nullptr },
161   { 1, "invalid-oss-fuzz-11479.ivf", "invalid-oss-fuzz-11479.ivf.res.2" },
162   { 1, "invalid-oss-fuzz-33030.ivf", nullptr },
163 #endif
164 };
165 
166 AV1_INSTANTIATE_TEST_SUITE(InvalidFileTest,
167                            ::testing::ValuesIn(kAV1InvalidFileTests));
168 
169 }  // namespace
170