Source for file Xtea2Test.php

Documentation is available at Xtea2Test.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 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: Xtea2Test.php,v 1.2 2004/09/08 19:21:08 jeroend Exp $
  21.  
  22.  
  23.  
  24. /** Xtea2 class */
  25. 'Crypt/Xtea2.php';
  26. /** PHPUnit class */
  27. 'PHPUnit.php';
  28.  
  29.  
  30. /**
  31. * Tester class for Xtea2 class.
  32. *
  33. * @package Crypt_Xtea2_Test
  34. * @access public
  35. *
  36. * @version $Revision: 1.2 $
  37. * @since 2004/Sep/06
  38. * @author Jeroen Derks <jeroen@derks.it>
  39. */
  40. class Crypt_Xtea2Test extends PHPUnit_TestCase
  41. {
  42. var $obj;
  43. var $data;
  44. var $key;
  45.  
  46. function Crypt_Xtea2Test($method) {
  47. global $profiling;
  48.  
  49. $this->profiling = $profiling;
  50. $this->PHPUnit_TestCase($method);
  51. }
  52.  
  53. function setUp() {
  54. $this->obj = new Crypt_Xtea2();
  55. $this->key = '0123456789abcdeffedcba9876543210';
  56.  
  57. if (!$this->profiling) $this->startTimer('data');
  58. //$this->data = '1'; return;
  59. //$this->data = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return;
  60. $this->data = '';
  61. for ($i = 0; $i < 256; ++$i) {
  62. $this->data .= chr($i & 0xff);
  63. }
  64. if (!$this->profiling) $this->endTimer('data');
  65. }
  66.  
  67. function testIter() {
  68. $this->obj->setIter(36);
  69. $this->assertEquals(36, $this->obj->getIter());
  70. }
  71.  
  72. function testCrypt() {
  73. $start = IsSet($this->profiling) && $this->profiling ? strlen($this->data) - 3 : 0;
  74. if (0 == $start) {
  75. $walker = '-\|/';
  76. echo "Testing... ";
  77. flush();
  78. }
  79.  
  80. for ($i = $start; $i < strlen($this->data); ++$i)
  81. {
  82. if (0 == $start) {
  83. echo $walker{$i % 4} . sprintf(" %4u", $i) . str_repeat("", 6);
  84. flush();
  85. }
  86.  
  87. if (!$this->profiling) $this->startTimer('data');
  88. $data = substr($this->data, 0, $i);
  89. if (!$this->profiling) $this->endTimer('data');
  90.  
  91. if (!$this->profiling) $this->startTimer('encrypt');
  92. $encrypted = $this->obj->encrypt($data, $this->key);
  93. if (!$this->profiling) $this->endTimer('encrypt');
  94. if (!$this->profiling) $this->startTimer('decrypt');
  95. $decrypted = $this->obj->decrypt($encrypted, $this->key);
  96. if (!$this->profiling) $this->endTimer('decrypt');
  97.  
  98. if (!$this->profiling) $this->startTimer('assert');
  99. $this->assertEquals(strlen($data), strlen($decrypted));
  100. $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
  101. if (!$this->profiling) $this->endTimer('assert');
  102. }
  103.  
  104. if (0 == $start) {
  105. echo " ";
  106. flush();
  107. }
  108. }
  109.  
  110. function _testHuge() {
  111. set_time_limit(99999);
  112.  
  113. if (!$this->profiling) $this->startTimer('data');
  114. $data = '';
  115. for($i = 0; $i < 1024 * 1024; ++$i)
  116. $data .= chr($i & 0xff);
  117. if (!$this->profiling) $this->endTimer('data');
  118.  
  119. if (!$this->profiling) $this->startTimer('encrypt');
  120. $encrypted = $this->obj->encrypt($data, $this->key);
  121. if (!$this->profiling) $this->endTimer('encrypt');
  122. if (!$this->profiling) $this->startTimer('decrypt');
  123. $decrypted = $this->obj->decrypt($encrypted, $this->key);
  124. if (!$this->profiling) $this->endTimer('decrypt');
  125.  
  126. if (!$this->profiling) $this->startTimer('assert');
  127. $this->assertEquals(strlen($data), strlen($decrypted));
  128. $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
  129. if (!$this->profiling) $this->endTimer('assert');
  130. }
  131.  
  132. function tearDown() {
  133. $this->obj = NULL;
  134. }
  135.  
  136. function startTimer($label) {
  137. $time0 = strtok(microtime(), ' ');
  138. $time1 = strtok(' ');
  139.  
  140. $timer =& $this->_getTimer();
  141. $timer[$label][] = (string) $time1 . substr($time0, 1);
  142. }
  143.  
  144. function endTimer($label) {
  145. $time0 = strtok(microtime(), ' ');
  146. $time1 = strtok(' ');
  147.  
  148. $timer =& $this->_getTimer();
  149. $timer[$label][] = (string) $time1 . substr($time0, 1);
  150. }
  151.  
  152. /**
  153. * @static
  154. */
  155. function &_getTimer() {
  156. static $timing = NULL;
  157.  
  158. if (!IsSet($timing))
  159. {
  160. $time0 = strtok(microtime(), ' ');
  161. $time1 = strtok(' ');
  162.  
  163. // start _global
  164. $timing = array('_global' => array((string) $time1 . substr($time0, 1)));
  165. }
  166.  
  167. return $timing;
  168. }
  169.  
  170. /**
  171. * @static
  172. */
  173. function getTimings() {
  174. $time0 = strtok(microtime(), ' ');
  175. $time1 = strtok(' ');
  176.  
  177. $timer =& Crypt_Xtea2Test::_getTimer();
  178. $timer['_global'][1] = (string) $time1 . substr($time0, 1);
  179. $labels = array_keys($timer);
  180. $results = array();
  181.  
  182. // calculate times and calls
  183. sort($labels);
  184. foreach ($labels as $label) {
  185. $results[$label]['time'] = 0.0;
  186. $results[$label]['calls'] = 0;
  187.  
  188. $n = count($timer[$label]);
  189. for ($i = 0; $i < $n; $i += 2) {
  190. $results[$label]['time'] += (float) $timer[$label][$i + 1] - $timer[$label][$i];
  191. ++$results[$label]['calls'];
  192. }
  193. }
  194.  
  195. // calculate percentages
  196. foreach ($labels as $label) {
  197. $results[$label]['perc'] = ( $results[$label]['time'] * 100.0 ) / $results['_global']['time'];
  198. }
  199.  
  200. // output results
  201. echo "Timing results:\n" .
  202. sprintf("%-20s %13s %8s %10s%%\n", 'Label', 'time', '#calls', 'perc') .
  203. str_repeat('-', 57) . "\n";
  204. foreach ($labels as $label) {
  205. printf("%-20s %13s %8lu %10s%%\n",
  206. $label,
  207. sprintf("%.8f", $results[$label]['time']),
  208. $results[$label]['calls'],
  209. sprintf("%.6f", $results[$label]['perc']));
  210. }
  211. }
  212. }
  213.  
  214. ?>

Documentation generated on Wed, 8 Sep 2004 21:20:47 +0200 by phpDocumentor 1.3.0RC3