1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
# Copyright 2015 - present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use Test::More;
use Test::Fatal;
my $class = "MongoDB::_URI";
require_ok($class);
subtest "basic parsing" => sub {
my $uri =
new_ok( $class, [ uri => 'mongodb://user:pass@localhost/example_db?w=1' ] );
is( $uri->username, 'user' );
is( $uri->password, 'pass' );
my @hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids );
is( $uri->db_name, 'example_db' );
is( keys( %{ $uri->options } ), 1 );
is( $uri->options->{w}, 1 );
like( exception { $class->new( uri => 'invalid' ) }, qr/could not be parsed/ );
};
subtest "host list parsing" => sub {
my ( $uri, @hostids );
$uri = new_ok( $class, [ uri => 'mongodb://localhost' ] );
@hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids, "single hostname" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost,' ] );
@hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids, "single hostname with trailing comma" );
$uri = new_ok( $class, [ uri => 'mongodb://@localhost' ] );
@hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids, "hostname with empty auth credentials" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/' ] );
@hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids, "hostname with with trailing slash" );
$uri =
new_ok( $class, [ uri => 'mongodb://example1.com:27017,example2.com:27017' ] );
@hostids = ( 'example1.com:27017', 'example2.com:27017' );
is_deeply( $uri->hostids, \@hostids, "multiple hostnames" );
$uri =
new_ok( $class, [ uri => 'mongodb://localhost,localhost:27018,localhost:27019' ] );
@hostids = ( 'localhost:27017', 'localhost:27018', 'localhost:27019' );
is_deeply( $uri->hostids, \@hostids, "multiple hostnames at localhost" );
$uri = new_ok( $class,
[ uri => 'mongodb://localhost,example1.com:27018,localhost:27019' ] );
@hostids = ( 'localhost:27017', 'example1.com:27018', 'localhost:27019' );
is_deeply( $uri->hostids, \@hostids, "multiple hostnames (localhost/domain)" );
like(
exception { $class->new( uri => 'mongodb://' ) },
qr/missing host list/,
"missing host list"
);
like(
exception { $class->new( uri => 'mongodb:///' ) },
qr/missing host list/,
"missing host list, with trailing slash"
);
like(
exception { $class->new( uri => 'mongodb:///?' ) },
qr/missing host list/,
"missing host list, with trailing slash and question mark"
);
like(
exception { $class->new( uri => 'mongodb://local?host' ) },
qr/could not be parsed/,
"host list contains unescaped question mark"
);
};
subtest "hostname normalization and validation" => sub {
my ( $uri, @hostids );
$uri =
new_ok( $class, [ uri => 'mongodb://eXaMpLe1.cOm:27017,eXAMPLe2.com:27017' ] );
@hostids = ( 'example1.com:27017', 'example2.com:27017' );
is_deeply( $uri->hostids, \@hostids, "hostname normalized for case" );
$uri = new_ok( $class, [ uri => 'mongodb://local%68ost' ] );
@hostids = ('localhost:27017');
is_deeply( $uri->hostids, \@hostids, "hostname url decoded" );
$uri = new_ok( $class, [ uri => 'mongodb://hostwithembeddedc%6fmma' ] );
@hostids = ("hostwithembeddedcomma:27017");
is_deeply( $uri->hostids, \@hostids, "hostname can contain embedded comma" );
like(
exception { $class->new( uri => 'mongodb://:27017' ) },
qr/contains empty host/,
"empty hostname"
);
like(
exception { $class->new( uri => 'mongodb://@:27017' ) },
qr/contains empty host/,
"empty hostname, with leading at sign"
);
like(
exception { $class->new( uri => 'mongodb://%2Fa.sock' ) },
qr/Unix domain sockets are not supported/,
"unix domain socket (unsupported) with absolute path"
);
like(
exception { $class->new( uri => 'mongodb://a%2Fb.sock' ) },
qr/Unix domain sockets are not supported/,
"unix domain socket (unsupported) with relative path"
);
like(
exception { $class->new( uri => 'mongodb://a%2fb.sock' ) },
qr/Unix domain sockets are not supported/,
"unix domain socket (unsupported) with alternate URL encoding"
);
like(
exception { $class->new( uri => 'mongodb://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]' ) },
qr/IP literals are not supported/,
"ip literal (unsupported)"
);
like(
exception { $class->new( uri => 'mongodb://[::1]' ) },
qr/IP literals are not supported/,
"ip literal (unsupported), short localhost"
);
like(
exception { $class->new( uri => 'mongodb://[::1]:27017' ) },
qr/IP literals are not supported/,
"ip literal (unsupported) with port"
);
like(
exception { $class->new( uri => 'mongodb://[::1]:' ) },
qr/IP literals are not supported/,
"ip literal (unsupported) with empty port"
);
like(
exception { $class->new( uri => 'mongodb://localhost:/' ) },
qr/invalid port/,
"hostname with empty port"
);
like(
exception { $class->new( uri => 'mongodb://example.com:http' ) },
qr/invalid port/,
"non-numeric port"
);
like(
exception { $class->new( uri => 'mongodb://example.com:-1' ) },
qr/invalid port/,
"negative port"
);
like(
exception { $class->new( uri => 'mongodb://example.com:0' ) },
qr/invalid port.*must be in range/,
"port of 0"
);
like(
exception { $class->new( uri => 'mongodb://example.com:65536' ) },
qr/invalid port.*must be in range/,
"port of 65536"
);
};
subtest "db_name" => sub {
my $uri = new_ok( $class, [ uri => 'mongodb://localhost/example_db' ] );
is( $uri->db_name, 'example_db', "parse db_name" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost,/example_db' ] );
is( $uri->db_name, 'example_db', "parse db_name with trailing comma on host" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/example_db?' ] );
is( $uri->db_name, 'example_db', "parse db_name with trailing question mark" );
$uri = new_ok( $class,
[ uri => 'mongodb://localhost,localhost:27020,localhost:27021/example_db' ] );
is( $uri->db_name, 'example_db', "parse db_name, many hosts" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost' ] );
is( $uri->db_name, '', "no db_name with trailing ?" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/' ] );
is( $uri->db_name, '', "no db_name with trailing /" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?' ] );
is( $uri->db_name, '', "no db_name with trailing /?" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/a%62c' ] );
is( $uri->db_name, 'abc', "db_name properly unescaped" );
like(
exception { $class->new( uri => 'mongodb://localhost//' ) },
qr/database name must be URL encoded, found unescaped '\/'/,
"database with unescaped slash"
);
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?authSource=foo' ] );
is( $uri->db_name, 'foo', "parse db_name from authSource option" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/example_db?authSource=foo' ] );
is( $uri->db_name, 'foo', "parse db_name authSource override URI db_name" );
};
subtest "auth credentials" => sub {
my $uri;
$uri = new_ok( $class, [ uri => 'mongodb://fred:foobar@localhost' ] );
is( $uri->username, 'fred', "basic username parsing" );
is( $uri->password, 'foobar', "basic password parsing" );
$uri = new_ok( $class, [ uri => 'mongodb://fred@localhost' ] );
is( $uri->username, 'fred', "username when no password present" );
is( $uri->password, undef, "undefined password, when password not given" );
$uri = new_ok( $class, [ uri => 'mongodb://:@localhost' ] );
is( $uri->username, '', "empty username" );
is( $uri->password, '', "empty password" );
$uri = new_ok( $class, [ uri => 'mongodb://@localhost' ] );
is( $uri->username, '', "empty username, when password not given" );
is( $uri->password, undef,
"undefined password, when username empty and password not given" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost' ] );
is( $uri->username, undef, "undefined username, when no credentials given" );
is( $uri->password, undef, "undefined password, when no credentials given" );
$uri = new_ok( $class, [ uri => 'mongodb://dog%3Adogston:p%40ssword@localhost' ] );
is( $uri->username, 'dog:dogston', "percent encoded username" );
is( $uri->password, 'p@ssword', "percent encoded password" );
like(
exception { $class->new( uri => 'mongodb://user@name:password@localhost' ) },
qr/username must be URL encoded/,
'username with unescaped at sign'
);
like(
exception { $class->new( uri => 'mongodb://username:pass:word@localhost' ) },
qr/password must be URL encoded/,
"password with unescaped colon"
);
};
subtest "options" => sub {
my ( $uri, @warnings, %expected_options );
$uri = new_ok( $class, [ uri => 'mongodb://localhost' ] );
%expected_options = ();
is_deeply( $uri->options, \%expected_options, "no options" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?' ] );
%expected_options = ();
is_deeply( $uri->options, \%expected_options, "no options with trailing question mark" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?&' ] );
%expected_options = ();
is_deeply( $uri->options, \%expected_options, "no options with trailing question mark and ampersand" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?w=1' ] );
%expected_options = (w => 1);
is_deeply( $uri->options, \%expected_options, "single option" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?w=1&replicaSet=set' ] );
%expected_options = (w => 1, replicaset => 'set');
is_deeply( $uri->options, \%expected_options, "multiple options" );
$uri = new_ok( $class,
[ uri => 'mongodb://localhost/?ReAdPrEfErEnCe=Primary&wTimeoutMS=1000' ] );
%expected_options = (readpreference => 'Primary', wtimeoutms => 1000);
is_deeply( $uri->options, \%expected_options, "multiple options with mixed case" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?%77=%74rue' ] );
%expected_options = (w => 'true');
is_deeply( $uri->options, \%expected_options, "percent encoded option" );
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?w=' ] );
%expected_options = (w => '');
is_deeply( $uri->options, \%expected_options, "options with empty option value" );
like(
exception { $class->new( uri => 'mongodb://localhost/?w' ) },
qr/expected key value pair/,
"no equals sign in option"
);
like(
exception { $class->new( uri => 'mongodb://localhost/?w==true' ) },
qr/expected key value pair/,
"extra equals signs at start of option value"
);
like(
exception { $class->new( uri => 'mongodb://localhost/?w=true=' ) },
qr/expected key value pair/,
"extra equals signs at end of option value"
);
like(
exception { $class->new( uri => 'mongodb://localhost/?w==true=' ) },
qr/expected key value pair/,
"equals signs at beginning and end of option value"
);
like(
exception { $class->new( uri => 'mongodb://localhost/?w==' ) },
qr/expected key value pair/,
"option value of equals sign"
);
@warnings = ();
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?unknown1=a&w=1&unknown2=b' ] );
}
is( scalar(@warnings), 2, "warnings for unknown options" );
is(
1,
scalar( grep /Unsupported option 'unknown1'/, @warnings ),
"warning for 'unknown1' option"
);
is(
1,
scalar( grep /Unsupported option 'unknown2'/, @warnings ),
"warning for 'unknown2' option"
);
%expected_options = (w => 1);
is_deeply( $uri->options, \%expected_options, "options with warnings" );
@warnings = ();
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$uri = new_ok( $class, [ uri => 'mongodb://localhost/?w=1&w=2&replicaSet=set' ] );
}
is( scalar(@warnings), 1, "warnings for duplicate options" );
is(
1,
scalar( grep /Multiple options were found for the same value 'w'/, @warnings ),
"warning for duplicate option 'w'"
);
%expected_options = (w => 1, replicaset => 'set');
is_deeply( $uri->options, \%expected_options, "options with duplicate values" );
@warnings = ();
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$uri = new_ok( $class,
[ uri => 'mongodb://localhost/?readPreferenceTags=a:b&readPreferenceTags=c:d' ] );
}
is( scalar(@warnings), 0, "no warning for duplicate option of list type" );
%expected_options = (readpreferencetags => [{a => 'b'}, {c => 'd'}]);
is_deeply( $uri->options, \%expected_options, "option of list type with duplicate values" );
};
done_testing;
|