summaryrefslogtreecommitdiff
path: root/tests/network/url/redirect.c
blob: 2c0b614d995fb54a16dad3e543852ce5525f70bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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", "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", "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", "/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", "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_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", 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", 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", 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", 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", "/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", "/info/refs?service=git-upload-pack"));
	cl_assert_equal_s(conndata.path, "/baz");
}