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"
17 #include <boost/test/auto_unit_test.hpp>
19 using boost::unit_test::test_case;
22 void testUrlAuthority( const Url & url_r,
23 const std::string & host_r, const std::string & port_r = std::string(),
24 const std::string & user_r = std::string(), const std::string & pass_r = std::string() )
26 BOOST_CHECK_EQUAL( url_r.getUsername(), user_r );
27 BOOST_CHECK_EQUAL( url_r.getPassword(), pass_r );
28 BOOST_CHECK_EQUAL( url_r.getHost(), host_r );
29 BOOST_CHECK_EQUAL( url_r.getPort(), port_r );
33 BOOST_AUTO_TEST_CASE(test_ipv6_url)
38 str = "http://[2001:DB8:0:F102::1]/64/sles11/RC1/CD1?device=eth0";
40 BOOST_CHECK_EQUAL( str,url.asString() );
41 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "", "", "" );
44 str = "http://[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
46 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "", "" );
49 str = "http://user:pass@[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
51 testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "user", "pass" );
54 BOOST_AUTO_TEST_CASE(test_url1)
56 std::string str, one, two;
60 // asString & asCompleteString should not print "mailto://"
61 str = "mailto:feedback@example.com?subject=hello";
63 BOOST_CHECK_EQUAL( str, url.asString() );
64 BOOST_CHECK_EQUAL( str, url.asCompleteString() );
66 // asString & asCompleteString should add empty authority
67 // "dvd://...", except we request to avoid it.
69 one = "dvd:///srv/ftp";
70 two = "dvd:///srv/ftp";
73 BOOST_CHECK_EQUAL( one, url.asString() );
74 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
75 BOOST_CHECK_EQUAL( str, url.asString(zypp::url::ViewOptions() -
76 zypp::url::ViewOption::EMPTY_AUTHORITY));
78 // asString shouldn't print the password, asCompleteString should
79 // further, the "//" at the begin of the path should become "/%2F"
80 str = "ftp://user:pass@localhost//srv/ftp";
81 one = "ftp://user@localhost/%2Fsrv/ftp";
82 two = "ftp://user:pass@localhost/%2Fsrv/ftp";
85 BOOST_CHECK_EQUAL( one, url.asString() );
86 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
88 // asString shouldn't print the password, asCompleteString should.
89 // further, the "//" at the begin of the path should be keept.
90 str = "http://user:pass@localhost//srv/ftp";
91 one = "http://user@localhost//srv/ftp";
95 BOOST_CHECK_EQUAL( one, url.asString() );
96 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
98 str = "file:./srv/ftp";
99 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
101 str = "ftp://foo//srv/ftp";
102 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
104 str = "FTP://user@local%68ost/%2f/srv/ftp";
105 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
107 str = "http://[::1]/foo/bar";
108 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
110 str = "http://:@just-localhost.example.net:8080/";
111 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
113 str = "mailto:feedback@example.com?subject=hello";
114 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
116 str = "nfs://nfs-server/foo/bar/trala";
117 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
119 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
120 BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
122 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
123 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
125 // parseable but invalid, since no host avaliable
126 str = "ldap:///dc=foo,dc=bar";
127 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
128 BOOST_CHECK( !zypp::Url(str).isValid());
130 // throws: host is mandatory
131 str = "ftp:///foo/bar";
132 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
134 // throws: host is mandatory
135 str = "http:///%2f/srv/ftp";
136 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
138 // OK, host allowed in file-url
139 str = "file://localhost/some/path";
140 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
142 // throws: host not allowed
143 str = "cd://localhost/some/path";
144 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
146 // throws: no path (email)
148 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
152 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
154 // OK, valid (no host, path is there)
155 str = "cd:///some/path";
156 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
157 BOOST_CHECK( zypp::Url(str).isValid());
160 BOOST_AUTO_TEST_CASE(test_url2)
162 zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
164 BOOST_CHECK_EQUAL( url.asString(),
165 "http://user@localhost/path/to?arg=val#frag" );
167 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
168 zypp::url::ViewOptions::WITH_PASSWORD),
169 "http://user:pass@localhost/path/to?arg=val#frag");
171 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
172 zypp::url::ViewOptions::WITH_PATH_PARAMS),
173 "http://user@localhost/path/to;version=1.1?arg=val#frag");
175 BOOST_CHECK_EQUAL( url.asCompleteString(),
176 "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
179 BOOST_AUTO_TEST_CASE(test_url3)
181 zypp::Url url("http://localhost/path/to#frag");
185 // will be encoded as "hoho=ha%20ha"
188 url.setQueryParam(key, val);
189 BOOST_CHECK_EQUAL( url.asString(),
190 "http://localhost/path/to?hoho=ha%20ha#frag");
192 // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
194 val = "foo&bar=value";
195 url.setQueryParam(key, val);
196 BOOST_CHECK_EQUAL( url.asString(),
197 "http://localhost/path/to?foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
199 // will be encoded as "foo%25bar=is%25de%25ad"
202 url.setQueryParam(key, val);
203 BOOST_CHECK_EQUAL( url.asString(),
204 "http://localhost/path/to?foo%25bar=is%25de%25ad&foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
206 // get encoded query parameters and compare with results:
207 zypp::url::ParamVec params( url.getQueryStringVec());
208 const char * const result[] = {
209 "foo%25bar=is%25de%25ad",
210 "foo%3Dbar%26key=foo%26bar%3Dvalue",
213 BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
214 for( size_t i=0; i<params.size(); i++)
216 BOOST_CHECK_EQUAL( params[i], result[i]);
220 BOOST_AUTO_TEST_CASE( test_url4)
224 zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
226 // fetch query params as vector
227 zypp::url::ParamVec pvec( url.getQueryStringVec());
228 BOOST_CHECK( pvec.size() == 3);
229 BOOST_CHECK_EQUAL( pvec[0], "cn,sn");
230 BOOST_CHECK_EQUAL( pvec[1], "sub");
231 BOOST_CHECK_EQUAL( pvec[2], "(cn=*)");
233 // fetch the query params map
234 // with its special ldap names/keys
235 zypp::url::ParamMap pmap( url.getQueryStringMap());
236 zypp::url::ParamMap::const_iterator m;
237 for(m=pmap.begin(); m!=pmap.end(); ++m)
239 if("attrs" == m->first)
241 BOOST_CHECK_EQUAL( m->second, "cn,sn");
244 if("filter" == m->first)
246 BOOST_CHECK_EQUAL( m->second, "(cn=*)");
249 if("scope" == m->first)
251 BOOST_CHECK_EQUAL( m->second, "sub");
255 BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
259 url.setQueryParam("attrs", "cn,sn,uid");
260 url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
262 BOOST_CHECK_EQUAL(url.getQueryParam("attrs"), "cn,sn,uid");
263 BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
266 catch(const zypp::url::UrlException &e)
272 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
274 // url querysting options without value must be possible
275 // e.g. for plugin schema
276 Url u( "plugin:script?loptv=lvalue&v=optv&lopt=&o" );
277 url::ParamMap pm( u.getQueryStringMap() );
278 BOOST_CHECK_EQUAL( pm.size(), 4 );
279 BOOST_CHECK_EQUAL( pm["loptv"], "lvalue" );
280 BOOST_CHECK_EQUAL( pm["v"], "optv" );
281 BOOST_CHECK_EQUAL( pm["lopt"], "" );
282 BOOST_CHECK_EQUAL( pm["o"], "" );
285 // vim: set ts=2 sts=2 sw=2 ai et: