1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-25 19:51:00 +01:00

Add user@address:port support

This patch allows users to specify the connection port
in the store URLS like so:

```
nix store info --store "ssh-ng://localhost:22" --json
```

Previously this failed with: `error: failed to start SSH connection to 'localhost:22'`,
because the code did not distinguish the port from the hostname. This
patch remedies that problem by introducing a ParsedURL::Authority type
for working with parsed authority components of URIs.

Now that the URL parsing code is less ad-hoc we can
add more long-awaited fixes for specifying SSH connection
ports in store URIs.

Builds upon the work from bd1d2d1041.

Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
Co-authored-by: John Ericson <John.Ericson@Obsidian.Systems>
This commit is contained in:
Maciej Krüger 2020-04-25 16:07:41 +02:00 committed by Sergei Zimmerman
parent c98af65da6
commit 49ba06175e
No known key found for this signature in database
18 changed files with 312 additions and 101 deletions

View file

@ -7,20 +7,8 @@ namespace nix {
/* ----------- tests for url.hh --------------------------------------------------*/
std::string print_map(StringMap m)
{
StringMap::iterator it;
std::string s = "{ ";
for (it = m.begin(); it != m.end(); ++it) {
s += "{ ";
s += it->first;
s += " = ";
s += it->second;
s += " } ";
}
s += "}";
return s;
}
using Authority = ParsedURL::Authority;
using HostType = Authority::HostType;
TEST(parseURL, parsesSimpleHttpUrl)
{
@ -29,13 +17,14 @@ TEST(parseURL, parsesSimpleHttpUrl)
ParsedURL expected{
.scheme = "http",
.authority = "www.example.org",
.authority = Authority{.hostType = HostType::Name, .host = "www.example.org"},
.path = "/file.tar.gz",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsesSimpleHttpsUrl)
@ -45,13 +34,14 @@ TEST(parseURL, parsesSimpleHttpsUrl)
ParsedURL expected{
.scheme = "https",
.authority = "www.example.org",
.authority = Authority{.hostType = HostType::Name, .host = "www.example.org"},
.path = "/file.tar.gz",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsesSimpleHttpUrlWithQueryAndFragment)
@ -61,13 +51,14 @@ TEST(parseURL, parsesSimpleHttpUrlWithQueryAndFragment)
ParsedURL expected{
.scheme = "https",
.authority = "www.example.org",
.authority = Authority{.hostType = HostType::Name, .host = "www.example.org"},
.path = "/file.tar.gz",
.query = (StringMap) {{"download", "fast"}, {"when", "now"}},
.fragment = "hello",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsesSimpleHttpUrlWithComplexFragment)
@ -77,7 +68,7 @@ TEST(parseURL, parsesSimpleHttpUrlWithComplexFragment)
ParsedURL expected{
.scheme = "http",
.authority = "www.example.org",
.authority = Authority{.hostType = HostType::Name, .host = "www.example.org"},
.path = "/file.tar.gz",
.query = (StringMap) {{"field", "value"}},
.fragment = "?foo=bar#",
@ -93,13 +84,14 @@ TEST(parseURL, parsesFilePlusHttpsUrl)
ParsedURL expected{
.scheme = "file+https",
.authority = "www.example.org",
.authority = Authority{.hostType = HostType::Name, .host = "www.example.org"},
.path = "/video.mp4",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, rejectsAuthorityInUrlsWithFileTransportation)
@ -115,13 +107,14 @@ TEST(parseURL, parseIPv4Address)
ParsedURL expected{
.scheme = "http",
.authority = "127.0.0.1:8080",
.authority = Authority{.hostType = HostType::IPv4, .host = "127.0.0.1", .port = 8080},
.path = "/file.tar.gz",
.query = (StringMap) {{"download", "fast"}, {"when", "now"}},
.fragment = "hello",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parseScopedRFC6874IPv6Address)
@ -131,13 +124,14 @@ TEST(parseURL, parseScopedRFC6874IPv6Address)
ParsedURL expected{
.scheme = "http",
.authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
.authority = Authority{.hostType = HostType::IPv6, .host = "fe80::818c:da4d:8975:415c\%enp0s25", .port = 8080},
.path = "",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parseIPv6Address)
@ -147,13 +141,19 @@ TEST(parseURL, parseIPv6Address)
ParsedURL expected{
.scheme = "http",
.authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
.authority =
Authority{
.hostType = HostType::IPv6,
.host = "2a02:8071:8192:c100:311d:192d:81ac:11ea",
.port = 8080,
},
.path = "",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parseEmptyQueryParams)
@ -170,13 +170,21 @@ TEST(parseURL, parseUserPassword)
ParsedURL expected{
.scheme = "http",
.authority = "user:pass@www.example.org:8080",
.authority =
Authority{
.hostType = HostType::Name,
.host = "www.example.org",
.user = "user",
.password = "pass",
.port = 8080,
},
.path = "/file.tar.gz",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parseFileURLWithQueryAndFragment)
@ -186,13 +194,14 @@ TEST(parseURL, parseFileURLWithQueryAndFragment)
ParsedURL expected{
.scheme = "file",
.authority = "",
.authority = Authority{},
.path = "/none/of//your/business",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsedUrlsIsEqualToItself)
@ -210,25 +219,28 @@ TEST(parseURL, parseFTPUrl)
ParsedURL expected{
.scheme = "ftp",
.authority = "ftp.nixos.org",
.authority = Authority{.hostType = HostType::Name, .host = "ftp.nixos.org"},
.path = "/downloads/nixos.iso",
.query = (StringMap) {},
.fragment = "",
};
ASSERT_EQ(parsed, expected);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsesAnythingInUriFormat)
{
auto s = "whatever://github.com/NixOS/nixpkgs.git";
auto parsed = parseURL(s);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, parsesAnythingInUriFormatWithoutDoubleSlash)
{
auto s = "whatever:github.com/NixOS/nixpkgs.git";
auto parsed = parseURL(s);
ASSERT_EQ(s, parsed.to_string());
}
TEST(parseURL, emptyStringIsInvalidURL)