From b63d9fbc8799b62eed0b21e595d22ef660c3924e Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Costa Date: Fri, 19 Sep 2025 18:23:35 +0000 Subject: [PATCH] test(libstore): additional ParsedS3Url tests Extracted from the work in #13752 --- src/libstore-tests/s3.cc | 43 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/libstore-tests/s3.cc b/src/libstore-tests/s3.cc index 44a31ddc9..799e102fe 100644 --- a/src/libstore-tests/s3.cc +++ b/src/libstore-tests/s3.cc @@ -8,6 +8,10 @@ namespace nix { +// ============================================================================= +// ParsedS3URL Tests +// ============================================================================= + struct ParsedS3URLTestCase { std::string url; @@ -86,18 +90,41 @@ INSTANTIATE_TEST_SUITE_P( }), [](const ::testing::TestParamInfo & info) { return info.param.description; }); -TEST(InvalidParsedS3URLTest, parseS3URLErrors) +// Parameterized test for invalid S3 URLs +struct InvalidS3URLTestCase { - auto invalidBucketMatcher = ::testing::ThrowsMessage( - testing::HasSubstrIgnoreANSIMatcher("error: URI has a missing or invalid bucket name")); + std::string url; + std::string expectedErrorSubstring; + std::string description; +}; - /* Empty bucket (authority) */ - ASSERT_THAT([]() { ParsedS3URL::parse(parseURL("s3:///key")); }, invalidBucketMatcher); - /* Invalid bucket name */ - ASSERT_THAT([]() { ParsedS3URL::parse(parseURL("s3://127.0.0.1")); }, invalidBucketMatcher); +class InvalidParsedS3URLTest : public ::testing::WithParamInterface, public ::testing::Test +{}; + +TEST_P(InvalidParsedS3URLTest, parseS3URLErrors) +{ + const auto & testCase = GetParam(); + + ASSERT_THAT( + [&testCase]() { ParsedS3URL::parse(parseURL(testCase.url)); }, + ::testing::ThrowsMessage(testing::HasSubstrIgnoreANSIMatcher(testCase.expectedErrorSubstring))); } -// Parameterized test for s3ToHttpsUrl conversion +INSTANTIATE_TEST_SUITE_P( + InvalidUrls, + InvalidParsedS3URLTest, + ::testing::Values( + InvalidS3URLTestCase{"s3:///key", "error: URI has a missing or invalid bucket name", "empty_bucket"}, + InvalidS3URLTestCase{"s3://127.0.0.1", "error: URI has a missing or invalid bucket name", "ip_address_bucket"}, + InvalidS3URLTestCase{"s3://bucket with spaces/key", "is not a valid URL", "bucket_with_spaces"}, + InvalidS3URLTestCase{"s3://", "error: URI has a missing or invalid bucket name", "completely_empty"}, + InvalidS3URLTestCase{"s3://bucket", "error: URI has a missing or invalid key", "missing_key"}), + [](const ::testing::TestParamInfo & info) { return info.param.description; }); + +// ============================================================================= +// S3 URL to HTTPS Conversion Tests +// ============================================================================= + struct S3ToHttpsConversionTestCase { ParsedS3URL input;