| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // |
| |
| #include "formats/bmff/assessor.h" |
| |
| #include <string> |
| |
| #include "absl/status/status_matchers.h" |
| #include "absl/strings/str_cat.h" |
| #include "formats/bmff/test_utils.h" |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "riegeli/bytes/string_reader.h" |
| |
| namespace credentio { |
| namespace { |
| |
| using ::absl_testing::IsOkAndHolds; |
| |
| TEST(IsSupportedTest, FailsForTooFewBytes) { |
| std::string image = "a"; |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| } |
| |
| TEST(IsSupportedTest, FailsForNotStartingWithFtyp) { |
| std::string image = "this_is_not_a_bmff"; |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| } |
| |
| TEST(IsSupportedTest, ValidStartingFtype) { |
| std::string image = credentio_testing::Box("ftyp", "heictest"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, ValidStartingMoov) { |
| std::string image = credentio_testing::Box("moov", "data"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, UnsupportedFileType) { |
| std::string image = credentio_testing::Box("ftyp", "testtest"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| } |
| |
| TEST(IsSupportedTest, SupportedFileTypeWithCompatibleBrand) { |
| std::string image = credentio_testing::Box("ftyp", "testtestmp42"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, ValidStartingBytesAtOffset2) { |
| std::string image = |
| absl::StrCat("ab", credentio_testing::Box("ftyp", "heictest")); |
| riegeli::StringReader<> input(image); |
| |
| // Invalid at 0 |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| EXPECT_EQ(input.pos(), 0); |
| |
| // Valid at 2 |
| ASSERT_TRUE(input.Seek(2)); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| EXPECT_EQ(input.pos(), 2); |
| } |
| |
| TEST(IsSupportedTest, StopsEarlyAfterFtyp) { |
| // Supported major brand followed by invalid truncated box data. |
| // Assessor should stop immediately and declare it supported, ignoring later |
| // invalid data. |
| std::string image = absl::StrCat(credentio_testing::Box("ftyp", "heictest"), |
| "invalid_truncated_box_data"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, StopsEarlyAfterFtypCompatibleBrand) { |
| // Supported compatible brand in ftyp followed by truncated box data should be |
| // supported. |
| std::string image = |
| absl::StrCat(credentio_testing::Box("ftyp", "testtestmp42"), |
| "invalid_truncated_box_data"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, StopsEarlyOnUnsupportedFtypAndIgnoresMoov) { |
| // Unsupported ftyp followed by a moov box. |
| // Assessor should terminate early and return unsupported (false) since it |
| // shouldn't inspect moov. |
| std::string image = absl::StrCat(credentio_testing::Box("ftyp", "testtest"), |
| credentio_testing::Box("moov", "data")); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| } |
| |
| TEST(IsSupportedTest, StopsEarlyAfterMoov) { |
| // Supported moov box followed by invalid truncated box data. |
| // Assessor should terminate early indicating supported (true). |
| std::string image = absl::StrCat(credentio_testing::Box("moov", "data"), |
| "invalid_truncated_box_data"); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(true)); |
| } |
| |
| TEST(IsSupportedTest, FtypBoxTooSmallForBrandsDoesNotUnderflow) { |
| // ftyp box size of 8 bytes followed by data containing a supported brand "qt |
| // " |
| std::string image = |
| absl::StrCat(credentio_testing::BadBox("ftyp", "", 8), "unkn0000qt "); |
| riegeli::StringReader<> input(image); |
| EXPECT_THAT(BmffAssessor().IsSupported(input), IsOkAndHolds(false)); |
| } |
| |
| } // namespace |
| } // namespace credentio |