Crypt_Xtea_Test
[ class tree: Crypt_Xtea_Test ] [ index: Crypt_Xtea_Test ] [ all elements ]

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

Documentation generated on Mon, 11 Mar 2019 15:17:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.