Skip to content

Commit ce328f1

Browse files
committed
PHPC-887: Throw for unexpected types in URI options array
Note: generic boolean options (e.g. "ssl") will continue to accept non-boolean types for BC, since we cast via bson_iter_as_bool(). However, RP and WC-specific boolean options (e.g. "slaveOk", "journal") will throw since they previously silently ignored non-boolean values. In addition to revising read preference tests, this adds new tests for read concern and write concern options.
1 parent c331014 commit ce328f1

21 files changed

+1063
-193
lines changed

php_phongo.c

Lines changed: 199 additions & 74 deletions
Large diffs are not rendered by default.

tests/manager/manager-ctor-004.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): Deprecated boolean options in URI string
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
// Deprecated URI string values for true
8+
'1',
9+
'yes',
10+
'y',
11+
't',
12+
// Deprecated URI string values for false
13+
'0',
14+
'-1',
15+
'no',
16+
'n',
17+
'f',
18+
];
19+
20+
foreach ($tests as $test) {
21+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?journal=' . $test);
22+
var_dump($manager->getWriteConcern()->getJournal());
23+
}
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
bool(true)
30+
bool(true)
31+
bool(true)
32+
bool(true)
33+
bool(false)
34+
bool(false)
35+
bool(false)
36+
bool(false)
37+
bool(false)
38+
===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(): read concern options
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
['mongodb://127.0.0.1/?readConcernLevel=local', []],
8+
// libmongoc does not discriminate numeric strings for expected string types
9+
['mongodb://127.0.0.1/?readConcernLevel=1', []],
10+
[null, ['readConcernLevel' => 'local']],
11+
];
12+
13+
foreach ($tests as $test) {
14+
list($uri, $options) = $test;
15+
16+
$manager = new MongoDB\Driver\Manager($uri, $options);
17+
var_dump($manager->getReadConcern());
18+
}
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
25+
["level"]=>
26+
string(5) "local"
27+
}
28+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
29+
["level"]=>
30+
string(1) "1"
31+
}
32+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
33+
["level"]=>
34+
string(5) "local"
35+
}
36+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): invalid read concern
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
new MongoDB\Driver\Manager(null, ['readConcernLevel' => 1]);
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECTF--
16+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
17+
Expected string for "readConcernLevel" URI option, 32-bit integer given
18+
===DONE===

tests/manager/manager-ctor-read_preference-003.phpt

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/manager/manager-ctor-read_preference-004.phpt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ $tests = [
2727
['mongodb://127.0.0.1/?SLAVEOK=TRUE', []],
2828
[null, ['slaveOk' => true]],
2929
[null, ['SLAVEOK' => true]],
30-
// Strict type checking on options array (non-booleans are ignored)
31-
[null, ['slaveok' => 'true']],
32-
[null, ['slaveok' => 1]],
3330
];
3431

3532
foreach ($tests as $test) {
@@ -114,12 +111,4 @@ object(MongoDB\Driver\ReadPreference)#%d (%d) {
114111
["mode"]=>
115112
string(18) "secondaryPreferred"
116113
}
117-
object(MongoDB\Driver\ReadPreference)#%d (%d) {
118-
["mode"]=>
119-
string(7) "primary"
120-
}
121-
object(MongoDB\Driver\ReadPreference)#%d (%d) {
122-
["mode"]=>
123-
string(7) "primary"
124-
}
125114
===DONE===

tests/manager/manager-ctor-read_preference-error-001.phpt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): invalid read preference
2+
MongoDB\Driver\Manager::__construct(): invalid read preference (mode and tags)
33
--FILE--
44
<?php
55

66
require_once __DIR__ . '/../utils/tools.php';
77

8+
// Invalid types in URI string
9+
10+
echo throws(function() {
11+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=1');
12+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
13+
14+
echo throws(function() {
15+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=invalid');
16+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
17+
18+
// Invalid types in URI options array
19+
820
echo throws(function() {
9-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary&readPreferenceTags=dc:ny');
21+
new MongoDB\Driver\Manager(null, ['readPreference' => 1]);
1022
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1123

1224
echo throws(function() {
13-
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'nothing']);
25+
new MongoDB\Driver\Manager(null, ['readPreference' => 'primary', 'readPreferenceTags' => 'invalid']);
1426
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1527

28+
// Invalid values
29+
1630
echo throws(function() {
17-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary', ['readPreferenceTags' => [[]]]);
31+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary&readPreferenceTags=dc:ny');
1832
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1933

2034
echo throws(function() {
21-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary', ['readPreferenceTags' => ['invalid']]);
35+
new MongoDB\Driver\Manager(null, ['readPreference' => 'nothing']);
36+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
37+
38+
echo throws(function() {
39+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary', ['readPreferenceTags' => [[]]]);
40+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
41+
42+
echo throws(function() {
43+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=primary', ['readPreferenceTags' => ['invalid']]);
2244
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
2345

2446
?>
2547
===DONE===
2648
<?php exit(0); ?>
2749
--EXPECT--
2850
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
51+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=1'. Unsupported readPreference value [readPreference=1]..
52+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
53+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=invalid'. Unknown option or value for 'readPreferenceTags=invalid'.
54+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
55+
Expected string for "readPreference" URI option, 32-bit integer given
56+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
57+
Expected array for "readPreferenceTags" URI option, string given
58+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2959
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=primary&readPreferenceTags=dc:ny'. Invalid readPreferences.
3060
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3161
Unsupported readPreference value: 'nothing'

tests/manager/manager-ctor-read_preference-error-002.phpt

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,67 @@ MongoDB\Driver\Manager::__construct(): invalid read preference (maxStalenessSeco
55

66
require_once __DIR__ . '/../utils/tools.php';
77

8+
// Invalid types
9+
10+
echo throws(function() {
11+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=invalid');
12+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
13+
814
echo throws(function() {
9-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxstalenessseconds=1231');
15+
new MongoDB\Driver\Manager(null, ['maxStalenessSeconds' => 'invalid']);
1016
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1117

18+
// Invalid range in URI string (array option is tested in 64-bit error test)
19+
1220
echo throws(function() {
13-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxStalenessSeconds=1231');
21+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=2147483648');
1422
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1523

24+
// Invalid values
25+
1626
echo throws(function() {
17-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=2147483648');
27+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxstalenessseconds=1231');
1828
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1929

2030
echo throws(function() {
21-
$manager = new MongoDB\Driver\Manager(null, ['maxstalenessseconds' => 1231]);
31+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxStalenessSeconds=1231');
2232
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
2333

2434
echo throws(function() {
25-
$manager = new MongoDB\Driver\Manager(null, ['maxStalenessSeconds' => 1231]);
35+
new MongoDB\Driver\Manager(null, ['maxstalenessseconds' => 1231]);
2636
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
2737

2838
echo throws(function() {
29-
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => -2]);
39+
new MongoDB\Driver\Manager(null, ['maxStalenessSeconds' => 1231]);
3040
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
3141

3242
echo throws(function() {
33-
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 0]);
43+
new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => -2]);
3444
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
3545

3646
echo throws(function() {
37-
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 42]);
47+
new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 0]);
48+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
49+
50+
echo throws(function() {
51+
new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 42]);
3852
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
3953

4054
?>
4155
===DONE===
4256
<?php exit(0); ?>
43-
--EXPECTF--
57+
--EXPECT--
4458
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
45-
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxstalenessseconds=1231'. Invalid readPreferences.
59+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=invalid'. Unknown option or value for 'maxStalenessSeconds=invalid'.
4660
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
47-
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxStalenessSeconds=1231'. Invalid readPreferences.
61+
Expected integer for "maxStalenessSeconds" URI option, string given
4862
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4963
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=2147483648'. Unknown option or value for 'maxStalenessSeconds=2147483648'.
5064
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
65+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxstalenessseconds=1231'. Invalid readPreferences.
66+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
67+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxStalenessSeconds=1231'. Invalid readPreferences.
68+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
5169
Primary read preference mode conflicts with maxStalenessSeconds
5270
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
5371
Primary read preference mode conflicts with maxStalenessSeconds

tests/manager/manager-ctor-read_preference-error-003.phpt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ MongoDB\Driver\Manager::__construct(): invalid read preference (slaveOk)
66
require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
9-
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?slaveok=other');
9+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/?slaveok=other');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
11+
12+
echo throws(function() {
13+
new MongoDB\Driver\Manager(null, ['slaveOk' => 1]);
1014
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1115

1216
?>
1317
===DONE===
1418
<?php exit(0); ?>
15-
--EXPECTF--
19+
--EXPECT--
1620
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1721
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?slaveok=other'. Unknown option or value for 'slaveok=other'.
22+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23+
Expected boolean for "slaveOk" URI option, 32-bit integer given
1824
===DONE===

tests/manager/manager-ctor-read_preference-error-004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ MongoDB\Driver\Manager::__construct(): invalid read preference (maxStalenessSeco
88
require_once __DIR__ . '/../utils/tools.php';
99

1010
echo throws(function() {
11-
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 2147483648]);
11+
new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 2147483648]);
1212
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1313

1414
?>
1515
===DONE===
1616
<?php exit(0); ?>
17-
--EXPECTF--
17+
--EXPECT--
1818
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1919
Expected maxStalenessSeconds to be <= 2147483647, 2147483648 given
2020
===DONE===

0 commit comments

Comments
 (0)