Skip to content

Commit

Permalink
setUp > setUpBeforeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed May 24, 2023
1 parent 05d6329 commit 44be810
Show file tree
Hide file tree
Showing 63 changed files with 705 additions and 716 deletions.
5 changes: 2 additions & 3 deletions composer.json
Expand Up @@ -43,11 +43,10 @@
"ext-spl": "*",

"simplesamlphp/assert": "^1.0.4",
"simplesamlphp/xml-common": "^1.9.0"
"simplesamlphp/xml-common": "^1.10.0"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.5.4",
"vimeo/psalm": "^5.11"
"simplesamlphp/simplesamlphp-test-framework": "^1.5.5"
},
"config": {
"allow-plugins": {
Expand Down
30 changes: 12 additions & 18 deletions src/TestUtils/SignedElementTestTrait.php
Expand Up @@ -41,38 +41,32 @@ trait SignedElementTestTrait
*
* @var \DOMDocument|null
*/
protected ?DOMDocument $xmlRepresentation = null;
protected static ?DOMDocument $xmlRepresentation;

/**
* The name of the class we are testing.
*
* @var class-string
* @var class-string|null
*/
protected string $testedClass;
protected static ?string $testedClass;


/**
* Test signing / verifying
*/
public function testSignatures(): void
{
if (!class_exists($this->testedClass)) {
if (!class_exists(self::$testedClass)) {
$this->markTestSkipped(
'Unable to run ' . self::class . '::testSignatures(). Please set ' . self::class
. ':$testedClass to a class-string representing the XML-class being tested',
);
} elseif (empty($this->xmlRepresentation)) {
} elseif (empty(self::$xmlRepresentation)) {
$this->markTestSkipped(
'Unable to run ' . self::class . '::testSignatures(). Please set ' . self::class
. ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
);
} else {
/** @psalm-var class-string|null */
$testedClass = $this->testedClass;

/** @psalm-var \DOMElement|null */
$xmlRepresentation = $this->xmlRepresentation;

$algorithms = array_keys(C::$RSA_DIGESTS);
foreach ($algorithms as $algorithm) {
if (
Expand Down Expand Up @@ -100,9 +94,9 @@ public function testSignatures(): void
)]),
]);

$unsigned = $testedClass::fromXML($xmlRepresentation->documentElement);
$unsigned = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
$unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $keyInfo);
$signed = $this->testedClass::fromXML($unsigned->toXML());
$signed = self::$testedClass::fromXML($unsigned->toXML());
$this->assertEquals(
$algorithm,
$signed->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(),
Expand All @@ -123,7 +117,7 @@ public function testSignatures(): void
) {
$this->fail(sprintf('%s: %s', $algorithm, $e->getMessage()));
}
$this->assertInstanceOf($this->testedClass, $verified);
$this->assertInstanceOf(self::$testedClass, $verified);

$this->assertEquals(
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY),
Expand All @@ -135,7 +129,7 @@ public function testSignatures(): void
// sign without certificates
//
$unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, null);
$signed = $this->testedClass::fromXML($unsigned->toXML());
$signed = self::$testedClass::fromXML($unsigned->toXML());

// verify signature
try {
Expand All @@ -147,7 +141,7 @@ public function testSignatures(): void
) {
$this->fail(sprintf('%s: %s', $algorithm, $e->getMessage()));
}
$this->assertInstanceOf($this->testedClass, $verified);
$this->assertInstanceOf(self::$testedClass, $verified);

$this->assertEquals(
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY),
Expand All @@ -163,7 +157,7 @@ public function testSignatures(): void
PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::OTHER_PRIVATE_KEY),
);
$unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, null);
$signed = $this->testedClass::fromXML($unsigned->toXML());
$signed = self::$testedClass::fromXML($unsigned->toXML());

// verify signature
try {
Expand All @@ -176,7 +170,7 @@ public function testSignatures(): void
) {
$this->assertEquals('Failed to verify signature.', $e->getMessage());
}
$this->assertInstanceOf($this->testedClass, $verified);
$this->assertInstanceOf(self::$testedClass, $verified);

$this->assertEquals(
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY),
Expand Down
23 changes: 13 additions & 10 deletions tests/Alg/Encryption/AESEncryptionTest.php
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Key\SymmetricKey;

Expand All @@ -17,16 +18,20 @@
class AESEncryptionTest extends TestCase
{
/** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
protected SymmetricKey $skey;
protected static SymmetricKey $skey;

/** @var EncryptionAlgorithmFactory */
protected EncryptionAlgorithmFactory $factory;
/** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory */
protected static EncryptionAlgorithmFactory $factory;

/** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface */
protected static EncryptionAlgorithmInterface $algo;

public function setUp(): void

public static function setUpBeforeClass(): void
{
$this->skey = new SymmetricKey(hex2bin('7392d8ec40a862fbb02786bab41481b2'));
$this->factory = new EncryptionAlgorithmFactory([]);
self::$skey = new SymmetricKey(hex2bin('7392d8ec40a862fbb02786bab41481b2'));
self::$factory = new EncryptionAlgorithmFactory([]);
self::$algo = self::$factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey);
}


Expand All @@ -35,8 +40,7 @@ public function setUp(): void
*/
public function testEncrypt(): void
{
$aes = $this->factory->getAlgorithm(C::BLOCK_ENC_AES128, $this->skey);
$ciphertext = $aes->encrypt('plaintext');
$ciphertext = self::$algo->encrypt('plaintext');
$this->assertNotEmpty($ciphertext);
$this->assertEquals(32, strlen($ciphertext));
}
Expand All @@ -48,8 +52,7 @@ public function testEncrypt(): void
public function testDecrypt(): void
{
$ciphertext = "r0YRkEixBnAKU032/ux7avHcVTH1CIIyKaPA2qr4KlIs0LVZp5CuwQKRRi6lji4cnaFbH4jETtJhMSEfbpSdvg==";
$aes = $this->factory->getAlgorithm(C::BLOCK_ENC_AES128, $this->skey);
$plaintext = $aes->decrypt(base64_decode($ciphertext));
$plaintext = self::$algo->decrypt(base64_decode($ciphertext));
$this->assertEquals("\n <Value>\n\tHello, World!\n </Value>\n", $plaintext);
}
}
38 changes: 19 additions & 19 deletions tests/Alg/Encryption/EncryptionAlgorithmFactoryTest.php
Expand Up @@ -21,12 +21,12 @@
class EncryptionAlgorithmFactoryTest extends TestCase
{
/** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
protected SymmetricKey $skey;
protected static SymmetricKey $skey;


public function setUp(): void
public static function setUpBeforeClass(): void
{
$this->skey = SymmetricKey::generate(16);
self::$skey = SymmetricKey::generate(16);
}


Expand All @@ -37,7 +37,7 @@ public function testGetUnknownAlgorithm(): void
{
$factory = new EncryptionAlgorithmFactory([]);
$this->expectException(UnsupportedAlgorithmException::class);
$factory->getAlgorithm('Unsupported algorithm identifier', $this->skey);
$factory->getAlgorithm('Unsupported algorithm identifier', self::$skey);
}


Expand All @@ -47,33 +47,33 @@ public function testGetUnknownAlgorithm(): void
public function testDefaultBlacklistedAlgorithms(): void
{
$factory = new EncryptionAlgorithmFactory();
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES128, $algorithm->getAlgorithmId());
$this->assertEquals($this->skey, $algorithm->getKey());
$this->assertEquals(self::$skey, $algorithm->getKey());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES128_GCM, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES192, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES192_GCM, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES256, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES256_GCM, $algorithm->getAlgorithmId());

$this->expectException(BlacklistedAlgorithmException::class);
$factory->getAlgorithm(C::BLOCK_ENC_3DES, $this->skey);
$factory->getAlgorithm(C::BLOCK_ENC_3DES, self::$skey);
}


Expand All @@ -83,31 +83,31 @@ public function testDefaultBlacklistedAlgorithms(): void
public function testBlacklistedAlgorithm(): void
{
$factory = new EncryptionAlgorithmFactory([C::BLOCK_ENC_AES256_GCM]);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_3DES, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_3DES, self::$skey);
$this->assertInstanceOf(TripleDES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_3DES, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES128, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES128_GCM, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES128_GCM, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES192, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES192_GCM, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES192_GCM, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, $this->skey);
$algorithm = $factory->getAlgorithm(C::BLOCK_ENC_AES256, self::$skey);
$this->assertInstanceOf(AES::class, $algorithm);
$this->assertEquals(C::BLOCK_ENC_AES256, $algorithm->getAlgorithmId());

$this->expectException(BlacklistedAlgorithmException::class);
$factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, $this->skey);
$factory->getAlgorithm(C::BLOCK_ENC_AES256_GCM, self::$skey);
}
}
23 changes: 13 additions & 10 deletions tests/Alg/Encryption/TripleDesEncryptionTest.php
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Key\SymmetricKey;

Expand All @@ -17,16 +18,20 @@
class TripleDesEncryptionTest extends TestCase
{
/** @var \SimpleSAML\XMLSecurity\Key\SymmetricKey */
protected SymmetricKey $skey;
protected static SymmetricKey $skey;

/** @var EncryptionAlgorithmFactory */
protected EncryptionAlgorithmFactory $factory;
/** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory */
protected static EncryptionAlgorithmFactory $factory;

/** @var \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface */
protected static EncryptionAlgorithmInterface $algo;

public function setUp(): void

public static function setUpBeforeClass(): void
{
$this->skey = new SymmetricKey(hex2bin('0d6d02528a57fd9797a79db307ce1558761a454a1c1f4a57'));
$this->factory = new EncryptionAlgorithmFactory([]);
self::$skey = new SymmetricKey(hex2bin('0d6d02528a57fd9797a79db307ce1558761a454a1c1f4a57'));
self::$factory = new EncryptionAlgorithmFactory([]);
self::$algo = self::$factory->getAlgorithm(C::BLOCK_ENC_3DES, self::$skey);
}


Expand All @@ -35,8 +40,7 @@ public function setUp(): void
*/
public function testEncrypt(): void
{
$tripleDes = $this->factory->getAlgorithm(C::BLOCK_ENC_3DES, $this->skey);
$ciphertext = $tripleDes->encrypt('plaintext');
$ciphertext = self::$algo->encrypt('plaintext');
$this->assertNotEmpty($ciphertext);
$this->assertEquals(24, strlen($ciphertext));
}
Expand All @@ -48,8 +52,7 @@ public function testEncrypt(): void
public function testDecrypt(): void
{
$ciphertext = "D+3dKq7MFK7U+8bqdlyRcvO12JV5Lahl5ALhF5eJXSfi+cbYKRbkRjvJsMKPp2Mk";
$tripleDes = $this->factory->getAlgorithm(C::BLOCK_ENC_3DES, $this->skey);
$plaintext = $tripleDes->decrypt(base64_decode($ciphertext));
$plaintext = self::$algo->decrypt(base64_decode($ciphertext));
$this->assertEquals("\n <Value>\n\tHello, World!\n </Value>\n", $plaintext);
}
}
22 changes: 11 additions & 11 deletions tests/Alg/KeyTransport/KeyTransportAlgorithmFactoryTest.php
Expand Up @@ -21,12 +21,12 @@
class KeyTransportAlgorithmFactoryTest extends TestCase
{
/** @var \SimpleSAML\XMLSecurity\Key\PublicKey */
protected PublicKey $pkey;
protected static PublicKey $pkey;


public function setUp(): void
public static function setUpBeforeClass(): void
{
$this->pkey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY);
self::$pkey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY);
}


Expand All @@ -37,7 +37,7 @@ public function testGetUnknownAlgorithm(): void
{
$factory = new KeyTransportAlgorithmFactory([]);
$this->expectException(UnsupportedAlgorithmException::class);
$factory->getAlgorithm('Unsupported algorithm identifier', $this->pkey);
$factory->getAlgorithm('Unsupported algorithm identifier', self::$pkey);
}


Expand All @@ -47,16 +47,16 @@ public function testGetUnknownAlgorithm(): void
public function testDefaultBlacklistedAlgorithm(): void
{
$factory = new KeyTransportAlgorithmFactory();
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, $this->pkey);
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$pkey);
$this->assertInstanceOf(RSA::class, $algorithm);
$this->assertEquals(C::KEY_TRANSPORT_OAEP, $algorithm->getAlgorithmId());

$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->pkey);
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$pkey);
$this->assertInstanceOf(RSA::class, $algorithm);
$this->assertEquals(C::KEY_TRANSPORT_OAEP_MGF1P, $algorithm->getAlgorithmId());

$this->expectException(BlacklistedAlgorithmException::class);
$factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, $this->pkey);
$factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$pkey);
}


Expand All @@ -66,16 +66,16 @@ public function testDefaultBlacklistedAlgorithm(): void
public function testBlacklistedAlgorithm(): void
{
$factory = new KeyTransportAlgorithmFactory([C::KEY_TRANSPORT_OAEP_MGF1P]);
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, $this->pkey);
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$pkey);
$this->assertInstanceOf(RSA::class, $algorithm);
$this->assertEquals(C::KEY_TRANSPORT_OAEP, $algorithm->getAlgorithmId());
$this->assertEquals($this->pkey, $algorithm->getKey());
$this->assertEquals(self::$pkey, $algorithm->getKey());

$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, $this->pkey);
$algorithm = $factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$pkey);
$this->assertInstanceOf(RSA::class, $algorithm);
$this->assertEquals(C::KEY_TRANSPORT_RSA_1_5, $algorithm->getAlgorithmId());

$this->expectException(BlacklistedAlgorithmException::class);
$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->pkey);
$factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$pkey);
}
}

0 comments on commit 44be810

Please sign in to comment.