Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/libstore-tests/s3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace nix {

// =============================================================================
// ParsedS3URL Tests
// =============================================================================

struct ParsedS3URLTestCase
{
std::string url;
Expand Down Expand Up @@ -86,18 +90,41 @@ INSTANTIATE_TEST_SUITE_P(
}),
[](const ::testing::TestParamInfo<ParsedS3URLTestCase> & info) { return info.param.description; });

TEST(InvalidParsedS3URLTest, parseS3URLErrors)
// Parameterized test for invalid S3 URLs
struct InvalidS3URLTestCase
{
auto invalidBucketMatcher = ::testing::ThrowsMessage<BadURL>(
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<InvalidS3URLTestCase>, public ::testing::Test
{};

TEST_P(InvalidParsedS3URLTest, parseS3URLErrors)
{
const auto & testCase = GetParam();

ASSERT_THAT(
[&testCase]() { ParsedS3URL::parse(parseURL(testCase.url)); },
::testing::ThrowsMessage<BadURL>(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<InvalidS3URLTestCase> & info) { return info.param.description; });

// =============================================================================
// S3 URL to HTTPS Conversion Tests
// =============================================================================

struct S3ToHttpsConversionTestCase
{
ParsedS3URL input;
Expand Down
Loading