|
| 1 | +# Copyright (c) 2021-2022, PostgreSQL Global Development Group |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +use PostgreSQL::Test::Utils; |
| 6 | +use Test::More; |
| 7 | +use IPC::Run; |
| 8 | + |
| 9 | + |
| 10 | +# List of URIs tests. For each test the first element is the input string, the |
| 11 | +# second the expected stdout and the third the expected stderr. |
| 12 | +my @tests = ( |
| 13 | + [ |
| 14 | + q{postgresql://uri-user:secret@host:12345/db}, |
| 15 | + q{user='uri-user' password='secret' dbname='db' host='host' port='12345' (inet)}, |
| 16 | + q{}, |
| 17 | + ], |
| 18 | + [ |
| 19 | + q{postgresql://uri-user@host:12345/db}, |
| 20 | + q{user='uri-user' dbname='db' host='host' port='12345' (inet)}, q{}, |
| 21 | + ], |
| 22 | + [ |
| 23 | + q{postgresql://uri-user@host/db}, |
| 24 | + q{user='uri-user' dbname='db' host='host' (inet)}, q{}, |
| 25 | + ], |
| 26 | + [ |
| 27 | + q{postgresql://host:12345/db}, |
| 28 | + q{dbname='db' host='host' port='12345' (inet)}, q{}, |
| 29 | + ], |
| 30 | + [ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ], |
| 31 | + [ |
| 32 | + q{postgresql://uri-user@host:12345/}, |
| 33 | + q{user='uri-user' host='host' port='12345' (inet)}, |
| 34 | + q{}, |
| 35 | + ], |
| 36 | + [ |
| 37 | + q{postgresql://uri-user@host/}, |
| 38 | + q{user='uri-user' host='host' (inet)}, |
| 39 | + q{}, |
| 40 | + ], |
| 41 | + [ q{postgresql://uri-user@}, q{user='uri-user' (local)}, q{}, ], |
| 42 | + [ q{postgresql://host:12345/}, q{host='host' port='12345' (inet)}, q{}, ], |
| 43 | + [ q{postgresql://host:12345}, q{host='host' port='12345' (inet)}, q{}, ], |
| 44 | + [ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ], |
| 45 | + [ q{postgresql://host/}, q{host='host' (inet)}, q{}, ], |
| 46 | + [ q{postgresql://host}, q{host='host' (inet)}, q{}, ], |
| 47 | + [ q{postgresql://}, q{(local)}, q{}, ], |
| 48 | + [ |
| 49 | + q{postgresql://?hostaddr=127.0.0.1}, q{hostaddr='127.0.0.1' (inet)}, |
| 50 | + q{}, |
| 51 | + ], |
| 52 | + [ |
| 53 | + q{postgresql://example.com?hostaddr=63.1.2.4}, |
| 54 | + q{host='example.com' hostaddr='63.1.2.4' (inet)}, |
| 55 | + q{}, |
| 56 | + ], |
| 57 | + [ q{postgresql://%68ost/}, q{host='host' (inet)}, q{}, ], |
| 58 | + [ |
| 59 | + q{postgresql://host/db?user=uri-user}, |
| 60 | + q{user='uri-user' dbname='db' host='host' (inet)}, |
| 61 | + q{}, |
| 62 | + ], |
| 63 | + [ |
| 64 | + q{postgresql://host/db?user=uri-user&port=12345}, |
| 65 | + q{user='uri-user' dbname='db' host='host' port='12345' (inet)}, |
| 66 | + q{}, |
| 67 | + ], |
| 68 | + [ |
| 69 | + q{postgresql://host/db?u%73er=someotheruser&port=12345}, |
| 70 | + q{user='someotheruser' dbname='db' host='host' port='12345' (inet)}, |
| 71 | + q{}, |
| 72 | + ], |
| 73 | + [ |
| 74 | + q{postgresql://host/db?u%7aer=someotheruser&port=12345}, q{}, |
| 75 | + q{uri-regress: invalid URI query parameter: "uzer"}, |
| 76 | + ], |
| 77 | + [ |
| 78 | + q{postgresql://host:12345?user=uri-user}, |
| 79 | + q{user='uri-user' host='host' port='12345' (inet)}, |
| 80 | + q{}, |
| 81 | + ], |
| 82 | + [ |
| 83 | + q{postgresql://host?user=uri-user}, |
| 84 | + q{user='uri-user' host='host' (inet)}, |
| 85 | + q{}, |
| 86 | + ], |
| 87 | + [ q{postgresql://host?}, q{host='host' (inet)}, q{}, ], |
| 88 | + [ |
| 89 | + q{postgresql://[::1]:12345/db}, |
| 90 | + q{dbname='db' host='::1' port='12345' (inet)}, |
| 91 | + q{}, |
| 92 | + ], |
| 93 | + [ q{postgresql://[::1]/db}, q{dbname='db' host='::1' (inet)}, q{}, ], |
| 94 | + [ |
| 95 | + q{postgresql://[2001:db8::1234]/}, q{host='2001:db8::1234' (inet)}, |
| 96 | + q{}, |
| 97 | + ], |
| 98 | + [ |
| 99 | + q{postgresql://[200z:db8::1234]/}, q{host='200z:db8::1234' (inet)}, |
| 100 | + q{}, |
| 101 | + ], |
| 102 | + [ q{postgresql://[::1]}, q{host='::1' (inet)}, q{}, ], |
| 103 | + [ q{postgres://}, q{(local)}, q{}, ], |
| 104 | + [ q{postgres:///}, q{(local)}, q{}, ], |
| 105 | + [ q{postgres:///db}, q{dbname='db' (local)}, q{}, ], |
| 106 | + [ |
| 107 | + q{postgres://uri-user@/db}, q{user='uri-user' dbname='db' (local)}, |
| 108 | + q{}, |
| 109 | + ], |
| 110 | + [ |
| 111 | + q{postgres://?host=/path/to/socket/dir}, |
| 112 | + q{host='/path/to/socket/dir' (local)}, |
| 113 | + q{}, |
| 114 | + ], |
| 115 | + [ |
| 116 | + q{postgresql://host?uzer=}, q{}, |
| 117 | + q{uri-regress: invalid URI query parameter: "uzer"}, |
| 118 | + ], |
| 119 | + [ |
| 120 | + q{postgre://}, |
| 121 | + q{}, |
| 122 | + q{uri-regress: missing "=" after "postgre://" in connection info string}, |
| 123 | + ], |
| 124 | + [ |
| 125 | + q{postgres://[::1}, |
| 126 | + q{}, |
| 127 | + q{uri-regress: end of string reached when looking for matching "]" in IPv6 host address in URI: "postgres://[::1"}, |
| 128 | + ], |
| 129 | + [ |
| 130 | + q{postgres://[]}, |
| 131 | + q{}, |
| 132 | + q{uri-regress: IPv6 host address may not be empty in URI: "postgres://[]"}, |
| 133 | + ], |
| 134 | + [ |
| 135 | + q{postgres://[::1]z}, |
| 136 | + q{}, |
| 137 | + q{uri-regress: unexpected character "z" at position 17 in URI (expected ":" or "/"): "postgres://[::1]z"}, |
| 138 | + ], |
| 139 | + [ |
| 140 | + q{postgresql://host?zzz}, |
| 141 | + q{}, |
| 142 | + q{uri-regress: missing key/value separator "=" in URI query parameter: "zzz"}, |
| 143 | + ], |
| 144 | + [ |
| 145 | + q{postgresql://host?value1&value2}, |
| 146 | + q{}, |
| 147 | + q{uri-regress: missing key/value separator "=" in URI query parameter: "value1"}, |
| 148 | + ], |
| 149 | + [ |
| 150 | + q{postgresql://host?key=key=value}, |
| 151 | + q{}, |
| 152 | + q{uri-regress: extra key/value separator "=" in URI query parameter: "key"}, |
| 153 | + ], |
| 154 | + [ |
| 155 | + q{postgres://host?dbname=%XXfoo}, q{}, |
| 156 | + q{uri-regress: invalid percent-encoded token: "%XXfoo"}, |
| 157 | + ], |
| 158 | + [ |
| 159 | + q{postgresql://a%00b}, |
| 160 | + q{}, |
| 161 | + q{uri-regress: forbidden value %00 in percent-encoded value: "a%00b"}, |
| 162 | + ], |
| 163 | + [ |
| 164 | + q{postgresql://%zz}, q{}, |
| 165 | + q{uri-regress: invalid percent-encoded token: "%zz"}, |
| 166 | + ], |
| 167 | + [ |
| 168 | + q{postgresql://%1}, q{}, |
| 169 | + q{uri-regress: invalid percent-encoded token: "%1"}, |
| 170 | + ], |
| 171 | + [ |
| 172 | + q{postgresql://%}, q{}, |
| 173 | + q{uri-regress: invalid percent-encoded token: "%"}, |
| 174 | + ], |
| 175 | + [ q{postgres://@host}, q{host='host' (inet)}, q{}, ], |
| 176 | + [ q{postgres://host:/}, q{host='host' (inet)}, q{}, ], |
| 177 | + [ q{postgres://:12345/}, q{port='12345' (local)}, q{}, ], |
| 178 | + [ |
| 179 | + q{postgres://otheruser@?host=/no/such/directory}, |
| 180 | + q{user='otheruser' host='/no/such/directory' (local)}, |
| 181 | + q{}, |
| 182 | + ], |
| 183 | + [ |
| 184 | + q{postgres://otheruser@/?host=/no/such/directory}, |
| 185 | + q{user='otheruser' host='/no/such/directory' (local)}, |
| 186 | + q{}, |
| 187 | + ], |
| 188 | + [ |
| 189 | + q{postgres://otheruser@:12345?host=/no/such/socket/path}, |
| 190 | + q{user='otheruser' host='/no/such/socket/path' port='12345' (local)}, |
| 191 | + q{}, |
| 192 | + ], |
| 193 | + [ |
| 194 | + q{postgres://otheruser@:12345/db?host=/path/to/socket}, |
| 195 | + q{user='otheruser' dbname='db' host='/path/to/socket' port='12345' (local)}, |
| 196 | + q{}, |
| 197 | + ], |
| 198 | + [ |
| 199 | + q{postgres://:12345/db?host=/path/to/socket}, |
| 200 | + q{dbname='db' host='/path/to/socket' port='12345' (local)}, |
| 201 | + q{}, |
| 202 | + ], |
| 203 | + [ |
| 204 | + q{postgres://:12345?host=/path/to/socket}, |
| 205 | + q{host='/path/to/socket' port='12345' (local)}, |
| 206 | + q{}, |
| 207 | + ], |
| 208 | + [ |
| 209 | + q{postgres://%2Fvar%2Flib%2Fpostgresql/dbname}, |
| 210 | + q{dbname='dbname' host='/var/lib/postgresql' (local)}, |
| 211 | + q{}, |
| 212 | + ]); |
| 213 | + |
| 214 | +# test to run for each of the above test definitions |
| 215 | +sub test_uri |
| 216 | +{ |
| 217 | + local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 218 | + |
| 219 | + my $uri; |
| 220 | + my %expect; |
| 221 | + my %result; |
| 222 | + |
| 223 | + ($uri, $expect{stdout}, $expect{stderr}) = @$_; |
| 224 | + |
| 225 | + $expect{'exit'} = $expect{stderr} eq ''; |
| 226 | + |
| 227 | + my $cmd = [ 'uri-regress', $uri ]; |
| 228 | + $result{exit} = IPC::Run::run $cmd, '>', \$result{stdout}, '2>', |
| 229 | + \$result{stderr}; |
| 230 | + |
| 231 | + chomp($result{stdout}); |
| 232 | + chomp($result{stderr}); |
| 233 | + |
| 234 | + # use is_deeply so there's one test result for each test above, without |
| 235 | + # loosing the information whether stdout/stderr mismatched. |
| 236 | + is_deeply(\%result, \%expect, $uri); |
| 237 | +} |
| 238 | + |
| 239 | +foreach (@tests) |
| 240 | +{ |
| 241 | + test_uri($_); |
| 242 | +} |
| 243 | + |
| 244 | +done_testing(); |
0 commit comments