1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 #include "precompiled_cppuhelper.hxx" 23 24 #include "cppuhelper/unourl.hxx" 25 #include "rtl/malformeduriexception.hxx" 26 #include "rtl/strbuf.hxx" 27 #include "rtl/string.h" 28 #include "rtl/textenc.h" 29 #include "rtl/ustring.hxx" 30 #include "sal/types.h" 31 #include "gtest/gtest.h" 32 33 namespace cppu_unourl 34 { 35 class UrlTest : public ::testing::Test 36 { 37 public: 38 }; 39 40 TEST_F(UrlTest, testDescriptorParsing) 41 { 42 struct Test 43 { 44 char const * pInput; 45 bool bValid; 46 }; 47 static Test const aTests[] 48 = { { "", false }, 49 { "abc", true }, 50 { "Abc", true }, 51 { "aBC", true }, 52 { "ABC", true }, 53 { "1abc", true }, 54 { "123", true }, 55 { "abc-1", false }, 56 { "ab%63", false }, 57 { "abc,", false }, 58 { "abc,def=", true }, 59 { "abc,Def=", true }, 60 { "abc,DEF=", true }, 61 { "abc,1def=", true }, 62 { "abc,123=", true }, 63 { "abc,def-1=", false }, 64 { "abc,def", false }, 65 { "abc,def=xxx,def=xxx", false }, 66 { "abc,def=xxx,ghi=xxx", true }, 67 { "abc,,def=xxx", false }, 68 { "abc,def=xxx,,ghi=xxx", false }, 69 { "abc,def=xxx,ghi=xxx,", false }, 70 { "abc,def=%", true }, 71 { "abc,def=%1", true }, 72 { "abc,def=%00", true }, 73 { "abc,def=%22", true }, 74 { "abc,def=\"", true }, 75 { "abc,def=%ed%a0%80", true } }; 76 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 77 { 78 bool bValid = false; 79 try 80 { 81 cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii( 82 aTests[i].pInput)); 83 bValid = true; 84 } 85 catch (rtl::MalformedUriException &) 86 {} 87 88 if (aTests[i].bValid) 89 { 90 ASSERT_TRUE(bValid) << "Valid uri parsed as invalid"; 91 } 92 else 93 { 94 ASSERT_TRUE(!bValid) << "Invalid uri parsed as valid"; 95 } 96 } 97 } 98 99 TEST_F(UrlTest, testDescriptorDescriptor) 100 { 101 struct Test 102 { 103 char const * pInput; 104 char const * pDescriptor; 105 }; 106 static Test const aTests[] 107 = {{ "abc", "abc" }, 108 { "Abc", "Abc" }, 109 { "aBC", "aBC" }, 110 { "ABC", "ABC" }, 111 { "1abc", "1abc" }, 112 { "123", "123" }, 113 { "abc,def=", "abc,def=" }, 114 { "abc,Def=", "abc,Def=" }, 115 { "abc,DEF=", "abc,DEF=" }, 116 { "abc,1def=", "abc,1def=" }, 117 { "abc,123=", "abc,123=" }, 118 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" }, 119 { "abc,def=%", "abc,def=%" }, 120 { "abc,def=%1", "abc,def=%1" }, 121 { "abc,def=%00", "abc,def=%00" }, 122 { "abc,def=%22", "abc,def=%22" }, 123 { "abc,def=\"", "abc,def=\"" }, 124 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } }; 125 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 126 { 127 bool bValid = false; 128 rtl::OUString aDescriptor; 129 try 130 { 131 aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 132 aTests[i].pInput)). 133 getDescriptor(); 134 bValid = true; 135 } 136 catch (rtl::MalformedUriException &) 137 {} 138 139 ASSERT_TRUE(bValid) << "Failed to parse URI"; 140 ASSERT_TRUE(aDescriptor.equalsAscii(aTests[i].pDescriptor)) << "Failed to parse URI correctly"; 141 } 142 } 143 144 145 TEST_F(UrlTest, testDescriptorName) 146 { 147 struct Test 148 { 149 char const * pInput; 150 char const * pName; 151 }; 152 static Test const aTests[] 153 = { { "abc", "abc" }, 154 { "Abc", "abc" }, 155 { "aBC", "abc" }, 156 { "ABC", "abc" }, 157 { "1abc", "1abc" }, 158 { "123", "123" }, 159 { "abc,def=", "abc" }, 160 { "abc,Def=", "abc" }, 161 { "abc,DEF=", "abc" }, 162 { "abc,1def=", "abc" }, 163 { "abc,123=", "abc" }, 164 { "abc,def=xxx,ghi=xxx", "abc" }, 165 { "abc,def=%", "abc" }, 166 { "abc,def=%1", "abc" }, 167 { "abc,def=%00", "abc" }, 168 { "abc,def=%22", "abc" }, 169 { "abc,def=\"", "abc" }, 170 { "abc,def=%ed%a0%80", "abc" } }; 171 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 172 { 173 bool bValid = false; 174 rtl::OUString aName; 175 try 176 { 177 aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 178 aTests[i].pInput)).getName(); 179 bValid = true; 180 } 181 catch (rtl::MalformedUriException &) 182 {} 183 184 ASSERT_TRUE(bValid) << "Failed to parse URI"; 185 ASSERT_TRUE(aName.equalsAscii(aTests[i].pName)) << "Failed to parse URI correctly"; 186 } 187 } 188 189 TEST_F(UrlTest, testDescriptorKey) 190 { 191 struct Test 192 { 193 char const * pInput; 194 char const * pKey; 195 bool bPresent; 196 }; 197 static Test const aTests[] 198 = { { "abc", "abc", false }, 199 { "abc", "def", false }, 200 { "1abc", "def", false }, 201 { "123", "def", false }, 202 { "abc,def=", "abc", false }, 203 { "abc,def=", "def", true }, 204 { "abc,def=", "defg", false }, 205 { "abc,def=", "de", false }, 206 { "abc,def=", "ghi", false }, 207 { "abc,Def=", "def", true }, 208 { "abc,Def=", "Def", true }, 209 { "abc,Def=", "dEF", true }, 210 { "abc,Def=", "DEF", true }, 211 { "abc,def=xxx,ghi=xxx", "abc", false }, 212 { "abc,def=xxx,ghi=xxx", "def", true }, 213 { "abc,def=xxx,ghi=xxx", "ghi", true }, 214 { "abc,def=xxx,ghi=xxx", "jkl", false } }; 215 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 216 { 217 bool bValid = false; 218 bool bPresent = false; 219 try 220 { 221 bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 222 aTests[i].pInput)). 223 hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey)); 224 bValid = true; 225 } 226 catch (rtl::MalformedUriException &) 227 {} 228 229 ASSERT_TRUE(bValid) << "Failed to parse URI"; 230 ASSERT_TRUE(bPresent == aTests[i].bPresent) << "Failed to detect parameter correctly"; 231 } 232 } 233 234 TEST_F(UrlTest, testDescriptorValue) 235 { 236 struct Test 237 { 238 char const * pInput; 239 char const * pKey; 240 char const * pValue; 241 }; 242 static Test const aTests[] 243 = { { "abc", "abc", "" }, 244 { "abc", "def", "" }, 245 { "1abc", "def", "" }, 246 { "123", "def", "" }, 247 { "abc,def=", "abc", "" }, 248 { "abc,def=", "def", "" }, 249 { "abc,def=", "defg", "" }, 250 { "abc,def=", "de", "" }, 251 { "abc,def=", "ghi", "" }, 252 { "abc,Def=", "def", "" }, 253 { "abc,Def=", "Def", "" }, 254 { "abc,Def=", "dEF", "" }, 255 { "abc,Def=", "DEF", "" }, 256 { "abc,def=xxx,ghi=xxx", "abc", "" }, 257 { "abc,def=xxx,ghi=xxx", "def", "xxx" }, 258 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" }, 259 { "abc,def=xxx,ghi=xxx", "jkl", "" }, 260 { "abc,def=%", "def", "%" }, 261 { "abc,def=%1", "def", "%1" }, 262 { "abc,def=%22", "def", "\"" }, 263 { "abc,def=\"", "def", "\"" }, 264 { "abc,def=abc", "def", "abc" }, 265 { "abc,def=Abc", "def", "Abc" }, 266 { "abc,def=aBC", "def", "aBC" }, 267 { "abc,def=ABC", "def", "ABC" }, 268 { "abc,def=%,ghi=", "def", "%" }, 269 { "abc,def=%1,ghi=", "def", "%1" }, 270 { "abc,def=%22,ghi=", "def", "\"" }, 271 { "abc,def=\",ghi=", "def", "\"" }, 272 { "abc,def=abc,ghi=", "def", "abc" }, 273 { "abc,def=Abc,ghi=", "def", "Abc" }, 274 { "abc,def=aBC,ghi=", "def", "aBC" }, 275 { "abc,def=ABC,ghi=", "def", "ABC" }, 276 { "abc,abc=,def=%", "def", "%" }, 277 { "abc,abc=,def=%1", "def", "%1" }, 278 { "abc,abc=,def=%22", "def", "\"" }, 279 { "abc,abc=,def=\"", "def", "\"" }, 280 { "abc,abc=,def=abc", "def", "abc" }, 281 { "abc,abc=,def=Abc", "def", "Abc" }, 282 { "abc,abc=,def=aBC", "def", "aBC" }, 283 { "abc,abc=,def=ABC", "def", "ABC" } }; 284 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 285 { 286 bool bValid = false; 287 rtl::OUString aValue; 288 try 289 { 290 aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 291 aTests[i].pInput)). 292 getParameter(rtl::OUString::createFromAscii(aTests[i].pKey)); 293 bValid = true; 294 } 295 catch (rtl::MalformedUriException &) 296 {} 297 ASSERT_TRUE(bValid) << "Failed to parse URI"; 298 ASSERT_TRUE(aValue.equalsAscii(aTests[i].pValue)) << "Failed to get param correctly"; 299 } 300 } 301 302 TEST_F(UrlTest, testUrlParsing) 303 { 304 struct Test 305 { 306 char const * pInput; 307 bool bValid; 308 }; 309 static Test const aTests[] 310 = { { "", false }, 311 { "abc", false }, 312 { "uno", false }, 313 { "uno:", false }, 314 { "uno:abc;def;ghi", true }, 315 { "Uno:abc;def;ghi", true }, 316 { "uNO:abc;def;ghi", true }, 317 { "UNO:abc;def;ghi", true }, 318 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true }, 319 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false }, 320 { "uno:abc;def;", false }, 321 { "uno:abc;def;a", true }, 322 { "uno:abc;def;A", true }, 323 { "uno:abc;def;1", true }, 324 { "uno:abc;def;$&+,/:=?@", true }, 325 { "uno:abc;def;%24&+,/:=?@", false } }; 326 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 327 { 328 bool bValid = false; 329 try 330 { 331 cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput)); 332 bValid = true; 333 } 334 catch (rtl::MalformedUriException &) 335 {} 336 337 if (aTests[i].bValid) 338 { 339 ASSERT_TRUE(bValid) << "Valid uri parsed as invalid"; 340 } 341 else 342 { 343 ASSERT_TRUE(!bValid) << "Invalid uri parsed as valid"; 344 } 345 346 } 347 } 348 349 TEST_F(UrlTest, testUrlConnection) 350 { 351 struct Test 352 { 353 char const * pInput; 354 char const * pConnection; 355 }; 356 static Test const aTests[] 357 = { { "uno:abc;def;ghi", "abc" }, 358 { "uno:Abc;def;ghi", "Abc" }, 359 { "uno:aBC;def;ghi", "aBC" }, 360 { "uno:ABC;def;ghi", "ABC" }, 361 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", 362 "abc,def=xxx,ghi=xxx" } }; 363 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 364 { 365 bool bValid = false; 366 rtl::OUString aConnection; 367 try 368 { 369 aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii( 370 aTests[i].pInput)). 371 getConnection().getDescriptor(); 372 bValid = true; 373 } 374 catch (rtl::MalformedUriException &) 375 {} 376 ASSERT_TRUE(bValid) << "Failed to parse URI"; 377 ASSERT_TRUE(aConnection.equalsAscii(aTests[i].pConnection)) << "Failed to get param correctly"; 378 } 379 } 380 381 TEST_F(UrlTest, testUrlProtocol) 382 { 383 struct Test 384 { 385 char const * pInput; 386 char const * pProtocol; 387 }; 388 static Test const aTests[] 389 = { { "uno:abc;def;ghi", "def" }, 390 { "uno:abc;Def;ghi", "Def" }, 391 { "uno:abc;dEF;ghi", "dEF" }, 392 { "uno:abc;DEF;ghi", "DEF" }, 393 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", 394 "def,ghi=xxx,jkl=xxx" } }; 395 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 396 { 397 bool bValid = false; 398 rtl::OUString aProtocol; 399 try 400 { 401 aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii( 402 aTests[i].pInput)). 403 getProtocol().getDescriptor(); 404 bValid = true; 405 } 406 catch (rtl::MalformedUriException &) 407 {} 408 ASSERT_TRUE(bValid) << "Failed to parse URI"; 409 ASSERT_TRUE(aProtocol.equalsAscii(aTests[i].pProtocol)) << "Failed to get protocol correctly"; 410 } 411 } 412 413 TEST_F(UrlTest, testUrlObjectName) 414 { 415 struct Test 416 { 417 char const * pInput; 418 char const * pObjectName; 419 }; 420 static Test const aTests[] 421 = { { "uno:abc;def;ghi", "ghi" }, 422 { "uno:abc;def;Ghi", "Ghi" }, 423 { "uno:abc;def;gHI", "gHI" }, 424 { "uno:abc;def;GHI", "GHI" }, 425 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" }, 426 { "uno:abc;def;a", "a" }, 427 { "uno:abc;def;A", "A" }, 428 { "uno:abc;def;1", "1" }, 429 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } }; 430 for (unsigned int i = 0; i < sizeof aTests / sizeof (Test); ++i) 431 { 432 bool bValid = false; 433 rtl::OUString aObjectName; 434 try 435 { 436 aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii( 437 aTests[i].pInput)).getObjectName(); 438 bValid = true; 439 } 440 catch (rtl::MalformedUriException &) 441 {} 442 ASSERT_TRUE(bValid) << "Failed to parse URI"; 443 ASSERT_TRUE(aObjectName.equalsAscii(aTests[i].pObjectName)) << "Failed to get protocol correctly"; 444 } 445 } 446 } // namespace cppu_ifcontainer 447 448 int main(int argc, char **argv) 449 { 450 ::testing::InitGoogleTest(&argc, argv); 451 return RUN_ALL_TESTS(); 452 } 453 454