Skip to content

Commit 9e776cd

Browse files
committed
PHPC-2542: Add test coverage for auth mechanism errors
1 parent 70e12b1 commit 9e776cd

4 files changed

+113
-28
lines changed
Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,36 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): authentication options are validated
2+
MongoDB\Driver\Manager::__construct(): GSSAPI authentication options are validated
33
--FILE--
44
<?php
55

66
require_once __DIR__ . '/../utils/basic.inc';
77

8+
// GSSAPI requires a username
89
echo throws(function() {
9-
create_test_manager('mongodb://localhost:27017/?authMechanism=GSSAPI&authSource=admin');
10+
create_test_manager('mongodb://localhost:27017/?authMechanism=GSSAPI');
1011
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
11-
12-
echo throws(function() {
13-
create_test_manager('mongodb://localhost:27017/', ['authMechanism' => 'GSSAPI', 'authSource' => 'admin']);
14-
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15-
16-
echo throws(function() {
17-
create_test_manager('mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin');
18-
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
19-
2012
echo throws(function() {
21-
create_test_manager('mongodb://localhost:27017/', ['authMechanism' => 'MONGODB-X509', 'authSource' => 'admin']);
13+
create_test_manager('mongodb://localhost:27017/', ['authMechanism' => 'GSSAPI']);
2214
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
2315

16+
// GSSAPI requires $external auth source
2417
echo throws(function() {
25-
create_test_manager('mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1');
18+
create_test_manager('mongodb://user@localhost:27017/?authMechanism=GSSAPI&authSource=admin');
2619
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
27-
28-
echo throws(function() {
29-
create_test_manager('mongodb://localhost:27017/', ['username' => '', 'authMechanism' => 'SCRAM-SHA-1']);
30-
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
31-
3220
echo throws(function() {
33-
create_test_manager('mongodb://localhost:27017/', ['password' => 'password', 'authMechanism' => 'MONGODB-X509']);
21+
create_test_manager('mongodb://localhost:27017/', ['authMechanism' => 'GSSAPI', 'authSource' => 'admin', 'username' => 'user']);
3422
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
3523

3624
?>
3725
===DONE===
3826
<?php exit(0); ?>
3927
--EXPECT--
4028
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
41-
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=GSSAPI&authSource=admin'. 'GSSAPI' authentication mechanism requires a username.
29+
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=GSSAPI'. 'GSSAPI' authentication mechanism requires a username.
4230
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4331
Failed to parse URI options: 'GSSAPI' authentication mechanism requires a username
4432
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
45-
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin'. 'MONGODB-X509' authentication mechanism requires "$external" authSource, but "admin" was specified.
46-
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
47-
Failed to parse URI options: 'MONGODB-X509' authentication mechanism requires "$external" authSource, but "admin" was specified
48-
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
49-
Failed to parse MongoDB URI: 'mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1'. 'SCRAM-SHA-1' authentication mechanism requires a username.
50-
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
51-
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires a username
33+
Failed to parse MongoDB URI: 'mongodb://user@localhost:27017/?authMechanism=GSSAPI&authSource=admin'. 'GSSAPI' authentication mechanism requires "$external" authSource, but "admin" was specified.
5234
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
53-
Failed to parse URI options: 'MONGODB-X509' authentication mechanism does not accept a password
35+
Failed to parse URI options: 'GSSAPI' authentication mechanism requires "$external" authSource, but "admin" was specified
5436
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): X509 authentication options are validated
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
// X509 requires $external authSource
9+
echo throws(function() {
10+
create_test_manager('mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin');
11+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
12+
echo throws(function() {
13+
create_test_manager('mongodb://localhost:27017/', ['authMechanism' => 'MONGODB-X509', 'authSource' => 'admin']);
14+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15+
16+
// X509 does not accept a password
17+
echo throws(function() {
18+
create_test_manager('mongodb://localhost:27017/', ['password' => 'password', 'authMechanism' => 'MONGODB-X509']);
19+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECT--
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin'. 'MONGODB-X509' authentication mechanism requires "$external" authSource, but "admin" was specified.
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
Failed to parse URI options: 'MONGODB-X509' authentication mechanism requires "$external" authSource, but "admin" was specified
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
Failed to parse URI options: 'MONGODB-X509' authentication mechanism does not accept a password
31+
===DONE===
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): SCRAM-SHA-1 authentication options are validated
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
// SCRAM-SHA-1 requires a username
9+
echo throws(function() {
10+
create_test_manager('mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1');
11+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
12+
echo throws(function() {
13+
create_test_manager('mongodb://localhost:27017/', ['username' => '', 'authMechanism' => 'SCRAM-SHA-1']);
14+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15+
16+
// SCRAM-SHA-1 requires password
17+
echo throws(function() {
18+
create_test_manager('mongodb://user@localhost:27017/?authMechanism=SCRAM-SHA-1');
19+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
20+
echo throws(function() {
21+
create_test_manager('mongodb://localhost:27017/', ['username' => 'user', 'authMechanism' => 'SCRAM-SHA-1']);
22+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Failed to parse MongoDB URI: 'mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1'. 'SCRAM-SHA-1' authentication mechanism requires a username.
30+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
31+
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires a username
32+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
33+
Failed to parse MongoDB URI: 'mongodb://user@localhost:27017/?authMechanism=SCRAM-SHA-1'. 'SCRAM-SHA-1' authentication mechanism requires a password.
34+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35+
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires a password
36+
===DONE===
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): MONGODB-AWS authentication options are validated
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/basic.inc';
7+
8+
// MONGODB-AWS requires a username
9+
echo throws(function() {
10+
create_test_manager('mongodb://@localhost:27017/?authMechanism=MONGODB-AWS');
11+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
12+
echo throws(function() {
13+
create_test_manager('mongodb://localhost:27017/', ['username' => '', 'authMechanism' => 'MONGODB-AWS']);
14+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15+
16+
// MONGODB-AWS requires a password
17+
echo throws(function() {
18+
create_test_manager('mongodb://user@localhost:27017/?authMechanism=MONGODB-AWS');
19+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
20+
echo throws(function() {
21+
create_test_manager('mongodb://localhost:27017/', ['username' => 'user', 'authMechanism' => 'MONGODB-AWS']);
22+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Failed to parse MongoDB URI: 'mongodb://@localhost:27017/?authMechanism=MONGODB-AWS'. 'MONGODB-AWS' authentication mechanism requires a non-empty username.
30+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
31+
Failed to parse URI options: 'MONGODB-AWS' authentication mechanism requires a non-empty username
32+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
33+
Failed to parse MongoDB URI: 'mongodb://user@localhost:27017/?authMechanism=MONGODB-AWS'. 'MONGODB-AWS' authentication mechanism does not accept a username or a password without the other.
34+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35+
Failed to parse URI options: 'MONGODB-AWS' authentication mechanism does not accept a username or a password without the other
36+
===DONE===

0 commit comments

Comments
 (0)