2020-05-20 14:27:53 +00:00
|
|
|
#include "url.hh"
|
2024-08-23 05:44:29 +00:00
|
|
|
#include "types.hh"
|
2020-05-20 14:27:53 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/* ----------- tests for url.hh --------------------------------------------------*/
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string print_map(std::map<std::string, std::string> m) {
|
|
|
|
std::map<std::string, std::string>::iterator it;
|
|
|
|
std::string s = "{ ";
|
2020-05-20 14:27:53 +00:00
|
|
|
for (it = m.begin(); it != m.end(); ++it) {
|
|
|
|
s += "{ ";
|
|
|
|
s += it->first;
|
|
|
|
s += " = ";
|
|
|
|
s += it->second;
|
|
|
|
s += " } ";
|
|
|
|
}
|
|
|
|
s += "}";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const ParsedURL& p) {
|
|
|
|
return os << "\n"
|
|
|
|
<< "url: " << p.url << "\n"
|
|
|
|
<< "base: " << p.base << "\n"
|
|
|
|
<< "scheme: " << p.scheme << "\n"
|
|
|
|
<< "authority: " << p.authority.value() << "\n"
|
|
|
|
<< "path: " << p.path << "\n"
|
|
|
|
<< "query: " << print_map(p.query) << "\n"
|
|
|
|
<< "fragment: " << p.fragment << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesSimpleHttpUrl) {
|
|
|
|
auto s = "http://www.example.org/file.tar.gz";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://www.example.org/file.tar.gz",
|
|
|
|
.base = "http://www.example.org/file.tar.gz",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "www.example.org",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesSimpleHttpsUrl) {
|
|
|
|
auto s = "https://www.example.org/file.tar.gz";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "https://www.example.org/file.tar.gz",
|
|
|
|
.base = "https://www.example.org/file.tar.gz",
|
|
|
|
.scheme = "https",
|
|
|
|
.authority = "www.example.org",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesSimpleHttpUrlWithQueryAndFragment) {
|
|
|
|
auto s = "https://www.example.org/file.tar.gz?download=fast&when=now#hello";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "https://www.example.org/file.tar.gz",
|
|
|
|
.base = "https://www.example.org/file.tar.gz",
|
|
|
|
.scheme = "https",
|
|
|
|
.authority = "www.example.org",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { { "download", "fast" }, { "when", "now" } },
|
|
|
|
.fragment = "hello",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesSimpleHttpUrlWithComplexFragment) {
|
|
|
|
auto s = "http://www.example.org/file.tar.gz?field=value#?foo=bar%23";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://www.example.org/file.tar.gz",
|
|
|
|
.base = "http://www.example.org/file.tar.gz",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "www.example.org",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { { "field", "value" } },
|
|
|
|
.fragment = "?foo=bar#",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
2023-01-20 09:31:26 +00:00
|
|
|
TEST(parseURL, parsesFilePlusHttpsUrl) {
|
|
|
|
auto s = "file+https://www.example.org/video.mp4";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "file+https://www.example.org/video.mp4",
|
|
|
|
.base = "https://www.example.org/video.mp4",
|
|
|
|
.scheme = "file+https",
|
|
|
|
.authority = "www.example.org",
|
|
|
|
.path = "/video.mp4",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, rejectsAuthorityInUrlsWithFileTransportation) {
|
|
|
|
auto s = "file://www.example.org/video.mp4";
|
|
|
|
ASSERT_THROW(parseURL(s), Error);
|
|
|
|
}
|
2020-05-20 14:27:53 +00:00
|
|
|
|
|
|
|
TEST(parseURL, parseIPv4Address) {
|
|
|
|
auto s = "http://127.0.0.1:8080/file.tar.gz?download=fast&when=now#hello";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://127.0.0.1:8080/file.tar.gz",
|
|
|
|
.base = "https://127.0.0.1:8080/file.tar.gz",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "127.0.0.1:8080",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { { "download", "fast" }, { "when", "now" } },
|
|
|
|
.fragment = "hello",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:51:17 +00:00
|
|
|
TEST(parseURL, parseScopedRFC4007IPv6Address) {
|
|
|
|
auto s = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
|
|
|
|
.base = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
|
|
|
|
.path = "",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:27:53 +00:00
|
|
|
TEST(parseURL, parseIPv6Address) {
|
|
|
|
auto s = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
|
|
|
|
.base = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
|
|
|
|
.path = "",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parseEmptyQueryParams) {
|
|
|
|
auto s = "http://127.0.0.1:8080/file.tar.gz?&&&&&";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
ASSERT_EQ(parsed.query, (StringMap) { });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parseUserPassword) {
|
|
|
|
auto s = "http://user:pass@www.example.org:8080/file.tar.gz";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "http://user:pass@www.example.org/file.tar.gz",
|
|
|
|
.base = "http://user:pass@www.example.org/file.tar.gz",
|
|
|
|
.scheme = "http",
|
|
|
|
.authority = "user:pass@www.example.org:8080",
|
|
|
|
.path = "/file.tar.gz",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parseFileURLWithQueryAndFragment) {
|
2022-04-07 19:09:39 +00:00
|
|
|
auto s = "file:///none/of//your/business";
|
2020-05-20 14:27:53 +00:00
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "",
|
|
|
|
.base = "",
|
|
|
|
.scheme = "file",
|
|
|
|
.authority = "",
|
2022-04-07 19:09:39 +00:00
|
|
|
.path = "/none/of//your/business",
|
2020-05-20 14:27:53 +00:00
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsedUrlsIsEqualToItself) {
|
|
|
|
auto s = "http://www.example.org/file.tar.gz";
|
|
|
|
auto url = parseURL(s);
|
|
|
|
|
|
|
|
ASSERT_TRUE(url == url);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parseFTPUrl) {
|
|
|
|
auto s = "ftp://ftp.nixos.org/downloads/nixos.iso";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
|
|
|
|
ParsedURL expected {
|
|
|
|
.url = "ftp://ftp.nixos.org/downloads/nixos.iso",
|
|
|
|
.base = "ftp://ftp.nixos.org/downloads/nixos.iso",
|
|
|
|
.scheme = "ftp",
|
|
|
|
.authority = "ftp.nixos.org",
|
|
|
|
.path = "/downloads/nixos.iso",
|
|
|
|
.query = (StringMap) { },
|
|
|
|
.fragment = "",
|
|
|
|
};
|
|
|
|
|
|
|
|
ASSERT_EQ(parsed, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesAnythingInUriFormat) {
|
|
|
|
auto s = "whatever://github.com/NixOS/nixpkgs.git";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, parsesAnythingInUriFormatWithoutDoubleSlash) {
|
|
|
|
auto s = "whatever:github.com/NixOS/nixpkgs.git";
|
|
|
|
auto parsed = parseURL(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(parseURL, emptyStringIsInvalidURL) {
|
|
|
|
ASSERT_THROW(parseURL(""), Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* decodeQuery
|
|
|
|
* --------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
TEST(decodeQuery, emptyStringYieldsEmptyMap) {
|
|
|
|
auto d = decodeQuery("");
|
|
|
|
ASSERT_EQ(d, (StringMap) { });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(decodeQuery, simpleDecode) {
|
|
|
|
auto d = decodeQuery("yi=one&er=two");
|
|
|
|
ASSERT_EQ(d, ((StringMap) { { "yi", "one" }, { "er", "two" } }));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(decodeQuery, decodeUrlEncodedArgs) {
|
|
|
|
auto d = decodeQuery("arg=%3D%3D%40%3D%3D");
|
|
|
|
ASSERT_EQ(d, ((StringMap) { { "arg", "==@==" } }));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(decodeQuery, decodeArgWithEmptyValue) {
|
|
|
|
auto d = decodeQuery("arg=");
|
|
|
|
ASSERT_EQ(d, ((StringMap) { { "arg", ""} }));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* percentDecode
|
|
|
|
* --------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
TEST(percentDecode, decodesUrlEncodedString) {
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string s = "==@==";
|
|
|
|
std::string d = percentDecode("%3D%3D%40%3D%3D");
|
2020-05-20 14:27:53 +00:00
|
|
|
ASSERT_EQ(d, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(percentDecode, multipleDecodesAreIdempotent) {
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string once = percentDecode("%3D%3D%40%3D%3D");
|
|
|
|
std::string twice = percentDecode(once);
|
2020-05-20 14:27:53 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(once, twice);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(percentDecode, trailingPercent) {
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string s = "==@==%";
|
|
|
|
std::string d = percentDecode("%3D%3D%40%3D%3D%25");
|
2020-05-20 14:27:53 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(d, s);
|
|
|
|
}
|
|
|
|
|
2023-02-07 15:44:37 +00:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* percentEncode
|
|
|
|
* --------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
TEST(percentEncode, encodesUrlEncodedString) {
|
|
|
|
std::string s = percentEncode("==@==");
|
|
|
|
std::string d = "%3D%3D%40%3D%3D";
|
|
|
|
ASSERT_EQ(d, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(percentEncode, keepArgument) {
|
|
|
|
std::string a = percentEncode("abd / def");
|
|
|
|
std::string b = percentEncode("abd / def", "/");
|
|
|
|
ASSERT_EQ(a, "abd%20%2F%20def");
|
|
|
|
ASSERT_EQ(b, "abd%20/%20def");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(percentEncode, inverseOfDecode) {
|
|
|
|
std::string original = "%3D%3D%40%3D%3D";
|
|
|
|
std::string once = percentEncode(original);
|
|
|
|
std::string back = percentDecode(once);
|
|
|
|
|
|
|
|
ASSERT_EQ(back, original);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(percentEncode, trailingPercent) {
|
|
|
|
std::string s = percentEncode("==@==%");
|
|
|
|
std::string d = "%3D%3D%40%3D%3D%25";
|
|
|
|
|
|
|
|
ASSERT_EQ(d, s);
|
|
|
|
}
|
|
|
|
|
2024-11-10 13:39:21 +00:00
|
|
|
TEST(percentEncode, utf8Input) {
|
|
|
|
std::string s = percentEncode("ä");
|
|
|
|
std::string d = "%C3%A4";
|
|
|
|
|
|
|
|
ASSERT_EQ(d, s);
|
|
|
|
}
|
2020-05-20 14:27:53 +00:00
|
|
|
}
|