Skip to content

Commit 0160871

Browse files
committed
PHPC-752: Validate range of maxStalenessMS Manager option
This also adds various tests for parsing of read preference Manager options.
1 parent eeb7c04 commit 0160871

6 files changed

+185
-47
lines changed

php_phongo.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,14 +1201,26 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
12011201
/* Handle maxStalenessMS, and make sure it is not combined with primary
12021202
* readPreference */
12031203
if (bson_iter_init_find_case(&iter, options, "maxstalenessms") && BSON_ITER_HOLDS_INT32(&iter)) {
1204+
int32_t max_staleness_ms = bson_iter_int32(&iter);
1205+
1206+
if (max_staleness_ms < 0) {
1207+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessMS to be >= 0, %" PRId32 " given", max_staleness_ms);
1208+
mongoc_read_prefs_destroy(new_rp);
1209+
1210+
return false;
1211+
}
1212+
1213+
/* max_staleness_ms is fetched as an INT32, so there is no need to check
1214+
* if it exists INT32_MAX as we do in the ReadPreference constructor. */
1215+
12041216
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
12051217
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessMS");
12061218
mongoc_read_prefs_destroy(new_rp);
12071219

12081220
return false;
12091221
}
12101222

1211-
mongoc_read_prefs_set_max_staleness_ms(new_rp, bson_iter_int32(&iter));
1223+
mongoc_read_prefs_set_max_staleness_ms(new_rp, max_staleness_ms);
12121224
}
12131225

12141226
/* This may be redundant in light of the last check (primary with tags), but
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): read preference options
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
['mongodb://127.0.0.1/?readPreference=primary', []],
8+
['mongodb://127.0.0.1/?readPreference=secondary&readPreferenceTags=tag:one&readPreferenceTags=', []],
9+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', []],
10+
[null, ['readPreference' => 'primary']],
11+
[null, ['readPreference' => 'secondary', 'readPreferenceTags' => [['tag' => 'one'], []]]],
12+
[null, ['readPreference' => 'secondary', 'maxStalenessMS' => 1000]],
13+
];
14+
15+
foreach ($tests as $test) {
16+
list($uri, $options) = $test;
17+
18+
$manager = new MongoDB\Driver\Manager($uri, $options);
19+
var_dump($manager->getReadPreference());
20+
}
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECTF--
26+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
27+
["mode"]=>
28+
string(7) "primary"
29+
}
30+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
31+
["mode"]=>
32+
string(9) "secondary"
33+
["tags"]=>
34+
array(2) {
35+
[0]=>
36+
array(1) {
37+
["tag"]=>
38+
string(3) "one"
39+
}
40+
[1]=>
41+
array(0) {
42+
}
43+
}
44+
}
45+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
46+
["mode"]=>
47+
string(9) "secondary"
48+
["maxStalenessMS"]=>
49+
int(1000)
50+
}
51+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
52+
["mode"]=>
53+
string(7) "primary"
54+
}
55+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
56+
["mode"]=>
57+
string(9) "secondary"
58+
["tags"]=>
59+
array(2) {
60+
[0]=>
61+
array(1) {
62+
["tag"]=>
63+
string(3) "one"
64+
}
65+
[1]=>
66+
array(0) {
67+
}
68+
}
69+
}
70+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
71+
["mode"]=>
72+
string(9) "secondary"
73+
["maxStalenessMS"]=>
74+
int(1000)
75+
}
76+
===DONE===
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): read preference options (maxStalenessMS)
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1231', []],
8+
['mongodb://127.0.0.1/?readPreference=secondary', ['maxStalenessMS' => 1231]],
9+
['mongodb://127.0.0.1/?readPreference=secondary&maxStalenessMS=1000', ['maxStalenessMS' => 2000]],
10+
['mongodb://127.0.0.1/?readpreference=secondary&maxstalenessms=1231', []],
11+
['mongodb://127.0.0.1/?readpreference=secondary', ['maxstalenessms' => 1231]],
12+
];
13+
14+
foreach ($tests as $test) {
15+
list($uri, $options) = $test;
16+
17+
$manager = new MongoDB\Driver\Manager($uri, $options);
18+
var_dump($manager->getReadPreference());
19+
}
20+
21+
?>
22+
===DONE===
23+
--EXPECTF--
24+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
25+
["mode"]=>
26+
string(9) "secondary"
27+
["maxStalenessMS"]=>
28+
int(1231)
29+
}
30+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
31+
["mode"]=>
32+
string(9) "secondary"
33+
["maxStalenessMS"]=>
34+
int(1231)
35+
}
36+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
37+
["mode"]=>
38+
string(9) "secondary"
39+
["maxStalenessMS"]=>
40+
int(2000)
41+
}
42+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
43+
["mode"]=>
44+
string(9) "secondary"
45+
["maxStalenessMS"]=>
46+
int(1231)
47+
}
48+
object(MongoDB\Driver\ReadPreference)#%d (%d) {
49+
["mode"]=>
50+
string(9) "secondary"
51+
["maxStalenessMS"]=>
52+
int(1231)
53+
}
54+
===DONE===
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): invalid read preference (maxStalenessMS)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxstalenessms=1231');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
11+
12+
echo throws(function() {
13+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxStalenessMS=1231');
14+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15+
16+
echo throws(function() {
17+
$manager = new MongoDB\Driver\Manager(null, ['maxstalenessms' => 1231]);
18+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
19+
20+
echo throws(function() {
21+
$manager = new MongoDB\Driver\Manager(null, ['maxStalenessMS' => 1231]);
22+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
23+
24+
echo throws(function() {
25+
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessMS' => -1]);
26+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
27+
28+
?>
29+
===DONE===
30+
<?php exit(0); ?>
31+
--EXPECTF--
32+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
33+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxstalenessms=1231'
34+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxStalenessMS=1231'
36+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37+
Primary read preference mode conflicts with maxStalenessMS
38+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
39+
Primary read preference mode conflicts with maxStalenessMS
40+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
41+
Expected maxStalenessMS to be >= 0, -1 given
42+
===DONE===

tests/manager/manager-maxstalenessms-001.phpt

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

tests/manager/manager-maxstalenessms_error-001.phpt

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

0 commit comments

Comments
 (0)