PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/slim/slim/tests/Http/UtilTest.php

https://bitbucket.org/caasjj/dnr
PHP | 388 lines | 287 code | 28 blank | 73 comment | 0 complexity | 1ffc57702c97c015dbb914768b068f3b MD5 | raw file
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@slimframework.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. *
  11. * MIT LICENSE
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files (the
  15. * "Software"), to deal in the Software without restriction, including
  16. * without limitation the rights to use, copy, modify, merge, publish,
  17. * distribute, sublicense, and/or sell copies of the Software, and to
  18. * permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. class SlimHttpUtilTest extends PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * Test strip slashes when magic quotes disabled
  36. */
  37. public function testStripSlashesWithoutMagicQuotes()
  38. {
  39. $data = "This should have \"quotes\" in it";
  40. $stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, false);
  41. $this->assertEquals($data, $stripped);
  42. }
  43. /**
  44. * Test strip slashes from array when magic quotes disabled
  45. */
  46. public function testStripSlashesFromArrayWithoutMagicQuotes()
  47. {
  48. $data = array("This should have \"quotes\" in it", "And this \"too\" has quotes");
  49. $stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, false);
  50. $this->assertEquals($data, $stripped);
  51. }
  52. /**
  53. * Test strip slashes when magic quotes enabled
  54. */
  55. public function testStripSlashesWithMagicQuotes()
  56. {
  57. $data = "This should have \"quotes\" in it";
  58. $stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, true);
  59. $this->assertEquals('This should have "quotes" in it', $stripped);
  60. }
  61. /**
  62. * Test strip slashes from array when magic quotes enabled
  63. */
  64. public function testStripSlashesFromArrayWithMagicQuotes()
  65. {
  66. $data = array("This should have \"quotes\" in it", "And this \"too\" has quotes");
  67. $stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, true);
  68. $this->assertEquals($data = array('This should have "quotes" in it', 'And this "too" has quotes'), $stripped);
  69. }
  70. /**
  71. * Test encrypt and decrypt with valid data
  72. */
  73. public function testEncryptAndDecryptWithValidData()
  74. {
  75. $data = 'foo';
  76. $key = 'secret';
  77. $iv = md5('initializationVector');
  78. $encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
  79. $decrypted = \Slim\Http\Util::decrypt($encrypted, $key, $iv);
  80. $this->assertEquals($data, $decrypted);
  81. $this->assertTrue($data !== $encrypted);
  82. }
  83. /**
  84. * Test encrypt when data is empty string
  85. */
  86. public function testEncryptWhenDataIsEmptyString()
  87. {
  88. $data = '';
  89. $key = 'secret';
  90. $iv = md5('initializationVector');
  91. $encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
  92. $this->assertEquals('', $encrypted);
  93. }
  94. /**
  95. * Test decrypt when data is empty string
  96. */
  97. public function testDecryptWhenDataIsEmptyString()
  98. {
  99. $data = '';
  100. $key = 'secret';
  101. $iv = md5('initializationVector');
  102. $decrypted = \Slim\Http\Util::decrypt($data, $key, $iv);
  103. $this->assertEquals('', $decrypted);
  104. }
  105. /**
  106. * Test encrypt when IV and key sizes are too long
  107. */
  108. public function testEncryptAndDecryptWhenKeyAndIvAreTooLong()
  109. {
  110. $data = 'foo';
  111. $key = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
  112. $iv = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
  113. $encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
  114. $decrypted = \Slim\Http\Util::decrypt($encrypted, $key, $iv);
  115. $this->assertEquals($data, $decrypted);
  116. $this->assertTrue($data !== $encrypted);
  117. }
  118. public function testEncodeAndDecodeSecureCookieWithValidData()
  119. {
  120. //Prepare cookie value
  121. $value = 'foo';
  122. $expires = time() + 86400;
  123. $secret = 'password';
  124. $algorithm = MCRYPT_RIJNDAEL_256;
  125. $mode = MCRYPT_MODE_CBC;
  126. $encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
  127. $decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
  128. //Test secure cookie value
  129. $parts = explode('|', $encodedValue);
  130. $this->assertEquals(3, count($parts));
  131. $this->assertEquals($expires, $parts[0]);
  132. $this->assertEquals($value, $decodedValue);
  133. }
  134. /**
  135. * Test encode/decode secure cookie with old expiration
  136. *
  137. * In this test, the expiration date is purposefully set to a time before now.
  138. * When decoding the encoded cookie value, FALSE is returned since the cookie
  139. * will have expired before it is decoded.
  140. */
  141. public function testEncodeAndDecodeSecureCookieWithOldExpiration()
  142. {
  143. $value = 'foo';
  144. $expires = time() - 100;
  145. $secret = 'password';
  146. $algorithm = MCRYPT_RIJNDAEL_256;
  147. $mode = MCRYPT_MODE_CBC;
  148. $encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
  149. $decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
  150. $this->assertFalse($decodedValue);
  151. }
  152. /**
  153. * Test encode/decode secure cookie with tampered data
  154. *
  155. * In this test, the encoded data is purposefully changed to simulate someone
  156. * tampering with the client-side cookie data. When decoding the encoded cookie value,
  157. * FALSE is returned since the verification key will not match.
  158. */
  159. public function testEncodeAndDecodeSecureCookieWithTamperedData()
  160. {
  161. $value = 'foo';
  162. $expires = time() + 86400;
  163. $secret = 'password';
  164. $algorithm = MCRYPT_RIJNDAEL_256;
  165. $mode = MCRYPT_MODE_CBC;
  166. $encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
  167. $encodedValueParts = explode('|', $encodedValue);
  168. $encodedValueParts[1] = $encodedValueParts[1] . 'changed';
  169. $encodedValue = implode('|', $encodedValueParts);
  170. $decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
  171. $this->assertFalse($decodedValue);
  172. }
  173. public function testSetCookieHeaderWithNameAndValue()
  174. {
  175. $name = 'foo';
  176. $value = 'bar';
  177. $header = array();
  178. \Slim\Http\Util::setCookieHeader($header, $name, $value);
  179. $this->assertEquals('foo=bar', $header['Set-Cookie']);
  180. }
  181. public function testSetCookieHeaderWithNameAndValueWhenCookieAlreadySet()
  182. {
  183. $name = 'foo';
  184. $value = 'bar';
  185. $header = array('Set-Cookie' => 'one=two');
  186. \Slim\Http\Util::setCookieHeader($header, $name, $value);
  187. $this->assertEquals("one=two\nfoo=bar", $header['Set-Cookie']);
  188. }
  189. public function testSetCookieHeaderWithNameAndValueAndDomain()
  190. {
  191. $name = 'foo';
  192. $value = 'bar';
  193. $domain = 'foo.com';
  194. $header = array();
  195. \Slim\Http\Util::setCookieHeader($header, $name, array(
  196. 'value' => $value,
  197. 'domain' => $domain
  198. ));
  199. $this->assertEquals('foo=bar; domain=foo.com', $header['Set-Cookie']);
  200. }
  201. public function testSetCookieHeaderWithNameAndValueAndDomainAndPath()
  202. {
  203. $name = 'foo';
  204. $value = 'bar';
  205. $domain = 'foo.com';
  206. $path = '/foo';
  207. $header = array();
  208. \Slim\Http\Util::setCookieHeader($header, $name, array(
  209. 'value' => $value,
  210. 'domain' => $domain,
  211. 'path' => $path
  212. ));
  213. $this->assertEquals('foo=bar; domain=foo.com; path=/foo', $header['Set-Cookie']);
  214. }
  215. public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsString()
  216. {
  217. $name = 'foo';
  218. $value = 'bar';
  219. $domain = 'foo.com';
  220. $path = '/foo';
  221. $expires = '2 days';
  222. $expiresFormat = gmdate('D, d-M-Y H:i:s e', strtotime($expires));
  223. $header = array();
  224. \Slim\Http\Util::setCookieHeader($header, $name, array(
  225. 'value' => $value,
  226. 'domain' => $domain,
  227. 'path' => '/foo',
  228. 'expires' => $expires
  229. ));
  230. $this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat, $header['Set-Cookie']);
  231. }
  232. public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsInteger()
  233. {
  234. $name = 'foo';
  235. $value = 'bar';
  236. $domain = 'foo.com';
  237. $path = '/foo';
  238. $expires = strtotime('2 days');
  239. $expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
  240. $header = array();
  241. \Slim\Http\Util::setCookieHeader($header, $name, array(
  242. 'value' => $value,
  243. 'domain' => $domain,
  244. 'path' => '/foo',
  245. 'expires' => $expires
  246. ));
  247. $this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat, $header['Set-Cookie']);
  248. }
  249. public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsZero()
  250. {
  251. $name = 'foo';
  252. $value = 'bar';
  253. $domain = 'foo.com';
  254. $path = '/foo';
  255. $expires = 0;
  256. $header = array();
  257. \Slim\Http\Util::setCookieHeader($header, $name, array(
  258. 'value' => $value,
  259. 'domain' => $domain,
  260. 'path' => '/foo',
  261. 'expires' => $expires
  262. ));
  263. $this->assertEquals('foo=bar; domain=foo.com; path=/foo', $header['Set-Cookie']);
  264. }
  265. public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAndSecure()
  266. {
  267. $name = 'foo';
  268. $value = 'bar';
  269. $domain = 'foo.com';
  270. $path = '/foo';
  271. $expires = strtotime('2 days');
  272. $expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
  273. $secure = true;
  274. $header = array();
  275. \Slim\Http\Util::setCookieHeader($header, $name, array(
  276. 'value' => $value,
  277. 'domain' => $domain,
  278. 'path' => '/foo',
  279. 'expires' => $expires,
  280. 'secure' => $secure
  281. ));
  282. $this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat . '; secure', $header['Set-Cookie']);
  283. }
  284. public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAndSecureAndHttpOnly()
  285. {
  286. $name = 'foo';
  287. $value = 'bar';
  288. $domain = 'foo.com';
  289. $path = '/foo';
  290. $expires = strtotime('2 days');
  291. $expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
  292. $secure = true;
  293. $httpOnly = true;
  294. $header = array();
  295. \Slim\Http\Util::setCookieHeader($header, $name, array(
  296. 'value' => $value,
  297. 'domain' => $domain,
  298. 'path' => '/foo',
  299. 'expires' => $expires,
  300. 'secure' => $secure,
  301. 'httponly' => $httpOnly
  302. ));
  303. $this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat . '; secure; HttpOnly', $header['Set-Cookie']);
  304. }
  305. public function testDeleteCookieHeaderWithSurvivingCookie()
  306. {
  307. $header = array('Set-Cookie' => "foo=bar\none=two");
  308. \Slim\Http\Util::deleteCookieHeader($header, 'foo');
  309. $this->assertEquals(1, preg_match("@^one=two\nfoo=; expires=@", $header['Set-Cookie']));
  310. }
  311. public function testDeleteCookieHeaderWithoutSurvivingCookie()
  312. {
  313. $header = array('Set-Cookie' => "foo=bar");
  314. \Slim\Http\Util::deleteCookieHeader($header, 'foo');
  315. $this->assertEquals(1, preg_match("@foo=; expires=@", $header['Set-Cookie']));
  316. }
  317. public function testDeleteCookieHeaderWithMatchingDomain()
  318. {
  319. $header = array('Set-Cookie' => "foo=bar; domain=foo.com");
  320. \Slim\Http\Util::deleteCookieHeader($header, 'foo', array(
  321. 'domain' => 'foo.com'
  322. ));
  323. $this->assertEquals(1, preg_match("@foo=; domain=foo.com; expires=@", $header['Set-Cookie']));
  324. }
  325. public function testDeleteCookieHeaderWithoutMatchingDomain()
  326. {
  327. $header = array('Set-Cookie' => "foo=bar; domain=foo.com");
  328. \Slim\Http\Util::deleteCookieHeader($header, 'foo', array(
  329. 'domain' => 'bar.com'
  330. ));
  331. $this->assertEquals(1, preg_match("@foo=bar; domain=foo\.com\nfoo=; domain=bar\.com@", $header['Set-Cookie']));
  332. }
  333. /**
  334. * Test parses Cookie: HTTP header
  335. */
  336. public function testParsesCookieHeader()
  337. {
  338. $header = 'foo=bar; one=two; colors=blue';
  339. $result = \Slim\Http\Util::parseCookieHeader($header);
  340. $this->assertEquals(3, count($result));
  341. $this->assertEquals('bar', $result['foo']);
  342. $this->assertEquals('two', $result['one']);
  343. $this->assertEquals('blue', $result['colors']);
  344. }
  345. public function testParsesCookieHeaderWithCommaSeparator()
  346. {
  347. $header = 'foo=bar, one=two, colors=blue';
  348. $result = \Slim\Http\Util::parseCookieHeader($header);
  349. $this->assertEquals(3, count($result));
  350. $this->assertEquals('bar', $result['foo']);
  351. $this->assertEquals('two', $result['one']);
  352. $this->assertEquals('blue', $result['colors']);
  353. }
  354. public function testPrefersLeftmostCookieWhenManyCookiesWithSameName()
  355. {
  356. $header = 'foo=bar; foo=beer';
  357. $result = \Slim\Http\Util::parseCookieHeader($header);
  358. $this->assertEquals('bar', $result['foo']);
  359. }
  360. }