PageRenderTime 42ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/suite/libraries/joomla/utilities/JSimpleCryptTest.php

https://bitbucket.org/ian_maclennan/joomla-cms
PHP | 146 lines | 76 code | 14 blank | 56 comment | 0 complexity | bb52f8bd0355b218bec33a4a122be46a MD5 | raw file
Possible License(s): JSON, GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * JSimpleCryptTest.php
  4. *
  5. * @version $Id$
  6. * @package Joomla.UnitTest
  7. * @copyright Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
  8. * @license GNU General Public License
  9. */
  10. require_once 'PHPUnit/Framework.php';
  11. require_once JPATH_BASE. DS . 'libraries' . DS . 'joomla' . DS . 'utilities' . DS . 'simplecrypt.php';
  12. /**
  13. * Test class for JSimpleCrypt.
  14. * Generated by PHPUnit on 2009-10-26 at 22:30:43.
  15. *
  16. * @package Joomla.UnitTest
  17. * @subpackage Utilities
  18. */
  19. class JSimpleCryptTest extends JoomlaTestCase
  20. {
  21. /**
  22. * @var JSimpleCrypt
  23. */
  24. protected $object;
  25. /**
  26. * Sets up the fixture, for example, opens a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. protected function setUp()
  32. {
  33. //$this->object = new JSimpleCrypt;
  34. $this->saveFactoryState();
  35. }
  36. /**
  37. * Tears down the fixture, for example, closes a network connection.
  38. * This method is called after a test is executed.
  39. *
  40. * @return void
  41. */
  42. protected function tearDown()
  43. {
  44. $this->restoreFactoryState();
  45. }
  46. /**
  47. * Test cases for encryption/decryption
  48. *
  49. * @return void
  50. */
  51. function casesEncryption()
  52. {
  53. return array(
  54. "HelloDef" => array(
  55. "Hello, World!",
  56. null,
  57. "2C515D 8574F446E57145C5443",
  58. ),
  59. "HelloKey" => array(
  60. "Hello, World!",
  61. "This is a new key",
  62. "1C D 51F4F455377 E52 2 156",
  63. ),
  64. "TypiDef" => array(
  65. "Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum",
  66. null,
  67. "304D41 D18 D B5718 E5152 75C4414 6555942594D584C 0 E46515A415E11 559 A445D1010194D154543425E5553 0574C594319505645 A F4B144342 C445250 75117445C5714455D",
  68. ),
  69. "TypiKey" => array(
  70. "Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum",
  71. "This is a new key",
  72. " 011191A 0 71C4E4148 F 7124E1F451A38 91B1A54 8 745 C 0 7 B 4491F 4146F48 C 05449 65314534E 91247 E B D3D1B491A4E491A4912 01F101E 0 D 41A3D1C49164F1B 64D",
  73. ),
  74. "WildDef" => array(
  75. chr(101).chr(23).chr(116).chr(3).chr(177).chr(99).chr(207).chr(249).chr(56).chr(107).chr(223).chr(49).chr(65).chr(119).chr(87).chr(189).chr(111).chr(133).chr(232).chr(48).chr(62).chr(201),
  76. null,
  77. " 123456789 0ABC0 0 DEF 123456789 ABCD0 0 EF0",
  78. ),
  79. "WildKey" => array(
  80. chr(101).chr(23).chr(116).chr(3).chr(177).chr(99).chr(207).chr(249).chr(56).chr(107).chr(223).chr(49).chr(65).chr(119).chr(87).chr(189).chr(111).chr(133).chr(232).chr(48).chr(62).chr(201),
  81. "This is a new key",
  82. "317F1D7091 ABCD9594BB15436573CD816D180594DE9",
  83. ),
  84. );
  85. }
  86. /**
  87. * Testing testDecrypt().
  88. *
  89. * @param string $expected The expected result of decryption
  90. * @param string $key The key to use
  91. * @param string $text The decrypted text
  92. *
  93. * @return void
  94. * @dataProvider casesEncryption
  95. */
  96. public function testDecrypt( $expected, $key, $text )
  97. {
  98. $cfg = $this->getMock('JObject', array('get'));
  99. $cfg->expects($this->any())
  100. ->method('get')
  101. ->will($this->returnValue(''));
  102. JFactory::$config = $cfg;
  103. $this->object = new JSimpleCrypt($key);
  104. $this->assertThat(
  105. $this->object->decrypt($text),
  106. $this->equalTo($expected)
  107. );
  108. }
  109. /**
  110. * Testing testEncrypt().
  111. *
  112. * @param string $text The text to be encrypted
  113. * @param string $key The key to use
  114. * @param string $expected The expected result of encryption
  115. *
  116. * @return void
  117. * @dataProvider casesEncryption
  118. */
  119. public function testEncrypt( $text, $key, $expected )
  120. {
  121. $cfg = $this->getMock('JObject', array('get'));
  122. $cfg->expects($this->any())
  123. ->method('get')
  124. ->will($this->returnValue(''));
  125. JFactory::$config = $cfg;
  126. $this->object = new JSimpleCrypt($key);
  127. $this->assertThat(
  128. $this->object->encrypt($text),
  129. $this->equalTo($expected)
  130. );
  131. }
  132. }