Let's make the tests also compile with boost 1.33 (auto_unit_test.hpp is
[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/Url.h"
10 #include <stdexcept>
11 #include <iostream>
12 #include <cassert>
13
14 // Boost.Test
15 #include <boost/test/auto_unit_test.hpp>
16
17 using boost::unit_test::test_case;
18 using namespace zypp;
19
20 BOOST_AUTO_TEST_CASE(test_url1)
21 {
22     std::string str, one, two;
23     zypp::Url   url;
24
25
26     // asString & asCompleteString should not print "mailto://"
27     str = "mailto:feedback@example.com?subject=hello";
28     url = str;
29     BOOST_CHECK_EQUAL( str, url.asString() );
30     BOOST_CHECK_EQUAL( str, url.asCompleteString() );
31
32     // asString & asCompleteString should add empty authority
33     // "dvd://...", except we request to avoid it.
34     str = "dvd:/srv/ftp";
35     one = "dvd:///srv/ftp";
36     two = "dvd:///srv/ftp";
37     url = str;
38
39     BOOST_CHECK_EQUAL( one, url.asString() );
40     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
41     BOOST_CHECK_EQUAL( str, url.asString(zypp::url::ViewOptions() -
42                                  zypp::url::ViewOption::EMPTY_AUTHORITY));
43
44     // asString shouldn't print the password, asCompleteString should
45     // further, the "//" at the begin of the path should become "/%2F"
46     str = "ftp://user:pass@localhost//srv/ftp";
47     one = "ftp://user@localhost/%2Fsrv/ftp";
48     two = "ftp://user:pass@localhost/%2Fsrv/ftp";
49     url = str;
50
51     BOOST_CHECK_EQUAL( one, url.asString() );
52     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
53
54     // asString shouldn't print the password, asCompleteString should.
55     // further, the "//" at the begin of the path should be keept.
56     str = "http://user:pass@localhost//srv/ftp";
57     one = "http://user@localhost//srv/ftp";
58     two = str;
59     url = str;
60
61     BOOST_CHECK_EQUAL( one, url.asString() );
62     BOOST_CHECK_EQUAL( two, url.asCompleteString() );
63
64     str = "file:./srv/ftp";
65     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
66
67     str = "ftp://foo//srv/ftp";
68     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
69
70     str = "FTP://user@local%68ost/%2f/srv/ftp";
71     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
72
73     str = "http://[::1]/foo/bar";
74     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
75
76     str = "http://:@just-localhost.example.net:8080/";
77     BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
78
79     str = "mailto:feedback@example.com?subject=hello";
80     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
81
82     str = "nfs://nfs-server/foo/bar/trala";
83     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
84
85     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
86     BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
87   
88     str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
89     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
90
91     // parseable but invalid, since no host avaliable
92     str = "ldap:///dc=foo,dc=bar";
93     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
94     BOOST_CHECK( !zypp::Url(str).isValid());
95
96     // throws:  host is mandatory
97     str = "ftp:///foo/bar";
98     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
99
100     // throws:  host is mandatory
101     str = "http:///%2f/srv/ftp";
102     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
103
104     // OK, host allowed in file-url
105     str = "file://localhost/some/path";
106     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
107
108     // throws:  host not allowed
109     str = "cd://localhost/some/path";
110     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
111
112     // throws: no path (email)
113     str = "mailto:";
114     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
115
116     // throws:  no path
117     str = "cd:";
118     BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
119
120     // OK, valid (no host, path is there)
121     str = "cd:///some/path";
122     BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
123     BOOST_CHECK( zypp::Url(str).isValid());
124 }
125
126 BOOST_AUTO_TEST_CASE(test_url2)
127 {
128   zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
129
130   BOOST_CHECK_EQUAL( url.asString(),
131   "http://user@localhost/path/to?arg=val#frag" );
132
133   BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
134                      zypp::url::ViewOptions::WITH_PASSWORD),
135   "http://user:pass@localhost/path/to?arg=val#frag");
136
137   BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
138                      zypp::url::ViewOptions::WITH_PATH_PARAMS),
139   "http://user@localhost/path/to;version=1.1?arg=val#frag");
140
141   BOOST_CHECK_EQUAL( url.asCompleteString(),
142   "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
143 }
144
145 BOOST_AUTO_TEST_CASE(test_url3)
146 {
147   zypp::Url   url("http://localhost/path/to#frag");
148   std::string key;
149   std::string val;
150
151   // will be encoded as "hoho=ha%20ha"
152   key = "hoho";
153   val = "ha ha";
154   url.setQueryParam(key, val);
155   BOOST_CHECK_EQUAL( url.asString(),
156   "http://localhost/path/to?hoho=ha%20ha#frag");
157
158   // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
159   key = "foo=bar&key";
160   val = "foo&bar=value";
161   url.setQueryParam(key, val);
162   BOOST_CHECK_EQUAL( url.asString(),
163   "http://localhost/path/to?foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
164
165   // will be encoded as "foo%25bar=is%25de%25ad"
166   key = "foo%bar";
167   val = "is%de%ad";
168   url.setQueryParam(key, val);
169   BOOST_CHECK_EQUAL( url.asString(),
170   "http://localhost/path/to?foo%25bar=is%25de%25ad&foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
171
172   // get encoded query parameters and compare with results:
173   zypp::url::ParamVec params( url.getQueryStringVec());
174   const char * const  result[] = {
175     "foo%25bar=is%25de%25ad",
176     "foo%3Dbar%26key=foo%26bar%3Dvalue",
177     "hoho=ha%20ha"
178   };
179   BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
180   for( size_t i=0; i<params.size(); i++)
181   {
182       BOOST_CHECK_EQUAL( params[i], result[i]);
183   }
184 }
185
186 BOOST_AUTO_TEST_CASE( test_url4)
187 {
188   try
189   {
190     zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
191
192     // fetch query params as vector
193     zypp::url::ParamVec pvec( url.getQueryStringVec());
194     BOOST_CHECK( pvec.size() == 3);
195     BOOST_CHECK_EQUAL( pvec[0], "cn,sn");
196     BOOST_CHECK_EQUAL( pvec[1], "sub");
197     BOOST_CHECK_EQUAL( pvec[2], "(cn=*)");
198
199     // fetch the query params map
200     // with its special ldap names/keys
201     zypp::url::ParamMap pmap( url.getQueryStringMap());
202     zypp::url::ParamMap::const_iterator m;
203     for(m=pmap.begin(); m!=pmap.end(); ++m)
204     {
205       if("attrs"  == m->first)
206       {
207         BOOST_CHECK_EQUAL( m->second, "cn,sn");
208       }
209       else
210       if("filter" == m->first)
211       {
212         BOOST_CHECK_EQUAL( m->second, "(cn=*)");
213       }
214       else
215       if("scope"  == m->first)
216       {
217         BOOST_CHECK_EQUAL( m->second, "sub");
218       }
219       else
220       {
221         BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
222       }
223     }
224
225     url.setQueryParam("attrs", "cn,sn,uid");
226     url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
227
228     BOOST_CHECK_EQUAL(url.getQueryParam("attrs"),  "cn,sn,uid");
229     BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
230
231   }
232   catch(const zypp::url::UrlException &e)
233   {
234     ZYPP_CAUGHT(e);
235   }
236 }
237
238
239 // vim: set ts=2 sts=2 sw=2 ai et: