]> git.proxmox.com Git - libgit2.git/blob - tests/network/url/redirect.c
a94db7daf1ebe4ef88dd53e34e39783cea24257b
[libgit2.git] / tests / network / url / redirect.c
1 #include "clar_libgit2.h"
2 #include "net.h"
3 #include "netops.h"
4
5 static git_net_url conndata;
6
7 void test_network_url_redirect__initialize(void)
8 {
9 memset(&conndata, 0, sizeof(conndata));
10 }
11
12 void test_network_url_redirect__cleanup(void)
13 {
14 git_net_url_dispose(&conndata);
15 }
16
17 void test_network_url_redirect__redirect_http(void)
18 {
19 cl_git_pass(git_net_url_parse(&conndata,
20 "http://example.com/foo/bar/baz"));
21 cl_git_pass(git_net_url_apply_redirect(&conndata,
22 "http://example.com/foo/bar/baz", false, "bar/baz"));
23 cl_assert_equal_s(conndata.scheme, "http");
24 cl_assert_equal_s(conndata.host, "example.com");
25 cl_assert_equal_s(conndata.port, "80");
26 cl_assert_equal_s(conndata.path, "/foo/");
27 cl_assert_equal_p(conndata.username, NULL);
28 cl_assert_equal_p(conndata.password, NULL);
29 }
30
31 void test_network_url_redirect__redirect_ssl(void)
32 {
33 cl_git_pass(git_net_url_parse(&conndata,
34 "https://example.com/foo/bar/baz"));
35 cl_git_pass(git_net_url_apply_redirect(&conndata,
36 "https://example.com/foo/bar/baz", false, "bar/baz"));
37 cl_assert_equal_s(conndata.scheme, "https");
38 cl_assert_equal_s(conndata.host, "example.com");
39 cl_assert_equal_s(conndata.port, "443");
40 cl_assert_equal_s(conndata.path, "/foo/");
41 cl_assert_equal_p(conndata.username, NULL);
42 cl_assert_equal_p(conndata.password, NULL);
43 }
44
45 void test_network_url_redirect__redirect_leaves_root_path(void)
46 {
47 cl_git_pass(git_net_url_parse(&conndata,
48 "https://example.com/foo/bar/baz"));
49 cl_git_pass(git_net_url_apply_redirect(&conndata,
50 "https://example.com/foo/bar/baz", false, "/foo/bar/baz"));
51 cl_assert_equal_s(conndata.scheme, "https");
52 cl_assert_equal_s(conndata.host, "example.com");
53 cl_assert_equal_s(conndata.port, "443");
54 cl_assert_equal_s(conndata.path, "/");
55 cl_assert_equal_p(conndata.username, NULL);
56 cl_assert_equal_p(conndata.password, NULL);
57 }
58
59 void test_network_url_redirect__redirect_encoded_username_password(void)
60 {
61 cl_git_pass(git_net_url_parse(&conndata,
62 "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz"));
63 cl_git_pass(git_net_url_apply_redirect(&conndata,
64 "https://user%2fname:pass%40word%zyx%v@example.com/foo/bar/baz", false, "bar/baz"));
65 cl_assert_equal_s(conndata.scheme, "https");
66 cl_assert_equal_s(conndata.host, "example.com");
67 cl_assert_equal_s(conndata.port, "443");
68 cl_assert_equal_s(conndata.path, "/foo/");
69 cl_assert_equal_s(conndata.username, "user/name");
70 cl_assert_equal_s(conndata.password, "pass@word%zyx%v");
71 }
72
73 void test_network_url_redirect__redirect_cross_host_allowed(void)
74 {
75 cl_git_pass(git_net_url_parse(&conndata,
76 "https://bar.com/bar/baz"));
77 cl_git_pass(git_net_url_apply_redirect(&conndata,
78 "https://foo.com/bar/baz", true, NULL));
79 cl_assert_equal_s(conndata.scheme, "https");
80 cl_assert_equal_s(conndata.host, "foo.com");
81 cl_assert_equal_s(conndata.port, "443");
82 cl_assert_equal_s(conndata.path, "/bar/baz");
83 cl_assert_equal_p(conndata.username, NULL);
84 cl_assert_equal_p(conndata.password, NULL);
85 }
86
87 void test_network_url_redirect__redirect_cross_host_denied(void)
88 {
89 cl_git_pass(git_net_url_parse(&conndata,
90 "https://bar.com/bar/baz"));
91 cl_git_fail_with(git_net_url_apply_redirect(&conndata,
92 "https://foo.com/bar/baz", false, NULL), -1);
93 }
94
95 void test_network_url_redirect__redirect_http_downgrade_denied(void)
96 {
97 cl_git_pass(git_net_url_parse(&conndata,
98 "https://foo.com/bar/baz"));
99 cl_git_fail_with(git_net_url_apply_redirect(&conndata,
100 "http://foo.com/bar/baz", true, NULL), -1);
101 }
102
103 void test_network_url_redirect__redirect_relative(void)
104 {
105 cl_git_pass(git_net_url_parse(&conndata,
106 "http://foo.com/bar/baz/biff"));
107 cl_git_pass(git_net_url_apply_redirect(&conndata,
108 "/zap/baz/biff?bam", true, NULL));
109 cl_assert_equal_s(conndata.scheme, "http");
110 cl_assert_equal_s(conndata.host, "foo.com");
111 cl_assert_equal_s(conndata.port, "80");
112 cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam");
113 cl_assert_equal_p(conndata.username, NULL);
114 cl_assert_equal_p(conndata.password, NULL);
115 }
116
117 void test_network_url_redirect__redirect_relative_ssl(void)
118 {
119 cl_git_pass(git_net_url_parse(&conndata,
120 "https://foo.com/bar/baz/biff"));
121 cl_git_pass(git_net_url_apply_redirect(&conndata,
122 "/zap/baz/biff?bam", true, NULL));
123 cl_assert_equal_s(conndata.scheme, "https");
124 cl_assert_equal_s(conndata.host, "foo.com");
125 cl_assert_equal_s(conndata.port, "443");
126 cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam");
127 cl_assert_equal_p(conndata.username, NULL);
128 cl_assert_equal_p(conndata.password, NULL);
129 }
130
131 void test_network_url_redirect__service_query_no_query_params_in_location(void)
132 {
133 cl_git_pass(git_net_url_parse(&conndata,
134 "https://foo.com/bar/info/refs?service=git-upload-pack"));
135 cl_git_pass(git_net_url_apply_redirect(&conndata,
136 "/baz/info/refs", true, "/info/refs?service=git-upload-pack"));
137 cl_assert_equal_s(conndata.path, "/baz");
138 }
139
140 void test_network_url_redirect__service_query_with_query_params_in_location(void)
141 {
142 cl_git_pass(git_net_url_parse(&conndata,
143 "https://foo.com/bar/info/refs?service=git-upload-pack"));
144 cl_git_pass(git_net_url_apply_redirect(&conndata,
145 "/baz/info/refs?service=git-upload-pack", true, "/info/refs?service=git-upload-pack"));
146 cl_assert_equal_s(conndata.path, "/baz");
147 }