Skip to content

Commit c5dda25

Browse files
committed
Rewrite test for SSL with cert verification
1 parent 632e35f commit c5dda25

5 files changed

+122
-63
lines changed

tests/connect/standalone-ssl-0002.phpt

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and cert verification
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
// libmongoc does not allow the hostname to be overridden as "server"
13+
'allow_invalid_hostname' => true,
14+
'weak_cert_validation' => false,
15+
'ca_file' => $SSL_DIR . '/ca.pem',
16+
];
17+
18+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
19+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
20+
var_dump($cursor->toArray()[0]);
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECTF--
26+
object(stdClass)#%d (%d) {
27+
["ok"]=>
28+
float(1)
29+
}
30+
===DONE===
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and cert verification (context options)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
'context' => stream_context_create([
13+
'ssl' => [
14+
// libmongoc does not allow the hostname to be overridden as "server"
15+
'allow_invalid_hostname' => true,
16+
'allow_self_signed' => false, // "weak_cert_validation" alias
17+
'cafile' => $SSL_DIR . '/ca.pem', // "ca_file" alias
18+
],
19+
]),
20+
];
21+
22+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
23+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
24+
var_dump($cursor->toArray()[0]);
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECTF--
30+
object(stdClass)#%d (%d) {
31+
["ok"]=>
32+
float(1)
33+
}
34+
===DONE===
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and cert verification error
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$driverOptions = [
10+
// libmongoc does not allow the hostname to be overridden as "server"
11+
'allow_invalid_hostname' => true,
12+
'weak_cert_validation' => false,
13+
];
14+
15+
echo throws(function() use ($driverOptions) {
16+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
17+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
18+
var_dump($cursor->toArray()[0]);
19+
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException', 'executeCommand'), "\n";
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECTF--
25+
OK: Got MongoDB\Driver\Exception\ConnectionTimeoutException thrown from executeCommand
26+
No suitable servers found (`serverSelectionTryOnce` set): [%s calling ismaster on '%s:%d']
27+
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and cert verification error (context options)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$driverOptions = [
10+
'context' => stream_context_create([
11+
'ssl' => [
12+
// libmongoc does not allow the hostname to be overridden as "server"
13+
'allow_invalid_hostname' => true,
14+
'allow_self_signed' => false, // "weak_cert_validation" alias
15+
],
16+
]),
17+
];
18+
19+
echo throws(function() use ($driverOptions) {
20+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
21+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
22+
var_dump($cursor->toArray()[0]);
23+
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException', 'executeCommand'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
OK: Got MongoDB\Driver\Exception\ConnectionTimeoutException thrown from executeCommand
30+
No suitable servers found (`serverSelectionTryOnce` set): [%s calling ismaster on '%s:%d']
31+
===DONE===

0 commit comments

Comments
 (0)