KVForm.php 8.3 KB

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