Source for file XteaTest.php

Documentation is available at XteaTest.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4.0 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 2002-2004 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available at through the world-wide-web at |
  12. // | http://www.php.net/license/2_02.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Jeroen Derks <jeroen@derks.it> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: XteaTest.php,v 1.12 2004/10/04 19:51:56 jeroend Exp $
  21.  
  22.  
  23.  
  24. /** Xtea class */
  25. 'Crypt/Xtea.php';
  26. /** PHPUnit class */
  27. 'PHPUnit.php';
  28.  
  29.  
  30. /**
  31. * Tester class for Xtea class.
  32. * Tester class for Xtea class.
  33. *
  34. * @package Crypt_Xtea_Test
  35. * @access public
  36. *
  37. * @version $Revision: 1.12 $
  38. * @since 2002/Aug/28
  39. * @author Jeroen Derks <jeroen@derks.it>
  40. */
  41. class Crypt_XteaTest extends PHPUnit_TestCase
  42. {
  43. var $obj;
  44. var $data;
  45. var $key;
  46.  
  47. function Crypt_XteaTest($method) {
  48. global $profiling;
  49.  
  50. $this->profiling = $profiling;
  51. $this->PHPUnit_TestCase($method);
  52. }
  53.  
  54. function setUp() {
  55. $this->obj = new Crypt_Xtea();
  56. $this->key = '0123456789abcdeffedcba9876543210';
  57.  
  58. if (!$this->profiling) $this->startTimer('data');
  59. //$this->data = '1'; return;
  60. //$this->data = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return;
  61. $this->data = '';
  62. for ($i = 0; $i < 256; ++$i) {
  63. $this->data .= chr($i & 0xff);
  64. }
  65. if (!$this->profiling) $this->endTimer('data');
  66. }
  67.  
  68. function testIter() {
  69. $this->obj->setIter(36);
  70. $this->assertEquals(36, $this->obj->getIter());
  71. }
  72.  
  73. function testCrypt() {
  74. $start = IsSet($this->profiling) && $this->profiling ? strlen($this->data) - 3 : 0;
  75. if (0 == $start) {
  76. $walker = '-\|/';
  77. echo "Testing... ";
  78. flush();
  79. }
  80.  
  81. for ($i = $start; $i < strlen($this->data); ++$i)
  82. {
  83. if (0 == $start) {
  84. echo $walker{$i % 4} . sprintf(" %4u", $i) . str_repeat("", 6);
  85. flush();
  86. }
  87.  
  88. if (!$this->profiling) $this->startTimer('data');
  89. $data = substr($this->data, 0, $i);
  90. if (!$this->profiling) $this->endTimer('data');
  91.  
  92. if (!$this->profiling) $this->startTimer('encrypt');
  93. $encrypted = $this->obj->encrypt($data, $this->key);
  94. if (!$this->profiling) $this->endTimer('encrypt');
  95. if (!$this->profiling) $this->startTimer('decrypt');
  96. $decrypted = $this->obj->decrypt($encrypted, $this->key);
  97. if (!$this->profiling) $this->endTimer('decrypt');
  98.  
  99. if (!$this->profiling) $this->startTimer('assert');
  100. $this->assertEquals(strlen($data), strlen($decrypted));
  101. $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
  102. if (!$this->profiling) $this->endTimer('assert');
  103. }
  104.  
  105. if (0 == $start) {
  106. echo " ";
  107. flush();
  108. }
  109. }
  110.  
  111. function _testHuge() {
  112. set_time_limit(99999);
  113.  
  114. if (!$this->profiling) $this->startTimer('data');
  115. $data = '';
  116. for($i = 0; $i < 1024 * 1024; ++$i)
  117. $data .= chr($i & 0xff);
  118. if (!$this->profiling) $this->endTimer('data');
  119.  
  120. if (!$this->profiling) $this->startTimer('encrypt');
  121. $encrypted = $this->obj->encrypt($data, $this->key);
  122. if (!$this->profiling) $this->endTimer('encrypt');
  123. if (!$this->profiling) $this->startTimer('decrypt');
  124. $decrypted = $this->obj->decrypt($encrypted, $this->key);
  125. if (!$this->profiling) $this->endTimer('decrypt');
  126.  
  127. if (!$this->profiling) $this->startTimer('assert');
  128. $this->assertEquals(strlen($data), strlen($decrypted));
  129. $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
  130. if (!$this->profiling) $this->endTimer('assert');
  131. }
  132.  
  133. function testCipher() {
  134. if (!$this->profiling) $this->startTimer('data');
  135. $v = array(0x1, 0x61000000);
  136. $v = array(0x12345678, 0xffffffff);
  137. $w = array(0, 0);
  138. $k = array(0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef);
  139. if (!$this->profiling) $this->endTimer('data');
  140.  
  141. printf("original:\n");
  142. printf("v[0] = %10lu (0x%8lx)\n", $v[0], $v[0]);
  143. printf("v[1] = %10lu (0x%8lx)\n", $v[1], $v[1]);
  144.  
  145. if (!$this->profiling) $this->startTimer('encipher');
  146. $this->obj->_encipherLong($v[0], $v[1], $w, $k);
  147. if (!$this->profiling) $this->endTimer('encipher');
  148.  
  149. printf("encrypted:\n");
  150. printf("w[0] = %10lu (0x%8lx)\n", $w[0], $w[0]);
  151. printf("w[1] = %10lu (0x%8lx)\n", $w[1], $w[1]);
  152.  
  153. if (!$this->profiling) $this->startTimer('decipher');
  154. $this->obj->_decipherLong($w[0], $w[1], $r, $k);
  155. if (!$this->profiling) $this->endTimer('decipher');
  156.  
  157. printf("decrypted:\n");
  158. printf("v[0] = %10lu (0x%8lx)\n", $r[0], $r[0]);
  159. printf("v[1] = %10lu (0x%8lx)\n", $r[1], $r[1]);
  160.  
  161. if (!$this->profiling) $this->startTimer('assert');
  162. $this->assertEquals((int) $v[0], $r[0], sprintf("$v[0] (%lu = 0x%lx) != $r[0] (%lu = 0x%lx)", $v[0], $v[0], $r[0], $r[0]));
  163. $this->assertEquals((int) $v[1], $r[1], sprintf("$v[1] (%lu = 0x%lx) != $r[1] (%lu = 0x%lx)", $v[1], $v[1], $r[1], $r[1]));
  164. if (!$this->profiling) $this->endTimer('assert');
  165. }
  166.  
  167. function testShift() {
  168. $x = -12345678;
  169. $n = 2;
  170. $y = $this->obj->_rshift($x, $n);
  171. $z = $x << $n;
  172. $this->assertEquals((int) 0x3fd0e7ac, $y, "$x >> $n");
  173. $this->assertEquals((int) 0xfd0e7ac8, $z, "$x << $n");
  174.  
  175. $x = 0xffffffff;
  176. $n = 5;
  177. $y = $this->obj->_rshift($x, $n);
  178. $z = $x << $n;
  179. $this->assertEquals((int) 0x7ffffff, $y, "$x >> $n");
  180. $this->assertEquals((int) 0xffffffe0, $z, "$x << $n");
  181.  
  182. $x = 0x90000000;
  183. $n = 2;
  184. $y = $this->obj->_rshift($x, $n);
  185. $z = $x << $n;
  186. $this->assertEquals((int) 0x24000000, $y, "$x >> $n");
  187. $this->assertEquals((int) 0x40000000, $z, "$x << $n");
  188. }
  189.  
  190. function testAdd() {
  191. $result = $this->obj->_add(-0x12345678, 0xfffffffe, 0x80000000, 0xfedcba98, 0xabcdef01);
  192. $this->assertEquals((int) 0x1876531f, $result, '_add(-0x12345678, 0xfffffffe, 0x80000000, 0xfedcba98, 0xabcdef01)');
  193. }
  194.  
  195. function tearDown() {
  196. $this->obj = NULL;
  197. }
  198.  
  199. function startTimer($label) {
  200. $time0 = strtok(microtime(), ' ');
  201. $time1 = strtok(' ');
  202.  
  203. $timer =& $this->_getTimer();
  204. $timer[$label][] = (string) $time1 . substr($time0, 1);
  205. }
  206.  
  207. function endTimer($label) {
  208. $time0 = strtok(microtime(), ' ');
  209. $time1 = strtok(' ');
  210.  
  211. $timer =& $this->_getTimer();
  212. $timer[$label][] = (string) $time1 . substr($time0, 1);
  213. }
  214.  
  215. /**
  216. * @static
  217. */
  218. function &_getTimer() {
  219. static $timing = NULL;
  220.  
  221. if (!IsSet($timing))
  222. {
  223. $time0 = strtok(microtime(), ' ');
  224. $time1 = strtok(' ');
  225.  
  226. // start _global
  227. $timing = array('_global' => array((string) $time1 . substr($time0, 1)));
  228. }
  229.  
  230. return $timing;
  231. }
  232.  
  233. /**
  234. * @static
  235. */
  236. function getTimings() {
  237. $time0 = strtok(microtime(), ' ');
  238. $time1 = strtok(' ');
  239.  
  240. $timer =& Crypt_XteaTest::_getTimer();
  241. $timer['_global'][1] = (string) $time1 . substr($time0, 1);
  242. $labels = array_keys($timer);
  243. $results = array();
  244.  
  245. // calculate times and calls
  246. sort($labels);
  247. foreach ($labels as $label) {
  248. $results[$label]['time'] = 0.0;
  249. $results[$label]['calls'] = 0;
  250.  
  251. $n = count($timer[$label]);
  252. for ($i = 0; $i < $n; $i += 2) {
  253. $results[$label]['time'] += (float) $timer[$label][$i + 1] - $timer[$label][$i];
  254. ++$results[$label]['calls'];
  255. }
  256. }
  257.  
  258. // calculate percentages
  259. foreach ($labels as $label) {
  260. $results[$label]['perc'] = ( $results[$label]['time'] * 100.0 ) / $results['_global']['time'];
  261. }
  262.  
  263. // output results
  264. echo "Timing results:\n" .
  265. sprintf("%-20s %13s %8s %10s%%\n", 'Label', 'time', '#calls', 'perc') .
  266. str_repeat('-', 57) . "\n";
  267. foreach ($labels as $label) {
  268. printf("%-20s %13s %8lu %10s%%\n",
  269. $label,
  270. sprintf("%.8f", $results[$label]['time']),
  271. $results[$label]['calls'],
  272. sprintf("%.6f", $results[$label]['perc']));
  273. }
  274. }
  275. }
  276.  
  277. ?>

Documentation generated on Thu, 25 May 2006 15:31:28 +0200 by phpDocumentor 1.3.0RC3