| 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 |
#define PN_USE_DEPRECATED_API 1 |
| 23 |
|
| 24 |
#include "./pn_test.hpp" |
| 25 |
|
| 26 |
#include "proton/error.h" |
| 27 |
#include "proton/type_compat.h" |
| 28 |
#include "proton/url.h" |
| 29 |
|
| 30 |
using namespace pn_test; |
| 31 |
using Catch::Matchers::Equals; |
| 32 |
|
| 33 |
void check_url(const char *url, const char *scheme, const char *user, |
| 34 |
const char *pass, const char *host, const char *port, |
| 35 |
const char *path, bool round_trip = true) { |
| 36 |
INFO("url=\"" << url << '"'); |
| 37 |
auto_free<pn_url_t, pn_url_free> purl(pn_url_parse(url)); |
| 38 |
CHECK_THAT(scheme, Equals(pn_url_get_scheme(purl))); |
| 39 |
CHECK_THAT(user, Equals(pn_url_get_username(purl))); |
| 40 |
CHECK_THAT(pass, Equals(pn_url_get_password(purl))); |
| 41 |
CHECK_THAT(host, Equals(pn_url_get_host(purl))); |
| 42 |
CHECK_THAT(port, Equals(pn_url_get_port(purl))); |
| 43 |
CHECK_THAT(path, Equals(pn_url_get_path(purl))); |
| 44 |
if (round_trip) CHECK_THAT(url, Equals(pn_url_str(purl))); |
| 45 |
} |
| 46 |
|
| 47 |
TEST_CASE("url") { |
| 48 |
const char *null = 0; |
| 49 |
check_url("/Foo.bar:90087@somewhere", null, null, null, null, null, |
| 50 |
"Foo.bar:90087@somewhere"); |
| 51 |
check_url("host", null, null, null, "host", null, null); |
| 52 |
check_url("host:423", null, null, null, "host", "423", null); |
| 53 |
check_url("user@host", null, "user", null, "host", null, null); |
| 54 |
|
| 55 |
// Can't round-trip passwords with ':', not strictly legal |
| 56 |
// but the parser allows it. |
| 57 |
check_url("user:1243^&^:pw@host:423", null, "user", "1243^&^:pw", "host", |
| 58 |
"423", null, false); |
| 59 |
check_url("user:1243^&^:pw@host:423/Foo.bar:90087", null, "user", |
| 60 |
"1243^&^:pw", "host", "423", "Foo.bar:90087", false); |
| 61 |
check_url("user:1243^&^:pw@host:423/Foo.bar:90087@somewhere", null, "user", |
| 62 |
"1243^&^:pw", "host", "423", "Foo.bar:90087@somewhere", false); |
| 63 |
|
| 64 |
check_url("[::1]:amqp", null, null, null, "::1", "amqp", null); |
| 65 |
check_url("user@[::1]", null, "user", null, "::1", null, null); |
| 66 |
check_url("user@[::1]:amqp", null, "user", null, "::1", "amqp", null); |
| 67 |
|
| 68 |
// Can't round-trip passwords with ':', not strictly legal |
| 69 |
// but the parser allows it. |
| 70 |
check_url("user:1243^&^:pw@[::1]:amqp", null, "user", "1243^&^:pw", "::1", |
| 71 |
"amqp", null, false); |
| 72 |
check_url("user:1243^&^:pw@[::1]:amqp/Foo.bar:90087", null, "user", |
| 73 |
"1243^&^:pw", "::1", "amqp", "Foo.bar:90087", false); |
| 74 |
check_url("user:1243^&^:pw@[::1:amqp/Foo.bar:90087", null, "user", |
| 75 |
"1243^&^:pw", "[::1", "amqp", "Foo.bar:90087", false); |
| 76 |
check_url("user:1243^&^:pw@::1]:amqp/Foo.bar:90087", null, "user", |
| 77 |
"1243^&^:pw", "::1]", "amqp", "Foo.bar:90087", false); |
| 78 |
|
| 79 |
check_url("amqp://user@[::1]", "amqp", "user", null, "::1", null, null); |
| 80 |
check_url("amqp://user@[::1]:amqp", "amqp", "user", null, "::1", "amqp", |
| 81 |
null); |
| 82 |
check_url("amqp://user@[1234:52:0:1260:f2de:f1ff:fe59:8f87]:amqp", "amqp", |
| 83 |
"user", null, "1234:52:0:1260:f2de:f1ff:fe59:8f87", "amqp", null); |
| 84 |
|
| 85 |
// Can't round-trip passwords with ':', not strictly legal |
| 86 |
// but the parser allows it. |
| 87 |
check_url("amqp://user:1243^&^:pw@[::1]:amqp", "amqp", "user", "1243^&^:pw", |
| 88 |
"::1", "amqp", null, false); |
| 89 |
check_url("amqp://user:1243^&^:pw@[::1]:amqp/Foo.bar:90087", "amqp", "user", |
| 90 |
"1243^&^:pw", "::1", "amqp", "Foo.bar:90087", false); |
| 91 |
|
| 92 |
check_url("amqp://host", "amqp", null, null, "host", null, null); |
| 93 |
check_url("amqp://user@host", "amqp", "user", null, "host", null, null); |
| 94 |
check_url("amqp://user@host/path:%", "amqp", "user", null, "host", null, |
| 95 |
"path:%"); |
| 96 |
check_url("amqp://user@host:5674/path:%", "amqp", "user", null, "host", |
| 97 |
"5674", "path:%"); |
| 98 |
check_url("amqp://user@host/path:%", "amqp", "user", null, "host", null, |
| 99 |
"path:%"); |
| 100 |
check_url("amqp://bigbird@host/queue@host", "amqp", "bigbird", null, "host", |
| 101 |
null, "queue@host"); |
| 102 |
check_url("amqp://host/queue@host", "amqp", null, null, "host", null, |
| 103 |
"queue@host"); |
| 104 |
check_url("amqp://host:9765/queue@host", "amqp", null, null, "host", "9765", |
| 105 |
"queue@host"); |
| 106 |
check_url("user:pass%2fword@host", null, "user", "pass/word", "host", null, |
| 107 |
null, false); |
| 108 |
check_url("user:pass%2Fword@host", null, "user", "pass/word", "host", null, |
| 109 |
null); |
| 110 |
// Can't round-trip passwords with lowercase hex encoding |
| 111 |
check_url("us%2fer:password@host", null, "us/er", "password", "host", null, |
| 112 |
null, false); |
| 113 |
check_url("us%2Fer:password@host", null, "us/er", "password", "host", null, |
| 114 |
null); |
| 115 |
// Can't round-trip passwords with lowercase hex encoding |
| 116 |
check_url("user:pass%2fword%@host", null, "user", "pass/word%", "host", null, |
| 117 |
null, false); |
| 118 |
check_url("localhost/temp-queue://" |
| 119 |
"ID:ganymede-36663-1408448359876-2:123:0", |
| 120 |
null, null, null, "localhost", null, |
| 121 |
"temp-queue://ID:ganymede-36663-1408448359876-2:123:0"); |
| 122 |
check_url("/temp-queue://ID:ganymede-36663-1408448359876-2:123:0", null, null, |
| 123 |
null, null, null, |
| 124 |
"temp-queue://ID:ganymede-36663-1408448359876-2:123:0"); |
| 125 |
check_url("amqp://localhost/temp-queue://" |
| 126 |
"ID:ganymede-36663-1408448359876-2:123:0", |
| 127 |
"amqp", null, null, "localhost", null, |
| 128 |
"temp-queue://ID:ganymede-36663-1408448359876-2:123:0"); |
| 129 |
// PROTON-995 |
| 130 |
check_url("amqps://%40user%2F%3A:%40pass%2F%[email protected]/" |
| 131 |
"some_topic", |
| 132 |
"amqps", "@user/:", "@pass/:", "example.net", null, "some_topic"); |
| 133 |
check_url("amqps://user%2F%3A=:pass%2F%[email protected]/some_topic", "amqps", |
| 134 |
"user/:=", "pass/:=", "example.net", null, "some_topic"); |
| 135 |
// Really perverse url |
| 136 |
check_url("://:@://:", "", "", "", null, "", "/:"); |
| 137 |
} |