From 96d2d42911c0f438109d4db23ef47c08782ab1ac Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 14 Apr 2022 10:22:51 -0400 Subject: net: move url tests into util --- tests/libgit2/network/url/joinpath.c | 194 ------------ tests/libgit2/network/url/parse.c | 557 ----------------------------------- tests/libgit2/network/url/pattern.c | 103 ------- tests/libgit2/network/url/redirect.c | 147 --------- tests/libgit2/network/url/scp.c | 321 -------------------- tests/libgit2/network/url/valid.c | 17 -- tests/util/url/joinpath.c | 194 ++++++++++++ tests/util/url/parse.c | 557 +++++++++++++++++++++++++++++++++++ tests/util/url/pattern.c | 103 +++++++ tests/util/url/redirect.c | 147 +++++++++ tests/util/url/scp.c | 321 ++++++++++++++++++++ tests/util/url/valid.c | 17 ++ 12 files changed, 1339 insertions(+), 1339 deletions(-) delete mode 100644 tests/libgit2/network/url/joinpath.c delete mode 100644 tests/libgit2/network/url/parse.c delete mode 100644 tests/libgit2/network/url/pattern.c delete mode 100644 tests/libgit2/network/url/redirect.c delete mode 100644 tests/libgit2/network/url/scp.c delete mode 100644 tests/libgit2/network/url/valid.c create mode 100644 tests/util/url/joinpath.c create mode 100644 tests/util/url/parse.c create mode 100644 tests/util/url/pattern.c create mode 100644 tests/util/url/redirect.c create mode 100644 tests/util/url/scp.c create mode 100644 tests/util/url/valid.c diff --git a/tests/libgit2/network/url/joinpath.c b/tests/libgit2/network/url/joinpath.c deleted file mode 100644 index bf4557138..000000000 --- a/tests/libgit2/network/url/joinpath.c +++ /dev/null @@ -1,194 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" -#include "netops.h" - -static git_net_url source, target; - -void test_network_url_joinpath__initialize(void) -{ - memset(&source, 0, sizeof(source)); - memset(&target, 0, sizeof(target)); -} - -void test_network_url_joinpath__cleanup(void) -{ - git_net_url_dispose(&source); - git_net_url_dispose(&target); -} - -void test_network_url_joinpath__target_paths_and_queries(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); - cl_assert_equal_s(target.path, "/a/b/c/d"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); - cl_assert_equal_s(target.path, "/a/b/c/d"); - cl_assert_equal_s(target.query, "foo"); - git_net_url_dispose(&target); -} - -void test_network_url_joinpath__source_query_removed(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b?query&one&two")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); - cl_assert_equal_s(target.path, "/a/b/c/d"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); - cl_assert_equal_s(target.path, "/a/b/c/d"); - cl_assert_equal_s(target.query, "foo"); - git_net_url_dispose(&target); -} - -void test_network_url_joinpath__source_lacks_path(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); -} - -void test_network_url_joinpath__source_is_slash(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com/")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); -} - - -void test_network_url_joinpath__source_has_query(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com?query")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "")); - cl_assert_equal_s(target.path, "/"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); - cl_assert_equal_s(target.path, "/asdf"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); - cl_assert_equal_s(target.path, "/foo/bar"); - cl_assert_equal_s(target.query, "hello"); - git_net_url_dispose(&target); -} - - -void test_network_url_joinpath__empty_query_ignored(void) -{ - cl_git_pass(git_net_url_parse(&source, "http://example.com/foo")); - - cl_git_pass(git_net_url_joinpath(&target, &source, "/bar/baz?")); - cl_assert_equal_s(target.path, "/foo/bar/baz"); - cl_assert_equal_p(target.query, NULL); - git_net_url_dispose(&target); -} diff --git a/tests/libgit2/network/url/parse.c b/tests/libgit2/network/url/parse.c deleted file mode 100644 index 8149ba52c..000000000 --- a/tests/libgit2/network/url/parse.c +++ /dev/null @@ -1,557 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" - -static git_net_url conndata; - -void test_network_url_parse__initialize(void) -{ - memset(&conndata, 0, sizeof(conndata)); -} - -void test_network_url_parse__cleanup(void) -{ - git_net_url_dispose(&conndata); -} - -/* Hostname */ - -void test_network_url_parse__hostname_trivial(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com/")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_implied_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_implied_root_custom_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com:42")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "42"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__hostname_implied_root_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com:")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_encoded_password(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@hostname.com:1234/")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "hostname.com"); - cl_assert_equal_s(conndata.port, "1234"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass/is@bad"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__hostname_user(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@example.com/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_user_pass(void) -{ - /* user:pass@hostname.tld/resource */ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@example.com/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_port(void) -{ - /* hostname.tld:port/resource */ - cl_git_pass(git_net_url_parse(&conndata, - "https://example.com:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__hostname_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://example.com:/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__hostname_user_port(void) -{ - /* user@hostname.tld:port/resource */ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@example.com:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__hostname_user_pass_port(void) -{ - /* user:pass@hostname.tld:port/resource */ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@example.com:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -/* IPv4 addresses */ - -void test_network_url_parse__ipv4_trivial(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_implied_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_implied_root_custom_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:42")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "42"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv4_implied_root_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_encoded_password(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@192.168.1.1:1234/")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "1234"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass/is@bad"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv4_user(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@192.168.1.1/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_user_pass(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@192.168.1.1/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://192.168.1.1:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv4_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv4_user_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@192.168.1.1:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv4_user_pass_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@192.168.1.1:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "192.168.1.1"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -/* IPv6 addresses */ - -void test_network_url_parse__ipv6_trivial(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_implied_root(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_implied_root_custom_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:42")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "42"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv6_implied_root_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_encoded_password(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001]:1234/")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "1234"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass/is@bad"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv6_user(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@[fe80::dcad:beff:fe00:0001]/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_user_pass(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@[fe80::dcad:beff:fe00:0001]/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://[fe80::dcad:beff:fe00:0001]:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv6_empty_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:/resource")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_parse__ipv6_user_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user@[fe80::dcad:beff:fe00:0001]:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv6_user_pass_port(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user:pass@[fe80::dcad:beff:fe00:0001]:9191/resource")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); - cl_assert_equal_s(conndata.port, "9191"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "user"); - cl_assert_equal_s(conndata.password, "pass"); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_parse__ipv6_invalid_addresses(void) -{ - /* Opening bracket missing */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]:42")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001]:1234/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@fe80::dcad:beff:fe00:0001]/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@fe80::dcad:beff:fe00:0001]/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://fe80::dcad:beff:fe00:0001]:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001]:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@fe80::dcad:beff:fe00:0001]:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@fe80::dcad:beff:fe00:0001]:9191/resource")); - - /* Closing bracket missing */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001:42")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001:1234/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@[fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@[fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://[fe80::dcad:beff:fe00:0001:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://[fe80::dcad:beff:fe00:0001:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@[fe80::dcad:beff:fe00:0001:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@[fe80::dcad:beff:fe00:0001:9191/resource")); - /* Both brackets missing */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001:42")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001:1234/")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@fe80::dcad:beff:fe00:0001/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://fe80::dcad:beff:fe00:0001:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "http://fe80::dcad:beff:fe00:0001:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user@fe80::dcad:beff:fe00:0001:9191/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "https://user:pass@fe80::dcad:beff:fe00:0001:9191/resource")); - - /* Invalid character inside address */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe8o::dcad:beff:fe00:0001]/resource")); -} diff --git a/tests/libgit2/network/url/pattern.c b/tests/libgit2/network/url/pattern.c deleted file mode 100644 index 5e4495f70..000000000 --- a/tests/libgit2/network/url/pattern.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" - -struct url_pattern { - const char *url; - const char *pattern; - bool matches; -}; - -void test_network_url_pattern__single(void) -{ - git_net_url url; - size_t i; - - struct url_pattern url_patterns[] = { - /* Wildcard matches */ - { "https://example.com/", "", false }, - { "https://example.com/", "*", true }, - - /* Literal and wildcard matches */ - { "https://example.com/", "example.com", true }, - { "https://example.com/", ".example.com", true }, - { "https://example.com/", "*.example.com", true }, - { "https://www.example.com/", "www.example.com", true }, - { "https://www.example.com/", ".example.com", true }, - { "https://www.example.com/", "*.example.com", true }, - - /* Literal and wildcard failures */ - { "https://example.com/", "example.org", false }, - { "https://example.com/", ".example.org", false }, - { "https://example.com/", "*.example.org", false }, - { "https://foo.example.com/", "www.example.com", false }, - - /* - * A port in the pattern is optional; if no port is - * present, it matches *all* ports. - */ - { "https://example.com/", "example.com:443", true }, - { "https://example.com/", "example.com:80", false }, - { "https://example.com:1443/", "example.com", true }, - - /* Failures with similar prefix/suffix */ - { "https://texample.com/", "example.com", false }, - { "https://example.com/", "mexample.com", false }, - { "https://example.com:44/", "example.com:443", false }, - { "https://example.com:443/", "example.com:44", false }, - }; - - for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { - cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); - cl_assert_(git_net_url_matches_pattern(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); - git_net_url_dispose(&url); - } -} - -void test_network_url_pattern__list(void) -{ - git_net_url url; - size_t i; - - struct url_pattern url_patterns[] = { - /* Wildcard matches */ - { "https://example.com/", "", false }, - { "https://example.com/", "*", true }, - { "https://example.com/", ",example.com,", true }, - { "https://example.com/", "foo,,example.com,,bar", true }, - { "https://example.com/", "foo,,zzz,,*,,bar", true }, - - /* Literals */ - { "https://example.com/", "example.com", true }, - { "https://example.com/", "foo.bar,example.com", true }, - { "https://example.com/", "foo.bar", false }, - { "https://example.com/", "foo.bar,example.org", false }, - { "https://www.example.com/", "foo.example.com,www.example.com,bar.example.com", true }, - { "https://www.example.com/", "foo.example.com,baz.example.com,bar.example.com", false }, - { "https://foo.example.com/", "www.example.com", false }, - { "https://foo.example.com/", "bar.example.com,www.example.com,", false }, - - /* Wildcards */ - { "https://example.com/", ".example.com", true }, - { "https://example.com/", "*.example.com", true }, - { "https://example.com/", "foo.com,bar.com,.example.com", true }, - { "https://example.com/", ".foo.com,.bar.com,.example.com", true }, - { "https://example.com/", ".foo.com,.bar.com,asdf.com", false }, - { "https://example.com/", "*.foo,*.bar,*.example.com,*.asdf", true }, - { "https://example.com/", "*.foo,*.bar,*.asdf", false }, - - - /* Ports! */ - { "https://example.com/", "example.com:443", true }, - { "https://example.com/", "example.com:42,example.com:443,example.com:99", true }, - { "https://example.com/", "example.com:42,example.com:80,example.org:443", false }, - { "https://example.com:1443/", "example.com", true }, - { "https://example.com:44/", "example.com:443", false }, - { "https://example.com:443/", "example.com:44", false }, - }; - - for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { - cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); - cl_assert_(git_net_url_matches_pattern_list(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); - git_net_url_dispose(&url); - } -} diff --git a/tests/libgit2/network/url/redirect.c b/tests/libgit2/network/url/redirect.c deleted file mode 100644 index a94db7daf..000000000 --- a/tests/libgit2/network/url/redirect.c +++ /dev/null @@ -1,147 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" -#include "netops.h" - -static git_net_url conndata; - -void test_network_url_redirect__initialize(void) -{ - memset(&conndata, 0, sizeof(conndata)); -} - -void test_network_url_redirect__cleanup(void) -{ - git_net_url_dispose(&conndata); -} - -void test_network_url_redirect__redirect_http(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "http://example.com/foo/bar/baz")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "http://example.com/foo/bar/baz", false, "bar/baz")); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/foo/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__redirect_ssl(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://example.com/foo/bar/baz")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "https://example.com/foo/bar/baz", false, "bar/baz")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/foo/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__redirect_leaves_root_path(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://example.com/foo/bar/baz")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "https://example.com/foo/bar/baz", false, "/foo/bar/baz")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__redirect_encoded_username_password(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz", false, "bar/baz")); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/foo/"); - cl_assert_equal_s(conndata.username, "user/name"); - cl_assert_equal_s(conndata.password, "pass@word%zyx%v"); -} - -void test_network_url_redirect__redirect_cross_host_allowed(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://bar.com/bar/baz")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "https://foo.com/bar/baz", true, NULL)); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "foo.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/bar/baz"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__redirect_cross_host_denied(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://bar.com/bar/baz")); - cl_git_fail_with(git_net_url_apply_redirect(&conndata, - "https://foo.com/bar/baz", false, NULL), -1); -} - -void test_network_url_redirect__redirect_http_downgrade_denied(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://foo.com/bar/baz")); - cl_git_fail_with(git_net_url_apply_redirect(&conndata, - "http://foo.com/bar/baz", true, NULL), -1); -} - -void test_network_url_redirect__redirect_relative(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "http://foo.com/bar/baz/biff")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "/zap/baz/biff?bam", true, NULL)); - cl_assert_equal_s(conndata.scheme, "http"); - cl_assert_equal_s(conndata.host, "foo.com"); - cl_assert_equal_s(conndata.port, "80"); - cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__redirect_relative_ssl(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://foo.com/bar/baz/biff")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "/zap/baz/biff?bam", true, NULL)); - cl_assert_equal_s(conndata.scheme, "https"); - cl_assert_equal_s(conndata.host, "foo.com"); - cl_assert_equal_s(conndata.port, "443"); - cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); -} - -void test_network_url_redirect__service_query_no_query_params_in_location(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://foo.com/bar/info/refs?service=git-upload-pack")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "/baz/info/refs", true, "/info/refs?service=git-upload-pack")); - cl_assert_equal_s(conndata.path, "/baz"); -} - -void test_network_url_redirect__service_query_with_query_params_in_location(void) -{ - cl_git_pass(git_net_url_parse(&conndata, - "https://foo.com/bar/info/refs?service=git-upload-pack")); - cl_git_pass(git_net_url_apply_redirect(&conndata, - "/baz/info/refs?service=git-upload-pack", true, "/info/refs?service=git-upload-pack")); - cl_assert_equal_s(conndata.path, "/baz"); -} diff --git a/tests/libgit2/network/url/scp.c b/tests/libgit2/network/url/scp.c deleted file mode 100644 index 8cdc832ae..000000000 --- a/tests/libgit2/network/url/scp.c +++ /dev/null @@ -1,321 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" - -static git_net_url conndata; - -void test_network_url_scp__initialize(void) -{ - memset(&conndata, 0, sizeof(conndata)); -} - -void test_network_url_scp__cleanup(void) -{ - git_net_url_dispose(&conndata); -} - -/* Hostname */ - -void test_network_url_scp__hostname_trivial(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__hostname_bracketed(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__hostname_root(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__hostname_user(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "git@example.com:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__hostname_user_bracketed(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[git@example.com]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__hostname_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com:42]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "42"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__hostname_user_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[git@example.com:42]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "example.com"); - cl_assert_equal_s(conndata.port, "42"); - cl_assert_equal_s(conndata.path, "/resource"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__ipv4_trivial(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "192.168.99.88:/resource/a/b/c")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "192.168.99.88"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/a/b/c"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__ipv4_bracketed(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88]:/resource/a/b/c")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "192.168.99.88"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/a/b/c"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__ipv4_user(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "git@192.168.99.88:/resource/a/b/c")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "192.168.99.88"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/a/b/c"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__ipv4_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88:1111]:/resource/a/b/c")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "192.168.99.88"); - cl_assert_equal_s(conndata.port, "1111"); - cl_assert_equal_s(conndata.path, "/resource/a/b/c"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__ipv4_user_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[git@192.168.99.88:1111]:/resource/a/b/c")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "192.168.99.88"); - cl_assert_equal_s(conndata.port, "1111"); - cl_assert_equal_s(conndata.path, "/resource/a/b/c"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__ipv6_trivial(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:/resource/foo")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/foo"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__ipv6_user(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:/resource/foo")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/foo"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__ipv6_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "99"); - cl_assert_equal_s(conndata.path, "/resource/foo"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__ipv6_user_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[git@[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "99"); - cl_assert_equal_s(conndata.path, "/resource/foo"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); -} - -void test_network_url_scp__hexhost_and_port(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[fe:22]:/resource/foo")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "fe"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "/resource/foo"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__malformed_ipv6_one(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "fe80::dcad:beff:fe00:0001]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "fe80"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, ":dcad:beff:fe00:0001]:/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__malformed_ipv6_two(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:42]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "42]:/resource"); - cl_assert_equal_p(conndata.username, NULL); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__malformed_ipv6_with_user(void) -{ - cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:42]:/resource")); - cl_assert_equal_s(conndata.scheme, "ssh"); - cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); - cl_assert_equal_s(conndata.port, "22"); - cl_assert_equal_s(conndata.path, "42]:/resource"); - cl_assert_equal_s(conndata.username, "git"); - cl_assert_equal_p(conndata.password, NULL); - cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); -} - -void test_network_url_scp__invalid_addresses(void) -{ - /* Path is required */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "example.com")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "example.com:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[example.com:42]:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[git@example.com:42]:")); - - /* Host is required */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - ":")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - ":foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "git@:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[]:")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "git@[]:")); - - /* User is required if specified */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "@example.com:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "@:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[@localhost:22]:foo")); - - /* Port is required in brackets */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[example.com:]:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[git@example.com:]:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[fe:]:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[@localhost]:foo")); - - /* Extra brackets are disallowed */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[git@[[fe80::dcad:beff:fe00:0001]]:42]:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[[git@[fe80::dcad:beff:fe00:0001]]:42]:foo")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[[git@[fe80::dcad:beff:fe00:0001]:42]]:foo")); - - /* Closing bracket missing */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[fe80::dcad:beff:fe00:0001:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[[fe80::dcad:beff:fe00:0001]:42:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[git@[fe80::dcad:beff:fe00:0001]:42:/resource")); - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, - "[git@[fe80::dcad:beff:fe00:0001:42]:/resource")); - - /* Invalid character inside address */ - cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, - "[fe8o::dcad:beff:fe00:0001]:/resource")); -} diff --git a/tests/libgit2/network/url/valid.c b/tests/libgit2/network/url/valid.c deleted file mode 100644 index 2b2cb7ba4..000000000 --- a/tests/libgit2/network/url/valid.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "clar_libgit2.h" -#include "net.h" - -void test_network_url_valid__test(void) -{ - cl_assert(git_net_str_is_url("http://example.com/")); - cl_assert(git_net_str_is_url("file://localhost/tmp/foo/")); - cl_assert(git_net_str_is_url("ssh://user@host:42/tmp")); - cl_assert(git_net_str_is_url("git+ssh://user@host:42/tmp")); - cl_assert(git_net_str_is_url("ssh+git://user@host:42/tmp")); - cl_assert(git_net_str_is_url("https://user:pass@example.com/foo/bar")); - - cl_assert(!git_net_str_is_url("host:foo.git")); - cl_assert(!git_net_str_is_url("host:/foo.git")); - cl_assert(!git_net_str_is_url("[host:42]:/foo.git")); - cl_assert(!git_net_str_is_url("[user@host:42]:/foo.git")); -} diff --git a/tests/util/url/joinpath.c b/tests/util/url/joinpath.c new file mode 100644 index 000000000..9fc02cde4 --- /dev/null +++ b/tests/util/url/joinpath.c @@ -0,0 +1,194 @@ +#include "clar_libgit2.h" +#include "net.h" +#include "netops.h" + +static git_net_url source, target; + +void test_url_joinpath__initialize(void) +{ + memset(&source, 0, sizeof(source)); + memset(&target, 0, sizeof(target)); +} + +void test_url_joinpath__cleanup(void) +{ + git_net_url_dispose(&source); + git_net_url_dispose(&target); +} + +void test_url_joinpath__target_paths_and_queries(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); + cl_assert_equal_s(target.path, "/a/b/c/d"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); + cl_assert_equal_s(target.path, "/a/b/c/d"); + cl_assert_equal_s(target.query, "foo"); + git_net_url_dispose(&target); +} + +void test_url_joinpath__source_query_removed(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b?query&one&two")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); + cl_assert_equal_s(target.path, "/a/b/c/d"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); + cl_assert_equal_s(target.path, "/a/b/c/d"); + cl_assert_equal_s(target.query, "foo"); + git_net_url_dispose(&target); +} + +void test_url_joinpath__source_lacks_path(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); +} + +void test_url_joinpath__source_is_slash(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com/")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); +} + + +void test_url_joinpath__source_has_query(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com?query")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "")); + cl_assert_equal_s(target.path, "/"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); + cl_assert_equal_s(target.path, "/asdf"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); + cl_assert_equal_s(target.path, "/foo/bar"); + cl_assert_equal_s(target.query, "hello"); + git_net_url_dispose(&target); +} + + +void test_url_joinpath__empty_query_ignored(void) +{ + cl_git_pass(git_net_url_parse(&source, "http://example.com/foo")); + + cl_git_pass(git_net_url_joinpath(&target, &source, "/bar/baz?")); + cl_assert_equal_s(target.path, "/foo/bar/baz"); + cl_assert_equal_p(target.query, NULL); + git_net_url_dispose(&target); +} diff --git a/tests/util/url/parse.c b/tests/util/url/parse.c new file mode 100644 index 000000000..1206aa381 --- /dev/null +++ b/tests/util/url/parse.c @@ -0,0 +1,557 @@ +#include "clar_libgit2.h" +#include "net.h" + +static git_net_url conndata; + +void test_url_parse__initialize(void) +{ + memset(&conndata, 0, sizeof(conndata)); +} + +void test_url_parse__cleanup(void) +{ + git_net_url_dispose(&conndata); +} + +/* Hostname */ + +void test_url_parse__hostname_trivial(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com/")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_implied_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_implied_root_custom_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com:42")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "42"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__hostname_implied_root_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com:")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_encoded_password(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@hostname.com:1234/")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "hostname.com"); + cl_assert_equal_s(conndata.port, "1234"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass/is@bad"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__hostname_user(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@example.com/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_user_pass(void) +{ + /* user:pass@hostname.tld/resource */ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@example.com/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_port(void) +{ + /* hostname.tld:port/resource */ + cl_git_pass(git_net_url_parse(&conndata, + "https://example.com:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__hostname_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://example.com:/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__hostname_user_port(void) +{ + /* user@hostname.tld:port/resource */ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@example.com:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__hostname_user_pass_port(void) +{ + /* user:pass@hostname.tld:port/resource */ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@example.com:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +/* IPv4 addresses */ + +void test_url_parse__ipv4_trivial(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_implied_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_implied_root_custom_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:42")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "42"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv4_implied_root_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_encoded_password(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@192.168.1.1:1234/")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "1234"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass/is@bad"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv4_user(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@192.168.1.1/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_user_pass(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@192.168.1.1/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://192.168.1.1:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv4_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv4_user_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@192.168.1.1:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv4_user_pass_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@192.168.1.1:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "192.168.1.1"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +/* IPv6 addresses */ + +void test_url_parse__ipv6_trivial(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_implied_root(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_implied_root_custom_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:42")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "42"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv6_implied_root_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_encoded_password(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001]:1234/")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "1234"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass/is@bad"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv6_user(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@[fe80::dcad:beff:fe00:0001]/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_user_pass(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@[fe80::dcad:beff:fe00:0001]/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://[fe80::dcad:beff:fe00:0001]:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv6_empty_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:/resource")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_parse__ipv6_user_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user@[fe80::dcad:beff:fe00:0001]:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv6_user_pass_port(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user:pass@[fe80::dcad:beff:fe00:0001]:9191/resource")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); + cl_assert_equal_s(conndata.port, "9191"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "user"); + cl_assert_equal_s(conndata.password, "pass"); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_parse__ipv6_invalid_addresses(void) +{ + /* Opening bracket missing */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]:42")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001]:1234/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@fe80::dcad:beff:fe00:0001]/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@fe80::dcad:beff:fe00:0001]/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://fe80::dcad:beff:fe00:0001]:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001]:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@fe80::dcad:beff:fe00:0001]:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@fe80::dcad:beff:fe00:0001]:9191/resource")); + + /* Closing bracket missing */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001:42")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001:1234/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@[fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@[fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://[fe80::dcad:beff:fe00:0001:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://[fe80::dcad:beff:fe00:0001:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@[fe80::dcad:beff:fe00:0001:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@[fe80::dcad:beff:fe00:0001:9191/resource")); + /* Both brackets missing */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001:42")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001:1234/")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@fe80::dcad:beff:fe00:0001/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://fe80::dcad:beff:fe00:0001:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "http://fe80::dcad:beff:fe00:0001:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user@fe80::dcad:beff:fe00:0001:9191/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "https://user:pass@fe80::dcad:beff:fe00:0001:9191/resource")); + + /* Invalid character inside address */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe8o::dcad:beff:fe00:0001]/resource")); +} diff --git a/tests/util/url/pattern.c b/tests/util/url/pattern.c new file mode 100644 index 000000000..f183d7f5f --- /dev/null +++ b/tests/util/url/pattern.c @@ -0,0 +1,103 @@ +#include "clar_libgit2.h" +#include "net.h" + +struct url_pattern { + const char *url; + const char *pattern; + bool matches; +}; + +void test_url_pattern__single(void) +{ + git_net_url url; + size_t i; + + struct url_pattern url_patterns[] = { + /* Wildcard matches */ + { "https://example.com/", "", false }, + { "https://example.com/", "*", true }, + + /* Literal and wildcard matches */ + { "https://example.com/", "example.com", true }, + { "https://example.com/", ".example.com", true }, + { "https://example.com/", "*.example.com", true }, + { "https://www.example.com/", "www.example.com", true }, + { "https://www.example.com/", ".example.com", true }, + { "https://www.example.com/", "*.example.com", true }, + + /* Literal and wildcard failures */ + { "https://example.com/", "example.org", false }, + { "https://example.com/", ".example.org", false }, + { "https://example.com/", "*.example.org", false }, + { "https://foo.example.com/", "www.example.com", false }, + + /* + * A port in the pattern is optional; if no port is + * present, it matches *all* ports. + */ + { "https://example.com/", "example.com:443", true }, + { "https://example.com/", "example.com:80", false }, + { "https://example.com:1443/", "example.com", true }, + + /* Failures with similar prefix/suffix */ + { "https://texample.com/", "example.com", false }, + { "https://example.com/", "mexample.com", false }, + { "https://example.com:44/", "example.com:443", false }, + { "https://example.com:443/", "example.com:44", false }, + }; + + for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { + cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); + cl_assert_(git_net_url_matches_pattern(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); + git_net_url_dispose(&url); + } +} + +void test_url_pattern__list(void) +{ + git_net_url url; + size_t i; + + struct url_pattern url_patterns[] = { + /* Wildcard matches */ + { "https://example.com/", "", false }, + { "https://example.com/", "*", true }, + { "https://example.com/", ",example.com,", true }, + { "https://example.com/", "foo,,example.com,,bar", true }, + { "https://example.com/", "foo,,zzz,,*,,bar", true }, + + /* Literals */ + { "https://example.com/", "example.com", true }, + { "https://example.com/", "foo.bar,example.com", true }, + { "https://example.com/", "foo.bar", false }, + { "https://example.com/", "foo.bar,example.org", false }, + { "https://www.example.com/", "foo.example.com,www.example.com,bar.example.com", true }, + { "https://www.example.com/", "foo.example.com,baz.example.com,bar.example.com", false }, + { "https://foo.example.com/", "www.example.com", false }, + { "https://foo.example.com/", "bar.example.com,www.example.com,", false }, + + /* Wildcards */ + { "https://example.com/", ".example.com", true }, + { "https://example.com/", "*.example.com", true }, + { "https://example.com/", "foo.com,bar.com,.example.com", true }, + { "https://example.com/", ".foo.com,.bar.com,.example.com", true }, + { "https://example.com/", ".foo.com,.bar.com,asdf.com", false }, + { "https://example.com/", "*.foo,*.bar,*.example.com,*.asdf", true }, + { "https://example.com/", "*.foo,*.bar,*.asdf", false }, + + + /* Ports! */ + { "https://example.com/", "example.com:443", true }, + { "https://example.com/", "example.com:42,example.com:443,example.com:99", true }, + { "https://example.com/", "example.com:42,example.com:80,example.org:443", false }, + { "https://example.com:1443/", "example.com", true }, + { "https://example.com:44/", "example.com:443", false }, + { "https://example.com:443/", "example.com:44", false }, + }; + + for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { + cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); + cl_assert_(git_net_url_matches_pattern_list(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); + git_net_url_dispose(&url); + } +} diff --git a/tests/util/url/redirect.c b/tests/util/url/redirect.c new file mode 100644 index 000000000..540177861 --- /dev/null +++ b/tests/util/url/redirect.c @@ -0,0 +1,147 @@ +#include "clar_libgit2.h" +#include "net.h" +#include "netops.h" + +static git_net_url conndata; + +void test_url_redirect__initialize(void) +{ + memset(&conndata, 0, sizeof(conndata)); +} + +void test_url_redirect__cleanup(void) +{ + git_net_url_dispose(&conndata); +} + +void test_url_redirect__redirect_http(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "http://example.com/foo/bar/baz")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "http://example.com/foo/bar/baz", false, "bar/baz")); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/foo/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__redirect_ssl(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://example.com/foo/bar/baz")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "https://example.com/foo/bar/baz", false, "bar/baz")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/foo/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__redirect_leaves_root_path(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://example.com/foo/bar/baz")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "https://example.com/foo/bar/baz", false, "/foo/bar/baz")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__redirect_encoded_username_password(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz", false, "bar/baz")); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/foo/"); + cl_assert_equal_s(conndata.username, "user/name"); + cl_assert_equal_s(conndata.password, "pass@word%zyx%v"); +} + +void test_url_redirect__redirect_cross_host_allowed(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://bar.com/bar/baz")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "https://foo.com/bar/baz", true, NULL)); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "foo.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/bar/baz"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__redirect_cross_host_denied(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://bar.com/bar/baz")); + cl_git_fail_with(git_net_url_apply_redirect(&conndata, + "https://foo.com/bar/baz", false, NULL), -1); +} + +void test_url_redirect__redirect_http_downgrade_denied(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://foo.com/bar/baz")); + cl_git_fail_with(git_net_url_apply_redirect(&conndata, + "http://foo.com/bar/baz", true, NULL), -1); +} + +void test_url_redirect__redirect_relative(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "http://foo.com/bar/baz/biff")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "/zap/baz/biff?bam", true, NULL)); + cl_assert_equal_s(conndata.scheme, "http"); + cl_assert_equal_s(conndata.host, "foo.com"); + cl_assert_equal_s(conndata.port, "80"); + cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__redirect_relative_ssl(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://foo.com/bar/baz/biff")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "/zap/baz/biff?bam", true, NULL)); + cl_assert_equal_s(conndata.scheme, "https"); + cl_assert_equal_s(conndata.host, "foo.com"); + cl_assert_equal_s(conndata.port, "443"); + cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); +} + +void test_url_redirect__service_query_no_query_params_in_location(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://foo.com/bar/info/refs?service=git-upload-pack")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "/baz/info/refs", true, "/info/refs?service=git-upload-pack")); + cl_assert_equal_s(conndata.path, "/baz"); +} + +void test_url_redirect__service_query_with_query_params_in_location(void) +{ + cl_git_pass(git_net_url_parse(&conndata, + "https://foo.com/bar/info/refs?service=git-upload-pack")); + cl_git_pass(git_net_url_apply_redirect(&conndata, + "/baz/info/refs?service=git-upload-pack", true, "/info/refs?service=git-upload-pack")); + cl_assert_equal_s(conndata.path, "/baz"); +} diff --git a/tests/util/url/scp.c b/tests/util/url/scp.c new file mode 100644 index 000000000..20fe1cc2d --- /dev/null +++ b/tests/util/url/scp.c @@ -0,0 +1,321 @@ +#include "clar_libgit2.h" +#include "net.h" + +static git_net_url conndata; + +void test_url_scp__initialize(void) +{ + memset(&conndata, 0, sizeof(conndata)); +} + +void test_url_scp__cleanup(void) +{ + git_net_url_dispose(&conndata); +} + +/* Hostname */ + +void test_url_scp__hostname_trivial(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__hostname_bracketed(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__hostname_root(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__hostname_user(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "git@example.com:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__hostname_user_bracketed(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[git@example.com]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__hostname_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com:42]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "42"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__hostname_user_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[git@example.com:42]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "example.com"); + cl_assert_equal_s(conndata.port, "42"); + cl_assert_equal_s(conndata.path, "/resource"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__ipv4_trivial(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "192.168.99.88:/resource/a/b/c")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "192.168.99.88"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/a/b/c"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__ipv4_bracketed(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88]:/resource/a/b/c")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "192.168.99.88"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/a/b/c"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__ipv4_user(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "git@192.168.99.88:/resource/a/b/c")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "192.168.99.88"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/a/b/c"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__ipv4_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88:1111]:/resource/a/b/c")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "192.168.99.88"); + cl_assert_equal_s(conndata.port, "1111"); + cl_assert_equal_s(conndata.path, "/resource/a/b/c"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__ipv4_user_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[git@192.168.99.88:1111]:/resource/a/b/c")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "192.168.99.88"); + cl_assert_equal_s(conndata.port, "1111"); + cl_assert_equal_s(conndata.path, "/resource/a/b/c"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__ipv6_trivial(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:/resource/foo")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/foo"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__ipv6_user(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:/resource/foo")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/foo"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__ipv6_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "99"); + cl_assert_equal_s(conndata.path, "/resource/foo"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__ipv6_user_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[git@[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "99"); + cl_assert_equal_s(conndata.path, "/resource/foo"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); +} + +void test_url_scp__hexhost_and_port(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[fe:22]:/resource/foo")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "fe"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "/resource/foo"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__malformed_ipv6_one(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "fe80::dcad:beff:fe00:0001]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "fe80"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, ":dcad:beff:fe00:0001]:/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__malformed_ipv6_two(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:42]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "42]:/resource"); + cl_assert_equal_p(conndata.username, NULL); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__malformed_ipv6_with_user(void) +{ + cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:42]:/resource")); + cl_assert_equal_s(conndata.scheme, "ssh"); + cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); + cl_assert_equal_s(conndata.port, "22"); + cl_assert_equal_s(conndata.path, "42]:/resource"); + cl_assert_equal_s(conndata.username, "git"); + cl_assert_equal_p(conndata.password, NULL); + cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); +} + +void test_url_scp__invalid_addresses(void) +{ + /* Path is required */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "example.com")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "example.com:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[example.com:42]:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[git@example.com:42]:")); + + /* Host is required */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + ":")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + ":foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "git@:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[]:")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "git@[]:")); + + /* User is required if specified */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "@example.com:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "@:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[@localhost:22]:foo")); + + /* Port is required in brackets */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[example.com:]:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[git@example.com:]:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[fe:]:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[@localhost]:foo")); + + /* Extra brackets are disallowed */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[git@[[fe80::dcad:beff:fe00:0001]]:42]:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[[git@[fe80::dcad:beff:fe00:0001]]:42]:foo")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[[git@[fe80::dcad:beff:fe00:0001]:42]]:foo")); + + /* Closing bracket missing */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[fe80::dcad:beff:fe00:0001:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[[fe80::dcad:beff:fe00:0001]:42:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[git@[fe80::dcad:beff:fe00:0001]:42:/resource")); + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, + "[git@[fe80::dcad:beff:fe00:0001:42]:/resource")); + + /* Invalid character inside address */ + cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, + "[fe8o::dcad:beff:fe00:0001]:/resource")); +} diff --git a/tests/util/url/valid.c b/tests/util/url/valid.c new file mode 100644 index 000000000..797b697bd --- /dev/null +++ b/tests/util/url/valid.c @@ -0,0 +1,17 @@ +#include "clar_libgit2.h" +#include "net.h" + +void test_url_valid__test(void) +{ + cl_assert(git_net_str_is_url("http://example.com/")); + cl_assert(git_net_str_is_url("file://localhost/tmp/foo/")); + cl_assert(git_net_str_is_url("ssh://user@host:42/tmp")); + cl_assert(git_net_str_is_url("git+ssh://user@host:42/tmp")); + cl_assert(git_net_str_is_url("ssh+git://user@host:42/tmp")); + cl_assert(git_net_str_is_url("https://user:pass@example.com/foo/bar")); + + cl_assert(!git_net_str_is_url("host:foo.git")); + cl_assert(!git_net_str_is_url("host:/foo.git")); + cl_assert(!git_net_str_is_url("[host:42]:/foo.git")); + cl_assert(!git_net_str_is_url("[user@host:42]:/foo.git")); +} -- cgit v1.2.1