From aec9571dc85c861ac0b6e081782aae30f9b6d2c1 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Sun, 14 Jun 2015 16:22:04 +0200 Subject: [PATCH] Add some tests for SimpleSAML\Utils\Crypto. --- tests/lib/SimpleSAML/Utils/CryptoTest.php | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/lib/SimpleSAML/Utils/CryptoTest.php diff --git a/tests/lib/SimpleSAML/Utils/CryptoTest.php b/tests/lib/SimpleSAML/Utils/CryptoTest.php new file mode 100644 index 000000000..3b0ad1409 --- /dev/null +++ b/tests/lib/SimpleSAML/Utils/CryptoTest.php @@ -0,0 +1,48 @@ +<?php + + +/** + * Tests for SimpleSAML\Utils\Crypto. + */ +class CryptoTest extends PHPUnit_Framework_TestCase +{ + + /** + * Test invalid input provided to the aesDecrypt() method. + * + * @expectedException InvalidArgumentException + */ + public function testAesDecryptBadInput() { + SimpleSAML\Utils\Crypto::aesDecrypt(array()); + } + + + /** + * Test that aesDecrypt() works properly, being able to decrypt some previously known (and correct) + * ciphertext. + */ + public function testAesDecrypt() { + $c = SimpleSAML_Configuration::loadFromArray(array( + 'secretsalt' => 'SUPER_SECRET_SALT', + )); + + $plaintext = 'SUPER_SECRET_TEXT'; + $ciphertext = 'GA5lvW9TbAErqGvHFfZMUkdIg6zCJYmjFSGERAhByYNcMts70N4fWLjUm7/aVygPm55GbGhEG2himXJUHR1Ibg=='; + $this->assertEquals($plaintext, SimpleSAML\Utils\Crypto::aesDecrypt(base64_decode($ciphertext))); + } + + + /** + * Test that aesEncrypt() produces ciphertexts that aesDecrypt() can decrypt. + */ + public function testAesEncrypt() { + $c = SimpleSAML_Configuration::loadFromArray(array( + 'secretsalt' => 'SUPER_SECRET_SALT', + )); + + $original_plaintext = 'SUPER_SECRET_TEXT'; + $ciphertext = SimpleSAML\Utils\Crypto::aesEncrypt($original_plaintext); + $decrypted_plaintext = SimpleSAML\Utils\Crypto::aesDecrypt($ciphertext); + $this->assertEquals($original_plaintext, $decrypted_plaintext); + } +} -- GitLab