EncryptorTest.php 2.0 KB

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