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 // absolute path defaults to 'file://'
99 str = "/some/local/path";
100 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "file://"+str );
102 str = "file:./srv/ftp";
103 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
105 str = "ftp://foo//srv/ftp";
106 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
108 str = "FTP://user@local%68ost/%2f/srv/ftp";
109 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
111 str = "http://[::1]/foo/bar";
112 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
114 str = "http://:@just-localhost.example.net:8080/";
115 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
117 str = "mailto:feedback@example.com?subject=hello";
118 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
120 str = "nfs://nfs-server/foo/bar/trala";
121 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
123 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
124 BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
126 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
127 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
129 // parseable but invalid, since no host avaliable
130 str = "ldap:///dc=foo,dc=bar";
131 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
132 BOOST_CHECK( !zypp::Url(str).isValid());
134 // throws: host is mandatory
135 str = "ftp:///foo/bar";
136 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
138 // throws: host is mandatory
139 str = "http:///%2f/srv/ftp";
140 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
142 // OK, host allowed in file-url
143 str = "file://localhost/some/path";
144 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
146 // throws: host not allowed
147 str = "cd://localhost/some/path";
148 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
150 // throws: no path (email)
152 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
156 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
158 // OK, valid (no host, path is there)
159 str = "cd:///some/path";
160 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
161 BOOST_CHECK( zypp::Url(str).isValid());
164 BOOST_AUTO_TEST_CASE(test_url2)
166 zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
168 BOOST_CHECK_EQUAL( url.asString(),
169 "http://user@localhost/path/to?arg=val#frag" );
171 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
172 zypp::url::ViewOptions::WITH_PASSWORD),
173 "http://user:pass@localhost/path/to?arg=val#frag");
175 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
176 zypp::url::ViewOptions::WITH_PATH_PARAMS),
177 "http://user@localhost/path/to;version=1.1?arg=val#frag");
179 BOOST_CHECK_EQUAL( url.asCompleteString(),
180 "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
183 BOOST_AUTO_TEST_CASE(test_url3)
185 zypp::Url url("http://localhost/path/to#frag");
189 // will be encoded as "hoho=ha%20ha"
192 url.setQueryParam(key, val);
193 BOOST_CHECK_EQUAL( url.asString(),
194 "http://localhost/path/to?hoho=ha%20ha#frag");
196 // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
198 val = "foo&bar=value";
199 url.setQueryParam(key, val);
200 BOOST_CHECK_EQUAL( url.asString(),
201 "http://localhost/path/to?foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
203 // will be encoded as "foo%25bar=is%25de%25ad"
206 url.setQueryParam(key, val);
207 BOOST_CHECK_EQUAL( url.asString(),
208 "http://localhost/path/to?foo%25bar=is%25de%25ad&foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
210 // get encoded query parameters and compare with results:
211 zypp::url::ParamVec params( url.getQueryStringVec());
212 const char * const result[] = {
213 "foo%25bar=is%25de%25ad",
214 "foo%3Dbar%26key=foo%26bar%3Dvalue",
217 BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
218 for( size_t i=0; i<params.size(); i++)
220 BOOST_CHECK_EQUAL( params[i], result[i]);
224 BOOST_AUTO_TEST_CASE( test_url4)
228 zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
230 // fetch query params as vector
231 zypp::url::ParamVec pvec( url.getQueryStringVec());
232 BOOST_CHECK( pvec.size() == 3);
233 BOOST_CHECK_EQUAL( pvec[0], "cn,sn");
234 BOOST_CHECK_EQUAL( pvec[1], "sub");
235 BOOST_CHECK_EQUAL( pvec[2], "(cn=*)");
237 // fetch the query params map
238 // with its special ldap names/keys
239 zypp::url::ParamMap pmap( url.getQueryStringMap());
240 zypp::url::ParamMap::const_iterator m;
241 for(m=pmap.begin(); m!=pmap.end(); ++m)
243 if("attrs" == m->first)
245 BOOST_CHECK_EQUAL( m->second, "cn,sn");
248 if("filter" == m->first)
250 BOOST_CHECK_EQUAL( m->second, "(cn=*)");
253 if("scope" == m->first)
255 BOOST_CHECK_EQUAL( m->second, "sub");
259 BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
263 url.setQueryParam("attrs", "cn,sn,uid");
264 url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
266 BOOST_CHECK_EQUAL(url.getQueryParam("attrs"), "cn,sn,uid");
267 BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
270 catch(const zypp::url::UrlException &e)
276 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
278 // url querysting options without value must be possible
279 // e.g. for plugin schema
280 Url u( "plugin:script?loptv=lvalue&v=optv&lopt=&o" );
281 url::ParamMap pm( u.getQueryStringMap() );
282 BOOST_CHECK_EQUAL( pm.size(), 4 );
283 BOOST_CHECK_EQUAL( pm["loptv"], "lvalue" );
284 BOOST_CHECK_EQUAL( pm["v"], "optv" );
285 BOOST_CHECK_EQUAL( pm["lopt"], "" );
286 BOOST_CHECK_EQUAL( pm["o"], "" );
289 // vim: set ts=2 sts=2 sw=2 ai et: