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.
8 #include "zypp/base/Exception.h"
9 #include "zypp/base/String.h"
11 #include "zypp/RepoInfo.h"
19 #include <boost/test/auto_unit_test.hpp>
21 using boost::unit_test::test_case;
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() )
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 );
35 BOOST_AUTO_TEST_CASE(test_ipv6_url)
40 str = "http://[2001:DB8:0:F102::1]/64/sles11/RC1/CD1?device=eth0";
42 BOOST_CHECK_EQUAL( str,url.asString() );
43 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "", "", "" );
46 str = "http://[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
48 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "", "" );
51 str = "http://user:pass@[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
53 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "user", "pass" );
56 BOOST_AUTO_TEST_CASE(test_url1)
58 std::string str, one, two;
62 // asString & asCompleteString should not print "mailto://"
63 str = "mailto:feedback@example.com?subject=hello";
65 BOOST_CHECK_EQUAL( str, url.asString() );
66 BOOST_CHECK_EQUAL( str, url.asCompleteString() );
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" );
73 BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asCompleteString(), "dvd:/srv/ftp" );
74 BOOST_CHECK_EQUAL( Url("dvd:///srv/ftp").asString(), "dvd:/srv/ftp" );
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" );
79 BOOST_CHECK_THROW( Url("dvd://authority/srv/ftp"), url::UrlNotAllowedException );
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";
88 BOOST_CHECK_EQUAL( one, url.asString() );
89 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
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";
98 BOOST_CHECK_EQUAL( one, url.asString() );
99 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
101 // absolute path defaults to 'file://'
102 str = "/some/local/path";
103 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "file://"+str );
105 str = "file:./srv/ftp";
106 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
108 str = "ftp://foo//srv/ftp";
109 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
111 str = "FTP://user@local%68ost/%2f/srv/ftp";
112 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
114 str = "http://[::1]/foo/bar";
115 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
117 str = "http://:@just-localhost.example.net:8080/";
118 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
120 str = "mailto:feedback@example.com?subject=hello";
121 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
123 str = "nfs://nfs-server/foo/bar/trala";
124 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
126 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
127 BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
129 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
130 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
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());
137 // throws: host is mandatory
138 str = "ftp:///foo/bar";
139 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
141 // throws: host is mandatory
142 str = "http:///%2f/srv/ftp";
143 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
145 // OK, host allowed in file-url
146 str = "file://localhost/some/path";
147 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
149 // throws: host not allowed
150 str = "cd://localhost/some/path";
151 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
153 // throws: no path (email)
155 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
159 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
161 // OK, valid (no host, path is there)
162 str = "cd:///some/path";
163 BOOST_CHECK( zypp::Url(str).isValid());
166 BOOST_AUTO_TEST_CASE(test_url2)
168 zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
170 BOOST_CHECK_EQUAL( url.asString(),
171 "http://user@localhost/path/to?arg=val#frag" );
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");
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");
181 BOOST_CHECK_EQUAL( url.asCompleteString(),
182 "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
185 BOOST_AUTO_TEST_CASE(test_url3)
187 zypp::Url url("http://localhost/path/to#frag");
191 // will be encoded as "hoho=ha%20ha"
194 url.setQueryParam(key, val);
195 BOOST_CHECK_EQUAL( url.asString(),
196 "http://localhost/path/to?hoho=ha%20ha#frag");
198 // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
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");
205 // will be encoded as "foo%25bar=is%25de%25ad"
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");
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",
219 BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
220 for( size_t i=0; i<params.size(); i++)
222 BOOST_CHECK_EQUAL( params[i], result[i]);
226 BOOST_AUTO_TEST_CASE( test_url4)
230 zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
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=*)");
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)
245 if("attrs" == m->first)
247 BOOST_CHECK_EQUAL( m->second, "cn,sn");
250 if("filter" == m->first)
252 BOOST_CHECK_EQUAL( m->second, "(cn=*)");
255 if("scope" == m->first)
257 BOOST_CHECK_EQUAL( m->second, "sub");
261 BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
265 url.setQueryParam("attrs", "cn,sn,uid");
266 url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
268 BOOST_CHECK_EQUAL(url.getQueryParam("attrs"), "cn,sn,uid");
269 BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
272 catch(const zypp::url::UrlException &e)
278 BOOST_AUTO_TEST_CASE( test_url5)
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 );
285 BOOST_AUTO_TEST_CASE(plugin_scriptpath)
287 // plugin script path must not be rewritten
288 for ( const std::string & t : { "script", "script/", "/script", "/script/", "./script", "./script/" } )
290 BOOST_CHECK_EQUAL( Url("plugin:"+t).getPathName(), t );
293 { // more cosmetic issue, but the RepoVarReplacer should
294 // not change the string representation (-> "plugin:/script")
295 Url u( "plugin:script?opt=val" );
298 BOOST_CHECK_EQUAL( u.asString(), i.url().asString() );
303 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
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"], "" );
316 // vim: set ts=2 sts=2 sw=2 ai et: