/tests/Zend/Filter/File/EncryptTest.php

https://bitbucket.org/ksekar/campus · PHP · 137 lines · 77 code · 18 blank · 42 comment · 3 complexity · 7cecebc4fbe2f26e5cd36d2ac3d992ff MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: EncryptTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /**
  23. * @see Zend_Filter_Encrypt
  24. */
  25. require_once 'Zend/Filter/File/Encrypt.php';
  26. require_once 'Zend/Filter/File/Decrypt.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Filter
  34. */
  35. class Zend_Filter_File_EncryptTest extends PHPUnit_Framework_TestCase
  36. {
  37. public function setUp()
  38. {
  39. if (!extension_loaded('mcrypt')) {
  40. $this->markTestSkipped('This filter needs the mcrypt extension');
  41. }
  42. if (file_exists(dirname(__FILE__).'/../_files/newencryption.txt')) {
  43. unlink(dirname(__FILE__).'/../_files/newencryption.txt');
  44. }
  45. }
  46. public function tearDown()
  47. {
  48. if (file_exists(dirname(__FILE__).'/../_files/newencryption.txt')) {
  49. unlink(dirname(__FILE__).'/../_files/newencryption.txt');
  50. }
  51. }
  52. /**
  53. * Ensures that the filter follows expected behavior
  54. *
  55. * @return void
  56. */
  57. public function testBasic()
  58. {
  59. $filter = new Zend_Filter_File_Encrypt();
  60. $filter->setFilename(dirname(__FILE__).'/../_files/newencryption.txt');
  61. $this->assertEquals(
  62. dirname(__FILE__).'/../_files/newencryption.txt',
  63. $filter->getFilename());
  64. $filter->setVector('testvect');
  65. $this->assertEquals(dirname(__FILE__).'/../_files/newencryption.txt',
  66. $filter->filter(dirname(__FILE__).'/../_files/encryption.txt'));
  67. $this->assertEquals(
  68. 'Encryption',
  69. file_get_contents(dirname(__FILE__).'/../_files/encryption.txt'));
  70. $this->assertNotEquals(
  71. 'Encryption',
  72. file_get_contents(dirname(__FILE__).'/../_files/newencryption.txt'));
  73. }
  74. public function testEncryptionWithDecryption()
  75. {
  76. $filter = new Zend_Filter_File_Encrypt();
  77. $filter->setFilename(dirname(__FILE__).'/../_files/newencryption.txt');
  78. $filter->setVector('testvect');
  79. $this->assertEquals(dirname(__FILE__).'/../_files/newencryption.txt',
  80. $filter->filter(dirname(__FILE__).'/../_files/encryption.txt'));
  81. $this->assertNotEquals(
  82. 'Encryption',
  83. file_get_contents(dirname(__FILE__).'/../_files/newencryption.txt'));
  84. $filter = new Zend_Filter_File_Decrypt();
  85. $filter->setVector('testvect');
  86. $input = $filter->filter(dirname(__FILE__).'/../_files/newencryption.txt');
  87. $this->assertEquals(dirname(__FILE__).'/../_files/newencryption.txt', $input);
  88. $this->assertEquals(
  89. 'Encryption',
  90. trim(file_get_contents(dirname(__FILE__).'/../_files/newencryption.txt')));
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testNonExistingFile()
  96. {
  97. $filter = new Zend_Filter_File_Encrypt();
  98. $filter->setVector('testvect');
  99. try {
  100. $filter->filter(dirname(__FILE__).'/../_files/nofile.txt');
  101. $this->fail();
  102. } catch (Zend_Filter_Exception $e) {
  103. $this->assertContains('not found', $e->getMessage());
  104. }
  105. }
  106. /**
  107. * @return void
  108. */
  109. public function testEncryptionInSameFile()
  110. {
  111. $filter = new Zend_Filter_File_Encrypt();
  112. $filter->setVector('testvect');
  113. copy(dirname(__FILE__).'/../_files/encryption.txt', dirname(__FILE__).'/../_files/newencryption.txt');
  114. $filter->filter(dirname(__FILE__).'/../_files/newencryption.txt');
  115. $this->assertNotEquals(
  116. 'Encryption',
  117. trim(file_get_contents(dirname(__FILE__).'/../_files/newencryption.txt')));
  118. }
  119. }