summaryrefslogtreecommitdiffstats
path: root/test/libapt/uri_test.cc
blob: 519de49baec11f92e2ed3db2b207951f90cbc048 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <config.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/proxy.h>
#include <apt-pkg/strutl.h>
#include <gtest/gtest.h>
#include <string>

TEST(URITest, BasicHTTP)
{
   URI U("http://www.debian.org:90/temp/test");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(90u, U.Port);
   EXPECT_EQ("www.debian.org", U.Host);
   EXPECT_EQ("/temp/test", U.Path);
   EXPECT_EQ("http://www.debian.org:90/temp/test", (std::string)U);
   EXPECT_EQ("http://www.debian.org:90", URI::SiteOnly(U));
   EXPECT_EQ("http://www.debian.org:90/temp/test", URI::ArchiveOnly(U));
   EXPECT_EQ("http://www.debian.org:90/temp/test", URI::NoUserPassword(U));
   // Login data
   U = URI("http://jgg:foo@ualberta.ca/blah");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("jgg", U.User);
   EXPECT_EQ("foo", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("ualberta.ca", U.Host);
   EXPECT_EQ("/blah", U.Path);
   EXPECT_EQ("http://jgg:foo@ualberta.ca/blah", (std::string)U);
   EXPECT_EQ("http://ualberta.ca", URI::SiteOnly(U));
   EXPECT_EQ("http://ualberta.ca/blah", URI::ArchiveOnly(U));
   EXPECT_EQ("http://ualberta.ca/blah", URI::NoUserPassword(U));
   // just a user
   U = URI("https://apt@example.org/blah");
   EXPECT_EQ("https", U.Access);
   EXPECT_EQ("apt", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("example.org", U.Host);
   EXPECT_EQ("/blah", U.Path);
   EXPECT_EQ("https://apt@example.org/blah", (std::string)U);
   EXPECT_EQ("https://example.org", URI::SiteOnly(U));
   EXPECT_EQ("https://example.org/blah", URI::ArchiveOnly(U));
   EXPECT_EQ("https://example.org/blah", URI::NoUserPassword(U));
}
TEST(URITest, SingeSlashFile)
{
   URI U("file:/usr/bin/foo");
   EXPECT_EQ("file", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("", U.Host);
   EXPECT_EQ("/usr/bin/foo", U.Path);
   EXPECT_EQ("file:/usr/bin/foo", (std::string)U);
   EXPECT_EQ("file:", URI::SiteOnly(U));
   EXPECT_EQ("file:/usr/bin/foo", URI::ArchiveOnly(U));
   EXPECT_EQ("file:/usr/bin/foo", URI::NoUserPassword(U));
}
TEST(URITest, BasicCDROM)
{
   URI U("cdrom:Moo Cow Rom:/debian");
   EXPECT_EQ("cdrom", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("Moo Cow Rom", U.Host);
   EXPECT_EQ("/debian", U.Path);
   EXPECT_EQ("cdrom://Moo Cow Rom/debian", (std::string)U);
   EXPECT_EQ("cdrom://Moo Cow Rom", URI::SiteOnly(U));
   EXPECT_EQ("cdrom://Moo Cow Rom/debian", URI::ArchiveOnly(U));
   EXPECT_EQ("cdrom://Moo Cow Rom/debian", URI::NoUserPassword(U));
}
TEST(URITest, RelativeGzip)
{
   URI U("gzip:./bar/cow");
   EXPECT_EQ("gzip", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ(".", U.Host);
   EXPECT_EQ("/bar/cow", U.Path);
   EXPECT_EQ("gzip://./bar/cow", (std::string)U);
   EXPECT_EQ("gzip://.", URI::SiteOnly(U));
   EXPECT_EQ("gzip://./bar/cow", URI::ArchiveOnly(U));
   EXPECT_EQ("gzip://./bar/cow", URI::NoUserPassword(U));
}
TEST(URITest, NoSlashFTP)
{
   URI U("ftp:ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb");
   EXPECT_EQ("ftp", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("ftp.fr.debian.org", U.Host);
   EXPECT_EQ("/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", U.Path);
   EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", (std::string)U);
   EXPECT_EQ("ftp://ftp.fr.debian.org", URI::SiteOnly(U));
   EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", URI::ArchiveOnly(U));
   EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", URI::NoUserPassword(U));
}
TEST(URITest, RFC2732)
{
   URI U("http://[1080::8:800:200C:417A]/foo");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("1080::8:800:200C:417A", U.Host);
   EXPECT_EQ("/foo", U.Path);
   EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", (std::string)U);
   EXPECT_EQ("http://[1080::8:800:200C:417A]", URI::SiteOnly(U));
   EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", URI::ArchiveOnly(U));
   EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", URI::NoUserPassword(U));
   // with port
   U = URI("http://[::FFFF:129.144.52.38]:80/index.html");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(80u, U.Port);
   EXPECT_EQ("::FFFF:129.144.52.38", U.Host);
   EXPECT_EQ("/index.html", U.Path);
   EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", (std::string)U);
   EXPECT_EQ("http://[::FFFF:129.144.52.38]:80", URI::SiteOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", URI::ArchiveOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", URI::NoUserPassword(U));
   // extra colon
   U = URI("http://[::FFFF:129.144.52.38:]:80/index.html");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(80u, U.Port);
   EXPECT_EQ("::FFFF:129.144.52.38:", U.Host);
   EXPECT_EQ("/index.html", U.Path);
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", (std::string)U);
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80", URI::SiteOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", URI::ArchiveOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", URI::NoUserPassword(U));
   // extra colon port
   U = URI("http://[::FFFF:129.144.52.38:]/index.html");
   EXPECT_EQ("http", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("::FFFF:129.144.52.38:", U.Host);
   EXPECT_EQ("/index.html", U.Path);
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", (std::string)U);
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]", URI::SiteOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", URI::ArchiveOnly(U));
   EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", URI::NoUserPassword(U));
   // My Evil Corruption of RFC 2732 to handle CDROM names!
   // Fun for the whole family! */
   U = URI("cdrom:[The Debian 1.2 disk, 1/2 R1:6]/debian/");
   EXPECT_EQ("cdrom", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("The Debian 1.2 disk, 1/2 R1:6", U.Host);
   EXPECT_EQ("/debian/", U.Path);
   EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian/", (std::string)U);
   EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]", URI::SiteOnly(U));
   EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian", URI::ArchiveOnly(U));
   EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian/", URI::NoUserPassword(U));
   // no brackets
   U = URI("cdrom:Foo Bar Cow/debian/");
   EXPECT_EQ("cdrom", U.Access);
   EXPECT_EQ("", U.User);
   EXPECT_EQ("", U.Password);
   EXPECT_EQ(0u, U.Port);
   EXPECT_EQ("Foo Bar Cow", U.Host);
   EXPECT_EQ("/debian/", U.Path);
   EXPECT_EQ("cdrom://Foo Bar Cow/debian/", (std::string)U);
   EXPECT_EQ("cdrom://Foo Bar Cow", URI::SiteOnly(U));
   EXPECT_EQ("cdrom://Foo Bar Cow/debian", URI::ArchiveOnly(U));
   EXPECT_EQ("cdrom://Foo Bar Cow/debian/", URI::NoUserPassword(U));
   // percent encoded password
   U = URI("ftp://foo:b%40r@example.org");
   EXPECT_EQ("foo", U.User);
   EXPECT_EQ("b@r", U.Password);
   EXPECT_EQ("ftp://foo:b%40r@example.org/", (std::string) U);
   EXPECT_EQ("ftp://example.org", URI::SiteOnly(U));
   EXPECT_EQ("ftp://example.org", URI::ArchiveOnly(U));
   EXPECT_EQ("ftp://example.org/", URI::NoUserPassword(U));
   // percent encoded user
   U = URI("ftp://f%40o:bar@example.org");
   EXPECT_EQ("f@o", U.User);
   EXPECT_EQ("bar", U.Password);
   EXPECT_EQ("ftp://f%40o:bar@example.org/", (std::string) U);
   EXPECT_EQ("ftp://example.org", URI::SiteOnly(U));
   EXPECT_EQ("ftp://example.org", URI::ArchiveOnly(U));
   EXPECT_EQ("ftp://example.org/", URI::NoUserPassword(U));
}
TEST(URITest, AutoProxyTest)
{
   URI u0("http://www.debian.org:90/temp/test");
   URI u1("http://www.debian.org:91/temp/test");

   _config->Set("Acquire::http::Proxy-Auto-Detect", "./apt-proxy-script");

   // Scenario 0: Autodetecting a simple proxy
   AutoDetectProxy(u0);
   EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com");

   // Scenario 1: Proxy stays the same if it is already set
   AutoDetectProxy(u1);
   EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com");

   // Scenario 2: Reading with stderr output works fine
   _config->Clear("Acquire::http::proxy::www.debian.org");
   AutoDetectProxy(u1);
   EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com/foo");

   // Scenario 1 again: Proxy stays the same if it is already set
   AutoDetectProxy(u0);
   EXPECT_EQ(_config->Find("Acquire::http::proxy::www.debian.org", ""), "http://example.com/foo");
}