|
| 1 | +<?php |
| 2 | + |
| 3 | +use AndroidSmsGateway\Encryptor; |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | + |
| 6 | +class EncryptorTest extends TestCase { |
| 7 | + public function testEncryptDecrypt(): void { |
| 8 | + $passphrase = 'MySecretPassphrase'; |
| 9 | + $encryptor = new Encryptor($passphrase); |
| 10 | + |
| 11 | + $originalData = 'Sensitive data here'; |
| 12 | + $encryptedData = $encryptor->Encrypt($originalData); |
| 13 | + $decryptedData = $encryptor->Decrypt($encryptedData); |
| 14 | + |
| 15 | + $this->assertEquals($originalData, $decryptedData, 'Decrypted data should match original data.'); |
| 16 | + } |
| 17 | + |
| 18 | + public function testDecryptWithWrongPassphrase(): void { |
| 19 | + $this->expectException(\RuntimeException::class); |
| 20 | + |
| 21 | + $passphrase = 'MySecretPassphrase'; |
| 22 | + $wrongPassphrase = 'WrongPassphrase'; |
| 23 | + $encryptor = new Encryptor($passphrase); |
| 24 | + $wrongEncryptor = new Encryptor($wrongPassphrase); |
| 25 | + |
| 26 | + $originalData = 'Sensitive data here'; |
| 27 | + $encryptedData = $encryptor->Encrypt($originalData); |
| 28 | + |
| 29 | + // Try to decrypt with wrong passphrase |
| 30 | + $wrongEncryptor->Decrypt($encryptedData); |
| 31 | + } |
| 32 | + |
| 33 | + public function testDecryptWithUnsupportedAlgorithm(): void { |
| 34 | + $this->expectException(\RuntimeException::class); |
| 35 | + |
| 36 | + $passphrase = 'MySecretPassphrase'; |
| 37 | + $encryptor = new Encryptor($passphrase); |
| 38 | + |
| 39 | + // Manually construct data with unsupported algorithm |
| 40 | + $unsupportedData = '$unsupported-algo$i=10000$fakeSalt$fakeEncryptedData'; |
| 41 | + |
| 42 | + $encryptor->Decrypt($unsupportedData); |
| 43 | + } |
| 44 | + |
| 45 | + public function testDecryptWithMissingIterationCount(): void { |
| 46 | + $this->expectException(\RuntimeException::class); |
| 47 | + |
| 48 | + $passphrase = 'MySecretPassphrase'; |
| 49 | + $encryptor = new Encryptor($passphrase); |
| 50 | + |
| 51 | + // Manually construct data with missing iteration count |
| 52 | + $missingIterationCountData = '$aes-256-cbc/pbkdf2-sha1$x=1$fakeSalt$fakeEncryptedData'; |
| 53 | + |
| 54 | + $encryptor->Decrypt($missingIterationCountData); |
| 55 | + } |
| 56 | +} |
0 commit comments