blob: 37bf5dc6f1d76bb72d3cc8a4940d8de7d8fbfa04 [file] [edit]
// 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 "assertion/assertion_referencer.h"
#include <memory>
#include <string>
#include <utility>
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/status_macros.h" // IWYU pragma: keep
#include "absl/status/status_matchers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "crypto/mock_hash.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "jumbf/box_builder.h"
#include "jumbf/constants.h"
#include "jumbf/test_utils.h"
#include "proto/assertion.pb.h"
#include "proto/hashed_uri.pb.h"
#include "testing/proto_test_utils.h"
namespace credentio {
namespace {
using ::absl_testing::IsOkAndHolds;
using ::absl_testing::StatusIs;
using ::credentio_testing::EqualsProto;
using ::credentio_testing::ParseTextProtoOrDie;
using ::testing::HasSubstr;
using ::testing::Return;
TEST(AssertionReferencerTest, Generate) {
jumbf::SuperBoxBuilder builder(
jumbf::kCborBoxTypeUuid,
{.label = jumbf::CordFromConstexpr("c2pa.actions.v2")});
ASSERT_TRUE(builder
.AddContent(jumbf::kCborBoxType,
jumbf::CordFromConstexpr("fake CBOR"))
.ok());
auto box = std::move(builder).Finalize();
ABSL_ASSERT_OK(box);
std::string expected_hash_input = absl::StrCat(
jumbf::EncodeDescriptionBox(jumbf::kCborBoxTypeUuid, "c2pa.actions.v2",
/*requestable=*/true),
jumbf::WrapBox("fake CBOR", jumbf::kCborBoxType));
testing::StrictMock<MockHasherFactory> hasher_factory;
auto hasher = std::make_unique<MockHasher>();
EXPECT_CALL(*hasher, Update(expected_hash_input)).Times(1);
EXPECT_CALL(*hasher, Digest()).WillOnce(Return("FAKE-HASH"));
EXPECT_CALL(hasher_factory, Create()).WillOnce(Return(std::move(hasher)));
AssertionReferencer assertion_referencer(&hasher_factory);
auto expected = ParseTextProtoOrDie<HashedUri>(R"pb(
url: "self#jumbf=c2pa.assertions/c2pa.actions.v2"
hash: "FAKE-HASH"
)pb");
auto result = assertion_referencer.Generate(*box);
EXPECT_THAT(result, IsOkAndHolds(EqualsProto(expected)));
}
TEST(AssertionReferencerTest, FailsToGenerateWithoutLabel) {
jumbf::SuperBoxBuilder builder(jumbf::kCborBoxTypeUuid, {});
ASSERT_TRUE(builder
.AddContent(jumbf::kCborBoxType,
jumbf::CordFromConstexpr("fake CBOR"))
.ok());
auto box = std::move(builder).Finalize();
ABSL_ASSERT_OK(box);
testing::StrictMock<MockHasherFactory> hasher_factory;
AssertionReferencer assertion_referencer(&hasher_factory);
EXPECT_THAT(
assertion_referencer.Generate(*box),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("the assertion box must have a description label")));
}
} // namespace
} // namespace credentio