diff options
author | Josh Hoyt <josh@janrain.com> | 2005-12-27 19:53:28 +0000 |
---|---|---|
committer | Josh Hoyt <josh@janrain.com> | 2005-12-27 19:53:28 +0000 |
commit | bcc97931d9b280e7b1a934ca5042bbd173695fad (patch) | |
tree | 970ee7eec3843bc3afcf186ad3be150a8f1086be | |
parent | f0077bb7e297e5d6a6d40a1cb81f68704ad73d17 (diff) | |
download | php-openid-bcc97931d9b280e7b1a934ca5042bbd173695fad.zip php-openid-bcc97931d9b280e7b1a934ca5042bbd173695fad.tar.gz php-openid-bcc97931d9b280e7b1a934ca5042bbd173695fad.tar.bz2 |
[project @ Untabify]
-rw-r--r-- | Net/OpenID/DiffieHellman.php | 192 | ||||
-rw-r--r-- | Net/OpenID/HMACSHA1.php | 50 | ||||
-rw-r--r-- | Net/OpenID/KVForm.php | 118 | ||||
-rw-r--r-- | Tests/Net/OpenID/CryptUtil.php | 34 | ||||
-rw-r--r-- | Tests/Net/OpenID/KVForm.php | 408 | ||||
-rw-r--r-- | Tests/TestDriver.php | 42 | ||||
-rw-r--r-- | texttest.php | 102 |
7 files changed, 473 insertions, 473 deletions
diff --git a/Net/OpenID/DiffieHellman.php b/Net/OpenID/DiffieHellman.php index 47b7cca..dae59ee 100644 --- a/Net/OpenID/DiffieHellman.php +++ b/Net/OpenID/DiffieHellman.php @@ -1,120 +1,120 @@ <?php if (extension_loaded('gmp') || @dl('gmp.' . PHP_SHLIB_SUFFIX) || - @dl('php_gmp.' . PHP_SHLIB_SUFFIX)) { + @dl('php_gmp.' . PHP_SHLIB_SUFFIX)) { - define('Net_OpenID_math_type', 'gmp'); + define('Net_OpenID_math_type', 'gmp'); - // XXX: untested! - class Net_OpenID_DiffieHellman { - var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; + // XXX: untested! + class Net_OpenID_DiffieHellman { + var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; - var $DEFAULT_GEN = '2'; + var $DEFAULT_GEN = '2'; - var $mod; - var $gen; - var $private; + var $mod; + var $gen; + var $private; - function generateRandom() { - // XXX: not cryptographically secure (potentially predictable) + function generateRandom() { + // XXX: not cryptographically secure (potentially predictable) $limb_cnt = 31; do { $rdm = gmp_random($limb_cnt--); } while (gmp_cmp( $minval, $rdm) > 0); return $rdm; - } + } - function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { - if ($mod === NULL) { - $this->mod = gmp_init($this->DEFAULT_MOD, 10); - } else { - $this->mod = $mod; - } + function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { + if ($mod === NULL) { + $this->mod = gmp_init($this->DEFAULT_MOD, 10); + } else { + $this->mod = $mod; + } - if ($gen === NULL) { - $this->gen = gmp_init($this->DEFAULT_GEN, 10); - } else { - $this->gen = $gen; - } + if ($gen === NULL) { + $this->gen = gmp_init($this->DEFAULT_GEN, 10); + } else { + $this->gen = $gen; + } - $this->private = - $private === NULL ? $this->generateRandom() : $private; + $this->private = + $private === NULL ? $this->generateRandom() : $private; - $this->public = user_error("not implemented", E_USER_ERROR); - } + $this->public = user_error("not implemented", E_USER_ERROR); + } - function createKeyExchange( ) { - return Net_OpenID_BigInt::powm( $this->g, $this->x, $this->p); - } + function createKeyExchange( ) { + return Net_OpenID_BigInt::powm( $this->g, $this->x, $this->p); + } - function decryptKeyExchange( $keyEx ) { - return Net_OpenID_BigInt::powm( $keyEx, $this->x, $this->p ); - } - } + function decryptKeyExchange( $keyEx ) { + return Net_OpenID_BigInt::powm( $keyEx, $this->x, $this->p ); + } + } } elseif (extension_loaded('bcmath') || @dl('bcmath.' . PHP_SHLIB_SUFFIX) || - @dl('php_bcmath.' . PHP_SHLIB_SUFFIX)) { - - define('Net_OpenID_math_type', 'bcmath'); - - if (!function_exists('bcpowmod')) { - // PHP4 does not expose bcpowmod, so we have to implement it here - /** - * (base ^ exponent) % modulus - */ - function bcpowmod($base, $exponent, $modulus) { - $square = bcmod($base, $modulus); - $result = '1'; - while( bccomp( $exponent, 0 ) > 0 ) { - if (bcmod($exponent, 2)) { - // result = (result * square) % modulus - $result = bcmod(bcmul($result, $square), $modulus); - } - $square = bcmod(bcmul($square, $square), $modulus); - $exponent = bcdiv($exponent, 2); - } - return $result; - } - } - - class Net_OpenID_DiffieHellman { - var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; - - var $DEFAULT_GEN = '2'; - - var $mod; - var $gen; - var $private; - var $public; - - function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { - $this->mod = $mod === NULL ? $this->DEFAULT_MOD : $mod; - $this->gen = $gen === NULL ? $this->DEFAULT_GEN : $gen; - $this->private = - $private === NULL ? $this->generateRandom() : $private; - - $this->public = bcpowmod($this->gen, $this->private, $this->mod); - } - - function generateRandom() { - // XXX: not cryptographically secure (predictable!!!) - // XXX: also, way too small (usually) - // FIXME - return mt_rand(1, $this->mod); - } - - function getSharedSecret($composite) { - return bcpowmod($composite, $this->private, $this->mod); - } - - function getPublicKey() { - return $this->public; - } - - } + @dl('php_bcmath.' . PHP_SHLIB_SUFFIX)) { + + define('Net_OpenID_math_type', 'bcmath'); + + if (!function_exists('bcpowmod')) { + // PHP4 does not expose bcpowmod, so we have to implement it here + /** + * (base ^ exponent) % modulus + */ + function bcpowmod($base, $exponent, $modulus) { + $square = bcmod($base, $modulus); + $result = '1'; + while( bccomp( $exponent, 0 ) > 0 ) { + if (bcmod($exponent, 2)) { + // result = (result * square) % modulus + $result = bcmod(bcmul($result, $square), $modulus); + } + $square = bcmod(bcmul($square, $square), $modulus); + $exponent = bcdiv($exponent, 2); + } + return $result; + } + } + + class Net_OpenID_DiffieHellman { + var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; + + var $DEFAULT_GEN = '2'; + + var $mod; + var $gen; + var $private; + var $public; + + function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { + $this->mod = $mod === NULL ? $this->DEFAULT_MOD : $mod; + $this->gen = $gen === NULL ? $this->DEFAULT_GEN : $gen; + $this->private = + $private === NULL ? $this->generateRandom() : $private; + + $this->public = bcpowmod($this->gen, $this->private, $this->mod); + } + + function generateRandom() { + // XXX: not cryptographically secure (predictable!!!) + // XXX: also, way too small (usually) + // FIXME + return mt_rand(1, $this->mod); + } + + function getSharedSecret($composite) { + return bcpowmod($composite, $this->private, $this->mod); + } + + function getPublicKey() { + return $this->public; + } + + } } else { - trigger_error("No usable big int library present (gmp or bcmath). " . - "Only dumb mode OpenID is available.", - E_USER_NOTICE); + trigger_error("No usable big int library present (gmp or bcmath). " . + "Only dumb mode OpenID is available.", + E_USER_NOTICE); } diff --git a/Net/OpenID/HMACSHA1.php b/Net/OpenID/HMACSHA1.php index 781e0cd..feb04ef 100644 --- a/Net/OpenID/HMACSHA1.php +++ b/Net/OpenID/HMACSHA1.php @@ -9,33 +9,33 @@ if (FALSE && function_exists('mhash')) { } else { if (!function_exists('sha1')) { - // XXX: include the SHA1 code from Dan Libby's OpenID library - trigger_error('No SHA1 function found', E_USER_ERROR); - } else { - function sha1_raw($text) { - $hex = sha1($text); - $raw = ''; - for ($i = 0; $i < 40; $i += 2) { - $hexcode = substr($hex, $i, 2); - $charcode = (int)base_convert($hexcode, 16, 10); - $raw .= chr($charcode); - } - return $raw; - } - } + // XXX: include the SHA1 code from Dan Libby's OpenID library + trigger_error('No SHA1 function found', E_USER_ERROR); + } else { + function sha1_raw($text) { + $hex = sha1($text); + $raw = ''; + for ($i = 0; $i < 40; $i += 2) { + $hexcode = substr($hex, $i, 2); + $charcode = (int)base_convert($hexcode, 16, 10); + $raw .= chr($charcode); + } + return $raw; + } + } - function Net_OpenID_HMACSHA1($key, $text) { - if (strlen($key) > SHA1_BLOCKSIZE) { - $key = sha1_raw($key, TRUE); - } + function Net_OpenID_HMACSHA1($key, $text) { + if (strlen($key) > SHA1_BLOCKSIZE) { + $key = sha1_raw($key, TRUE); + } - $key = str_pad($key, SHA1_BLOCKSIZE, chr(0x00)); - $ipad = str_repeat(chr(0x36), SHA1_BLOCKSIZE); - $opad = str_repeat(chr(0x5c), SHA1_BLOCKSIZE); - $hash1 = sha1_raw(($key ^ $ipad) . $text, TRUE); - $hmac = sha1_raw(($key ^ $opad) . $hash1, TRUE); - return $hmac; - } + $key = str_pad($key, SHA1_BLOCKSIZE, chr(0x00)); + $ipad = str_repeat(chr(0x36), SHA1_BLOCKSIZE); + $opad = str_repeat(chr(0x5c), SHA1_BLOCKSIZE); + $hash1 = sha1_raw(($key ^ $ipad) . $text, TRUE); + $hmac = sha1_raw(($key ^ $opad) . $hash1, TRUE); + return $hmac; + } } ?>
\ No newline at end of file diff --git a/Net/OpenID/KVForm.php b/Net/OpenID/KVForm.php index 764ad77..ab06d32 100644 --- a/Net/OpenID/KVForm.php +++ b/Net/OpenID/KVForm.php @@ -1,74 +1,74 @@ <?php class Net_OpenID_KVForm { - function arrayToKV($values) { - $serialized = ''; - foreach ($values as $key => $value) { - if (strpos($key, ':') !== FALSE) { - trigger_error('":" in key:' . addslashes($key), - E_USER_WARNING); - return NULL; - } + function arrayToKV($values) { + $serialized = ''; + foreach ($values as $key => $value) { + if (strpos($key, ':') !== FALSE) { + trigger_error('":" in key:' . addslashes($key), + E_USER_WARNING); + return NULL; + } - if (strpos($key, "\n") !== FALSE) { - trigger_error('"\n" in key:' . addslashes($key), - E_USER_WARNING); - return NULL; - } + if (strpos($key, "\n") !== FALSE) { + trigger_error('"\n" in key:' . addslashes($key), + E_USER_WARNING); + return NULL; + } - if (strpos($value, "\n") !== FALSE) { - trigger_error('"\n" in value:' . addslashes($key), - E_USER_WARNING); - return NULL; - } - $serialized .= "$key:$value\n"; - } - return $serialized; - } + if (strpos($value, "\n") !== FALSE) { + trigger_error('"\n" in value:' . addslashes($key), + E_USER_WARNING); + return NULL; + } + $serialized .= "$key:$value\n"; + } + return $serialized; + } - function kvToArray($kvs) { - $lines = explode("\n", $kvs); + function kvToArray($kvs) { + $lines = explode("\n", $kvs); - $last = array_pop($lines); - if ($last !== '') { - trigger_error('No newline at end of kv string:' . addslashes($kvs), - E_USER_WARNING); - array_push($lines, $last); - } + $last = array_pop($lines); + if ($last !== '') { + trigger_error('No newline at end of kv string:' . addslashes($kvs), + E_USER_WARNING); + array_push($lines, $last); + } - $values = array(); + $values = array(); - for ($lineno = 0; $lineno < count($lines); $lineno++) { - $line = $lines[$lineno]; - $kv = explode(':', $line, 2); - if (count($kv) != 2) { - $esc = addslashes($line); - trigger_error("No colon on line $lineno: $esc", - E_USER_WARNING); - continue; - } + for ($lineno = 0; $lineno < count($lines); $lineno++) { + $line = $lines[$lineno]; + $kv = explode(':', $line, 2); + if (count($kv) != 2) { + $esc = addslashes($line); + trigger_error("No colon on line $lineno: $esc", + E_USER_WARNING); + continue; + } - $key = $kv[0]; - $tkey = trim($key); - if ($tkey != $key) { - $esc = addslashes($key); - trigger_error("Whitespace in key on line $lineno: '$esc'", - E_USER_WARNING); - } + $key = $kv[0]; + $tkey = trim($key); + if ($tkey != $key) { + $esc = addslashes($key); + trigger_error("Whitespace in key on line $lineno: '$esc'", + E_USER_WARNING); + } - $value = $kv[1]; - $tval = trim($value); - if ($tval != $value) { - $esc = addslashes($value); - trigger_error("Whitespace in value on line $lineno: '$esc'", - E_USER_WARNING); - } + $value = $kv[1]; + $tval = trim($value); + if ($tval != $value) { + $esc = addslashes($value); + trigger_error("Whitespace in value on line $lineno: '$esc'", + E_USER_WARNING); + } - $values[$tkey] = $tval; - } - - return $values; - } + $values[$tkey] = $tval; + } + + return $values; + } } ?>
\ No newline at end of file diff --git a/Tests/Net/OpenID/CryptUtil.php b/Tests/Net/OpenID/CryptUtil.php index 3ad3174..ee1cfe5 100644 --- a/Tests/Net/OpenID/CryptUtil.php +++ b/Tests/Net/OpenID/CryptUtil.php @@ -4,25 +4,25 @@ require_once('PHPUnit.php'); require_once('Net/OpenID/CryptUtil.php'); class Tests_Net_OpenID_CryptUtil extends PHPUnit_TestCase { - function test_length() { - $cases = array(1, 10, 255); - foreach ($cases as $length) { - $data = Net_OpenID_CryptUtil::getBytes($length); - $this->assertEquals(strlen($data), $length); - } - } + function test_length() { + $cases = array(1, 10, 255); + foreach ($cases as $length) { + $data = Net_OpenID_CryptUtil::getBytes($length); + $this->assertEquals(strlen($data), $length); + } + } - function test_different() { - $num_iterations = 100; - $data_length = 20; + function test_different() { + $num_iterations = 100; + $data_length = 20; - $data = Net_OpenID_CryptUtil::getBytes($num_iterations); - for ($i = 0; $i < $num_iterations; $i++) { - $last = $data; - $data = Net_OpenID_CryptUtil::getBytes($num_iterations); - $this->assertFalse($data == $last); - } - } + $data = Net_OpenID_CryptUtil::getBytes($num_iterations); + for ($i = 0; $i < $num_iterations; $i++) { + $last = $data; + $data = Net_OpenID_CryptUtil::getBytes($num_iterations); + $this->assertFalse($data == $last); + } + } } ?>
\ No newline at end of file diff --git a/Tests/Net/OpenID/KVForm.php b/Tests/Net/OpenID/KVForm.php index bc97e13..10ac54b 100644 --- a/Tests/Net/OpenID/KVForm.php +++ b/Tests/Net/OpenID/KVForm.php @@ -8,231 +8,231 @@ $_Tests_Net_OpenID_kverrors = NULL; * Keep a list of the logged errors */ function Tests_Net_OpenID_kvHandleError($errno, $errmsg) { - global $_Tests_Net_OpenID_kverrors; - $_Tests_Net_OpenID_kverrors[] = $errmsg; + global $_Tests_Net_OpenID_kverrors; + $_Tests_Net_OpenID_kverrors[] = $errmsg; } class Tests_Net_OpenID_KVForm_TestCase extends PHPUnit_TestCase { - var $errs; + var $errs; - function runTest() { - // Re-set the number of logged errors - global $_Tests_Net_OpenID_kverrors; - $_Tests_Net_OpenID_kverrors = array(); + function runTest() { + // Re-set the number of logged errors + global $_Tests_Net_OpenID_kverrors; + $_Tests_Net_OpenID_kverrors = array(); - set_error_handler("Tests_Net_OpenID_kvHandleError"); + set_error_handler("Tests_Net_OpenID_kvHandleError"); - $this->_runTest(); + $this->_runTest(); - // Check to make sure we have the expected number of logged errors - //$this->assertEquals($this->errs, count($_Tests_Net_OpenID_kverrors)); + // Check to make sure we have the expected number of logged errors + //$this->assertEquals($this->errs, count($_Tests_Net_OpenID_kverrors)); - restore_error_handler(); - } + restore_error_handler(); + } - function _runTest() { - trigger_error('Must be overridden', E_USER_ERROR); - } + function _runTest() { + trigger_error('Must be overridden', E_USER_ERROR); + } } class Tests_Net_OpenID_KVForm_TestCase_Parse extends Tests_Net_OpenID_KVForm_TestCase { - function Tests_Net_OpenID_KVForm_TestCase_Parse( - $arr, $str, $lossy, $errs) { - - $this->arr = $arr; - $this->str = $str; - $this->lossy = $lossy; - $this->errs = $errs; - } - - function _runTest() { - // Do one parse, after which arrayToKV and kvToArray should be - // inverses. - $parsed1 = Net_OpenID_KVForm::kvToArray($this->str); - $serial1 = Net_OpenID_KVForm::arrayToKV($this->arr); - - if ($this->lossy == "neither" || $this->lossy == "str") { - $this->assertEquals($this->arr, $parsed1, "str was lossy"); - } - - if ($this->lossy == "neither" || $this->lossy == "arr") { - $this->assertEquals($this->str, $serial1, "array was lossy"); - } - - $parsed2 = Net_OpenID_KVForm::kvToArray($serial1); - $serial2 = Net_OpenID_KVForm::arrayToKV($parsed1); - - // Round-trip both - $parsed3 = Net_OpenID_KVForm::kvToArray($serial2); - $serial3 = Net_OpenID_KVForm::arrayToKV($parsed2); - - $this->assertEquals($serial2, $serial3, "serialized forms differ"); - - // Check to make sure that they're inverses. - $this->assertEquals($parsed2, $parsed3, "parsed forms differ"); - - } + function Tests_Net_OpenID_KVForm_TestCase_Parse( + $arr, $str, $lossy, $errs) { + + $this->arr = $arr; + $this->str = $str; + $this->lossy = $lossy; + $this->errs = $errs; + } + + function _runTest() { + // Do one parse, after which arrayToKV and kvToArray should be + // inverses. + $parsed1 = Net_OpenID_KVForm::kvToArray($this->str); + $serial1 = Net_OpenID_KVForm::arrayToKV($this->arr); + + if ($this->lossy == "neither" || $this->lossy == "str") { + $this->assertEquals($this->arr, $parsed1, "str was lossy"); + } + + if ($this->lossy == "neither" || $this->lossy == "arr") { + $this->assertEquals($this->str, $serial1, "array was lossy"); + } + + $parsed2 = Net_OpenID_KVForm::kvToArray($serial1); + $serial2 = Net_OpenID_KVForm::arrayToKV($parsed1); + + // Round-trip both + $parsed3 = Net_OpenID_KVForm::kvToArray($serial2); + $serial3 = Net_OpenID_KVForm::arrayToKV($parsed2); + + $this->assertEquals($serial2, $serial3, "serialized forms differ"); + + // Check to make sure that they're inverses. + $this->assertEquals($parsed2, $parsed3, "parsed forms differ"); + + } } class Tests_Net_OpenID_KVForm_TestCase_Null extends Tests_Net_OpenID_KVForm_TestCase { - function Tests_Net_OpenID_KVForm_TestCase_Null($arr, $errs) { - $this->arr = $arr; - $this->errs = $errs; - } - - function _runTest() { - $serialized = Net_OpenID_KVForm::arrayToKV($this->arr); - $this->assertTrue($serialized === NULL, - 'serialization unexpectedly succeeded'); - } + function Tests_Net_OpenID_KVForm_TestCase_Null($arr, $errs) { + $this->arr = $arr; + $this->errs = $errs; + } + + function _runTest() { + $serialized = Net_OpenID_KVForm::arrayToKV($this->arr); + $this->assertTrue($serialized === NULL, + 'serialization unexpectedly succeeded'); + } } class Tests_Net_OpenID_KVForm extends PHPUnit_TestSuite { - function Tests_Net_OpenID_KVForm($name) { - $this->setName($name); - $testdata_list = array( - array("name" => "simple", - "str" => "college:harvey mudd\n", - "arr" => array("college" => "harvey mudd"), - ), - array("name" => "empty", - "str" => "", - "arr" => array(), - ), - array("name" => "empty (just newline)", - "str" => "\n", - "arr" => array(), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "empty (double newline)", - "str" => "\n\n", - "arr" => array(), - "lossy" => "str", - "errors" => 2, - ), - array("name" => "empty (no colon)", - "str" => "East is least\n", - "arr" => array(), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "two keys", - "str" => "city:claremont\nstate:CA\n", - "arr" => array('city' => 'claremont', - 'state' => 'CA'), - ), - array("name" => "real life", - "str" => "is_valid:true\ninvalidate_handle:" . - "{HMAC-SHA1:2398410938412093}\n", - "arr" => array('is_valid' => 'true', - 'invalidate_handle' => - '{HMAC-SHA1:2398410938412093}'), - ), - array("name" => "empty key and value", - "str" => ":\n", - "arr" => array(''=>''), - ), - array("name" => "empty key, not value", - "str" => ":missing key\n", - "arr" => array(''=>'missing key'), - ), - array("name" => "whitespace at front of key", - "str" => " street:foothill blvd\n", - "arr" => array('street'=>'foothill blvd'), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "whitespace at front of value", - "str" => "major: computer science\n", - "arr" => array('major'=>'computer science'), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "whitespace around key and value", - "str" => " dorm : east \n", - "arr" => array('dorm'=>'east'), - "lossy" => "str", - "errors" => 2, - ), - array("name" => "missing trailing newline", - "str" => "e^(i*pi)+1:0", - "arr" => array('e^(i*pi)+1'=>'0'), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "missing trailing newline (two key)", - "str" => "east:west\nnorth:south", - "arr" => array('east'=>'west', - 'north'=>'south'), - "lossy" => "str", - "errors" => 1, - ), - array("name" => "colon in key", - "arr" => array("k:k" => 'v'), - "errors" => 1, - ), - array("name" => "newline in key", - "arr" => array("k\nk" => 'v'), - "errors" => 1, - ), - array("name" => "newline in value", - "arr" => array('k' => "v\nv"), - "errors" => 1, - ), - array("name" => "array whitespace", - "arr" => array(" k " => "v"), - "lossy" => "both", - "str" => " k :v\n", - "errors" => 2, - ), - array("name" => "array ordering 1", - "arr" => array("a" => "x", - "b" => "x", - "c" => "x"), - "str" => "a:x\nb:x\nc:x\n", - ), - array("name" => "array ordering 2", - "arr" => array("a" => "x", - "c" => "x", - "b" => "x"), - "str" => "a:x\nc:x\nb:x\n", - ), - ); - - foreach ($testdata_list as $testdata) { - if (isset($testdata['str'])) { - $str = $testdata['str']; - } else { - $str = NULL; - } - - $arr = $testdata["arr"]; - - if (isset($testdata['errors'])) { - $errs = $testdata["errors"]; - } else { - $errs = 0; - } - - if (is_null($str)) { - $test = new Tests_Net_OpenID_KVForm_TestCase_Null($arr, $errs); - } else { - if (isset($testdata['lossy'])) { - $lossy = $testdata["lossy"]; - } else { - $lossy = 'neither'; - } - $test = new Tests_Net_OpenID_KVForm_TestCase( - $arr, $str, $lossy, $errs); - } - $test->setName($testdata["name"]); - $this->addTest($test); - } - } + function Tests_Net_OpenID_KVForm($name) { + $this->setName($name); + $testdata_list = array( + array("name" => "simple", + "str" => "college:harvey mudd\n", + "arr" => array("college" => "harvey mudd"), + ), + array("name" => "empty", + "str" => "", + "arr" => array(), + ), + array("name" => "empty (just newline)", + "str" => "\n", + "arr" => array(), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "empty (double newline)", + "str" => "\n\n", + "arr" => array(), + "lossy" => "str", + "errors" => 2, + ), + array("name" => "empty (no colon)", + "str" => "East is least\n", + "arr" => array(), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "two keys", + "str" => "city:claremont\nstate:CA\n", + "arr" => array('city' => 'claremont', + 'state' => 'CA'), + ), + array("name" => "real life", + "str" => "is_valid:true\ninvalidate_handle:" . + "{HMAC-SHA1:2398410938412093}\n", + "arr" => array('is_valid' => 'true', + 'invalidate_handle' => + '{HMAC-SHA1:2398410938412093}'), + ), + array("name" => "empty key and value", + "str" => ":\n", + "arr" => array(''=>''), + ), + array("name" => "empty key, not value", + "str" => ":missing key\n", + "arr" => array(''=>'missing key'), + ), + array("name" => "whitespace at front of key", + "str" => " street:foothill blvd\n", + "arr" => array('street'=>'foothill blvd'), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "whitespace at front of value", + "str" => "major: computer science\n", + "arr" => array('major'=>'computer science'), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "whitespace around key and value", + "str" => " dorm : east \n", + "arr" => array('dorm'=>'east'), + "lossy" => "str", + "errors" => 2, + ), + array("name" => "missing trailing newline", + "str" => "e^(i*pi)+1:0", + "arr" => array('e^(i*pi)+1'=>'0'), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "missing trailing newline (two key)", + "str" => "east:west\nnorth:south", + "arr" => array('east'=>'west', + 'north'=>'south'), + "lossy" => "str", + "errors" => 1, + ), + array("name" => "colon in key", + "arr" => array("k:k" => 'v'), + "errors" => 1, + ), + array("name" => "newline in key", + "arr" => array("k\nk" => 'v'), + "errors" => 1, + ), + array("name" => "newline in value", + "arr" => array('k' => "v\nv"), + "errors" => 1, + ), + array("name" => "array whitespace", + "arr" => array(" k " => "v"), + "lossy" => "both", + "str" => " k :v\n", + "errors" => 2, + ), + array("name" => "array ordering 1", + "arr" => array("a" => "x", + "b" => "x", + "c" => "x"), + "str" => "a:x\nb:x\nc:x\n", + ), + array("name" => "array ordering 2", + "arr" => array("a" => "x", + "c" => "x", + "b" => "x"), + "str" => "a:x\nc:x\nb:x\n", + ), + ); + + foreach ($testdata_list as $testdata) { + if (isset($testdata['str'])) { + $str = $testdata['str']; + } else { + $str = NULL; + } + + $arr = $testdata["arr"]; + + if (isset($testdata['errors'])) { + $errs = $testdata["errors"]; + } else { + $errs = 0; + } + + if (is_null($str)) { + $test = new Tests_Net_OpenID_KVForm_TestCase_Null($arr, $errs); + } else { + if (isset($testdata['lossy'])) { + $lossy = $testdata["lossy"]; + } else { + $lossy = 'neither'; + } + $test = new Tests_Net_OpenID_KVForm_TestCase( + $arr, $str, $lossy, $errs); + } + $test->setName($testdata["name"]); + $this->addTest($test); + } + } } ?> diff --git a/Tests/TestDriver.php b/Tests/TestDriver.php index e92c53b..1cd3ca0 100644 --- a/Tests/TestDriver.php +++ b/Tests/TestDriver.php @@ -19,35 +19,35 @@ require_once('PHPUnit/GUI/HTML.php'); * loadTests('Tests/', array('Foo')) */ function loadTests($test_dir, $test_names) { - $suites = array(); + $suites = array(); - foreach ($test_names as $filename) { - $filename = $test_dir . $filename . '.php'; - $class_name = str_replace(DIRECTORY_SEPARATOR, '_', $filename); - $class_name = basename($class_name, '.php'); - include_once($filename); - $test = new $class_name($class_name); - if (is_a($test, 'PHPUnit_TestCase')) { - $test = new PHPUnit_TestSuite($class_name); - } - $suites[] = $test; - } + foreach ($test_names as $filename) { + $filename = $test_dir . $filename . '.php'; + $class_name = str_replace(DIRECTORY_SEPARATOR, '_', $filename); + $class_name = basename($class_name, '.php'); + include_once($filename); + $test = new $class_name($class_name); + if (is_a($test, 'PHPUnit_TestCase')) { + $test = new PHPUnit_TestSuite($class_name); + } + $suites[] = $test; + } - return $suites; + return $suites; } $_test_dir = 'Tests/Net/OpenID/'; $_test_names = array( - 'KVForm', - 'CryptUtil', - 'DiffieHellman', - 'HMACSHA1', - ); + 'KVForm', + 'CryptUtil', + 'DiffieHellman', + 'HMACSHA1', + ); // Load OpenID library tests function loadSuite() { - global $_test_names; - global $_test_dir; - return loadTests($_test_dir, $_test_names); + global $_test_names; + global $_test_dir; + return loadTests($_test_dir, $_test_names); } ?> diff --git a/texttest.php b/texttest.php index 2aed0dd..0764eef 100644 --- a/texttest.php +++ b/texttest.php @@ -4,30 +4,30 @@ require_once('Tests/TestDriver.php'); require_once('PHPUnit/TestResult.php'); class TextTestResult extends PHPUnit_TestResult { - function addError(&$test, &$t) { - parent::addError($test, $t); - echo "E"; - } - - function addFailure(&$test, &$t) { - parent::addFailure($test, $t); - echo "F"; - } - - function addPassedTest(&$test) { - parent::addPassedTest($test); - echo "."; - } - - function dumpBadResults() { - foreach ($this->failures() as $failure) { - echo $failure->toString(); - } - - foreach ($this->errors() as $failure) { - echo $failure->toString(); - } - } + function addError(&$test, &$t) { + parent::addError($test, $t); + echo "E"; + } + + function addFailure(&$test, &$t) { + parent::addFailure($test, $t); + echo "F"; + } + + function addPassedTest(&$test) { + parent::addPassedTest($test); + echo "."; + } + + function dumpBadResults() { + foreach ($this->failures() as $failure) { + echo $failure->toString(); + } + + foreach ($this->errors() as $failure) { + echo $failure->toString(); + } + } } function microtime_float() @@ -41,43 +41,43 @@ array_shift($argv); $t = array_search('thorough', $argv); if ($t !== FALSE && $t !== NULL) { - define('Tests_Net_OpenID_DH_thorough', TRUE); + define('Tests_Net_OpenID_DH_thorough', TRUE); } $suites = loadSuite(); $totals = array( - 'run' => 0, - 'error' => 0, - 'failure' => 0, - 'time' => 0 - ); + 'run' => 0, + 'error' => 0, + 'failure' => 0, + 'time' => 0 + ); foreach ($suites as $suite) { - $name = $suite->getName(); - echo "========================================== + $name = $suite->getName(); + echo "========================================== Test suite: $name ------------------------------------------ "; - $result = new TextTestResult(); - $before = microtime_float(); - $suite->run($result); - $after = microtime_float(); - - $run = $result->runCount(); - $error = $result->errorCount(); - $failure = $result->failureCount(); - $delta = $after - $before; - $totals['run'] += $run; - $totals['error'] += $error; - $totals['failure'] += $failure; - $totals['time'] += $delta; - $human_delta = round($delta, 3); - echo "\nRan $run tests in $human_delta seconds"; - if ($error || $failure) { - echo " with $error errors, $failure failures"; - } + $result = new TextTestResult(); + $before = microtime_float(); + $suite->run($result); + $after = microtime_float(); + + $run = $result->runCount(); + $error = $result->errorCount(); + $failure = $result->failureCount(); + $delta = $after - $before; + $totals['run'] += $run; + $totals['error'] += $error; + $totals['failure'] += $failure; + $totals['time'] += $delta; + $human_delta = round($delta, 3); + echo "\nRan $run tests in $human_delta seconds"; + if ($error || $failure) { + echo " with $error errors, $failure failures"; + } echo " ========================================== @@ -91,7 +91,7 @@ $failure = $totals['failure']; $time = round($totals['time'], 3); echo "Ran a total of $run tests in $time seconds with $error errors, $failure failures\n"; if ($totals['error'] || $totals['failure']) { - exit(1); + exit(1); } ?>
\ No newline at end of file |