Core\Test::tlsSession
Core\Test::tlsSession({…}): Core\Http\TlsInfoCore\Test::tlsSession( {…}, ): Core\Http\TlsInfo
Core\Test::tlsSession() builds a TLS session for a test. TLS is the encryption that an https:// connection uses. The result is a Core\Http\TlsInfo, the same object that Core\Http\Response::tls() returns for a real connection. It has a real certificate chain with two certificates: the server's certificate, and the certificate that signed it.
Every option has a default, so tlsSession() alone gives a TLS 1.3 session for CN=example.com whose certificate was checked. You can choose the version, the cipher, whether the certificate was checked, the names in the certificates and the expiry date. A value that a real certificate cannot have throws a LogicError.
You can call tlsSession in a test and in a command-line program. In server code, it throws a LogicError. Server code is a request, a connection, a scheduled task or a queued job. There is one exception: a request that a test sends with Core\Test::request may call it.
The examples below show the default session, a fake reply that reports a session, and a test of a client that accepts only TLS 1.3.
Options
versionstringoptionalTLSv1.3orTLSv1.2. The default isTLSv1.3.cipherstringoptionalThe cipher suite: the set of encryption methods the session uses. Write its standard name, and use one this build supports for that version. The default isTLS_AES_128_GCM_SHA256for TLS 1.3 andTLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256for TLS 1.2.verifiedbooloptionalWhatisVerified()returns. The default istrue.subjectstringoptionalThe subject of the server's own certificate, which is called the leaf certificate. The subject names who the certificate is for. Write it assubject()returns it:KEY=valuepairs such asCN=api.example.com, O=Shop, with the keysCN,O,OU,C,STandL. The default isCN=example.com.issuerstringoptionalThe subject of the certificate authority (CA) that signed the leaf certificate.issuer()returns it. Write it likesubject. The default isCN=Novis Test CA.expiryCore\Time\InstantoptionalThe date and time when the leaf certificate expires. It is kept in whole seconds. The default is 90 days afterCore\Time::now(). A test that sets its clock to a fixed time gets the same default every run.
Return value
Returns Core\Http\TlsInfo. A Core\Http\TlsInfo. Its peerChain() returns two certificates: the leaf certificate, then the CA certificate that signed it. Pass the result to Core\Test::answerHttp as tls, and the fake reply reports this session.
Errors
LogicErrorThe version is notTLSv1.3orTLSv1.2. Or the cipher is not one this build supports for that version. Or a subject or issuer is not written asKEY=valuepairs with the keys above. Or the expiry is before 1950 or after 9999. Or the call runs in a server, such as in a request, a connection, a scheduled task or a queued job. There is one exception: a request that a test sends withCore\Test::requestmay call it. Tests and command-line programs may always call it.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Test::answerHttp, Core\Test::answerSocket, Core\Test::tlsSession and Core\Test::scriptAnswers throw a LogicError in a request, a connection, a scheduled task and a queued job. They work in a test, and in a request or a program that a test starts. They also work in a command-line program. |
Examples
The default session
<?nvs// Shows the TLS session that `Core\Test::tlsSession()` builds when you give it no options.
Core\Http\TlsInfo $session = Core\Test::tlsSession();
// This prints `TLSv1.3 TLS_AES_128_GCM_SHA256`.echo $session->version(), ' ', $session->cipher(), "\n";
// The certificate was checked. This prints `verified: yes`.echo 'verified: ', $session->isVerified() ? 'yes' : 'no', "\n";
// This prints `CN=example.com` and then `CN=Novis Test CA`.echo $session->subject(), "\n";echo $session->issuer(), "\n";
// The chain has two certificates: the server's own, and the one that signed it.// This prints `2`.echo Core\Arr::count($session->peerChain()), "\n";TLSv1.3 TLS_AES_128_GCM_SHA256verified: yesCN=example.comCN=Novis Test CA2A reply with a session
<?nvs// Shows how to give a fake reply a TLS session, so your program can read it.
use Core\Test;
// The `tls` option of `answerHttp` takes the session. Every reply// for this URL then reports it.Test::answerHttp('https://legacy.example.com/status', 200, { tls: Test::tlsSession({ version: 'TLSv1.2', verified: false, subject: 'CN=legacy.example.com, O=Shop', }),});
?Core\Http\TlsInfo $info = Core\Http\Client::get('https://legacy.example.com/status')->tls();if ($info != null) { // This prints `TLSv1.2 CN=legacy.example.com, O=Shop`. echo $info->version(), ' ', $info->subject(), "\n";
// This prints `not verified`. echo $info->isVerified() ? 'verified' : 'not verified', "\n";}TLSv1.2 CN=legacy.example.com, O=Shopnot verifiedA client that requires tls 1 3
<?nvs// Shows how to test that a payment client stops when the server uses an old TLS version.
use Core\Http\Client;use Core\Test;
class PaymentClient{ // Returns the reply status, or throws an error if the connection is not TLS 1.3. public static function charge(string $url): int { var $response = Client::post($url, { json: ['amount' => 1200] }); ?Core\Http\TlsInfo $info = $response->tls(); if ($info != null) { if ($info->version() == 'TLSv1.3') { return $response->status(); } } throw new RuntimeError('the payment server must use TLS 1.3'); }}
// Two fake servers: one with TLS 1.3, and one with TLS 1.2.Test::answerHttp('https://pay.example.com/charge', 200, { tls: Test::tlsSession() });Test::answerHttp('https://old-pay.example.com/charge', 200, { tls: Test::tlsSession({ version: 'TLSv1.2' }),});
// This prints `200`.echo PaymentClient::charge('https://pay.example.com/charge'), "\n";
// This prints `the payment server must use TLS 1.3`.try { PaymentClient::charge('https://old-pay.example.com/charge');} catch (RuntimeError $error) { echo $error->message, "\n";}200the payment server must use TLS 1.3