PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php-openid-1.2.3/Tests/Auth/OpenID/KVForm.php

https://github.com/alx/openid-server
PHP | 259 lines | 208 code | 29 blank | 22 comment | 16 complexity | 69926933a2fa23636c27611c1c4e73b2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Tests for the KVForm module.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005 Janrain, Inc.
  12. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  13. */
  14. require_once 'PHPUnit.php';
  15. require_once 'Auth/OpenID/KVForm.php';
  16. $_Tests_Auth_OpenID_kverrors = null;
  17. /**
  18. * Keep a list of the logged errors
  19. */
  20. function Tests_Auth_OpenID_kvHandleError($errno, $errmsg)
  21. {
  22. global $_Tests_Auth_OpenID_kverrors;
  23. $_Tests_Auth_OpenID_kverrors[] = $errmsg;
  24. }
  25. class Tests_Auth_OpenID_KVForm_TestCase extends PHPUnit_TestCase {
  26. var $errs;
  27. function runTest()
  28. {
  29. // Re-set the number of logged errors
  30. global $_Tests_Auth_OpenID_kverrors;
  31. $_Tests_Auth_OpenID_kverrors = array();
  32. set_error_handler("Tests_Auth_OpenID_kvHandleError");
  33. $this->_runTest();
  34. // Check to make sure we have the expected number of logged errors
  35. //$this->assertEquals($this->errs, count($_Tests_Auth_OpenID_kverrors));
  36. restore_error_handler();
  37. }
  38. function _runTest()
  39. {
  40. trigger_error('Must be overridden', E_USER_ERROR);
  41. }
  42. }
  43. class Tests_Auth_OpenID_KVForm_TestCase_Parse
  44. extends Tests_Auth_OpenID_KVForm_TestCase {
  45. function Tests_Auth_OpenID_KVForm_TestCase_Parse(
  46. $arr, $str, $lossy, $errs)
  47. {
  48. $this->arr = $arr;
  49. $this->str = $str;
  50. $this->lossy = $lossy;
  51. $this->errs = $errs;
  52. }
  53. function _runTest()
  54. {
  55. // Do one parse, after which arrayToKV and kvToArray should be
  56. // inverses.
  57. $parsed1 = Auth_OpenID_KVForm::toArray($this->str);
  58. $serial1 = Auth_OpenID_KVForm::fromArray($this->arr);
  59. if ($this->lossy == "neither" || $this->lossy == "str") {
  60. $this->assertEquals($this->arr, $parsed1, "str was lossy");
  61. }
  62. if ($this->lossy == "neither" || $this->lossy == "arr") {
  63. $this->assertEquals($this->str, $serial1, "array was lossy");
  64. }
  65. $parsed2 = Auth_OpenID_KVForm::toArray($serial1);
  66. $serial2 = Auth_OpenID_KVForm::fromArray($parsed1);
  67. // Round-trip both
  68. $parsed3 = Auth_OpenID_KVForm::toArray($serial2);
  69. $serial3 = Auth_OpenID_KVForm::fromArray($parsed2);
  70. $this->assertEquals($serial2, $serial3, "serialized forms differ");
  71. // Check to make sure that they're inverses.
  72. $this->assertEquals($parsed2, $parsed3, "parsed forms differ");
  73. }
  74. }
  75. class Tests_Auth_OpenID_KVForm_TestCase_Null
  76. extends Tests_Auth_OpenID_KVForm_TestCase {
  77. function Tests_Auth_OpenID_KVForm_TestCase_Null($arr, $errs)
  78. {
  79. $this->arr = $arr;
  80. $this->errs = $errs;
  81. }
  82. function _runTest()
  83. {
  84. $serialized = Auth_OpenID_KVForm::fromArray($this->arr);
  85. $this->assertTrue($serialized === null,
  86. 'serialization unexpectedly succeeded');
  87. }
  88. }
  89. class Tests_Auth_OpenID_KVForm extends PHPUnit_TestSuite {
  90. function Tests_Auth_OpenID_KVForm($name)
  91. {
  92. $this->setName($name);
  93. $testdata_list = array(
  94. array("name" => "simple",
  95. "str" => "college:harvey mudd\n",
  96. "arr" => array("college" => "harvey mudd"),
  97. ),
  98. array("name" => "empty",
  99. "str" => "",
  100. "arr" => array(),
  101. ),
  102. array("name" => "empty (just newline)",
  103. "str" => "\n",
  104. "arr" => array(),
  105. "lossy" => "str",
  106. "errors" => 1,
  107. ),
  108. array("name" => "empty (double newline)",
  109. "str" => "\n\n",
  110. "arr" => array(),
  111. "lossy" => "str",
  112. "errors" => 2,
  113. ),
  114. array("name" => "empty (no colon)",
  115. "str" => "East is least\n",
  116. "arr" => array(),
  117. "lossy" => "str",
  118. "errors" => 1,
  119. ),
  120. array("name" => "two keys",
  121. "str" => "city:claremont\nstate:CA\n",
  122. "arr" => array('city' => 'claremont',
  123. 'state' => 'CA'),
  124. ),
  125. array("name" => "real life",
  126. "str" => "is_valid:true\ninvalidate_handle:" .
  127. "{HMAC-SHA1:2398410938412093}\n",
  128. "arr" => array('is_valid' => 'true',
  129. 'invalidate_handle' =>
  130. '{HMAC-SHA1:2398410938412093}'),
  131. ),
  132. array("name" => "empty key and value",
  133. "str" => ":\n",
  134. "arr" => array(''=>''),
  135. ),
  136. array("name" => "empty key, not value",
  137. "str" => ":missing key\n",
  138. "arr" => array(''=>'missing key'),
  139. ),
  140. array("name" => "whitespace at front of key",
  141. "str" => " street:foothill blvd\n",
  142. "arr" => array('street'=>'foothill blvd'),
  143. "lossy" => "str",
  144. "errors" => 1,
  145. ),
  146. array("name" => "whitespace at front of value",
  147. "str" => "major: computer science\n",
  148. "arr" => array('major'=>'computer science'),
  149. "lossy" => "str",
  150. "errors" => 1,
  151. ),
  152. array("name" => "whitespace around key and value",
  153. "str" => " dorm : east \n",
  154. "arr" => array('dorm'=>'east'),
  155. "lossy" => "str",
  156. "errors" => 2,
  157. ),
  158. array("name" => "missing trailing newline",
  159. "str" => "e^(i*pi)+1:0",
  160. "arr" => array('e^(i*pi)+1'=>'0'),
  161. "lossy" => "str",
  162. "errors" => 1,
  163. ),
  164. array("name" => "missing trailing newline (two key)",
  165. "str" => "east:west\nnorth:south",
  166. "arr" => array('east'=>'west',
  167. 'north'=>'south'),
  168. "lossy" => "str",
  169. "errors" => 1,
  170. ),
  171. array("name" => "colon in key",
  172. "arr" => array("k:k" => 'v'),
  173. "errors" => 1,
  174. ),
  175. array("name" => "newline in key",
  176. "arr" => array("k\nk" => 'v'),
  177. "errors" => 1,
  178. ),
  179. array("name" => "newline in value",
  180. "arr" => array('k' => "v\nv"),
  181. "errors" => 1,
  182. ),
  183. array("name" => "array whitespace",
  184. "arr" => array(" k " => "v"),
  185. "lossy" => "both",
  186. "str" => " k :v\n",
  187. "errors" => 2,
  188. ),
  189. array("name" => "array ordering 1",
  190. "arr" => array("a" => "x",
  191. "b" => "x",
  192. "c" => "x"),
  193. "str" => "a:x\nb:x\nc:x\n",
  194. ),
  195. array("name" => "array ordering 2",
  196. "arr" => array("a" => "x",
  197. "c" => "x",
  198. "b" => "x"),
  199. "str" => "a:x\nc:x\nb:x\n",
  200. ),
  201. );
  202. foreach ($testdata_list as $testdata) {
  203. if (isset($testdata['str'])) {
  204. $str = $testdata['str'];
  205. } else {
  206. $str = null;
  207. }
  208. $arr = $testdata["arr"];
  209. if (isset($testdata['errors'])) {
  210. $errs = $testdata["errors"];
  211. } else {
  212. $errs = 0;
  213. }
  214. if (is_null($str)) {
  215. $test = new Tests_Auth_OpenID_KVForm_TestCase_Null($arr, $errs);
  216. } else {
  217. if (isset($testdata['lossy'])) {
  218. $lossy = $testdata["lossy"];
  219. } else {
  220. $lossy = 'neither';
  221. }
  222. $test = new Tests_Auth_OpenID_KVForm_TestCase(
  223. $arr, $str, $lossy, $errs);
  224. }
  225. $test->setName($testdata["name"]);
  226. $this->addTest($test);
  227. }
  228. }
  229. }
  230. ?>