| // 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/zlib_util.h" | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/status/status_matchers.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| namespace credentio { | |
| namespace { | |
| TEST(ZlibUtilTest, CompressUncompressE2E) { | |
| std::string data = | |
| "hello world! this is a test string to be compressed. hello world!"; | |
| auto compressed = Compress(data); | |
| ABSL_ASSERT_OK(compressed); | |
| auto uncompressed = Uncompress(*compressed); | |
| ABSL_ASSERT_OK(uncompressed); | |
| EXPECT_EQ(*uncompressed, data); | |
| } | |
| TEST(ZlibUtilTest, UncompressInvalidDataFailed) { | |
| std::string data = "this is not a compressed string"; | |
| EXPECT_THAT(Uncompress(data), | |
| absl_testing::StatusIs(absl::StatusCode::kInternal, | |
| "Failed to uncompress data.")); | |
| } | |
| TEST(ZlibUtilTest, UncompressLargePayloadFailed) { | |
| std::string large_string(2 * 1024 * 1024, 'a'); | |
| auto compressed = Compress(large_string); | |
| ABSL_ASSERT_OK(compressed); | |
| // To prevent ZIP bomb attacks, uncompressing a payload exceeding the maximum | |
| // allowed size will fail. | |
| EXPECT_THAT(Uncompress(*compressed), | |
| absl_testing::StatusIs(absl::StatusCode::kInternal, | |
| "Failed to uncompress data.")); | |
| // Adjusting the maximum uncompressed size to a larger value will succeed. | |
| auto uncompressed = | |
| Uncompress(*compressed, /*max_uncompressed_size=*/3 * 1024 * 1024); | |
| ABSL_ASSERT_OK(uncompressed); | |
| EXPECT_EQ(*uncompressed, large_string); | |
| } | |
| } // namespace | |
| } // namespace credentio |