PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Auth/OpenID/KVForm.php

http://github.com/openid/php-openid
PHP | 290 lines | 237 code | 31 blank | 22 comment | 16 complexity | 225c55b62a50a65c897198be581342f5 MD5 | raw file
Possible License(s): Apache-2.0
  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 'Auth/OpenID/KVForm.php';
  15. global $_Tests_Auth_OpenID_kverrors;
  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_Framework_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 = [];
  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 __construct(
  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 __construct($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_Framework_TestSuite {
  90. function __construct($name)
  91. {
  92. $this->setName($name);
  93. $testdata_list = [
  94. [
  95. "name" => "simple",
  96. "str" => "college:harvey mudd\n",
  97. "arr" => ["college" => "harvey mudd"],
  98. ],
  99. [
  100. "name" => "empty",
  101. "str" => "",
  102. "arr" => [],
  103. ],
  104. [
  105. "name" => "empty (just newline)",
  106. "str" => "\n",
  107. "arr" => [],
  108. "lossy" => "str",
  109. "errors" => 1,
  110. ],
  111. [
  112. "name" => "empty (double newline)",
  113. "str" => "\n\n",
  114. "arr" => [],
  115. "lossy" => "str",
  116. "errors" => 2,
  117. ],
  118. [
  119. "name" => "empty (no colon)",
  120. "str" => "East is least\n",
  121. "arr" => [],
  122. "lossy" => "str",
  123. "errors" => 1,
  124. ],
  125. [
  126. "name" => "two keys",
  127. "str" => "city:claremont\nstate:CA\n",
  128. "arr" => [
  129. 'city' => 'claremont',
  130. 'state' => 'CA',
  131. ],
  132. ],
  133. [
  134. "name" => "real life",
  135. "str" => "is_valid:true\ninvalidate_handle:" .
  136. "{HMAC-SHA1:2398410938412093}\n",
  137. "arr" => [
  138. 'is_valid' => 'true',
  139. 'invalidate_handle' =>
  140. '{HMAC-SHA1:2398410938412093}',
  141. ],
  142. ],
  143. [
  144. "name" => "empty key and value",
  145. "str" => ":\n",
  146. "arr" => ['' => ''],
  147. ],
  148. [
  149. "name" => "empty key, not value",
  150. "str" => ":missing key\n",
  151. "arr" => ['' => 'missing key'],
  152. ],
  153. [
  154. "name" => "whitespace at front of key",
  155. "str" => " street:foothill blvd\n",
  156. "arr" => ['street' => 'foothill blvd'],
  157. "lossy" => "str",
  158. "errors" => 1,
  159. ],
  160. [
  161. "name" => "whitespace at front of value",
  162. "str" => "major: computer science\n",
  163. "arr" => ['major' => 'computer science'],
  164. "lossy" => "str",
  165. "errors" => 1,
  166. ],
  167. [
  168. "name" => "whitespace around key and value",
  169. "str" => " dorm : east \n",
  170. "arr" => ['dorm' => 'east'],
  171. "lossy" => "str",
  172. "errors" => 2,
  173. ],
  174. [
  175. "name" => "missing trailing newline",
  176. "str" => "e^(i*pi)+1:0",
  177. "arr" => ['e^(i*pi)+1' => '0'],
  178. "lossy" => "str",
  179. "errors" => 1,
  180. ],
  181. [
  182. "name" => "missing trailing newline (two key)",
  183. "str" => "east:west\nnorth:south",
  184. "arr" => [
  185. 'east' => 'west',
  186. 'north' => 'south',
  187. ],
  188. "lossy" => "str",
  189. "errors" => 1,
  190. ],
  191. [
  192. "name" => "colon in key",
  193. "arr" => ["k:k" => 'v'],
  194. "errors" => 1,
  195. ],
  196. [
  197. "name" => "newline in key",
  198. "arr" => ["k\nk" => 'v'],
  199. "errors" => 1,
  200. ],
  201. [
  202. "name" => "newline in value",
  203. "arr" => ['k' => "v\nv"],
  204. "errors" => 1,
  205. ],
  206. [
  207. "name" => "array whitespace",
  208. "arr" => [" k " => "v"],
  209. "lossy" => "both",
  210. "str" => " k :v\n",
  211. "errors" => 2,
  212. ],
  213. [
  214. "name" => "array ordering 1",
  215. "arr" => [
  216. "a" => "x",
  217. "b" => "x",
  218. "c" => "x",
  219. ],
  220. "str" => "a:x\nb:x\nc:x\n",
  221. ],
  222. [
  223. "name" => "array ordering 2",
  224. "arr" => [
  225. "a" => "x",
  226. "c" => "x",
  227. "b" => "x",
  228. ],
  229. "str" => "a:x\nc:x\nb:x\n",
  230. ],
  231. ];
  232. foreach ($testdata_list as $testdata) {
  233. if (isset($testdata['str'])) {
  234. $str = $testdata['str'];
  235. } else {
  236. $str = null;
  237. }
  238. $arr = $testdata["arr"];
  239. if (isset($testdata['errors'])) {
  240. $errs = $testdata["errors"];
  241. } else {
  242. $errs = 0;
  243. }
  244. if (is_null($str)) {
  245. $test = new Tests_Auth_OpenID_KVForm_TestCase_Null($arr, $errs);
  246. } else {
  247. if (isset($testdata['lossy'])) {
  248. $lossy = $testdata["lossy"];
  249. } else {
  250. $lossy = 'neither';
  251. }
  252. $test = new Tests_Auth_OpenID_KVForm_TestCase_Parse(
  253. $arr, $str, $lossy, $errs);
  254. }
  255. $test->setName($testdata["name"]);
  256. $this->addTest($test);
  257. }
  258. }
  259. }