Imported Upstream version 17.11.1
[platform/upstream/libzypp.git] / tests / zypp / Url_test.cc
1 /*
2 ** Check if the url by scheme repository works, e.g.
3 ** if there are some initialization order problems
4 ** (ViewOption) causing asString to format its string
5 ** differently than configured.
6 */
7
8 #include "zypp/base/Exception.h"
9 #include "zypp/base/String.h"
10
11 #include "zypp/RepoInfo.h"
12
13 #include "zypp/Url.h"
14 #include <stdexcept>
15 #include <iostream>
16 #include <cassert>
17
18 // Boost.Test
19 #include <boost/test/auto_unit_test.hpp>
20
21 using boost::unit_test::test_case;
22 using namespace zypp;
23
24 void testUrlAuthority( const Url & url_r,
25                        const std::string & host_r, const std::string & port_r = std::string(),
26                        const std::string & user_r = std::string(), const std::string & pass_r = std::string() )
27 {
28   BOOST_CHECK_EQUAL( url_r.getUsername(),       user_r );
29   BOOST_CHECK_EQUAL( url_r.getPassword(),       pass_r );
30   BOOST_CHECK_EQUAL( url_r.getHost(),           host_r );
31   BOOST_CHECK_EQUAL( url_r.getPort(),           port_r );
32 }
33
34
35 BOOST_AUTO_TEST_CASE(test_ipv6_url)
36 {
37     std::string str;
38     zypp::Url   url;
39
40     str = "http://[2001:DB8:0:F102::1]/64/sles11/RC1/CD1?device=eth0";
41     url = Url( str );
42     BOOST_CHECK_EQUAL( str,url.asString() );
43     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "", "", "" );
44
45     // bnc#
46     str = "http://[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
47     url = Url( str );
48     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "", "" );
49
50
51     str = "http://user:pass@[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
52     url = Url( str );
53     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "user", "pass" );
54 }
55
56 BOOST_AUTO_TEST_CASE(test_url1)
57 {
58     std::string str, one, two;
59     zypp::Url   url;
60
61
62     // asString & asCompleteString should not print "mailto://"
63     str = "mailto:feedback@example.com?subject=hello";
64     url = str;
65     BOOST_CHECK_EQUAL( str, url.asString() );
66     BOOST_CHECK_EQUAL( str, url.asCompleteString() );
67
68     // In general, schema without authority allows specifying an empty authority
69     // though it should not be printed (unless explicitly requested).
70     BOOST_CHECK_EQUAL( Url("dvd:/srv/ftp").asCompleteString(),   "dvd:/srv/ftp" );
71     BOOST_CHECK_EQUAL( Url("dvd:/srv/ftp").asString(),           "dvd:/srv/ftp" );
72
73     BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asCompleteString(), "dvd:/srv/ftp" );
74     BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asString(),         "dvd:/srv/ftp" );
75
76     BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asString(url::ViewOption::DEFAULTS+url::ViewOption::EMPTY_AUTHORITY),        "dvd:///srv/ftp" );
77     BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asString(url::ViewOption::DEFAULTS-url::ViewOption::EMPTY_AUTHORITY),        "dvd:/srv/ftp" );
78
79     BOOST_CHECK_THROW( Url("dvd://authority/srv/ftp"), url::UrlNotAllowedException );
80
81     // asString shouldn't print the password, asCompleteString should
82     // further, the "//" at the begin of the path should become "/%2F"
83     str = "ftp://user:pass@localhost//srv/ftp";
84     one = "ftp://user@localhost/%2Fsrv/ftp";
85     two = "ftp://user:pass@localhost/%2Fsrv/ftp";
86     url = str;
87
88     BOOST_CHECK_EQUAL( one, url.asString() );
89     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
90
91     // asString shouldn't print the password, asCompleteString should.
92     // further, the "//" at the begin of the path should be keept.
93     str = "http://user:pass@localhost//srv/ftp";
94     one = "http://user@localhost//srv/ftp";
95     two = str;
96     url = str;
97
98     BOOST_CHECK_EQUAL( one, url.asString() );
99     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
100
101     // absolute path defaults to 'file://'
102     str = "/some/local/path";
103     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "file://"+str );
104
105     str = "file:./srv/ftp";
106     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
107
108     str = "ftp://foo//srv/ftp";
109     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
110
111     str = "FTP://user@local%68ost/%2f/srv/ftp";
112     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
113
114     str = "http://[::1]/foo/bar";
115     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
116
117     str = "http://:@just-localhost.example.net:8080/";
118     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
119
120     str = "mailto:feedback@example.com?subject=hello";
121     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
122
123     str = "nfs://nfs-server/foo/bar/trala";
124     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
125
126     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
127     BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
128
129     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
130     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
131
132     // parseable but invalid, since no host avaliable
133     str = "ldap:///dc=foo,dc=bar";
134     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
135     BOOST_CHECK( !zypp::Url(str).isValid());
136
137     // throws:  host is mandatory
138     str = "ftp:///foo/bar";
139     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
140
141     // throws:  host is mandatory
142     str = "http:///%2f/srv/ftp";
143     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
144
145     // OK, host allowed in file-url
146     str = "file://localhost/some/path";
147     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
148
149     // throws:  host not allowed
150     str = "cd://localhost/some/path";
151     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
152
153     // throws: no path (email)
154     str = "mailto:";
155     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
156
157     // throws:  no path
158     str = "cd:";
159     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
160
161     // OK, valid (no host, path is there)
162     str = "cd:///some/path";
163     BOOST_CHECK( zypp::Url(str).isValid());
164 }
165
166 BOOST_AUTO_TEST_CASE(test_url2)
167 {
168   zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
169
170   BOOST_CHECK_EQUAL( url.asString(),
171   "http://user@localhost/path/to?arg=val#frag" );
172
173   BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
174                      zypp::url::ViewOptions::WITH_PASSWORD),
175   "http://user:pass@localhost/path/to?arg=val#frag");
176
177   BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
178                      zypp::url::ViewOptions::WITH_PATH_PARAMS),
179   "http://user@localhost/path/to;version=1.1?arg=val#frag");
180
181   BOOST_CHECK_EQUAL( url.asCompleteString(),
182   "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
183 }
184
185 BOOST_AUTO_TEST_CASE(test_url3)
186 {
187   zypp::Url   url("http://localhost/path/to#frag");
188   std::string key;
189   std::string val;
190
191   // will be encoded as "hoho=ha%20ha"
192   key = "hoho";
193   val = "ha ha";
194   url.setQueryParam(key, val);
195   BOOST_CHECK_EQUAL( url.asString(),
196   "http://localhost/path/to?hoho=ha%20ha#frag");
197
198   // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
199   key = "foo=bar&key";
200   val = "foo&bar=value";
201   url.setQueryParam(key, val);
202   BOOST_CHECK_EQUAL( url.asString(),
203   "http://localhost/path/to?foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
204
205   // will be encoded as "foo%25bar=is%25de%25ad"
206   key = "foo%bar";
207   val = "is%de%ad";
208   url.setQueryParam(key, val);
209   BOOST_CHECK_EQUAL( url.asString(),
210   "http://localhost/path/to?foo%25bar=is%25de%25ad&foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
211
212   // get encoded query parameters and compare with results:
213   zypp::url::ParamVec params( url.getQueryStringVec());
214   const char * const  result[] = {
215     "foo%25bar=is%25de%25ad",
216     "foo%3Dbar%26key=foo%26bar%3Dvalue",
217     "hoho=ha%20ha"
218   };
219   BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
220   for( size_t i=0; i<params.size(); i++)
221   {
222       BOOST_CHECK_EQUAL( params[i], result[i]);
223   }
224 }
225
226 BOOST_AUTO_TEST_CASE( test_url4)
227 {
228   try
229   {
230     zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
231
232     // fetch query params as vector
233     zypp::url::ParamVec pvec( url.getQueryStringVec());
234     BOOST_CHECK( pvec.size() == 3);
235     BOOST_CHECK_EQUAL( pvec[0], "cn,sn");
236     BOOST_CHECK_EQUAL( pvec[1], "sub");
237     BOOST_CHECK_EQUAL( pvec[2], "(cn=*)");
238
239     // fetch the query params map
240     // with its special ldap names/keys
241     zypp::url::ParamMap pmap( url.getQueryStringMap());
242     zypp::url::ParamMap::const_iterator m;
243     for(m=pmap.begin(); m!=pmap.end(); ++m)
244     {
245       if("attrs"  == m->first)
246       {
247         BOOST_CHECK_EQUAL( m->second, "cn,sn");
248       }
249       else
250       if("filter" == m->first)
251       {
252         BOOST_CHECK_EQUAL( m->second, "(cn=*)");
253       }
254       else
255       if("scope"  == m->first)
256       {
257         BOOST_CHECK_EQUAL( m->second, "sub");
258       }
259       else
260       {
261         BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
262       }
263     }
264
265     url.setQueryParam("attrs", "cn,sn,uid");
266     url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
267
268     BOOST_CHECK_EQUAL(url.getQueryParam("attrs"),  "cn,sn,uid");
269     BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
270
271   }
272   catch(const zypp::url::UrlException &e)
273   {
274     ZYPP_CAUGHT(e);
275   }
276 }
277
278 BOOST_AUTO_TEST_CASE( test_url5)
279 {
280   std::string str( "file:/some/${var:+path}/${var:-with}/${vars}" );
281   BOOST_CHECK_EQUAL( Url(str).asString(), str );
282   BOOST_CHECK_EQUAL( Url(zypp::url::encode( str, URL_SAFE_CHARS )).asString(), str );
283 }
284
285 BOOST_AUTO_TEST_CASE(plugin_scriptpath)
286 {
287   // plugin script path must not be rewritten
288   for ( const std::string & t : { "script", "script/", "/script", "/script/", "./script", "./script/" } )
289   {
290     BOOST_CHECK_EQUAL( Url("plugin:"+t).getPathName(),  t );
291   }
292
293   { // more cosmetic issue, but the RepoVarReplacer should
294     // not change the string representation (-> "plugin:/script")
295     Url u( "plugin:script?opt=val" );
296     RepoInfo i;
297     i.setBaseUrl( u );
298     BOOST_CHECK_EQUAL( u.asString(), i.url().asString() );
299   }
300
301 }
302
303 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
304 {
305   // url querysting options without value must be possible
306   // e.g. for plugin schema
307   Url u( "plugin:script?loptv=lvalue&v=optv&lopt=&o" );
308   url::ParamMap pm( u.getQueryStringMap() );
309   BOOST_CHECK_EQUAL( pm.size(), 4 );
310   BOOST_CHECK_EQUAL( pm["loptv"], "lvalue" );
311   BOOST_CHECK_EQUAL( pm["v"], "optv" );
312   BOOST_CHECK_EQUAL( pm["lopt"], "" );
313   BOOST_CHECK_EQUAL( pm["o"], "" );
314 }
315
316 // vim: set ts=2 sts=2 sw=2 ai et: