Imported Upstream version 15.0.0
[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/Url.h"
12 #include <stdexcept>
13 #include <iostream>
14 #include <cassert>
15
16 // Boost.Test
17 #include <boost/test/auto_unit_test.hpp>
18
19 using boost::unit_test::test_case;
20 using namespace zypp;
21
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() )
25 {
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 );
30 }
31
32
33 BOOST_AUTO_TEST_CASE(test_ipv6_url)
34 {
35     std::string str;
36     zypp::Url   url;
37
38     str = "http://[2001:DB8:0:F102::1]/64/sles11/RC1/CD1?device=eth0";
39     url = Url( str );
40     BOOST_CHECK_EQUAL( str,url.asString() );
41     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "", "", "" );
42
43     // bnc#
44     str = "http://[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
45     url = Url( str );
46     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "", "" );
47
48
49     str = "http://user:pass@[2001:DB8:0:F102::1]:8080/64/sles11/RC1/CD1?device=eth0";
50     url = Url( str );
51     testUrlAuthority( url, "[2001:DB8:0:F102::1]", "8080", "user", "pass" );
52 }
53
54 BOOST_AUTO_TEST_CASE(test_url1)
55 {
56     std::string str, one, two;
57     zypp::Url   url;
58
59
60     // asString & asCompleteString should not print "mailto://"
61     str = "mailto:feedback@example.com?subject=hello";
62     url = str;
63     BOOST_CHECK_EQUAL( str, url.asString() );
64     BOOST_CHECK_EQUAL( str, url.asCompleteString() );
65
66     // asString & asCompleteString should add empty authority
67     // "dvd://...", except we request to avoid it.
68     str = "dvd:/srv/ftp";
69     one = "dvd:///srv/ftp";
70     two = "dvd:///srv/ftp";
71     url = str;
72
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));
77
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";
83     url = str;
84
85     BOOST_CHECK_EQUAL( one, url.asString() );
86     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
87
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";
92     two = str;
93     url = str;
94
95     BOOST_CHECK_EQUAL( one, url.asString() );
96     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
97
98     // absolute path defaults to 'file://'
99     str = "/some/local/path";
100     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "file://"+str );
101
102     str = "file:./srv/ftp";
103     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
104
105     str = "ftp://foo//srv/ftp";
106     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
107
108     str = "FTP://user@local%68ost/%2f/srv/ftp";
109     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
110
111     str = "http://[::1]/foo/bar";
112     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
113
114     str = "http://:@just-localhost.example.net:8080/";
115     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
116
117     str = "mailto:feedback@example.com?subject=hello";
118     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
119
120     str = "nfs://nfs-server/foo/bar/trala";
121     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
122
123     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
124     BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
125
126     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
127     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
128
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());
133
134     // throws:  host is mandatory
135     str = "ftp:///foo/bar";
136     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
137
138     // throws:  host is mandatory
139     str = "http:///%2f/srv/ftp";
140     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
141
142     // OK, host allowed in file-url
143     str = "file://localhost/some/path";
144     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
145
146     // throws:  host not allowed
147     str = "cd://localhost/some/path";
148     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
149
150     // throws: no path (email)
151     str = "mailto:";
152     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
153
154     // throws:  no path
155     str = "cd:";
156     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
157
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());
162 }
163
164 BOOST_AUTO_TEST_CASE(test_url2)
165 {
166   zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
167
168   BOOST_CHECK_EQUAL( url.asString(),
169   "http://user@localhost/path/to?arg=val#frag" );
170
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");
174
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");
178
179   BOOST_CHECK_EQUAL( url.asCompleteString(),
180   "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
181 }
182
183 BOOST_AUTO_TEST_CASE(test_url3)
184 {
185   zypp::Url   url("http://localhost/path/to#frag");
186   std::string key;
187   std::string val;
188
189   // will be encoded as "hoho=ha%20ha"
190   key = "hoho";
191   val = "ha ha";
192   url.setQueryParam(key, val);
193   BOOST_CHECK_EQUAL( url.asString(),
194   "http://localhost/path/to?hoho=ha%20ha#frag");
195
196   // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
197   key = "foo=bar&key";
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");
202
203   // will be encoded as "foo%25bar=is%25de%25ad"
204   key = "foo%bar";
205   val = "is%de%ad";
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");
209
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",
215     "hoho=ha%20ha"
216   };
217   BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
218   for( size_t i=0; i<params.size(); i++)
219   {
220       BOOST_CHECK_EQUAL( params[i], result[i]);
221   }
222 }
223
224 BOOST_AUTO_TEST_CASE( test_url4)
225 {
226   try
227   {
228     zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
229
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=*)");
236
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)
242     {
243       if("attrs"  == m->first)
244       {
245         BOOST_CHECK_EQUAL( m->second, "cn,sn");
246       }
247       else
248       if("filter" == m->first)
249       {
250         BOOST_CHECK_EQUAL( m->second, "(cn=*)");
251       }
252       else
253       if("scope"  == m->first)
254       {
255         BOOST_CHECK_EQUAL( m->second, "sub");
256       }
257       else
258       {
259         BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
260       }
261     }
262
263     url.setQueryParam("attrs", "cn,sn,uid");
264     url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
265
266     BOOST_CHECK_EQUAL(url.getQueryParam("attrs"),  "cn,sn,uid");
267     BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
268
269   }
270   catch(const zypp::url::UrlException &e)
271   {
272     ZYPP_CAUGHT(e);
273   }
274 }
275
276 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
277 {
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"], "" );
287 }
288
289 // vim: set ts=2 sts=2 sw=2 ai et: