Skip to content

Commit 0ba8520

Browse files
committed
PHON-85: Improve test
- Add certificate expiry example - Make sure we can fix the error and reconnect
1 parent a940ebb commit 0ba8520

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

tests/connect/standalone-ssl-0002.phpt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ require_once "tests/utils/basic.inc";
99
$dsn = sprintf("%s/?ssl=true", MONGODB_STANDALONE_SSL_URI);
1010

1111
$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/");
12+
function isValid(array $cert) {
13+
$from = $cert["validFrom_time_t"];
14+
$to = $cert["validTo_time_t"];
15+
16+
$current = time();
17+
18+
if ($from > $current && $current < $to) {
19+
return true;
20+
}
21+
22+
return false;
23+
}
24+
1225
$opts = array(
1326
"ssl" => array(
1427
"peer_name" => "MongoDB",
@@ -26,7 +39,7 @@ $opts = array(
2639
"capture_peer_cert_chain" => true,
2740
"SNI_enabled" => true,
2841
"disable_compression" => false,
29-
"peer_fingerprint" => "",
42+
"peer_fingerprint" => "0d6dbd95",
3043
),
3144
);
3245
$context = stream_context_create($opts);
@@ -39,17 +52,29 @@ echo throws(function() use($mc) {
3952
$batch->insert(array("my" => "value"));
4053
$retval = $mc->executeWriteBatch(NS, $batch);
4154
}, "MongoDB\\SSLConnectionException", "executeWriteBatch"), "\n";
42-
/*
55+
56+
57+
echo "Changing to server\n";
58+
stream_context_set_option($context, "ssl", "CN_match", "server");
59+
$batch = new MongoDB\WriteBatch;
60+
$batch->insert(array("my" => "value"));
61+
$retval = $mc->executeWriteBatch(NS, $batch);
62+
printf("Inserted: %d\n", $retval->getInsertedCount());
63+
64+
4365

4466
$opts = stream_context_get_params($context);
45-
var_dump($opts);
4667
$cert = openssl_x509_parse($opts["options"]["ssl"]["peer_certificate"]);
47-
var_dump($cert["name"]);
48-
*/
68+
printf("Certificate name: %s\n", $cert["name"]);
69+
printf("Certificate valid (not expired): %s\n", isValid($cert) ? "OK" : "NO");
4970
?>
5071
===DONE===
5172
<?php exit(0); ?>
5273
--EXPECTF--
5374
OK: Got MongoDB\SSLConnectionException thrown from executeWriteBatch
5475
%s
76+
Changing to server
77+
Inserted: 1
78+
Certificate name: /CN=server/OU=Kernel/O=MongoDB/L=New York City/ST=New York/C=US
79+
Certificate valid (not expired): NO
5580
===DONE===

0 commit comments

Comments
 (0)