| // 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/zip/assessor.h" | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/status/status_matchers.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, ErrorTooFewBytes) { | |
| std::string zip = "P"; | |
| riegeli::StringReader<> reader(zip); | |
| EXPECT_THAT( | |
| ZipAssessor().IsSupported(reader), | |
| StatusIs(absl::StatusCode::kDataLoss, HasSubstr("kUnexpectedEof"))); | |
| } | |
| TEST(IsSupportedTest, FalseForInvalidBeginning) { | |
| std::string zip = "this_is_not_a_zip"; | |
| riegeli::StringReader<> reader(zip); | |
| EXPECT_THAT(ZipAssessor().IsSupported(reader), IsOkAndHolds(false)); | |
| } | |
| TEST(IsSupportedTest, ValidStartingBytes) { | |
| std::string zip = "PK\x03\x04\xab\xcd"; | |
| riegeli::StringReader<> reader(zip); | |
| EXPECT_THAT(ZipAssessor().IsSupported(reader), IsOkAndHolds(true)); | |
| } | |
| TEST(IsSupportedTest, ValidStartingBytesAtOffset2) { | |
| std::string zip = "\xab\xcdPK\x03\x04"; | |
| riegeli::StringReader<> reader(zip); | |
| // Invalid at offset 0 | |
| EXPECT_THAT(ZipAssessor().IsSupported(reader), IsOkAndHolds(false)); | |
| ASSERT_TRUE(reader.Seek(2)); | |
| // Valid at offset 2 | |
| EXPECT_THAT(ZipAssessor().IsSupported(reader), IsOkAndHolds(true)); | |
| } | |
| } // namespace | |
| } // namespace credentio |