blob: 75251a60ca135935639d5eb15fcd14347f8a21f6 [file]
// 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/jpeg/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 image = "\xab";
riegeli::StringReader<> input(image);
EXPECT_THAT(
JpegAssessor().IsSupported(input),
StatusIs(absl::StatusCode::kDataLoss, HasSubstr("kUnexpectedEof")));
EXPECT_EQ(input.pos(), 0);
}
TEST(IsSupportedTest, FalseForInvalidBeginning) {
std::string image = "\xab\xcd";
riegeli::StringReader<> input(image);
EXPECT_THAT(JpegAssessor().IsSupported(input), IsOkAndHolds(false));
EXPECT_EQ(input.pos(), 0);
}
TEST(IsSupportedTest, ValidStartingBytes) {
std::string image = "\xff\xd8";
riegeli::StringReader<> input(image);
EXPECT_THAT(JpegAssessor().IsSupported(input), IsOkAndHolds(true));
EXPECT_EQ(input.pos(), 0);
}
TEST(IsSupportedTest, ValidStartingBytesAtOffset2) {
std::string image = "ab\xff\xd8";
riegeli::StringReader<> input(image);
// Invalid at 0
EXPECT_THAT(JpegAssessor().IsSupported(input), IsOkAndHolds(false));
EXPECT_EQ(input.pos(), 0);
// Valid at 2
ASSERT_TRUE(input.Seek(2));
EXPECT_THAT(JpegAssessor().IsSupported(input), IsOkAndHolds(true));
EXPECT_EQ(input.pos(), 2);
}
} // namespace
} // namespace credentio