PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Transform/Decrypt/MCryptTest.php

https://github.com/carmenpopescu/Q
PHP | 185 lines | 112 code | 25 blank | 48 comment | 2 complexity | 19291cda841cc1e5cc06bba23e70ca21 MD5 | raw file
  1. <?php
  2. use Q\Transform_Decrypt_MCrypt, Q\Transform;
  3. require_once 'TestHelper.php';
  4. require_once 'Q/Transform/Decrypt/MCrypt.php';
  5. require_once 'Q/Fs/File.php';
  6. /**
  7. * Decrypt_MCrypt test case.
  8. */
  9. class Transform_Decrypt_MCryptTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var Transform_Decrypt_MCrypt
  13. */
  14. private $Decrypt_MCrypt;
  15. /**
  16. * Prepares the environment before running a test.
  17. */
  18. protected function setUp()
  19. {
  20. if (!extension_loaded('mcrypt')) $this->markTestSkipped("mcrypt extension is not available");
  21. $this->Decrypt_MCrypt = new Transform_Decrypt_MCrypt(array('method'=>'blowfish', 'secret'=>'s3cret'));
  22. $this->file = sys_get_temp_dir() . '/q-crypt_test-' . md5(uniqid());
  23. parent::setUp();
  24. }
  25. /**
  26. * Cleans up the environment after running a test.
  27. */
  28. protected function tearDown()
  29. {
  30. $this->Decrypt_MCrypt = null;
  31. if (file_exists($this->file)) unlink($this->file);
  32. parent::tearDown();
  33. }
  34. /**
  35. * Tests Decrypt_MCrypt->process() with blowfish method
  36. */
  37. public function testDecrypt()
  38. {
  39. $encrypted = mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB);
  40. $this->assertEquals("a test string", $this->Decrypt_MCrypt->process($encrypted));
  41. }
  42. /**
  43. * Tests Decrypt_MCrypt->process() with DES method
  44. */
  45. public function testDecrypt_des()
  46. {
  47. $this->Decrypt_MCrypt->method = 'des';
  48. $encrypted = mcrypt_encrypt('des', 's3cret', "a test string", MCRYPT_MODE_ECB);
  49. $this->assertEquals("a test string", $this->Decrypt_MCrypt->process($encrypted));
  50. }
  51. /**
  52. * Tests Decrypt_MCrypt->process() with a file
  53. */
  54. public function testDecrypt_File()
  55. {
  56. $encrypted = mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB);
  57. $file = $this->getMock('Q\Fs_File', array('__toString', 'getContents'), array(), '', false);
  58. $file->expects($this->any())->method('__toString')->will($this->returnValue($this->file));
  59. $file->expects($this->once())->method('getContents')->will($this->returnValue($encrypted));
  60. $this->assertEquals("a test string", $this->Decrypt_MCrypt->process($file));
  61. }
  62. /**
  63. * Tests Decrypt_MCrypt->process() where process fails because of incorrect secret phrase
  64. */
  65. public function testDecrypt_WrongSecret()
  66. {
  67. $encrypted = mcrypt_encrypt('blowfish', 'another_secret', "a test string", MCRYPT_MODE_ECB);
  68. $this->assertNotEquals("a test string", $this->Decrypt_MCrypt->process($encrypted));
  69. }
  70. /**
  71. * Tests Decrypt_MCrypt->process() with a chain
  72. */
  73. public function testDecrypt_Chain()
  74. {
  75. $mock = $this->getMock('Q\Transform', array('process'));
  76. $mock->expects($this->once())->method('process')->with($this->equalTo('test'))->will($this->returnValue(mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB)));
  77. $this->Decrypt_MCrypt->chainInput($mock);
  78. $contents = $this->Decrypt_MCrypt->process('test');
  79. $this->assertType('Q\Transform_Decrypt_MCrypt', $this->Decrypt_MCrypt);
  80. $this->assertEquals("a test string", $contents);
  81. }
  82. /**
  83. * Tests Transform_Decrypt_MCrypt->output()
  84. */
  85. public function testOutput()
  86. {
  87. $encrypted = mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB);
  88. ob_start();
  89. try{
  90. $this->Decrypt_MCrypt->output($encrypted);
  91. } catch (Expresion $e) {
  92. ob_end_clean();
  93. throw $e;
  94. }
  95. $contents = ob_get_contents();
  96. ob_end_clean();
  97. $this->assertType('Q\Transform_Decrypt_MCrypt', $this->Decrypt_MCrypt);
  98. $this->assertEquals("a test string", $contents);
  99. }
  100. /**
  101. * Tests Transform_Decrypt_MCrypt->save()
  102. */
  103. public function testSave()
  104. {
  105. $encrypted = mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB);
  106. $this->Decrypt_MCrypt->save($this->file, $encrypted);
  107. $this->assertType('Q\Transform_Decrypt_MCrypt', $this->Decrypt_MCrypt);
  108. $this->assertEquals("a test string", file_get_contents($this->file));
  109. }
  110. /**
  111. * Tests Decrypt_MCrypt->getReverse()
  112. */
  113. public function testGetReverse()
  114. {
  115. $reverse = $this->Decrypt_MCrypt->getReverse();
  116. $this->assertType('Q\Transform_Crypt_MCrypt', $reverse);
  117. }
  118. /**
  119. * Tests Decrypt_MCrypt->getReverse() with a chain
  120. */
  121. public function testGetReverse_Chain()
  122. {
  123. $mock = $this->getMock('Q\Transform', array('getReverse', 'process'));
  124. $mock->expects($this->once())->method('getReverse')->with($this->isInstanceOf('Q\Transform_Crypt_MCrypt'))->will($this->returnValue('reverse of mock transformer'));
  125. $this->Decrypt_MCrypt->chainInput($mock);
  126. $this->assertEquals('reverse of mock transformer', $this->Decrypt_MCrypt->getReverse());
  127. }
  128. /**
  129. * Tests Transform_Decrypt_MCrypt->getReverse() with a chain
  130. */
  131. public function testGetReverse_ChainDouble()
  132. {
  133. $mock = $this->getMock('Q\Transform', array('getReverse', 'process'));
  134. $mock->expects($this->once())->method('getReverse')->with($this->isInstanceOf('Q\Transform_Crypt_MCrypt'))->will($this->returnValue('reverse of mock transformer'));
  135. $transform1 = new Transform_Decrypt_MCrypt();
  136. $transform2 = new Transform_Decrypt_MCrypt();
  137. $transform2->chainInput($mock);
  138. $transform1->chainInput($transform2);
  139. $this->assertEquals('reverse of mock transformer', $transform1->getReverse());
  140. }
  141. /**
  142. * Tests Transform_Decrypt_MCrypt->process() -null method
  143. */
  144. public function testProcessException_EmptyMethod()
  145. {
  146. $this->setExpectedException('Exception', 'Unable to decrypt: Algoritm not specified.');
  147. $transform = new Transform_Decrypt_MCrypt(array('method'=>null));
  148. $transform->process(mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB));
  149. }
  150. /**
  151. * Tests Transform_Decrypt_MCrypt->process() - unsupported method
  152. */
  153. public function testProcessException_UnsupportedMethod()
  154. {
  155. $method = "a_method";
  156. $this->setExpectedException('Exception', "Unable to decrypt: Algoritm '{$method}' is not supported.");
  157. $transform = new Transform_Decrypt_MCrypt(array('method'=>$method));
  158. $transform->process(mcrypt_encrypt('blowfish', 's3cret', "a test string", MCRYPT_MODE_ECB));
  159. }
  160. }