CookieTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Http
  5. * @subpackage UnitTests
  6. * @version $Id$
  7. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
  8. * @license http://framework.zend.com/license/new-bsd New BSD License
  9. */
  10. require_once dirname(__FILE__) . '/../../TestHelper.php';
  11. require_once 'Zend/Http/Cookie.php';
  12. /**
  13. * Zend_Http_Cookie unit tests
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. class Zend_Http_CookieTest extends PHPUnit_Framework_TestCase
  22. {
  23. /**
  24. * Make sure we can't set invalid names
  25. */
  26. public function testSetInvalidName()
  27. {
  28. $invalidcharacters = "=,; \t\r\n\013\014";
  29. $l = strlen($invalidcharacters) - 1;
  30. for ($i = 0; $i < $l; $i++) {
  31. $name = 'cookie_' . $invalidcharacters[$i];
  32. try {
  33. $cookie = new Zend_Http_Cookie($name, 'foo', 'example.com');
  34. $this->fail('Expected invalid cookie name exception was not thrown for "' . $name . '"');
  35. } catch (Zend_Http_Exception $e) {
  36. // We're good!
  37. }
  38. }
  39. }
  40. /**
  41. * Test we get the cookie name properly
  42. */
  43. public function testGetName()
  44. {
  45. // Array of cookies and their names. We need to test each 'keyword' in
  46. // a cookie string
  47. $cookies = array(
  48. 'justacookie' => 'justacookie=foo; domain=example.com',
  49. 'expires' => 'expires=tomorrow; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com',
  50. 'domain' => 'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com',
  51. 'path' => 'path=indexAction; path=/; domain=.foo.com',
  52. 'secure' => 'secure=sha1; secure; domain=.foo.com',
  53. 'PHPSESSID' => 'PHPSESSID=1234567890abcdef; secure; domain=.foo.com; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT;'
  54. );
  55. foreach ($cookies as $name => $cstr) {
  56. $cookie = Zend_Http_Cookie::fromString($cstr);
  57. if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Cookie ' . $name . ' is not a proper Cookie object');
  58. $this->assertEquals($name, $cookie->getName(), 'Cookie name is not as expected');
  59. }
  60. }
  61. /**
  62. * Make sure we get the correct value if it was set through the constructor
  63. *
  64. */
  65. public function testGetValueConstructor()
  66. {
  67. $values = array(
  68. 'simpleCookie', 'space cookie', '!@#$%^*&()* ][{}?;', "line\n\rbreaks"
  69. );
  70. foreach ($values as $val) {
  71. $cookie = new Zend_Http_Cookie('cookie', $val, 'example.com', time(), '/', true);
  72. $this->assertEquals($val, $cookie->getValue());
  73. }
  74. }
  75. /**
  76. * Make sure we get the correct value if it was set through fromString()
  77. *
  78. */
  79. public function testGetValueFromString()
  80. {
  81. $values = array(
  82. 'simpleCookie', 'space cookie', '!@#$%^*&()* ][{}?;', "line\n\rbreaks"
  83. );
  84. foreach ($values as $val) {
  85. $cookie = Zend_Http_Cookie::fromString('cookie=' . urlencode($val) . '; domain=example.com');
  86. $this->assertEquals($val, $cookie->getValue());
  87. }
  88. }
  89. /**
  90. * Make sure we get the correct domain when it's set in the cookie string
  91. *
  92. */
  93. public function testGetDomainInStr()
  94. {
  95. $domains = array(
  96. 'cookie=foo; domain=example.com' => 'example.com',
  97. 'cookie=foo; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => '.example.com',
  98. 'cookie=foo; domain=some.really.deep.domain.com; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT;' => 'some.really.deep.domain.com'
  99. );
  100. foreach ($domains as $cstr => $domain) {
  101. $cookie = Zend_Http_Cookie::fromString($cstr);
  102. if (! $cookie instanceof Zend_Http_Cookie) $this->fail('We didn\'t get a valid Cookie object');
  103. $this->assertEquals($domain, $cookie->getDomain());
  104. }
  105. }
  106. /**
  107. * Make sure we get the correct domain when it's set in a reference URL
  108. *
  109. */
  110. public function testGetDomainInRefUrl()
  111. {
  112. $domains = array(
  113. 'example.com', 'www.example.com', 'some.really.deep.domain.com'
  114. );
  115. foreach ($domains as $domain) {
  116. $cookie = Zend_Http_Cookie::fromString('foo=baz; path=/', 'http://' . $domain);
  117. if (! $cookie instanceof Zend_Http_Cookie) $this->fail('We didn\'t get a valid Cookie object');
  118. $this->assertEquals($domain, $cookie->getDomain());
  119. }
  120. }
  121. /**
  122. * Make sure we get the correct path when it's set in the cookie string
  123. */
  124. public function testGetPathInStr()
  125. {
  126. $cookies = array(
  127. 'cookie=foo; domain=example.com' => '/',
  128. 'cookie=foo; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => '/foo/baz',
  129. 'cookie=foo; domain=some.really.deep.domain.com; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT;' => '/Space Out/'
  130. );
  131. foreach ($cookies as $cstr => $path) {
  132. $cookie = Zend_Http_Cookie::fromString($cstr);
  133. if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Failed generatic a valid cookie object');
  134. $this->assertEquals($path, $cookie->getPath(), 'Cookie path is not as expected');
  135. }
  136. }
  137. /**
  138. * Make sure we get the correct path when it's set a reference URL
  139. */
  140. public function testGetPathInRefUrl()
  141. {
  142. $refUrls = array(
  143. 'http://www.example.com/foo/bar/' => '/foo/bar',
  144. 'http://foo.com' => '/',
  145. 'http://qua.qua.co.uk/path/to/very/deep/file.php' => '/path/to/very/deep'
  146. );
  147. foreach ($refUrls as $url => $path) {
  148. $cookie = Zend_Http_Cookie::fromString('foo=bar', $url);
  149. if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Failed generating a valid cookie object');
  150. $this->assertEquals($path, $cookie->getPath(), 'Cookie path is not as expected');
  151. }
  152. }
  153. /**
  154. * Test we get the correct expiry time
  155. *
  156. */
  157. public function testGetExpiryTime()
  158. {
  159. $now = time();
  160. $yesterday = $now - (3600 * 24);
  161. $cookies = array(
  162. 'cookie=bar; domain=example.com; expires=' . date(DATE_COOKIE, $now) . ';' => $now,
  163. 'cookie=foo; expires=' . date(DATE_COOKIE, $yesterday) . '; domain=some.really.deep.domain.com; path=/;' => $yesterday,
  164. 'cookie=baz; domain=foo.com; path=/some/path; secure' => null
  165. );
  166. foreach ($cookies as $cstr => $exp) {
  167. $cookie = Zend_Http_Cookie::fromString($cstr);
  168. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  169. $this->assertEquals($exp, $cookie->getExpiryTime(), 'Expiry time is not as expected');
  170. }
  171. }
  172. /**
  173. * Make sure the "is secure" flag is correctly set
  174. */
  175. public function testIsSecure()
  176. {
  177. $cookies = array(
  178. 'cookie=foo; path=/foo/baz; secure; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => true,
  179. 'cookie=foo; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => false,
  180. 'cookie=foo; path=/; SECURE; domain=.example.com;' => true,
  181. 'cookie=foo; path=/; domain=.example.com; SECURE' => true
  182. );
  183. foreach ($cookies as $cstr => $secure) {
  184. $cookie = Zend_Http_Cookie::fromString($cstr);
  185. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  186. $this->assertEquals($secure, $cookie->isSecure(), 'isSecure is not as expected');
  187. }
  188. }
  189. /**
  190. * Make sure we get the correct value for 'isExpired'
  191. */
  192. public function testIsExpired()
  193. {
  194. $notexpired = time() + 3600;
  195. $expired = time() - 3600;
  196. $cookies = array(
  197. 'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $notexpired) => false,
  198. 'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $expired) => true,
  199. 'cookie=foo; domain=example.com;' => false
  200. );
  201. foreach ($cookies as $cstr => $isexp) {
  202. $cookie = Zend_Http_Cookie::fromString($cstr);
  203. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  204. $this->assertEquals($isexp, $cookie->isExpired(), 'Got the wrong value for isExpired()');
  205. }
  206. }
  207. /**
  208. * Make sure we get the correct value for 'isExpired', when time is manually set
  209. */
  210. public function testIsExpiredDifferentTime()
  211. {
  212. $notexpired = time() + 3600;
  213. $expired = time() - 3600;
  214. $now = time() + 7200;
  215. $cookies = array(
  216. 'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $notexpired),
  217. 'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $expired)
  218. );
  219. // Make sure all cookies are expired
  220. foreach ($cookies as $cstr) {
  221. $cookie = Zend_Http_Cookie::fromString($cstr);
  222. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  223. $this->assertTrue($cookie->isExpired($now), 'Cookie is expected to be expired');
  224. }
  225. // Make sure all cookies are not expired
  226. $now = time() - 7200;
  227. foreach ($cookies as $cstr) {
  228. $cookie = Zend_Http_Cookie::fromString($cstr);
  229. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  230. $this->assertFalse($cookie->isExpired($now), 'Cookie is expected not to be expired');
  231. }
  232. }
  233. /**
  234. * Test we can properly check if a cookie is a session cookie (has no expiry time)
  235. */
  236. public function testIsSessionCookie()
  237. {
  238. $cookies = array(
  239. 'cookie=foo; path=/foo/baz; secure; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => false,
  240. 'cookie=foo; path=/; domain=.example.com;' => true,
  241. 'cookie=foo; path=/; secure; domain=.example.com;' => true,
  242. 'cookie=foo; path=/; domain=.example.com; secure; expires=' . date(DATE_COOKIE) => false
  243. );
  244. foreach ($cookies as $cstr => $issession) {
  245. $cookie = Zend_Http_Cookie::fromString($cstr);
  246. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  247. $this->assertEquals($issession, $cookie->isSessionCookie(), 'isSessionCookie is not as expected');
  248. }
  249. }
  250. /**
  251. * Make sure cookies are properly converted back to strings
  252. */
  253. public function testToString()
  254. {
  255. $cookies = array(
  256. 'name=value;',
  257. 'blank=;',
  258. 'urlencodedstuff=' . urlencode('!@#$)(@$%_+{} !@#?^&') . ';',
  259. );
  260. foreach ($cookies as $cstr) {
  261. $cookie = Zend_Http_Cookie::fromString($cstr, 'http://example.com');
  262. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  263. $this->assertEquals($cstr, $cookie->__toString(), 'Cookie is not converted back to the expected string');
  264. }
  265. }
  266. public function testGarbageInStrIsIgnored()
  267. {
  268. $cookies = array(
  269. 'name=value; domain=foo.com; silly=place; secure',
  270. 'foo=value; someCrap; secure; domain=foo.com; ',
  271. 'anothercookie=value; secure; has some crap; ignore=me; domain=foo.com; '
  272. );
  273. foreach ($cookies as $cstr) {
  274. $cookie = Zend_Http_Cookie::fromString($cstr);
  275. if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
  276. $this->assertEquals('value', $cookie->getValue(), 'Value is not as expected');
  277. $this->assertEquals('foo.com', $cookie->getDomain(), 'Domain is not as expected');
  278. $this->assertTrue($cookie->isSecure(), 'Cookie is expected to be secure');
  279. }
  280. }
  281. /**
  282. * Test the match() method against a domain
  283. *
  284. */
  285. public function testMatchDomain()
  286. {
  287. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
  288. $this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
  289. $this->assertFalse($cookie->match('http://www.somexample.com/foo/bar.php'), 'Cookie expected not to match, but did');
  290. $uri = Zend_Uri::factory('http://www.foo.com/some/file.txt');
  291. $cookie = Zend_Http_Cookie::fromString('cookie=value; domain=www.foo.com');
  292. $this->assertTrue($cookie->match($uri), 'Cookie expected to match, but didn\'t');
  293. $this->assertTrue($cookie->match('http://il.www.foo.com'), 'Cookie expected to match, but didn\'t');
  294. $this->assertFalse($cookie->match('http://bar.foo.com'), 'Cookie expected not to match, but did');
  295. }
  296. /**
  297. * Test the match() method against a domain
  298. *
  299. */
  300. public function testMatchPath()
  301. {
  302. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; path=/foo');
  303. $this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
  304. $this->assertFalse($cookie->match('http://www.example.com/bar.php'), 'Cookie expected not to match, but did');
  305. $cookie = Zend_Http_Cookie::fromString('cookie=value; domain=www.foo.com; path=/some/long/path');
  306. $this->assertTrue($cookie->match('http://www.foo.com/some/long/path/file.txt'), 'Cookie expected to match, but didn\'t');
  307. $this->assertTrue($cookie->match('http://www.foo.com/some/long/path/and/even/more'), 'Cookie expected to match, but didn\'t');
  308. $this->assertFalse($cookie->match('http://www.foo.com/some/long/file.txt'), 'Cookie expected not to match, but did');
  309. $this->assertFalse($cookie->match('http://www.foo.com/some/different/path/file.txt'), 'Cookie expected not to match, but did');
  310. }
  311. /**
  312. * Test the match() method against secure / non secure connections
  313. *
  314. */
  315. public function testMatchSecure()
  316. {
  317. // A non secure cookie, should match both
  318. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
  319. $this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
  320. $this->assertTrue($cookie->match('https://www.example.com/bar.php'), 'Cookie expected to match, but didn\'t');
  321. // A secure cookie, should match secure connections only
  322. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; secure');
  323. $this->assertFalse($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected not to match, but it did');
  324. $this->assertTrue($cookie->match('https://www.example.com/bar.php'), 'Cookie expected to match, but didn\'t');
  325. }
  326. /**
  327. * Test the match() method against different expiry times
  328. *
  329. */
  330. public function testMatchExpire()
  331. {
  332. // A session cookie - should always be valid
  333. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
  334. $this->assertTrue($cookie->match('http://www.example.com/'), 'Cookie expected to match, but didn\'t');
  335. $this->assertTrue($cookie->match('http://www.example.com/', true, time() + 3600), 'Cookie expected to match, but didn\'t');
  336. // A session cookie, should not match
  337. $this->assertFalse($cookie->match('https://www.example.com/', false), 'Cookie expected not to match, but it did');
  338. $this->assertFalse($cookie->match('https://www.example.com/', false, time() - 3600), 'Cookie expected not to match, but it did');
  339. // A cookie with expiry time in the future
  340. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; expires=' . date(DATE_COOKIE, time() + 3600));
  341. $this->assertTrue($cookie->match('http://www.example.com/'), 'Cookie expected to match, but didn\'t');
  342. $this->assertFalse($cookie->match('https://www.example.com/', true, time() + 7200), 'Cookie expected not to match, but it did');
  343. // A cookie with expiry time in the past
  344. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; expires=' . date(DATE_COOKIE, time() - 3600));
  345. $this->assertFalse($cookie->match('http://www.example.com/'), 'Cookie expected not to match, but it did');
  346. $this->assertTrue($cookie->match('https://www.example.com/', true, time() - 7200), 'Cookie expected to match, but didn\'t');
  347. }
  348. public function testFromStringFalse()
  349. {
  350. $cookie = Zend_Http_Cookie::fromString('foo; domain=www.exmaple.com');
  351. $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
  352. $cookie = Zend_Http_Cookie::fromString('=bar; secure; domain=foo.nl');
  353. $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
  354. $cookie = Zend_Http_Cookie::fromString('fo;o=bar; secure; domain=foo.nl');
  355. $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
  356. }
  357. /**
  358. * Test that cookies with far future expiry date (beyond the 32 bit unsigned int range) are
  359. * not mistakenly marked as 'expired'
  360. *
  361. * @link http://framework.zend.com/issues/browse/ZF-5690
  362. */
  363. public function testZF5690OverflowingExpiryDate()
  364. {
  365. $expTime = "Sat, 29-Jan-2039 00:54:42 GMT";
  366. $cookie = Zend_Http_Cookie::fromString("foo=bar; domain=.example.com; expires=$expTime");
  367. $this->assertFalse($cookie->isExpired(), 'Expiry: ' . $cookie->getExpiryTime());
  368. }
  369. }