PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 2ms 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
  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 */
 10require_once 'PHPUnit/Framework.php';
 11require_once JPATH_BASE. DS . 'libraries' . DS . 'joomla' . DS . 'utilities' . DS . 'simplecrypt.php';
 12
 13/**
 14 * Test class for JSimpleCrypt.
 15 * Generated by PHPUnit on 2009-10-26 at 22:30:43.
 16 *
 17 * @package	Joomla.UnitTest
 18 * @subpackage Utilities
 19 */
 20class JSimpleCryptTest extends JoomlaTestCase
 21{
 22	/**
 23	 * @var JSimpleCrypt
 24	 */
 25	protected $object;
 26
 27	/**
 28	 * Sets up the fixture, for example, opens a network connection.
 29	 * This method is called before a test is executed.
 30	 *
 31	 * @return void
 32	 */
 33	protected function setUp()
 34	{
 35		//$this->object = new JSimpleCrypt;
 36		$this->saveFactoryState();
 37	}
 38
 39	/**
 40	 * Tears down the fixture, for example, closes a network connection.
 41	 * This method is called after a test is executed.
 42	 *
 43	 * @return void
 44	 */
 45	protected function tearDown()
 46	{
 47		$this->restoreFactoryState();
 48	}
 49
 50	/**
 51	 * Test cases for encryption/decryption
 52	 *
 53	 * @return void
 54	 */
 55	function casesEncryption()
 56	{
 57		return array(
 58			"HelloDef" => array(
 59				"Hello, World!",
 60				null,
 61				"2C515D 8574F446E57145C5443",
 62			),
 63			"HelloKey" => array(
 64				"Hello, World!",
 65				"This is a new key",
 66				"1C D 51F4F455377 E52 2 156",
 67			),
 68			"TypiDef" => array(
 69				"Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum",
 70				null,
 71				"304D41 D18 D B5718 E5152 75C4414 6555942594D584C 0 E46515A415E11 559 A445D1010194D154543425E5553 0574C594319505645 A F4B144342 C445250 75117445C5714455D",
 72			),
 73			"TypiKey" => array(
 74				"Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum",
 75				"This is a new key",
 76				" 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",
 77			),
 78			"WildDef" => array(
 79				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),
 80				null,
 81				" 123456789 0ABC0 0 DEF 123456789 ABCD0 0 EF0",
 82			),
 83			"WildKey" => array(
 84				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),
 85				"This is a new key",
 86				"317F1D7091 ABCD9594BB15436573CD816D180594DE9",
 87			),
 88		);
 89	}
 90	/**
 91	 * Testing testDecrypt().
 92	 *
 93	 * @param string $expected The expected result of decryption
 94	 * @param string $key	The key to use
 95	 * @param string $text	The decrypted text
 96	 *
 97	 * @return void
 98	 * @dataProvider casesEncryption
 99	 */
100	public function testDecrypt( $expected, $key, $text )
101	{
102		$cfg = $this->getMock('JObject', array('get'));
103
104		$cfg->expects($this->any())
105			->method('get')
106			->will($this->returnValue(''));
107
108		JFactory::$config = $cfg;
109
110		$this->object = new JSimpleCrypt($key);
111
112		$this->assertThat(
113			$this->object->decrypt($text),
114			$this->equalTo($expected)
115		);
116	}
117
118	/**
119	 * Testing testEncrypt().
120	 *
121	 * @param string $text	The text to be encrypted
122	 * @param string $key	The key to use
123	 * @param string $expected The expected result of encryption
124	 *
125	 * @return void
126	 * @dataProvider casesEncryption
127	 */
128	public function testEncrypt( $text, $key, $expected )
129	{
130		$cfg = $this->getMock('JObject', array('get'));
131
132		$cfg->expects($this->any())
133			->method('get')
134			->will($this->returnValue(''));
135
136		JFactory::$config = $cfg;
137
138		$this->object = new JSimpleCrypt($key);
139
140		$this->assertThat(
141			$this->object->encrypt($text),
142			$this->equalTo($expected)
143		);
144	}
145}
146