| // 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/block.h" | |
| #include <cstdint> | |
| #include <string> | |
| #include <vector> | |
| #include "absl/status/status_matchers.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "riegeli/bytes/string_writer.h" | |
| namespace credentio { | |
| namespace { | |
| TEST(BlockTest, Write10BytePayload) { | |
| std::string manifest_store = "0123456789"; | |
| std::vector<uint8_t> expected_output = { | |
| 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, | |
| }; | |
| std::string expected(expected_output.begin(), expected_output.end()); | |
| std::string output_s; | |
| riegeli::StringWriter<> output(&output_s); | |
| ABSL_ASSERT_OK(WriteGifC2paBlock(manifest_store, output)); | |
| ASSERT_TRUE(output.Close()); | |
| EXPECT_EQ(output_s, expected); | |
| EXPECT_EQ(output_s.size(), 26); | |
| } | |
| TEST(BlockTest, Write300BytePayload) { | |
| // Manifest store is 300 bytes long. | |
| std::string manifest_store(300, 'a'); | |
| // Construct the expected output. | |
| std::vector<uint8_t> expected_output = { | |
| 0x21, 0xFF, 0x0B, 'C', '2', 'P', 'A', '_', | |
| 'G', 'I', 'F', 0x01, 0x00, 0x00, 0xFF, | |
| }; | |
| expected_output.reserve(expected_output.size() + 302); | |
| for (uint32_t i = 0; i < 255; ++i) { | |
| expected_output.push_back('a'); | |
| } | |
| expected_output.push_back(45); | |
| for (uint32_t i = 0; i < 45; ++i) { | |
| expected_output.push_back('a'); | |
| } | |
| expected_output.push_back(0x00); | |
| std::string expected(expected_output.begin(), expected_output.end()); | |
| std::string output_s; | |
| riegeli::StringWriter<> output(&output_s); | |
| ABSL_ASSERT_OK(WriteGifC2paBlock(manifest_store, output)); | |
| ASSERT_TRUE(output.Close()); | |
| EXPECT_EQ(output_s, expected); | |
| EXPECT_EQ(output_s.size(), 317); | |
| } | |
| TEST(BlockTest, CalculateSizeOneSubBlock) { | |
| EXPECT_EQ(CalculateTotalGifC2paBlockSize(10), 26); | |
| } | |
| TEST(BlockTest, CalculateSizeMultipleSubBlocks) { | |
| EXPECT_EQ(CalculateTotalGifC2paBlockSize(1000), 1019); | |
| } | |
| } // namespace | |
| } // namespace credentio |