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 (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 bool bResult = true; 126 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 127 { 128 bool bValid = false; 129 rtl::OUString aDescriptor; 130 try 131 { 132 aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 133 aTests[i].pInput)). 134 getDescriptor(); 135 bValid = true; 136 } 137 catch (rtl::MalformedUriException &) 138 {} 139 140 ASSERT_TRUE(bValid) << "Failed to parse URI"; 141 ASSERT_TRUE(aDescriptor.equalsAscii(aTests[i].pDescriptor)) << "Failed to parse URI correctly"; 142 } 143 } 144 145 146 TEST_F(UrlTest, testDescriptorName) 147 { 148 struct Test 149 { 150 char const * pInput; 151 char const * pName; 152 }; 153 static Test const aTests[] 154 = { { "abc", "abc" }, 155 { "Abc", "abc" }, 156 { "aBC", "abc" }, 157 { "ABC", "abc" }, 158 { "1abc", "1abc" }, 159 { "123", "123" }, 160 { "abc,def=", "abc" }, 161 { "abc,Def=", "abc" }, 162 { "abc,DEF=", "abc" }, 163 { "abc,1def=", "abc" }, 164 { "abc,123=", "abc" }, 165 { "abc,def=xxx,ghi=xxx", "abc" }, 166 { "abc,def=%", "abc" }, 167 { "abc,def=%1", "abc" }, 168 { "abc,def=%00", "abc" }, 169 { "abc,def=%22", "abc" }, 170 { "abc,def=\"", "abc" }, 171 { "abc,def=%ed%a0%80", "abc" } }; 172 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 173 { 174 bool bValid = false; 175 rtl::OUString aName; 176 try 177 { 178 aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 179 aTests[i].pInput)).getName(); 180 bValid = true; 181 } 182 catch (rtl::MalformedUriException &) 183 {} 184 185 ASSERT_TRUE(bValid) << "Failed to parse URI"; 186 ASSERT_TRUE(aName.equalsAscii(aTests[i].pName)) << "Failed to parse URI correctly"; 187 } 188 } 189 190 TEST_F(UrlTest, testDescriptorKey) 191 { 192 struct Test 193 { 194 char const * pInput; 195 char const * pKey; 196 bool bPresent; 197 }; 198 static Test const aTests[] 199 = { { "abc", "abc", false }, 200 { "abc", "def", false }, 201 { "1abc", "def", false }, 202 { "123", "def", false }, 203 { "abc,def=", "abc", false }, 204 { "abc,def=", "def", true }, 205 { "abc,def=", "defg", false }, 206 { "abc,def=", "de", false }, 207 { "abc,def=", "ghi", false }, 208 { "abc,Def=", "def", true }, 209 { "abc,Def=", "Def", true }, 210 { "abc,Def=", "dEF", true }, 211 { "abc,Def=", "DEF", true }, 212 { "abc,def=xxx,ghi=xxx", "abc", false }, 213 { "abc,def=xxx,ghi=xxx", "def", true }, 214 { "abc,def=xxx,ghi=xxx", "ghi", true }, 215 { "abc,def=xxx,ghi=xxx", "jkl", false } }; 216 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 217 { 218 bool bValid = false; 219 bool bPresent = false; 220 try 221 { 222 bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 223 aTests[i].pInput)). 224 hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey)); 225 bValid = true; 226 } 227 catch (rtl::MalformedUriException &) 228 {} 229 230 ASSERT_TRUE(bValid) << "Failed to parse URI"; 231 ASSERT_TRUE(bPresent == aTests[i].bPresent) << "Failed to detect parameter correctly"; 232 } 233 } 234 235 TEST_F(UrlTest, testDescriptorValue) 236 { 237 struct Test 238 { 239 char const * pInput; 240 char const * pKey; 241 char const * pValue; 242 }; 243 static Test const aTests[] 244 = { { "abc", "abc", "" }, 245 { "abc", "def", "" }, 246 { "1abc", "def", "" }, 247 { "123", "def", "" }, 248 { "abc,def=", "abc", "" }, 249 { "abc,def=", "def", "" }, 250 { "abc,def=", "defg", "" }, 251 { "abc,def=", "de", "" }, 252 { "abc,def=", "ghi", "" }, 253 { "abc,Def=", "def", "" }, 254 { "abc,Def=", "Def", "" }, 255 { "abc,Def=", "dEF", "" }, 256 { "abc,Def=", "DEF", "" }, 257 { "abc,def=xxx,ghi=xxx", "abc", "" }, 258 { "abc,def=xxx,ghi=xxx", "def", "xxx" }, 259 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" }, 260 { "abc,def=xxx,ghi=xxx", "jkl", "" }, 261 { "abc,def=%", "def", "%" }, 262 { "abc,def=%1", "def", "%1" }, 263 { "abc,def=%22", "def", "\"" }, 264 { "abc,def=\"", "def", "\"" }, 265 { "abc,def=abc", "def", "abc" }, 266 { "abc,def=Abc", "def", "Abc" }, 267 { "abc,def=aBC", "def", "aBC" }, 268 { "abc,def=ABC", "def", "ABC" }, 269 { "abc,def=%,ghi=", "def", "%" }, 270 { "abc,def=%1,ghi=", "def", "%1" }, 271 { "abc,def=%22,ghi=", "def", "\"" }, 272 { "abc,def=\",ghi=", "def", "\"" }, 273 { "abc,def=abc,ghi=", "def", "abc" }, 274 { "abc,def=Abc,ghi=", "def", "Abc" }, 275 { "abc,def=aBC,ghi=", "def", "aBC" }, 276 { "abc,def=ABC,ghi=", "def", "ABC" }, 277 { "abc,abc=,def=%", "def", "%" }, 278 { "abc,abc=,def=%1", "def", "%1" }, 279 { "abc,abc=,def=%22", "def", "\"" }, 280 { "abc,abc=,def=\"", "def", "\"" }, 281 { "abc,abc=,def=abc", "def", "abc" }, 282 { "abc,abc=,def=Abc", "def", "Abc" }, 283 { "abc,abc=,def=aBC", "def", "aBC" }, 284 { "abc,abc=,def=ABC", "def", "ABC" } }; 285 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 286 { 287 bool bValid = false; 288 rtl::OUString aValue; 289 try 290 { 291 aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii( 292 aTests[i].pInput)). 293 getParameter(rtl::OUString::createFromAscii(aTests[i].pKey)); 294 bValid = true; 295 } 296 catch (rtl::MalformedUriException &) 297 {} 298 ASSERT_TRUE(bValid) << "Failed to parse URI"; 299 ASSERT_TRUE(aValue.equalsAscii(aTests[i].pValue)) << "Failed to get param correctly"; 300 } 301 } 302 303 TEST_F(UrlTest, testUrlParsing) 304 { 305 struct Test 306 { 307 char const * pInput; 308 bool bValid; 309 }; 310 static Test const aTests[] 311 = { { "", false }, 312 { "abc", false }, 313 { "uno", false }, 314 { "uno:", false }, 315 { "uno:abc;def;ghi", true }, 316 { "Uno:abc;def;ghi", true }, 317 { "uNO:abc;def;ghi", true }, 318 { "UNO:abc;def;ghi", true }, 319 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true }, 320 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false }, 321 { "uno:abc;def;", false }, 322 { "uno:abc;def;a", true }, 323 { "uno:abc;def;A", true }, 324 { "uno:abc;def;1", true }, 325 { "uno:abc;def;$&+,/:=?@", true }, 326 { "uno:abc;def;%24&+,/:=?@", false } }; 327 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 328 { 329 bool bValid = false; 330 try 331 { 332 cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput)); 333 bValid = true; 334 } 335 catch (rtl::MalformedUriException &) 336 {} 337 338 if (aTests[i].bValid) 339 { 340 ASSERT_TRUE(bValid) << "Valid uri parsed as invalid"; 341 } 342 else 343 { 344 ASSERT_TRUE(!bValid) << "Invalid uri parsed as valid"; 345 } 346 347 } 348 } 349 350 TEST_F(UrlTest, testUrlConnection) 351 { 352 struct Test 353 { 354 char const * pInput; 355 char const * pConnection; 356 }; 357 static Test const aTests[] 358 = { { "uno:abc;def;ghi", "abc" }, 359 { "uno:Abc;def;ghi", "Abc" }, 360 { "uno:aBC;def;ghi", "aBC" }, 361 { "uno:ABC;def;ghi", "ABC" }, 362 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", 363 "abc,def=xxx,ghi=xxx" } }; 364 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 365 { 366 bool bValid = false; 367 rtl::OUString aConnection; 368 try 369 { 370 aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii( 371 aTests[i].pInput)). 372 getConnection().getDescriptor(); 373 bValid = true; 374 } 375 catch (rtl::MalformedUriException &) 376 {} 377 ASSERT_TRUE(bValid) << "Failed to parse URI"; 378 ASSERT_TRUE(aConnection.equalsAscii(aTests[i].pConnection)) << "Failed to get param correctly"; 379 } 380 } 381 382 TEST_F(UrlTest, testUrlProtocol) 383 { 384 struct Test 385 { 386 char const * pInput; 387 char const * pProtocol; 388 }; 389 static Test const aTests[] 390 = { { "uno:abc;def;ghi", "def" }, 391 { "uno:abc;Def;ghi", "Def" }, 392 { "uno:abc;dEF;ghi", "dEF" }, 393 { "uno:abc;DEF;ghi", "DEF" }, 394 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", 395 "def,ghi=xxx,jkl=xxx" } }; 396 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 397 { 398 bool bValid = false; 399 rtl::OUString aProtocol; 400 try 401 { 402 aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii( 403 aTests[i].pInput)). 404 getProtocol().getDescriptor(); 405 bValid = true; 406 } 407 catch (rtl::MalformedUriException &) 408 {} 409 ASSERT_TRUE(bValid) << "Failed to parse URI"; 410 ASSERT_TRUE(aProtocol.equalsAscii(aTests[i].pProtocol)) << "Failed to get protocol correctly"; 411 } 412 } 413 414 TEST_F(UrlTest, testUrlObjectName) 415 { 416 struct Test 417 { 418 char const * pInput; 419 char const * pObjectName; 420 }; 421 static Test const aTests[] 422 = { { "uno:abc;def;ghi", "ghi" }, 423 { "uno:abc;def;Ghi", "Ghi" }, 424 { "uno:abc;def;gHI", "gHI" }, 425 { "uno:abc;def;GHI", "GHI" }, 426 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" }, 427 { "uno:abc;def;a", "a" }, 428 { "uno:abc;def;A", "A" }, 429 { "uno:abc;def;1", "1" }, 430 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } }; 431 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i) 432 { 433 bool bValid = false; 434 rtl::OUString aObjectName; 435 try 436 { 437 aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii( 438 aTests[i].pInput)).getObjectName(); 439 bValid = true; 440 } 441 catch (rtl::MalformedUriException &) 442 {} 443 ASSERT_TRUE(bValid) << "Failed to parse URI"; 444 ASSERT_TRUE(aObjectName.equalsAscii(aTests[i].pObjectName)) << "Failed to get protocol correctly"; 445 } 446 } 447 } // namespace cppu_ifcontainer 448 449 int main(int argc, char **argv) 450 { 451 ::testing::InitGoogleTest(&argc, argv); 452 return RUN_ALL_TESTS(); 453 } 454 455