| 1 |
/* |
| 2 |
* Copyright (c) 2011-2022, The DART development contributors |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* The list of contributors can be found at: |
| 6 |
* https://github.com/dartsim/dart/blob/master/LICENSE |
| 7 |
* |
| 8 |
* This file is provided under the following "BSD-style" License: |
| 9 |
* Redistribution and use in source and binary forms, with or |
| 10 |
* without modification, are permitted provided that the following |
| 11 |
* conditions are met: |
| 12 |
* * Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* * Redistributions in binary form must reproduce the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer in the documentation and/or other materials provided |
| 17 |
* with the distribution. |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 26 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
* POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <gtest/gtest.h> |
| 34 |
|
| 35 |
#include "dart/common/Uri.hpp" |
| 36 |
|
| 37 |
#include "TestHelpers.hpp" |
| 38 |
|
| 39 |
using dart::common::Uri; |
| 40 |
|
| 41 |
TEST(UriHelpers, fromString_ValidUri_ReturnsTrue) |
| 42 |
{ |
| 43 |
// These examples are from Section 1.1.2 of RFC 3986. |
| 44 |
Uri uri; |
| 45 |
|
| 46 |
ASSERT_TRUE(uri.fromString("ftp://ftp.is.co.za/rfc/rfc1808.txt")); |
| 47 |
ASSERT_TRUE(uri.mScheme); |
| 48 |
ASSERT_TRUE(uri.mAuthority); |
| 49 |
ASSERT_TRUE(uri.mPath); |
| 50 |
EXPECT_FALSE(uri.mQuery); |
| 51 |
EXPECT_FALSE(uri.mFragment); |
| 52 |
EXPECT_EQ("ftp", *uri.mScheme); |
| 53 |
EXPECT_EQ("ftp.is.co.za", *uri.mAuthority); |
| 54 |
EXPECT_EQ("/rfc/rfc1808.txt", *uri.mPath); |
| 55 |
|
| 56 |
ASSERT_TRUE(uri.fromString("http://www.ietf.org/rfc/rfc2396.txt")); |
| 57 |
ASSERT_TRUE(uri.mScheme); |
| 58 |
ASSERT_TRUE(uri.mAuthority); |
| 59 |
ASSERT_TRUE(uri.mPath); |
| 60 |
EXPECT_FALSE(uri.mQuery); |
| 61 |
EXPECT_FALSE(uri.mFragment); |
| 62 |
EXPECT_EQ("http", *uri.mScheme); |
| 63 |
EXPECT_EQ("www.ietf.org", *uri.mAuthority); |
| 64 |
EXPECT_EQ("/rfc/rfc2396.txt", *uri.mPath); |
| 65 |
|
| 66 |
ASSERT_TRUE(uri.fromString("ldap://[2001:db8::7]/c=GB?objectClass?one")); |
| 67 |
ASSERT_TRUE(uri.mScheme); |
| 68 |
ASSERT_TRUE(uri.mAuthority); |
| 69 |
ASSERT_TRUE(uri.mPath); |
| 70 |
ASSERT_TRUE(uri.mQuery); |
| 71 |
EXPECT_FALSE(uri.mFragment); |
| 72 |
EXPECT_EQ("ldap", *uri.mScheme); |
| 73 |
EXPECT_EQ("[2001:db8::7]", *uri.mAuthority); |
| 74 |
EXPECT_EQ("/c=GB", *uri.mPath); |
| 75 |
EXPECT_EQ("objectClass?one", *uri.mQuery); |
| 76 |
|
| 77 |
ASSERT_TRUE(uri.fromString("mailto:[email protected]")); |
| 78 |
ASSERT_TRUE(uri.mScheme); |
| 79 |
ASSERT_FALSE(uri.mAuthority); |
| 80 |
ASSERT_TRUE(uri.mPath); |
| 81 |
ASSERT_FALSE(uri.mQuery); |
| 82 |
EXPECT_FALSE(uri.mFragment); |
| 83 |
EXPECT_EQ("mailto", *uri.mScheme); |
| 84 |
EXPECT_EQ("[email protected]", *uri.mPath); |
| 85 |
|
| 86 |
ASSERT_TRUE(uri.fromString("news:comp.infosystems.www.servers.unix")); |
| 87 |
ASSERT_TRUE(uri.mScheme); |
| 88 |
ASSERT_FALSE(uri.mAuthority); |
| 89 |
ASSERT_TRUE(uri.mPath); |
| 90 |
ASSERT_FALSE(uri.mQuery); |
| 91 |
EXPECT_FALSE(uri.mFragment); |
| 92 |
EXPECT_EQ("news", *uri.mScheme); |
| 93 |
EXPECT_EQ("comp.infosystems.www.servers.unix", *uri.mPath); |
| 94 |
|
| 95 |
ASSERT_TRUE(uri.fromString("tel:+1-816-555-1212")); |
| 96 |
ASSERT_TRUE(uri.mScheme); |
| 97 |
ASSERT_FALSE(uri.mAuthority); |
| 98 |
ASSERT_TRUE(uri.mPath); |
| 99 |
ASSERT_FALSE(uri.mQuery); |
| 100 |
EXPECT_FALSE(uri.mFragment); |
| 101 |
EXPECT_EQ("tel", *uri.mScheme); |
| 102 |
EXPECT_EQ("+1-816-555-1212", *uri.mPath); |
| 103 |
|
| 104 |
ASSERT_TRUE(uri.fromString("telnet://192.0.2.16:80/")); |
| 105 |
ASSERT_TRUE(uri.mScheme); |
| 106 |
ASSERT_TRUE(uri.mAuthority); |
| 107 |
ASSERT_TRUE(uri.mPath); |
| 108 |
ASSERT_FALSE(uri.mQuery); |
| 109 |
EXPECT_FALSE(uri.mFragment); |
| 110 |
EXPECT_EQ("telnet", *uri.mScheme); |
| 111 |
EXPECT_EQ("192.0.2.16:80", *uri.mAuthority); |
| 112 |
EXPECT_EQ("/", *uri.mPath); |
| 113 |
|
| 114 |
ASSERT_TRUE( |
| 115 |
uri.fromString("urn:oasis:names:specification:docbook:dtd:xml:4.1.2")); |
| 116 |
ASSERT_TRUE(uri.mScheme); |
| 117 |
ASSERT_FALSE(uri.mAuthority); |
| 118 |
ASSERT_TRUE(uri.mPath); |
| 119 |
ASSERT_FALSE(uri.mQuery); |
| 120 |
EXPECT_FALSE(uri.mFragment); |
| 121 |
EXPECT_FALSE(uri.mAuthority); |
| 122 |
EXPECT_EQ("oasis:names:specification:docbook:dtd:xml:4.1.2", *uri.mPath); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(UriHelpers, fromPath_PathNotUri_ReturnsFileURIwithEmptyAuthority) |
| 126 |
{ |
| 127 |
#ifdef _WIN32 |
| 128 |
std::vector<std::string> testPaths = { |
| 129 |
"C:\\foo", |
| 130 |
"C:\\foo\\", |
| 131 |
"C:\\foo\\bar", |
| 132 |
}; |
| 133 |
#else |
| 134 |
std::vector<std::string> testPaths = {"/foo", "/foo/", "/foo/bar"}; |
| 135 |
#endif |
| 136 |
|
| 137 |
Uri uri; |
| 138 |
|
| 139 |
ASSERT_TRUE(uri.fromPath(testPaths[0])); |
| 140 |
ASSERT_TRUE(uri.mScheme); |
| 141 |
ASSERT_TRUE(uri.mAuthority); |
| 142 |
ASSERT_TRUE(uri.mPath); |
| 143 |
ASSERT_FALSE(uri.mQuery); |
| 144 |
EXPECT_FALSE(uri.mFragment); |
| 145 |
EXPECT_EQ("file", *uri.mScheme); |
| 146 |
EXPECT_EQ("", *uri.mAuthority); |
| 147 |
EXPECT_EQ(testPaths[0], uri.getFilesystemPath()); |
| 148 |
|
| 149 |
ASSERT_TRUE(uri.fromPath(testPaths[1])); |
| 150 |
ASSERT_TRUE(uri.mScheme); |
| 151 |
ASSERT_TRUE(uri.mAuthority); |
| 152 |
ASSERT_TRUE(uri.mPath); |
| 153 |
ASSERT_FALSE(uri.mQuery); |
| 154 |
EXPECT_FALSE(uri.mFragment); |
| 155 |
EXPECT_EQ("file", *uri.mScheme); |
| 156 |
EXPECT_EQ("", *uri.mAuthority); |
| 157 |
EXPECT_EQ(testPaths[1], uri.getFilesystemPath()); |
| 158 |
|
| 159 |
ASSERT_TRUE(uri.fromPath(testPaths[2])); |
| 160 |
ASSERT_TRUE(uri.mScheme); |
| 161 |
ASSERT_TRUE(uri.mAuthority); |
| 162 |
ASSERT_TRUE(uri.mPath); |
| 163 |
ASSERT_FALSE(uri.mQuery); |
| 164 |
EXPECT_FALSE(uri.mFragment); |
| 165 |
EXPECT_EQ("file", *uri.mScheme); |
| 166 |
EXPECT_EQ("", *uri.mAuthority); |
| 167 |
EXPECT_EQ(testPaths[2], uri.getFilesystemPath()); |
| 168 |
} |
| 169 |
|
| 170 |
TEST(UriHelpers, fromStringOrPath_UriNotPathNorFileUri_ReturnsUriNotFileUri) |
| 171 |
{ |
| 172 |
std::vector<std::string> testUris |
| 173 |
= {"ftp://ftp.is.co.za/rfc/rfc1808.txt", |
| 174 |
"http://www.ietf.org/rfc/rfc2396.txt", |
| 175 |
"ldap://[2001:db8::7]/c=GB?objectClass?one", |
| 176 |
"mailto:[email protected]", |
| 177 |
"news:comp.infosystems.www.servers.unix", |
| 178 |
"tel:+1-816-555-1212", |
| 179 |
"telnet://192.0.2.16:80/", |
| 180 |
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2"}; |
| 181 |
|
| 182 |
Uri uri; |
| 183 |
|
| 184 |
for (const std::string& testUri : testUris) |
| 185 |
{ |
| 186 |
uri.fromStringOrPath(testUri); |
| 187 |
|
| 188 |
EXPECT_NE("file", *uri.mScheme); |
| 189 |
EXPECT_EQ(testUri, uri.toString()); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
TEST(UriHelpers, fromString_InputIsUri_DoesNotChange) |
| 194 |
{ |
| 195 |
Uri uri; |
| 196 |
|
| 197 |
ASSERT_TRUE(uri.fromString("ftp://ftp.is.co.za/rfc/rfc1808.txt")); |
| 198 |
ASSERT_TRUE(uri.mScheme); |
| 199 |
ASSERT_TRUE(uri.mAuthority); |
| 200 |
ASSERT_TRUE(uri.mPath); |
| 201 |
EXPECT_FALSE(uri.mQuery); |
| 202 |
EXPECT_FALSE(uri.mFragment); |
| 203 |
EXPECT_EQ("ftp", *uri.mScheme); |
| 204 |
EXPECT_EQ("ftp.is.co.za", *uri.mAuthority); |
| 205 |
EXPECT_EQ("/rfc/rfc1808.txt", *uri.mPath); |
| 206 |
} |
| 207 |
|
| 208 |
TEST(UriHelpers, getUri_InputIsUri_DoesNotChange) |
| 209 |
{ |
| 210 |
std::vector<std::string> testUris |
| 211 |
= {"ftp://ftp.is.co.za/rfc/rfc1808.txt", |
| 212 |
"http://www.ietf.org/rfc/rfc2396.txt", |
| 213 |
"ldap://[2001:db8::7]/c=GB?objectClass?one", |
| 214 |
"mailto:[email protected]", |
| 215 |
"news:comp.infosystems.www.servers.unix", |
| 216 |
"tel:+1-816-555-1212", |
| 217 |
"telnet://192.0.2.16:80/", |
| 218 |
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2"}; |
| 219 |
|
| 220 |
for (const std::string& testUri : testUris) |
| 221 |
EXPECT_EQ(testUri, Uri::createFromString(testUri).toString()); |
| 222 |
} |
| 223 |
|
| 224 |
TEST(UriHelpers, getUri_InputIsPath_AppendsFileSchema) |
| 225 |
{ |
| 226 |
#ifdef _WIN32 |
| 227 |
std::vector<std::string> testPaths = { |
| 228 |
"C:\\foo", |
| 229 |
"C:\\foo\\", |
| 230 |
"C:\\foo\\bar", |
| 231 |
}; |
| 232 |
#else |
| 233 |
std::vector<std::string> testPaths = {"/foo", "/foo/", "/foo/bar"}; |
| 234 |
#endif |
| 235 |
|
| 236 |
for (const std::string& testPath : testPaths) |
| 237 |
{ |
| 238 |
#ifdef _WIN32 |
| 239 |
// On Windows, an absolute path does not begin with forward slash but a |
| 240 |
// file URI needs it to represent an empty authority, |
| 241 |
const std::string testUri = "file:///" + testPath; |
| 242 |
#else |
| 243 |
// wherease on Unix systems the additional forward slash is not required |
| 244 |
// since an absolute path already has it. |
| 245 |
const std::string testUri = "file://" + testPath; |
| 246 |
#endif |
| 247 |
EXPECT_EQ(testUri, Uri::getUri(testPath)); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
TEST(UriHelpers, getRelativeUri) |
| 252 |
{ |
| 253 |
std::vector<std::pair<std::string, std::string> > testPairs = { |
| 254 |
// RFC 3986, Section 5.4.1.: Normal Examples |
| 255 |
{"g:h", "g:h"}, |
| 256 |
{"g", "http://a/b/c/g"}, |
| 257 |
{"./g", "http://a/b/c/g"}, |
| 258 |
{"g/", "http://a/b/c/g/"}, |
| 259 |
{"/g", "http://a/g"}, |
| 260 |
{"//g", "http://g"}, |
| 261 |
{"?y", "http://a/b/c/d;p?y"}, |
| 262 |
{"g?y", "http://a/b/c/g?y"}, |
| 263 |
{"#s", "http://a/b/c/d;p?q#s"}, |
| 264 |
{"g#s", "http://a/b/c/g#s"}, |
| 265 |
{"g?y#s", "http://a/b/c/g?y#s"}, |
| 266 |
{";x", "http://a/b/c/;x"}, |
| 267 |
{"g;x", "http://a/b/c/g;x"}, |
| 268 |
{"g;x?y#s", "http://a/b/c/g;x?y#s"}, |
| 269 |
{"", "http://a/b/c/d;p?q"}, |
| 270 |
{".", "http://a/b/c/"}, |
| 271 |
{"./", "http://a/b/c/"}, |
| 272 |
{"..", "http://a/b/"}, |
| 273 |
{"../", "http://a/b/"}, |
| 274 |
{"../g", "http://a/b/g"}, |
| 275 |
{"../..", "http://a/"}, |
| 276 |
{"../../", "http://a/"}, |
| 277 |
{"../../g", "http://a/g"}, |
| 278 |
// RFC 3986, Section 5.4.2.: Abnormal Examples |
| 279 |
{"../../../g", "http://a/g"}, |
| 280 |
{"../../../../g", "http://a/g"}, |
| 281 |
{"/./g", "http://a/g"}, |
| 282 |
{"/../g", "http://a/g"}, |
| 283 |
{"g.", "http://a/b/c/g."}, |
| 284 |
{".g", "http://a/b/c/.g"}, |
| 285 |
{"g..", "http://a/b/c/g.."}, |
| 286 |
{"..g", "http://a/b/c/..g"}, |
| 287 |
{"./../g", "http://a/b/g"}, |
| 288 |
{"./g/.", "http://a/b/c/g/"}, |
| 289 |
{"g/./h", "http://a/b/c/g/h"}, |
| 290 |
{"g/../h", "http://a/b/c/h"}, |
| 291 |
{"g;x=1/./y", "http://a/b/c/g;x=1/y"}, |
| 292 |
{"g;x=1/../y", "http://a/b/c/y"}, |
| 293 |
{"g?y/./x", "http://a/b/c/g?y/./x"}, |
| 294 |
{"g?y/../x", "http://a/b/c/g?y/../x"}, |
| 295 |
{"g#s/./x", "http://a/b/c/g#s/./x"}, |
| 296 |
{"g#s/../x", "http://a/b/c/g#s/../x"}, |
| 297 |
}; |
| 298 |
|
| 299 |
Uri baseUri, relativeUri, mergedUri; |
| 300 |
ASSERT_TRUE(baseUri.fromString("http://a/b/c/d;p?q")); |
| 301 |
|
| 302 |
for (const auto& it : testPairs) |
| 303 |
{ |
| 304 |
const std::string& expectedUri = it.second; |
| 305 |
|
| 306 |
ASSERT_TRUE(relativeUri.fromString(it.first)); |
| 307 |
|
| 308 |
// Strict mode |
| 309 |
ASSERT_TRUE(mergedUri.fromRelativeUri(baseUri, relativeUri, true)); |
| 310 |
EXPECT_EQ(expectedUri, mergedUri.toString()); |
| 311 |
|
| 312 |
// Backwards compatability mode |
| 313 |
ASSERT_TRUE(mergedUri.fromRelativeUri(baseUri, relativeUri, false)); |
| 314 |
EXPECT_EQ(expectedUri, mergedUri.toString()); |
| 315 |
} |
| 316 |
|
| 317 |
// Strict mode behavior. |
| 318 |
ASSERT_TRUE(relativeUri.fromString("http:g")); |
| 319 |
ASSERT_TRUE(mergedUri.fromRelativeUri(baseUri, "http:g", true)); |
| 320 |
EXPECT_EQ("http:g", mergedUri.toString()); |
| 321 |
|
| 322 |
// TODO: I'm not sure what we have to do to implement this. The approach |
| 323 |
// described in the RFC doesn't seem to produce the expected result. |
| 324 |
#if 0 |
| 325 |
// Backwards compatability mode behavior. |
| 326 |
ASSERT_TRUE(relativeUri.fromString("http:g")); |
| 327 |
ASSERT_TRUE(mergedUri.fromRelativeUri(baseUri, "http:g", false)); |
| 328 |
EXPECT_EQ("http://a/b/c/g", mergedUri.toString()); |
| 329 |
#endif |
| 330 |
} |