1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cppu_unourl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <cppunit/simpleheader.hxx>
33 #include "cppuhelper/unourl.hxx"
34 #include "rtl/malformeduriexception.hxx"
35 #include "rtl/strbuf.hxx"
36 #include "rtl/string.h"
37 #include "rtl/textenc.h"
38 #include "rtl/ustring.hxx"
39 #include "sal/types.h"
43 class UrlTest
: public CppUnit::TestFixture
46 void testDescriptorParsing()
53 static Test
const aTests
[]
67 { "abc,1def=", true },
69 { "abc,def-1=", false },
71 { "abc,def=xxx,def=xxx", false },
72 { "abc,def=xxx,ghi=xxx", true },
73 { "abc,,def=xxx", false },
74 { "abc,def=xxx,,ghi=xxx", false },
75 { "abc,def=xxx,ghi=xxx,", false },
76 { "abc,def=%", true },
77 { "abc,def=%1", true },
78 { "abc,def=%00", true },
79 { "abc,def=%22", true },
80 { "abc,def=\"", true },
81 { "abc,def=%ed%a0%80", true } };
82 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
87 cppu::UnoUrlDescriptor
aDescriptor(rtl::OUString::createFromAscii(
91 catch (rtl::MalformedUriException
&)
96 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid
);
100 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid
);
105 void testDescriptorDescriptor()
110 char const * pDescriptor
;
112 static Test
const aTests
[]
119 { "abc,def=", "abc,def=" },
120 { "abc,Def=", "abc,Def=" },
121 { "abc,DEF=", "abc,DEF=" },
122 { "abc,1def=", "abc,1def=" },
123 { "abc,123=", "abc,123=" },
124 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
125 { "abc,def=%", "abc,def=%" },
126 { "abc,def=%1", "abc,def=%1" },
127 { "abc,def=%00", "abc,def=%00" },
128 { "abc,def=%22", "abc,def=%22" },
129 { "abc,def=\"", "abc,def=\"" },
130 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
132 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
135 rtl::OUString aDescriptor
;
138 aDescriptor
= cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
143 catch (rtl::MalformedUriException
&)
146 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
147 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
148 aDescriptor
.equalsAscii(
149 aTests
[i
].pDescriptor
));
154 void testDescriptorName()
161 static Test
const aTests
[]
162 = { { "abc", "abc" },
168 { "abc,def=", "abc" },
169 { "abc,Def=", "abc" },
170 { "abc,DEF=", "abc" },
171 { "abc,1def=", "abc" },
172 { "abc,123=", "abc" },
173 { "abc,def=xxx,ghi=xxx", "abc" },
174 { "abc,def=%", "abc" },
175 { "abc,def=%1", "abc" },
176 { "abc,def=%00", "abc" },
177 { "abc,def=%22", "abc" },
178 { "abc,def=\"", "abc" },
179 { "abc,def=%ed%a0%80", "abc" } };
180 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
186 aName
= cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
187 aTests
[i
].pInput
)).getName();
190 catch (rtl::MalformedUriException
&)
193 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
194 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
195 aName
.equalsAscii(aTests
[i
].pName
));
199 void testDescriptorKey(void)
207 static Test
const aTests
[]
208 = { { "abc", "abc", false },
209 { "abc", "def", false },
210 { "1abc", "def", false },
211 { "123", "def", false },
212 { "abc,def=", "abc", false },
213 { "abc,def=", "def", true },
214 { "abc,def=", "defg", false },
215 { "abc,def=", "de", false },
216 { "abc,def=", "ghi", false },
217 { "abc,Def=", "def", true },
218 { "abc,Def=", "Def", true },
219 { "abc,Def=", "dEF", true },
220 { "abc,Def=", "DEF", true },
221 { "abc,def=xxx,ghi=xxx", "abc", false },
222 { "abc,def=xxx,ghi=xxx", "def", true },
223 { "abc,def=xxx,ghi=xxx", "ghi", true },
224 { "abc,def=xxx,ghi=xxx", "jkl", false } };
225 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
228 bool bPresent
= false;
231 bPresent
= cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
233 hasParameter(rtl::OUString::createFromAscii(aTests
[i
].pKey
));
236 catch (rtl::MalformedUriException
&)
239 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
240 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
241 bPresent
== aTests
[i
].bPresent
);
245 void testDescriptorValue()
253 static Test
const aTests
[]
254 = { { "abc", "abc", "" },
255 { "abc", "def", "" },
256 { "1abc", "def", "" },
257 { "123", "def", "" },
258 { "abc,def=", "abc", "" },
259 { "abc,def=", "def", "" },
260 { "abc,def=", "defg", "" },
261 { "abc,def=", "de", "" },
262 { "abc,def=", "ghi", "" },
263 { "abc,Def=", "def", "" },
264 { "abc,Def=", "Def", "" },
265 { "abc,Def=", "dEF", "" },
266 { "abc,Def=", "DEF", "" },
267 { "abc,def=xxx,ghi=xxx", "abc", "" },
268 { "abc,def=xxx,ghi=xxx", "def", "xxx" },
269 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
270 { "abc,def=xxx,ghi=xxx", "jkl", "" },
271 { "abc,def=%", "def", "%" },
272 { "abc,def=%1", "def", "%1" },
273 { "abc,def=%22", "def", "\"" },
274 { "abc,def=\"", "def", "\"" },
275 { "abc,def=abc", "def", "abc" },
276 { "abc,def=Abc", "def", "Abc" },
277 { "abc,def=aBC", "def", "aBC" },
278 { "abc,def=ABC", "def", "ABC" },
279 { "abc,def=%,ghi=", "def", "%" },
280 { "abc,def=%1,ghi=", "def", "%1" },
281 { "abc,def=%22,ghi=", "def", "\"" },
282 { "abc,def=\",ghi=", "def", "\"" },
283 { "abc,def=abc,ghi=", "def", "abc" },
284 { "abc,def=Abc,ghi=", "def", "Abc" },
285 { "abc,def=aBC,ghi=", "def", "aBC" },
286 { "abc,def=ABC,ghi=", "def", "ABC" },
287 { "abc,abc=,def=%", "def", "%" },
288 { "abc,abc=,def=%1", "def", "%1" },
289 { "abc,abc=,def=%22", "def", "\"" },
290 { "abc,abc=,def=\"", "def", "\"" },
291 { "abc,abc=,def=abc", "def", "abc" },
292 { "abc,abc=,def=Abc", "def", "Abc" },
293 { "abc,abc=,def=aBC", "def", "aBC" },
294 { "abc,abc=,def=ABC", "def", "ABC" } };
295 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
298 rtl::OUString aValue
;
301 aValue
= cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
303 getParameter(rtl::OUString::createFromAscii(aTests
[i
].pKey
));
306 catch (rtl::MalformedUriException
&)
308 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
309 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
310 aValue
.equalsAscii(aTests
[i
].pValue
));
314 void testUrlParsing()
321 static Test
const aTests
[]
326 { "uno:abc;def;ghi", true },
327 { "Uno:abc;def;ghi", true },
328 { "uNO:abc;def;ghi", true },
329 { "UNO:abc;def;ghi", true },
330 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
331 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
332 { "uno:abc;def;", false },
333 { "uno:abc;def;a", true },
334 { "uno:abc;def;A", true },
335 { "uno:abc;def;1", true },
336 { "uno:abc;def;$&+,/:=?@", true },
337 { "uno:abc;def;%24&+,/:=?@", false } };
338 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
343 cppu::UnoUrl
aUrl(rtl::OUString::createFromAscii(aTests
[i
].pInput
));
346 catch (rtl::MalformedUriException
&)
349 if (aTests
[i
].bValid
)
351 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid
);
355 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid
);
361 void testUrlConnection()
366 char const * pConnection
;
368 static Test
const aTests
[]
369 = { { "uno:abc;def;ghi", "abc" },
370 { "uno:Abc;def;ghi", "Abc" },
371 { "uno:aBC;def;ghi", "aBC" },
372 { "uno:ABC;def;ghi", "ABC" },
373 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
374 "abc,def=xxx,ghi=xxx" } };
375 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
378 rtl::OUString aConnection
;
381 aConnection
= cppu::UnoUrl(rtl::OUString::createFromAscii(
383 getConnection().getDescriptor();
386 catch (rtl::MalformedUriException
&)
388 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
389 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
390 aConnection
.equalsAscii(
391 aTests
[i
].pConnection
));
395 void testUrlProtocol()
400 char const * pProtocol
;
402 static Test
const aTests
[]
403 = { { "uno:abc;def;ghi", "def" },
404 { "uno:abc;Def;ghi", "Def" },
405 { "uno:abc;dEF;ghi", "dEF" },
406 { "uno:abc;DEF;ghi", "DEF" },
407 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
408 "def,ghi=xxx,jkl=xxx" } };
409 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
412 rtl::OUString aProtocol
;
415 aProtocol
= cppu::UnoUrl(rtl::OUString::createFromAscii(
417 getProtocol().getDescriptor();
420 catch (rtl::MalformedUriException
&)
422 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
423 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
424 aProtocol
.equalsAscii(
425 aTests
[i
].pProtocol
));
429 void testUrlObjectName()
434 char const * pObjectName
;
436 static Test
const aTests
[]
437 = { { "uno:abc;def;ghi", "ghi" },
438 { "uno:abc;def;Ghi", "Ghi" },
439 { "uno:abc;def;gHI", "gHI" },
440 { "uno:abc;def;GHI", "GHI" },
441 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
442 { "uno:abc;def;a", "a" },
443 { "uno:abc;def;A", "A" },
444 { "uno:abc;def;1", "1" },
445 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
446 for (int i
= 0; i
< sizeof aTests
/ sizeof (Test
); ++i
)
449 rtl::OUString aObjectName
;
452 aObjectName
= cppu::UnoUrl(rtl::OUString::createFromAscii(
453 aTests
[i
].pInput
)).getObjectName();
456 catch (rtl::MalformedUriException
&)
458 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
459 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
460 aObjectName
.equalsAscii(
461 aTests
[i
].pObjectName
));
465 // Automatic registration code
466 CPPUNIT_TEST_SUITE(UrlTest
);
467 CPPUNIT_TEST(testDescriptorParsing
);
468 CPPUNIT_TEST(testDescriptorDescriptor
);
469 CPPUNIT_TEST(testDescriptorName
);
470 CPPUNIT_TEST(testDescriptorKey
);
471 CPPUNIT_TEST(testDescriptorValue
);
472 CPPUNIT_TEST(testUrlParsing
);
473 CPPUNIT_TEST(testUrlConnection
);
474 CPPUNIT_TEST(testUrlProtocol
);
475 CPPUNIT_TEST(testUrlObjectName
);
476 CPPUNIT_TEST_SUITE_END();
478 } // namespace cppu_ifcontainer
480 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(cppu_unourl::UrlTest
,