| // 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/hasher.h" | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/status/status_matchers.h" | |
| #include "absl/strings/str_cat.h" | |
| #include "absl/strings/string_view.h" | |
| #include "constants/labels.h" | |
| #include "crypto/algorithms.h" | |
| #include "formats/bmff/test_utils.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "proto/bmff_based_hash_assertion.pb.h" | |
| #include "riegeli/bytes/string_reader.h" | |
| #include "testing/proto_test_utils.h" | |
| namespace credentio { | |
| namespace { | |
| using ::absl_testing::StatusIs; | |
| using ::credentio::kC2paBmffBoxUuid; | |
| using ::credentio_testing::Box; | |
| using ::credentio_testing::BoxFlags; | |
| using ::credentio_testing::BoxVersion; | |
| using ::credentio_testing::EqualsProto; | |
| using ::credentio_testing::ParseTextProtoOrDie; | |
| using ::credentio_testing::UuidBoxPayload; | |
| using ::testing::HasSubstr; | |
| TEST(BmffHasherTest, GetHashAssertionTemplate) { | |
| auto expected = ParseTextProtoOrDie<BmffBasedHashAssertion>(R"pb( | |
| exclusions { xpath: "/ftyp" } | |
| exclusions { | |
| xpath: "/uuid" | |
| data { | |
| offset: 8 | |
| value: "\330\376\303\326\033\016H<\222\227X(\207~\304\201" | |
| } | |
| } | |
| exclusions { xpath: "/mfra" } | |
| exclusions { xpath: "/free" } | |
| alg: "sha256" | |
| hash: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" | |
| name: "BMFF file hash" | |
| )pb"); | |
| EXPECT_THAT(GetBmffHashTemplate(HashAlgorithm::kSha256), | |
| EqualsProto(expected)); | |
| } | |
| TEST(BmffHasherTest, Digest) { | |
| auto assertion = ParseTextProtoOrDie<BmffBasedHashAssertion>(R"pb( | |
| exclusions { xpath: "/ftyp" } | |
| exclusions { | |
| xpath: "/uuid" | |
| data { | |
| offset: 8 | |
| value: "\330\376\303\326\033\016H<\222\227X(\207~\304\201" | |
| } | |
| } | |
| exclusions { xpath: "/mfra" } | |
| exclusions { xpath: "/free" } | |
| alg: "sha256" | |
| hash: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" | |
| name: "BMFF file hash" | |
| )pb"); | |
| std::string input_contents = absl::StrCat( | |
| Box("uuid", | |
| UuidBoxPayload(kC2paBmffBoxUuid, BoxVersion(0), BoxFlags(0), "")), | |
| Box("trak", "ijkl")); | |
| riegeli::StringReader<> input(input_contents); | |
| ABSL_ASSERT_OK(DigestBmff(input, assertion)); | |
| } | |
| TEST(BmffHasherTest, HashSizeMismatch) { | |
| auto assertion = ParseTextProtoOrDie<BmffBasedHashAssertion>(R"pb( | |
| exclusions { xpath: "/ftyp" } | |
| exclusions { | |
| xpath: "/uuid" | |
| data { | |
| offset: 8 | |
| value: "\330\376\303\326\033\016H<\222\227X(\207~\304\201" | |
| } | |
| } | |
| exclusions { xpath: "/mfra" } | |
| exclusions { xpath: "/free" } | |
| alg: "sha256" | |
| hash: "invalid hash" | |
| name: "BMFF file hash" | |
| )pb"); | |
| std::string input_contents = absl::StrCat( | |
| Box("uuid", | |
| UuidBoxPayload(kC2paBmffBoxUuid, BoxVersion(0), BoxFlags(0), "")), | |
| Box("trak", "ijkl")); | |
| riegeli::StringReader<> input(input_contents); | |
| EXPECT_THAT( | |
| DigestBmff(input, assertion), | |
| StatusIs(absl::StatusCode::kInternal, HasSubstr("digest size mismatch"))); | |
| } | |
| } // namespace | |
| } // namespace credentio |