| // 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/gif/embedder.h" | |
| #include <cstdint> | |
| #include <optional> | |
| #include <string> | |
| #include <vector> | |
| #include "absl/status/status_matchers.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "riegeli/bytes/string_reader.h" | |
| #include "riegeli/bytes/string_writer.h" | |
| namespace credentio { | |
| namespace { | |
| TEST(EmbedderTest, Write10BytePayload) { | |
| std::string image = "insert_@underscore"; | |
| riegeli::StringReader<> input(image); | |
| std::string manifest_store = "0123456789"; | |
| std::vector<uint8_t> expected_output = { | |
| 'i', 'n', 's', 'e', 'r', 't', 0x21, 0xFF, 0x0B, 'C', '2', | |
| 'P', 'A', '_', 'G', 'I', 'F', 0x01, 0x00, 0x00, 0x0a, '0', | |
| '1', '2', '3', '4', '5', '6', '7', '8', '9', 0x00, '_', | |
| '@', 'u', 'n', 'd', 'e', 'r', 's', 'c', 'o', 'r', 'e', | |
| }; | |
| std::string expected(expected_output.begin(), expected_output.end()); | |
| std::string output_s; | |
| riegeli::StringWriter<> output(&output_s); | |
| GifEmbedder embedder; | |
| ABSL_ASSERT_OK( | |
| embedder.CopyAndEmbed(input, manifest_store, 6, std::nullopt, output)); | |
| ASSERT_TRUE(output.Close()); | |
| EXPECT_EQ(output_s, expected); | |
| } | |
| TEST(EmbedderTest, ReplaceExistingPayload) { | |
| std::string image = "before_OLD_C2PA_after"; | |
| riegeli::StringReader<> input(image); | |
| std::string manifest_store = "0123456789"; | |
| std::vector<uint8_t> expected_output = { | |
| 'b', 'e', 'f', 'o', 'r', 'e', '_', 0x21, 0xFF, 0x0B, 'C', '2', 'P', | |
| 'A', '_', 'G', 'I', 'F', 0x01, 0x00, 0x00, 0x0a, '0', '1', '2', '3', | |
| '4', '5', '6', '7', '8', '9', 0x00, 'a', 'f', 't', 'e', 'r', | |
| }; | |
| std::string expected(expected_output.begin(), expected_output.end()); | |
| std::string output_s; | |
| riegeli::StringWriter<> output(&output_s); | |
| GifEmbedder embedder; | |
| ABSL_ASSERT_OK(embedder.CopyAndEmbed(input, manifest_store, 7, 9, output)); | |
| ASSERT_TRUE(output.Close()); | |
| EXPECT_EQ(output_s, expected); | |
| } | |
| } // namespace | |
| } // namespace credentio |