|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SpecShaper\EncryptBundle\tests\Unit\Encryptors; |
| 6 | + |
| 7 | +use SpecShaper\EncryptBundle\Encryptors\AesGcmEncryptor; |
| 8 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author David Dadon <david.dadon@neftys.fr> |
| 12 | + */ |
| 13 | +class AesGcmEncryptorTest extends \PHPUnit\Framework\TestCase |
| 14 | +{ |
| 15 | + private const TEST_KEY = 'YBmNcBGfrZoayB+V254wdYa/abvxSUWJsjCtlMc1tRI='; |
| 16 | + |
| 17 | + public function testEncryptException(): void |
| 18 | + { |
| 19 | + $this->expectException(\TypeError::class); |
| 20 | + |
| 21 | + // Given |
| 22 | + $object = new \stdClass(); |
| 23 | + $object->test = 'Test'; |
| 24 | + |
| 25 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 26 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 27 | + |
| 28 | + // When |
| 29 | + $encryptor->encrypt($object); |
| 30 | + } |
| 31 | + |
| 32 | + public function testEncryptNullReturnsNull(): void |
| 33 | + { |
| 34 | + // Given |
| 35 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 36 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 37 | + $encryptor->setDefaultAssociatedData(null); |
| 38 | + |
| 39 | + // When |
| 40 | + $result = $encryptor->encrypt(null); |
| 41 | + |
| 42 | + // Then |
| 43 | + $this->assertTrue($result === null); |
| 44 | + } |
| 45 | + |
| 46 | + public function testEncryptOnlySuffix(): void |
| 47 | + { |
| 48 | + // Given |
| 49 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 50 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 51 | + $encryptor->setDefaultAssociatedData(null); |
| 52 | + |
| 53 | + // When |
| 54 | + $result = $encryptor->encrypt('<ENC>'); |
| 55 | + |
| 56 | + // Then |
| 57 | + $this->assertTrue($result === '<ENC>'); |
| 58 | + } |
| 59 | + |
| 60 | + public function testEncryptAndDecryptReturnsOriginalValue(): void |
| 61 | + { |
| 62 | + // Given |
| 63 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 64 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 65 | + $encryptor->setDefaultAssociatedData(null); |
| 66 | + $value = 'Honey, where are my pants?'; |
| 67 | + |
| 68 | + // When |
| 69 | + $encryptedValue = $encryptor->encrypt($value); |
| 70 | + |
| 71 | + // Then |
| 72 | + $this->assertTrue($encryptedValue !== $value); |
| 73 | + |
| 74 | + // When |
| 75 | + $decrypted = $encryptor->decrypt($encryptedValue); |
| 76 | + |
| 77 | + // Then |
| 78 | + $this->assertTrue($decrypted === $value); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @throws \Exception |
| 83 | + */ |
| 84 | + public function testDecryptException(): void |
| 85 | + { |
| 86 | + $this->expectException(\TypeError::class); |
| 87 | + // or for PHPUnit < 5.2 |
| 88 | + // $this->setExpectedException(InvalidArgumentException::class); |
| 89 | + |
| 90 | + // Given |
| 91 | + $object = new \stdClass(); |
| 92 | + $object->test = 'Test'; |
| 93 | + |
| 94 | + // ...and then add your test code that generates the exception |
| 95 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 96 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 97 | + |
| 98 | + // When |
| 99 | + $encryptor->decrypt($object); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @throws \Exception |
| 104 | + */ |
| 105 | + public function testDecryptNullReturnsNull(): void |
| 106 | + { |
| 107 | + // Given |
| 108 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 109 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 110 | + $encryptor->setDefaultAssociatedData(null); |
| 111 | + |
| 112 | + // When |
| 113 | + $result = $encryptor->decrypt(null); |
| 114 | + |
| 115 | + // Then |
| 116 | + $this->assertTrue($result === null); |
| 117 | + } |
| 118 | + |
| 119 | + public function testDecryptWithoutSuffixReturnsOrignialValue(): void |
| 120 | + { |
| 121 | + // Given |
| 122 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 123 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 124 | + $encryptor->setDefaultAssociatedData(null); |
| 125 | + |
| 126 | + // When |
| 127 | + $result = $encryptor->decrypt('Test value <ENC'); |
| 128 | + |
| 129 | + // Then |
| 130 | + $this->assertTrue($result === 'Test value <ENC'); |
| 131 | + } |
| 132 | + |
| 133 | + public function testDecryptReturnsExpectedValue(): void |
| 134 | + { |
| 135 | + // Given |
| 136 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 137 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 138 | + $encryptor->setDefaultAssociatedData(null); |
| 139 | + |
| 140 | + // When |
| 141 | + $decrypted = $encryptor->decrypt('g5wofClWz/wG44umXsUw+wAHQiqhTmo0eGIcODXvV6bjU3xDR8paa7wzu8EoJh0xGOJPD+Ue<ENC>'); |
| 142 | + |
| 143 | + // Then |
| 144 | + $this->assertTrue($decrypted === 'Honey, where are my pants?'); |
| 145 | + } |
| 146 | + |
| 147 | + public function testEncryptWithColumnName(): void |
| 148 | + { |
| 149 | + // Given |
| 150 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 151 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 152 | + $value = 'Honey, where are my pants?'; |
| 153 | + |
| 154 | + // When |
| 155 | + $encryptedValue = $encryptor->encrypt($value, 'columnName'); |
| 156 | + |
| 157 | + // Then |
| 158 | + $this->assertFalse($encryptedValue === $value); |
| 159 | + |
| 160 | + // When |
| 161 | + $decrypted = $encryptor->decrypt($encryptedValue, 'columnName'); |
| 162 | + |
| 163 | + // Then |
| 164 | + $this->assertTrue($decrypted === $value); |
| 165 | + } |
| 166 | + |
| 167 | + public function testEncryptWithDefaultAssociatedData(): void |
| 168 | + { |
| 169 | + // Given |
| 170 | + $encryptor = new AesGcmEncryptor(new EventDispatcher()); |
| 171 | + $encryptor->setSecretKey(self::TEST_KEY); |
| 172 | + $encryptor->setDefaultAssociatedData('DefaultAssociatedData'); |
| 173 | + $value = 'Honey, where are my pants?'; |
| 174 | + |
| 175 | + // When |
| 176 | + $encryptedValue = $encryptor->encrypt($value); |
| 177 | + |
| 178 | + // Then |
| 179 | + $this->assertFalse($encryptedValue === $value); |
| 180 | + |
| 181 | + // When |
| 182 | + $decrypted = $encryptor->decrypt($encryptedValue); |
| 183 | + |
| 184 | + // Then |
| 185 | + $this->assertTrue($decrypted === $value); |
| 186 | + } |
| 187 | +} |
0 commit comments