c455c87c327bd13183cb5a959a315a3bce42ca52
[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     str = "file:./srv/ftp";
99     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
100
101     str = "ftp://foo//srv/ftp";
102     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
103
104     str = "FTP://user@local%68ost/%2f/srv/ftp";
105     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
106
107     str = "http://[::1]/foo/bar";
108     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
109
110     str = "http://:@just-localhost.example.net:8080/";
111     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
112
113     str = "mailto:feedback@example.com?subject=hello";
114     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
115
116     str = "nfs://nfs-server/foo/bar/trala";
117     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
118
119     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
120     BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
121
122     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
123     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
124
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());
129
130     // throws:  host is mandatory
131     str = "ftp:///foo/bar";
132     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
133
134     // throws:  host is mandatory
135     str = "http:///%2f/srv/ftp";
136     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
137
138     // OK, host allowed in file-url
139     str = "file://localhost/some/path";
140     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
141
142     // throws:  host not allowed
143     str = "cd://localhost/some/path";
144     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
145
146     // throws: no path (email)
147     str = "mailto:";
148     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
149
150     // throws:  no path
151     str = "cd:";
152     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
153
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());
158 }
159
160 BOOST_AUTO_TEST_CASE(test_url2)
161 {
162   zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
163
164   BOOST_CHECK_EQUAL( url.asString(),
165   "http://user@localhost/path/to?arg=val#frag" );
166
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");
170
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");
174
175   BOOST_CHECK_EQUAL( url.asCompleteString(),
176   "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
177 }
178
179 BOOST_AUTO_TEST_CASE(test_url3)
180 {
181   zypp::Url   url("http://localhost/path/to#frag");
182   std::string key;
183   std::string val;
184
185   // will be encoded as "hoho=ha%20ha"
186   key = "hoho";
187   val = "ha ha";
188   url.setQueryParam(key, val);
189   BOOST_CHECK_EQUAL( url.asString(),
190   "http://localhost/path/to?hoho=ha%20ha#frag");
191
192   // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
193   key = "foo=bar&key";
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");
198
199   // will be encoded as "foo%25bar=is%25de%25ad"
200   key = "foo%bar";
201   val = "is%de%ad";
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");
205
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",
211     "hoho=ha%20ha"
212   };
213   BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
214   for( size_t i=0; i<params.size(); i++)
215   {
216       BOOST_CHECK_EQUAL( params[i], result[i]);
217   }
218 }
219
220 BOOST_AUTO_TEST_CASE( test_url4)
221 {
222   try
223   {
224     zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
225
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=*)");
232
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)
238     {
239       if("attrs"  == m->first)
240       {
241         BOOST_CHECK_EQUAL( m->second, "cn,sn");
242       }
243       else
244       if("filter" == m->first)
245       {
246         BOOST_CHECK_EQUAL( m->second, "(cn=*)");
247       }
248       else
249       if("scope"  == m->first)
250       {
251         BOOST_CHECK_EQUAL( m->second, "sub");
252       }
253       else
254       {
255         BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
256       }
257     }
258
259     url.setQueryParam("attrs", "cn,sn,uid");
260     url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
261
262     BOOST_CHECK_EQUAL(url.getQueryParam("attrs"),  "cn,sn,uid");
263     BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
264
265   }
266   catch(const zypp::url::UrlException &e)
267   {
268     ZYPP_CAUGHT(e);
269   }
270 }
271
272 BOOST_AUTO_TEST_CASE(plugin_querystring_args)
273 {
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"], "" );
283 }
284
285 // vim: set ts=2 sts=2 sw=2 ai et: