EncryptorTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Tests\RNCryptor;
  3. use PHPUnit\Framework\TestCase;
  4. use RNCryptor\RNCryptor\Encryptor;
  5. class EncryptorTest extends TestCase
  6. {
  7. const SAMPLE_PLAINTEXT = 'Hello, how are you today? I am doing fine.';
  8. const SAMPLE_PASSWORD = 'keep-out-123';
  9. public function testCanEncryptWithDefaultVersion()
  10. {
  11. $encryptor = new Encryptor;
  12. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD);
  13. $this->assertNotEmpty($encrypted);
  14. }
  15. public function testCanEncryptWithVersion0()
  16. {
  17. $encryptor = new Encryptor;
  18. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 0);
  19. $this->assertNotEmpty($encrypted);
  20. }
  21. public function testCanEncryptWithVersion1()
  22. {
  23. $encryptor = new Encryptor;
  24. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 1);
  25. $this->assertNotEmpty($encrypted);
  26. }
  27. public function testCanEncryptWithVersion2()
  28. {
  29. $encryptor = new Encryptor;
  30. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 2);
  31. $this->assertNotEmpty($encrypted);
  32. }
  33. public function testSelfEncryptedVersion0VectorIsVersion0()
  34. {
  35. $encryptor = new Encryptor;
  36. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 0);
  37. $actualVersion = ord(substr(base64_decode($encrypted), 0, 1));
  38. $this->assertEquals(0, $actualVersion);
  39. }
  40. public function testSelfEncryptedVersion1VectorIsVersion1()
  41. {
  42. $encryptor = new Encryptor;
  43. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 1);
  44. $actualVersion = ord(substr(base64_decode($encrypted), 0, 1));
  45. $this->assertEquals(1, $actualVersion);
  46. }
  47. public function testSelfEncryptedVersion2VectorIsVersion2()
  48. {
  49. $encryptor = new Encryptor;
  50. $encrypted = $encryptor->encrypt(self::SAMPLE_PLAINTEXT, self::SAMPLE_PASSWORD, 2);
  51. $actualVersion = ord(substr(base64_decode($encrypted), 0, 1));
  52. $this->assertEquals(2, $actualVersion);
  53. }
  54. }