Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 277 lines (231 sloc) 9.763 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
//
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jeroen Derks <jeroen@derks.it> |
// +----------------------------------------------------------------------+
//
// $Id$

/** Xtea class */
require_once 'Crypt/Xtea.php';
/** PHPUnit class */
require_once 'PHPUnit.php';


/**
* Tester class for Xtea class.
* Tester class for Xtea class.
*
* @package Crypt_Xtea_Test
* @access public
*
* @version $Revision$
* @since 2002/Aug/28
* @author Jeroen Derks <jeroen@derks.it>
*/
class Crypt_XteaTest extends PHPUnit_TestCase
{
    var $obj;
    var $data;
    var $key;

    function Crypt_XteaTest($method) {
        global $profiling;

        $this->profiling = $profiling;
        $this->PHPUnit_TestCase($method);
    }

    function setUp() {
        $this->obj = new Crypt_Xtea();
        $this->key = '0123456789abcdeffedcba9876543210';

        if (!$this->profiling) $this->startTimer('data');
        //$this->data = '1'; return;
        //$this->data = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return;
        $this->data = '';
        for ($i = 0; $i < 256; ++$i) {
            $this->data .= chr($i & 0xff);
        }
        if (!$this->profiling) $this->endTimer('data');
    }

    function testIter() {
        $this->obj->setIter(36);
        $this->assertEquals(36, $this->obj->getIter());
    }

    function testCrypt() {
        $start = IsSet($this->profiling) && $this->profiling ? strlen($this->data) - 3 : 0;
        if (0 == $start) {
            $walker = '-\|/';
            echo "Testing... ";
            flush();
        }

        for ($i = $start; $i < strlen($this->data); ++$i)
        {
            if (0 == $start) {
                echo $walker{$i % 4} . sprintf(" %4u", $i) . str_repeat("", 6);
                flush();
            }

            if (!$this->profiling) $this->startTimer('data');
            $data = substr($this->data, 0, $i);
            if (!$this->profiling) $this->endTimer('data');

            if (!$this->profiling) $this->startTimer('encrypt');
            $encrypted = $this->obj->encrypt($data, $this->key);
            if (!$this->profiling) $this->endTimer('encrypt');
            if (!$this->profiling) $this->startTimer('decrypt');
            $decrypted = $this->obj->decrypt($encrypted, $this->key);
            if (!$this->profiling) $this->endTimer('decrypt');

            if (!$this->profiling) $this->startTimer('assert');
            $this->assertEquals(strlen($data), strlen($decrypted));
            $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
            if (!$this->profiling) $this->endTimer('assert');
        }

        if (0 == $start) {
            echo "
";
            flush();
        }
    }

    function _testHuge() {
        set_time_limit(99999);

        if (!$this->profiling) $this->startTimer('data');
        $data = '';
        for($i = 0; $i < 1024 * 1024; ++$i)
            $data .= chr($i & 0xff);
        if (!$this->profiling) $this->endTimer('data');

        if (!$this->profiling) $this->startTimer('encrypt');
        $encrypted = $this->obj->encrypt($data, $this->key);
        if (!$this->profiling) $this->endTimer('encrypt');
        if (!$this->profiling) $this->startTimer('decrypt');
        $decrypted = $this->obj->decrypt($encrypted, $this->key);
        if (!$this->profiling) $this->endTimer('decrypt');

        if (!$this->profiling) $this->startTimer('assert');
        $this->assertEquals(strlen($data), strlen($decrypted));
        $this->assertEquals($data, $decrypted, "run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
        if (!$this->profiling) $this->endTimer('assert');
    }

    function testCipher() {
        if (!$this->profiling) $this->startTimer('data');
        $v = array(0x1, 0x61000000);
        $v = array(0x12345678, 0xffffffff);
        $w = array(0, 0);
        $k = array(0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef);
        if (!$this->profiling) $this->endTimer('data');

        printf("original:\n");
        printf("v[0] = %10lu (0x%8lx)\n", $v[0], $v[0]);
        printf("v[1] = %10lu (0x%8lx)\n", $v[1], $v[1]);

        if (!$this->profiling) $this->startTimer('encipher');
        $this->obj->_encipherLong($v[0], $v[1], $w, $k);
        if (!$this->profiling) $this->endTimer('encipher');

        printf("encrypted:\n");
        printf("w[0] = %10lu (0x%8lx)\n", $w[0], $w[0]);
        printf("w[1] = %10lu (0x%8lx)\n", $w[1], $w[1]);

        if (!$this->profiling) $this->startTimer('decipher');
        $this->obj->_decipherLong($w[0], $w[1], $r, $k);
        if (!$this->profiling) $this->endTimer('decipher');

        printf("decrypted:\n");
        printf("v[0] = %10lu (0x%8lx)\n", $r[0], $r[0]);
        printf("v[1] = %10lu (0x%8lx)\n", $r[1], $r[1]);

        if (!$this->profiling) $this->startTimer('assert');
        $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]));
        $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]));
        if (!$this->profiling) $this->endTimer('assert');
    }

    function testShift() {
        $x = -12345678;
        $n = 2;
        $y = $this->obj->_rshift($x, $n);
        $z = $x << $n;
        $this->assertEquals((int) 0x3fd0e7ac, $y, "$x >> $n");
        $this->assertEquals((int) 0xfd0e7ac8, $z, "$x << $n");

        $x = 0xffffffff;
        $n = 5;
        $y = $this->obj->_rshift($x, $n);
        $z = $x << $n;
        $this->assertEquals((int) 0x7ffffff, $y, "$x >> $n");
        $this->assertEquals((int) 0xffffffe0, $z, "$x << $n");

        $x = 0x90000000;
        $n = 2;
        $y = $this->obj->_rshift($x, $n);
        $z = $x << $n;
        $this->assertEquals((int) 0x24000000, $y, "$x >> $n");
        $this->assertEquals((int) 0x40000000, $z, "$x << $n");
    }

    function testAdd() {
        $result = $this->obj->_add(-0x12345678, 0xfffffffe, 0x80000000, 0xfedcba98, 0xabcdef01);
        $this->assertEquals((int) 0x1876531f, $result, '_add(-0x12345678, 0xfffffffe, 0x80000000, 0xfedcba98, 0xabcdef01)');
    }

    function tearDown() {
        $this->obj = NULL;
    }

    function startTimer($label) {
        $time0 = strtok(microtime(), ' ');
        $time1 = strtok(' ');

        $timer =& $this->_getTimer();
        $timer[$label][] = (string) $time1 . substr($time0, 1);
    }

    function endTimer($label) {
        $time0 = strtok(microtime(), ' ');
        $time1 = strtok(' ');

        $timer =& $this->_getTimer();
        $timer[$label][] = (string) $time1 . substr($time0, 1);
    }

    /**
* @static
*/
    function &_getTimer() {
        static $timing = NULL;

        if (!IsSet($timing))
        {
            $time0 = strtok(microtime(), ' ');
            $time1 = strtok(' ');

            // start _global
            $timing = array('_global' => array((string) $time1 . substr($time0, 1)));
        }

        return $timing;
    }

    /**
* @static
*/
    function getTimings() {
        $time0 = strtok(microtime(), ' ');
        $time1 = strtok(' ');

        $timer =& Crypt_XteaTest::_getTimer();
        $timer['_global'][1] = (string) $time1 . substr($time0, 1);
        $labels = array_keys($timer);
        $results = array();

        // calculate times and calls
        sort($labels);
        foreach ($labels as $label) {
            $results[$label]['time'] = 0.0;
            $results[$label]['calls'] = 0;

            $n = count($timer[$label]);
            for ($i = 0; $i < $n; $i += 2) {
                $results[$label]['time'] += (float) $timer[$label][$i + 1] - $timer[$label][$i];
                ++$results[$label]['calls'];
            }
        }

        // calculate percentages
        foreach ($labels as $label) {
            $results[$label]['perc'] = ( $results[$label]['time'] * 100.0 ) / $results['_global']['time'];
        }

        // output results
        echo "Timing results:\n" .
            sprintf("%-20s %13s %8s %10s%%\n", 'Label', 'time', '#calls', 'perc') .
            str_repeat('-', 57) . "\n";
        foreach ($labels as $label) {
            printf("%-20s %13s %8lu %10s%%\n",
                    $label,
                    sprintf("%.8f", $results[$label]['time']),
                    $results[$label]['calls'],
                    sprintf("%.6f", $results[$label]['perc']));
        }
    }
}

?>
Something went wrong with that request. Please try again.