| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.cxx . . $ `<file>/tests/test_parse_url.c`/ |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | #include "config.h"cxx . . $ `<file>/build/librabbitmq/config.h`/ |
| 5 | |
| 6 | #ifdef _MSC_VER |
| 7 | /* MSVC complains about strdup being deprecated in favor of _strdup */ |
| 8 | #define _CRT_NONSTDC_NO_DEPRECATE |
| 9 | #endif |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <inttypes.h> |
| 16 | |
| 17 | #include <rabbitmq-c/amqp.h>cxx . . $ `<file>/include/rabbitmq-c/amqp.h`/ |
| 18 | |
| 19 | static void match_stringcxx . . $ match_string(facfdab5246d4be7).(const char *what, const char *expect, |
| 20 | const char *got) { |
| 21 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(got, expect)) { |
| 22 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Expected %s '%s', got '%s'\n", what, expect, got); |
| 23 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | static void match_intcxx . . $ match_int(7b85637613228108).(const char *what, int expect, int got) { |
| 28 | if (got != expect) { |
| 29 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Expected %s '%d', got '%d'\n", what, expect, got); |
| 30 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static void parse_successcxx . . $ parse_success(61359c13d5c0c87b).(const char *url, const char *user, |
| 35 | const char *password, const char *host, int port, |
| 36 | const char *vhost) { |
| 37 | char *s = strdupcxx . . $ strdup(a8b8e32347182212).(url); |
| 38 | struct amqp_connection_infocxx . . $ amqp_connection_info# ci; |
| 39 | int res; |
| 40 | |
| 41 | res = amqp_parse_urlcxx . . $ amqp_parse_url(72f41d20fb31aed6).(s, &ci); |
| 42 | if (res) { |
| 43 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Expected to successfully parse URL, but didn't: %s (%s)\n", |
| 44 | url, amqp_error_string2cxx . . $ amqp_error_string2(f7ae7e56280f18ae).(res)); |
| 45 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 46 | } |
| 47 | |
| 48 | match_stringcxx . . $ match_string(facfdab5246d4be7).("user", user, ci.usercxx . . $ amqp_connection_info#user.); |
| 49 | match_stringcxx . . $ match_string(facfdab5246d4be7).("password", password, ci.passwordcxx . . $ amqp_connection_info#password.); |
| 50 | match_stringcxx . . $ match_string(facfdab5246d4be7).("host", host, ci.hostcxx . . $ amqp_connection_info#host.); |
| 51 | match_intcxx . . $ match_int(7b85637613228108).("port", port, ci.portcxx . . $ amqp_connection_info#port.); |
| 52 | match_stringcxx . . $ match_string(facfdab5246d4be7).("vhost", vhost, ci.vhostcxx . . $ amqp_connection_info#vhost.); |
| 53 | |
| 54 | freecxx . . $ free(5d22bdacc48458e8).(s); |
| 55 | } |
| 56 | |
| 57 | static void parse_failcxx . . $ parse_fail(85c52e162fed56f9).(const char *url) { |
| 58 | char *s = strdupcxx . . $ strdup(a8b8e32347182212).(url); |
| 59 | struct amqp_connection_infocxx . . $ amqp_connection_info# ci; |
| 60 | |
| 61 | amqp_default_connection_infocxx . . $ amqp_default_connection_info(93849f2ee9951fc6).(&ci); |
| 62 | if (amqp_parse_urlcxx . . $ amqp_parse_url(72f41d20fb31aed6).(s, &ci) >= 0) { |
| 63 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Expected to fail parsing URL, but didn't: %s\n", url); |
| 64 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 65 | } |
| 66 | |
| 67 | freecxx . . $ free(5d22bdacc48458e8).(s); |
| 68 | } |
| 69 | |
| 70 | int maincxx . . $ main(b126dc7c1de90089).(void) { |
| 71 | /* From the spec */ |
| 72 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@host:10000/vhost", "user", "pass", "host", |
| 73 | 10000, "vhost"); |
| 74 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@host:10000/vhost", "user", "pass", "host", |
| 75 | 10000, "vhost"); |
| 76 | |
| 77 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user%61:%61pass@ho%61st:10000/v%2fhost", "usera", |
| 78 | "apass", "hoast", 10000, "v/host"); |
| 79 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user%61:%61pass@ho%61st:10000/v%2fhost", "usera", |
| 80 | "apass", "hoast", 10000, "v/host"); |
| 81 | |
| 82 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://", "guest", "guest", "localhost", 5672, "/"); |
| 83 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://", "guest", "guest", "localhost", 5671, "/"); |
| 84 | |
| 85 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://:@/", "", "", "localhost", 5672, ""); |
| 86 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://:@/", "", "", "localhost", 5671, ""); |
| 87 | |
| 88 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user@", "user", "guest", "localhost", 5672, "/"); |
| 89 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user@", "user", "guest", "localhost", 5671, "/"); |
| 90 | |
| 91 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@", "user", "pass", "localhost", 5672, "/"); |
| 92 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@", "user", "pass", "localhost", 5671, "/"); |
| 93 | |
| 94 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host", "guest", "guest", "host", 5672, "/"); |
| 95 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host", "guest", "guest", "host", 5671, "/"); |
| 96 | |
| 97 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://:10000", "guest", "guest", "localhost", 10000, "/"); |
| 98 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://:10000", "guest", "guest", "localhost", 10000, "/"); |
| 99 | |
| 100 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp:///vhost", "guest", "guest", "localhost", 5672, "vhost"); |
| 101 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps:///vhost", "guest", "guest", "localhost", 5671, "vhost"); |
| 102 | |
| 103 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host/", "guest", "guest", "host", 5672, ""); |
| 104 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host/", "guest", "guest", "host", 5671, ""); |
| 105 | |
| 106 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host/%2f", "guest", "guest", "host", 5672, "/"); |
| 107 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host/%2f", "guest", "guest", "host", 5671, "/"); |
| 108 | |
| 109 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://[::1]", "guest", "guest", "::1", 5672, "/"); |
| 110 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://[::1]", "guest", "guest", "::1", 5671, "/"); |
| 111 | |
| 112 | /* Various other success cases */ |
| 113 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host:100", "guest", "guest", "host", 100, "/"); |
| 114 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host:100", "guest", "guest", "host", 100, "/"); |
| 115 | |
| 116 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://[::1]:100", "guest", "guest", "::1", 100, "/"); |
| 117 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://[::1]:100", "guest", "guest", "::1", 100, "/"); |
| 118 | |
| 119 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host/blah", "guest", "guest", "host", 5672, "blah"); |
| 120 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host/blah", "guest", "guest", "host", 5671, "blah"); |
| 121 | |
| 122 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://host:100/blah", "guest", "guest", "host", 100, "blah"); |
| 123 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://host:100/blah", "guest", "guest", "host", 100, "blah"); |
| 124 | |
| 125 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://:100/blah", "guest", "guest", "localhost", 100, "blah"); |
| 126 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://:100/blah", "guest", "guest", "localhost", 100, |
| 127 | "blah"); |
| 128 | |
| 129 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://[::1]/blah", "guest", "guest", "::1", 5672, "blah"); |
| 130 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://[::1]/blah", "guest", "guest", "::1", 5671, "blah"); |
| 131 | |
| 132 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://[::1]:100/blah", "guest", "guest", "::1", 100, "blah"); |
| 133 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://[::1]:100/blah", "guest", "guest", "::1", 100, "blah"); |
| 134 | |
| 135 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@host", "user", "pass", "host", 5672, "/"); |
| 136 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@host", "user", "pass", "host", 5671, "/"); |
| 137 | |
| 138 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@host:100", "user", "pass", "host", 100, "/"); |
| 139 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@host:100", "user", "pass", "host", 100, "/"); |
| 140 | |
| 141 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@:100", "user", "pass", "localhost", 100, "/"); |
| 142 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@:100", "user", "pass", "localhost", 100, |
| 143 | "/"); |
| 144 | |
| 145 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@[::1]", "user", "pass", "::1", 5672, "/"); |
| 146 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@[::1]", "user", "pass", "::1", 5671, "/"); |
| 147 | |
| 148 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqp://user:pass@[::1]:100", "user", "pass", "::1", 100, "/"); |
| 149 | parse_successcxx . . $ parse_success(61359c13d5c0c87b).("amqps://user:pass@[::1]:100", "user", "pass", "::1", 100, "/"); |
| 150 | |
| 151 | /* Various failure cases */ |
| 152 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("http://www.rabbitmq.com"); |
| 153 | |
| 154 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo:bar:baz"); |
| 155 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo:bar:baz"); |
| 156 | |
| 157 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo[::1]"); |
| 158 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo[::1]"); |
| 159 | |
| 160 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo[::1]"); |
| 161 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo[::1]"); |
| 162 | |
| 163 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo:[::1]"); |
| 164 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo:[::1]"); |
| 165 | |
| 166 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://[::1]foo"); |
| 167 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://[::1]foo"); |
| 168 | |
| 169 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo:1000xyz"); |
| 170 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo:1000xyz"); |
| 171 | |
| 172 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo:1000000"); |
| 173 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo:1000000"); |
| 174 | |
| 175 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo/bar/baz"); |
| 176 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo/bar/baz"); |
| 177 | |
| 178 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo%1"); |
| 179 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo%1"); |
| 180 | |
| 181 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo%1x"); |
| 182 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo%1x"); |
| 183 | |
| 184 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqp://foo%xy"); |
| 185 | parse_failcxx . . $ parse_fail(85c52e162fed56f9).("amqps://foo%xy"); |
| 186 | |
| 187 | return 0; |
| 188 | } |