| // 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/png/assessor.h" | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/status/status_matchers.h" | |
| #include "absl/strings/str_cat.h" | |
| #include "formats/png/constants.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "riegeli/bytes/string_reader.h" | |
| namespace credentio { | |
| namespace { | |
| using ::absl_testing::IsOkAndHolds; | |
| using ::absl_testing::StatusIs; | |
| using ::testing::HasSubstr; | |
| TEST(IsSupportedTest, FailureTooShort) { | |
| std::string image = "test"; | |
| riegeli::StringReader<> input(image); | |
| EXPECT_THAT( | |
| PngAssessor().IsSupported(input), | |
| StatusIs(absl::StatusCode::kDataLoss, HasSubstr("kUnexpectedEof"))); | |
| } | |
| TEST(IsSupportedTest, FailureIncorrectHeader) { | |
| std::string image = "this_is_not_a_png"; | |
| riegeli::StringReader<> input(image); | |
| EXPECT_THAT(PngAssessor().IsSupported(input), IsOkAndHolds(false)); | |
| } | |
| TEST(IsSupportedTest, Succeeds) { | |
| std::string image = absl::StrCat(kPngHeader, "this_is_a_png"); | |
| riegeli::StringReader<> input(image); | |
| EXPECT_THAT(PngAssessor().IsSupported(input), IsOkAndHolds(true)); | |
| } | |
| TEST(IsSupportedTest, SucceedsAtOffset2) { | |
| std::string image = absl::StrCat("ab", kPngHeader, "this_is_a_png"); | |
| riegeli::StringReader<> input(image); | |
| // Invalid at 0 | |
| EXPECT_THAT(PngAssessor().IsSupported(input), IsOkAndHolds(false)); | |
| EXPECT_EQ(input.pos(), 0); | |
| // Valid at 2 | |
| ASSERT_TRUE(input.Seek(2)); | |
| EXPECT_THAT(PngAssessor().IsSupported(input), IsOkAndHolds(true)); | |
| EXPECT_EQ(input.pos(), 2); | |
| } | |
| } // namespace | |
| } // namespace credentio |