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
|
<?php
/**
* Tests for the KVForm module.
*
* PHP versions 4 and 5
*
* LICENSE: See the COPYING file included in this distribution.
*
* @package OpenID
* @author JanRain, Inc. <openid@janrain.com>
* @copyright 2005-2008 Janrain, Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
*/
require_once 'Auth/OpenID/KVForm.php';
global $_Tests_Auth_OpenID_kverrors;
$_Tests_Auth_OpenID_kverrors = null;
/**
* Keep a list of the logged errors
*/
function Tests_Auth_OpenID_kvHandleError($errno, $errmsg)
{
global $_Tests_Auth_OpenID_kverrors;
$_Tests_Auth_OpenID_kverrors[] = $errmsg;
}
class Tests_Auth_OpenID_KVForm_TestCase extends PHPUnit_Framework_TestCase {
var $errs;
function runTest()
{
// Re-set the number of logged errors
global $_Tests_Auth_OpenID_kverrors;
$_Tests_Auth_OpenID_kverrors = array();
set_error_handler("Tests_Auth_OpenID_kvHandleError");
$this->_runTest();
// Check to make sure we have the expected number of logged errors
//$this->assertEquals($this->errs, count($_Tests_Auth_OpenID_kverrors));
restore_error_handler();
}
function _runTest()
{
trigger_error('Must be overridden', E_USER_ERROR);
}
}
class Tests_Auth_OpenID_KVForm_TestCase_Parse
extends Tests_Auth_OpenID_KVForm_TestCase {
function Tests_Auth_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 = Auth_OpenID_KVForm::toArray($this->str);
$serial1 = Auth_OpenID_KVForm::fromArray($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 = Auth_OpenID_KVForm::toArray($serial1);
$serial2 = Auth_OpenID_KVForm::fromArray($parsed1);
// Round-trip both
$parsed3 = Auth_OpenID_KVForm::toArray($serial2);
$serial3 = Auth_OpenID_KVForm::fromArray($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_Auth_OpenID_KVForm_TestCase_Null
extends Tests_Auth_OpenID_KVForm_TestCase {
function Tests_Auth_OpenID_KVForm_TestCase_Null($arr, $errs)
{
$this->arr = $arr;
$this->errs = $errs;
}
function _runTest()
{
$serialized = Auth_OpenID_KVForm::fromArray($this->arr);
$this->assertTrue($serialized === null,
'serialization unexpectedly succeeded');
}
}
class Tests_Auth_OpenID_KVForm extends PHPUnit_Framework_TestSuite {
function Tests_Auth_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_Auth_OpenID_KVForm_TestCase_Null($arr, $errs);
} else {
if (isset($testdata['lossy'])) {
$lossy = $testdata["lossy"];
} else {
$lossy = 'neither';
}
$test = new Tests_Auth_OpenID_KVForm_TestCase_Parse(
$arr, $str, $lossy, $errs);
}
$test->setName($testdata["name"]);
$this->addTest($test);
}
}
}
|