Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Nov 1, 2017
1 parent ef03a72 commit 0efa8dd
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 79 deletions.
2 changes: 1 addition & 1 deletion docs/.shared
Submodule .shared updated 1 files
+13 −2 asset/css/screen.css
108 changes: 68 additions & 40 deletions test/ClientTlsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public function minimumVersionDataProvider() {
* @dataProvider minimumVersionDataProvider
*/
public function testWithMinimumVersion($version) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withMinimumVersion($version);
$this->assertSame(ClientTlsContext::TLSv1_0, $origContext->getMinimumVersion());
$context = new ClientTlsContext;
$clonedContext = $context->withMinimumVersion($version);

$this->assertSame(ClientTlsContext::TLSv1_0, $context->getMinimumVersion());
$this->assertSame($version, $clonedContext->getMinimumVersion());
}

Expand All @@ -36,8 +37,7 @@ public function minimumVersionInvalidDataProvider() {
* @expectedExceptionMessage Invalid minimum version, only TLSv1.0, TLSv1.1 or TLSv1.2 allowed
*/
public function testWithMinimumVersionInvalid($version) {
$origContext = new ClientTlsContext();
$origContext->withMinimumVersion($version);
(new ClientTlsContext)->withMinimumVersion($version);
}

public function peerNameDataProvider() {
Expand All @@ -51,23 +51,26 @@ public function peerNameDataProvider() {
* @dataProvider peerNameDataProvider
*/
public function testWithPeerName($peerName) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withPeerName($peerName);
$this->assertNull($origContext->getPeerName());
$context = new ClientTlsContext;
$clonedContext = $context->withPeerName($peerName);

$this->assertNull($context->getPeerName());
$this->assertSame($peerName, $clonedContext->getPeerName());
}

public function testWithPeerVerification() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withPeerVerification();
$this->assertTrue($origContext->hasPeerVerification());
$context = new ClientTlsContext;
$clonedContext = $context->withPeerVerification();

$this->assertTrue($context->hasPeerVerification());
$this->assertTrue($clonedContext->hasPeerVerification());
}

public function testWithoutPeerVerification() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withoutPeerVerification();
$this->assertTrue($origContext->hasPeerVerification());
$context = new ClientTlsContext;
$clonedContext = $context->withoutPeerVerification();

$this->assertTrue($context->hasPeerVerification());
$this->assertFalse($clonedContext->hasPeerVerification());
}

Expand All @@ -82,9 +85,10 @@ public function verifyDepthDataProvider() {
* @dataProvider verifyDepthDataProvider
*/
public function testWithVerificationDepth($verifyDepth) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withVerificationDepth($verifyDepth);
$this->assertSame(10, $origContext->getVerificationDepth());
$context = new ClientTlsContext;
$clonedContext = $context->withVerificationDepth($verifyDepth);

$this->assertSame(10, $context->getVerificationDepth());
$this->assertSame($verifyDepth, $clonedContext->getVerificationDepth());
}

Expand All @@ -101,8 +105,7 @@ public function verifyDepthInvalidDataProvider() {
* @expectedExceptionMessageRegExp /Invalid verification depth (.*), must be greater than or equal to 0/
*/
public function testWithVerificationDepthInvalid($verifyDepth) {
$origContext = new ClientTlsContext();
$origContext->withVerificationDepth($verifyDepth);
(new ClientTlsContext)->withVerificationDepth($verifyDepth);
}

public function ciphersDataProvider() {
Expand All @@ -116,9 +119,10 @@ public function ciphersDataProvider() {
* @dataProvider ciphersDataProvider
*/
public function testWithCiphers($ciphers) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withCiphers($ciphers);
$this->assertSame(\OPENSSL_DEFAULT_STREAM_CIPHERS, $origContext->getCiphers());
$context = new ClientTlsContext;
$clonedContext = $context->withCiphers($ciphers);

$this->assertSame(\OPENSSL_DEFAULT_STREAM_CIPHERS, $context->getCiphers());
$this->assertSame($ciphers, $clonedContext->getCiphers());
}

Expand All @@ -133,9 +137,10 @@ public function caFileDataProvider() {
* @dataProvider caFileDataProvider
*/
public function testWithCaFile($caFile) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withCaFile($caFile);
$this->assertNull($origContext->getCaFile());
$context = new ClientTlsContext;
$clonedContext = $context->withCaFile($caFile);

$this->assertNull($context->getCaFile());
$this->assertSame($caFile, $clonedContext->getCaFile());
}

Expand All @@ -150,37 +155,60 @@ public function caPathDataProvider() {
* @dataProvider caPathDataProvider
*/
public function testWithCaPath($caPath) {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withCaPath($caPath);
$this->assertNull($origContext->getCaPath());
$context = new ClientTlsContext;
$clonedContext = $context->withCaPath($caPath);

$this->assertNull($context->getCaPath());
$this->assertSame($caPath, $clonedContext->getCaPath());
}

public function testWithPeerCapturing() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withPeerCapturing();
$this->assertFalse($origContext->hasPeerCapturing());
$context = new ClientTlsContext;
$clonedContext = $context->withPeerCapturing();

$this->assertFalse($context->hasPeerCapturing());
$this->assertTrue($clonedContext->hasPeerCapturing());
}

public function testWithoutPeerCapturing() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withoutPeerCapturing();
$this->assertFalse($origContext->hasPeerCapturing());
$context = new ClientTlsContext;
$clonedContext = $context->withoutPeerCapturing();

$this->assertFalse($context->hasPeerCapturing());
$this->assertFalse($clonedContext->hasPeerCapturing());
}

public function testWithSni() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withSni();
$this->assertTrue($origContext->hasSni());
$context = new ClientTlsContext;
$clonedContext = $context->withSni();

$this->assertTrue($context->hasSni());
$this->assertTrue($clonedContext->hasSni());
}

public function testWithoutSni() {
$origContext = new ClientTlsContext();
$clonedContext = $origContext->withoutSni();
$this->assertTrue($origContext->hasSni());
$context = new ClientTlsContext;
$clonedContext = $context->withoutSni();

$this->assertTrue($context->hasSni());
$this->assertFalse($clonedContext->hasSni());
}

public function testStreamContextArray() {
$context = (new ClientTlsContext)
->withCaPath("/var/foobar");

$this->assertSame(["ssl" => [
"crypto_method" => $context->toStreamCryptoMethod(),
"peer_name" => $context->getPeerName(),
"verify_peer" => $context->hasPeerVerification(),
"verify_peer_name" => $context->hasPeerVerification(),
"verify_depth" => $context->getVerificationDepth(),
"ciphers" => $context->getCiphers(),
"capture_peer_cert" => $context->hasPeerCapturing(),
"capture_peer_cert_chain" => $context->hasPeerCapturing(),
"SNI_enabled" => $context->hasSni(),
"capath" => $context->getCaPath(),
]], $context->toStreamContextArray());
}
}
106 changes: 68 additions & 38 deletions test/ServerTlsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public function minimumVersionDataProvider() {
* @dataProvider minimumVersionDataProvider
*/
public function testWithMinimumVersion($version) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withMinimumVersion($version);
$this->assertSame(ServerTlsContext::TLSv1_0, $origContext->getMinimumVersion());
$context = new ServerTlsContext;
$clonedContext = $context->withMinimumVersion($version);

$this->assertSame(ServerTlsContext::TLSv1_0, $context->getMinimumVersion());
$this->assertSame($version, $clonedContext->getMinimumVersion());
}

Expand All @@ -37,8 +38,7 @@ public function minimumVersionInvalidDataProvider() {
* @expectedExceptionMessage Invalid minimum version, only TLSv1.0, TLSv1.1 or TLSv1.2 allowed
*/
public function testWithMinimumVersionInvalid($version) {
$origContext = new ServerTlsContext();
$origContext->withMinimumVersion($version);
(new ServerTlsContext)->withMinimumVersion($version);
}

public function peerNameDataProvider() {
Expand All @@ -52,23 +52,26 @@ public function peerNameDataProvider() {
* @dataProvider peerNameDataProvider
*/
public function testWithPeerName($peerName) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withPeerName($peerName);
$this->assertNull($origContext->getPeerName());
$context = new ServerTlsContext;
$clonedContext = $context->withPeerName($peerName);

$this->assertNull($context->getPeerName());
$this->assertSame($peerName, $clonedContext->getPeerName());
}

public function testWithPeerVerification() {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withPeerVerification();
$this->assertFalse($origContext->hasPeerVerification());
$context = new ServerTlsContext;
$clonedContext = $context->withPeerVerification();

$this->assertFalse($context->hasPeerVerification());
$this->assertTrue($clonedContext->hasPeerVerification());
}

public function testWithoutPeerVerification() {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withoutPeerVerification();
$this->assertFalse($origContext->hasPeerVerification());
$context = new ServerTlsContext;
$clonedContext = $context->withoutPeerVerification();

$this->assertFalse($context->hasPeerVerification());
$this->assertFalse($clonedContext->hasPeerVerification());
}

Expand All @@ -83,9 +86,10 @@ public function verifyDepthDataProvider() {
* @dataProvider verifyDepthDataProvider
*/
public function testWithVerificationDepth($verifyDepth) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withVerificationDepth($verifyDepth);
$this->assertSame(10, $origContext->getVerificationDepth());
$context = new ServerTlsContext;
$clonedContext = $context->withVerificationDepth($verifyDepth);

$this->assertSame(10, $context->getVerificationDepth());
$this->assertSame($verifyDepth, $clonedContext->getVerificationDepth());
}

Expand All @@ -102,8 +106,7 @@ public function verifyDepthInvalidDataProvider() {
* @expectedExceptionMessageRegExp /Invalid verification depth (.*), must be greater than or equal to 0/
*/
public function testWithVerificationDepthInvalid($verifyDepth) {
$origContext = new ServerTlsContext();
$origContext->withVerificationDepth($verifyDepth);
(new ServerTlsContext)->withVerificationDepth($verifyDepth);
}

public function ciphersDataProvider() {
Expand All @@ -117,9 +120,10 @@ public function ciphersDataProvider() {
* @dataProvider ciphersDataProvider
*/
public function testWithCiphers($ciphers) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withCiphers($ciphers);
$this->assertSame(\OPENSSL_DEFAULT_STREAM_CIPHERS, $origContext->getCiphers());
$context = new ServerTlsContext;
$clonedContext = $context->withCiphers($ciphers);

$this->assertSame(\OPENSSL_DEFAULT_STREAM_CIPHERS, $context->getCiphers());
$this->assertSame($ciphers, $clonedContext->getCiphers());
}

Expand All @@ -134,9 +138,10 @@ public function caFileDataProvider() {
* @dataProvider caFileDataProvider
*/
public function testWithCaFile($caFile) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withCaFile($caFile);
$this->assertNull($origContext->getCaFile());
$context = new ServerTlsContext;
$clonedContext = $context->withCaFile($caFile);

$this->assertNull($context->getCaFile());
$this->assertSame($caFile, $clonedContext->getCaFile());
}

Expand All @@ -151,40 +156,65 @@ public function caPathDataProvider() {
* @dataProvider caPathDataProvider
*/
public function testWithCaPath($caPath) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withCaPath($caPath);
$this->assertNull($origContext->getCaPath());
$context = new ServerTlsContext;
$clonedContext = $context->withCaPath($caPath);

$this->assertNull($context->getCaPath());
$this->assertSame($caPath, $clonedContext->getCaPath());
}

public function testWithPeerCapturing() {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withPeerCapturing();
$this->assertFalse($origContext->hasPeerCapturing());
$context = new ServerTlsContext;
$clonedContext = $context->withPeerCapturing();

$this->assertFalse($context->hasPeerCapturing());
$this->assertTrue($clonedContext->hasPeerCapturing());
}

public function testWithoutPeerCapturing() {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withoutPeerCapturing();
$this->assertFalse($origContext->hasPeerCapturing());
$context = new ServerTlsContext;
$clonedContext = $context->withoutPeerCapturing();

$this->assertFalse($context->hasPeerCapturing());
$this->assertFalse($clonedContext->hasPeerCapturing());
}

public function defaultCertificateDataProvider() {
return [
[null],
[new Certificate('test')]
[new Certificate('test')],
];
}

/**
* @dataProvider defaultCertificateDataProvider
*/
public function testWithDefaultCertificate($defaultCertificate) {
$origContext = new ServerTlsContext();
$clonedContext = $origContext->withDefaultCertificate($defaultCertificate);
$this->assertNull($origContext->getDefaultCertificate());
$context = new ServerTlsContext;
$clonedContext = $context->withDefaultCertificate($defaultCertificate);

$this->assertNull($context->getDefaultCertificate());
$this->assertSame($defaultCertificate, $clonedContext->getDefaultCertificate());
}

public function testWithCertificatesErrorWithoutStringKeys() {
$this->expectException(\TypeError::class);
$this->expectExceptionMessage("Expected an array mapping domain names to Certificate instances");

(new ServerTlsContext)->withCertificates([new Certificate("/foo/bar")]);
}

public function testWithCertificatesErrorWithoutCertificateInstances() {
$this->expectException(\TypeError::class);
$this->expectExceptionMessage("Expected an array of Certificate instances");

(new ServerTlsContext)->withCertificates(["example.com" => "/foo/bar"]);
}

public function testWithCertificatesWithDifferentPathsBeforePhp72() {
$this->expectException(\Error::class);
$this->expectExceptionMessage("Different files for cert and key are not supported on this version of PHP. Please upgrade to PHP 7.2 or later.");

(new ServerTlsContext)->withCertificates(["example.com" => new Certificate("/var/foo", "/foo/bar")]);
}
}

0 comments on commit 0efa8dd

Please sign in to comment.