PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 5ms app.codeStats 0ms

/tests/PHPUnit/Core/CookieTest.php

https://github.com/CodeYellowBV/piwik
PHP | 157 lines | 131 code | 11 blank | 15 comment | 0 complexity | c30d94b62a53c821a0844895c29ca1c0 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. class CookieTest extends PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * Dataprovider for testJsonSerialize
  12. */
  13. public function getJsonSerializeData()
  14. {
  15. return array(
  16. array('null', null),
  17. array('bool false', false),
  18. array('bool true', true),
  19. array('negative int', -42),
  20. array('zero', 0),
  21. array('positive int', 42),
  22. array('float', 1.25),
  23. array('empty string', ''),
  24. array('nul in string', "\0"),
  25. array('carriage return in string', "first line\r\nsecond line"),
  26. array('utf7 in string', 'hello, world'),
  27. array('utf8 in string', '是'),
  28. array('empty array', array()),
  29. array('single element array', array("test")),
  30. array('associative array', array("alpha", 2 => "beta")),
  31. array('mixed keys', array('first' => 'john', 'last' => 'doe', 10 => 'age')),
  32. array('nested arrays', array('top' => array('middle' => 2, array('bottom'), 'last'), 'the end' => true)),
  33. array('array confusion', array('"', "'", '}', ';', ':')),
  34. );
  35. }
  36. /**
  37. * @group Core
  38. *
  39. * @dataProvider getJsonSerializeData
  40. */
  41. public function testJsonSerialize($testData, $id)
  42. {
  43. // @see http://bugs.php.net/38680
  44. if (PHP_VERSION >= '5.2.0' && PHP_VERSION < '5.2.1') {
  45. $this->markTestSkipped('see http://bugs.php.net/38680');
  46. }
  47. $this->assertEquals($testData, json_decode(json_encode($testData), $assoc = true), $id);
  48. }
  49. /**
  50. * Dataprovider for testSafeSerialize
  51. */
  52. public function getSafeSerializeData()
  53. {
  54. return array(
  55. array('null', null),
  56. array('bool false', false),
  57. array('bool true', true),
  58. array('negative int', -42),
  59. array('zero', 0),
  60. array('positive int', 42),
  61. array('float', 1.25),
  62. array('empty string', ''),
  63. array('nul in string', "\0"),
  64. array('carriage return in string', "first line\r\nsecond line"),
  65. array('utf7 in string', 'hello, world'),
  66. array('utf8 in string', '是'),
  67. array('empty array', array()),
  68. array('single element array', array("test")),
  69. array('associative array', array("alpha", 2 => "beta")),
  70. array('mixed keys', array('first' => 'john', 'last' => 'doe', 10 => 'age')),
  71. array('nested arrays', array('top' => array('middle' => 2, array('bottom'), 'last'), 'the end' => true)),
  72. array('array confusion', array('"', "'", '}', ';', ':')),
  73. );
  74. }
  75. /**
  76. * @group Core
  77. *
  78. * @dataProvider getSafeSerializeData
  79. */
  80. public function testSafeSerialize($id, $testData)
  81. {
  82. $this->assertEquals(serialize($testData), safe_serialize($testData), $id);
  83. $this->assertEquals($testData, unserialize(safe_serialize($testData)), $id);
  84. $this->assertSame($testData, safe_unserialize(safe_serialize($testData)), $id);
  85. $this->assertSame($testData, safe_unserialize(serialize($testData)), $id);
  86. }
  87. /**
  88. * @group Core
  89. */
  90. public function testSafeUnserialize()
  91. {
  92. /*
  93. * serialize() uses its internal maachine representation when floats expressed in E-notation,
  94. * which may vary between php versions, OS, and hardware platforms
  95. */
  96. $testData = $tests['exp float'] = -5.0E+142;
  97. // intentionally disabled; this doesn't work
  98. // $this->assertEquals( safe_serialize($testData), serialize($testData) );
  99. $this->assertEquals($testData, unserialize(safe_serialize($testData)));
  100. $this->assertSame($testData, safe_unserialize(safe_serialize($testData)));
  101. // workaround: cast floats into strings
  102. $this->assertSame($testData, safe_unserialize(serialize($testData)));
  103. $unserialized = array(
  104. 'announcement' => true,
  105. 'source' => array(
  106. array(
  107. 'filename' => 'php-5.3.3.tar.bz2',
  108. 'name' => 'PHP 5.3.3 (tar.bz2)',
  109. 'md5' => '21ceeeb232813c10283a5ca1b4c87b48',
  110. 'date' => '22 July 2010',
  111. ),
  112. array(
  113. 'filename' => 'php-5.3.3.tar.gz',
  114. 'name' => 'PHP 5.3.3 (tar.gz)',
  115. 'md5' => '5adf1a537895c2ec933fddd48e78d8a2',
  116. 'date' => '22 July 2010',
  117. ),
  118. ),
  119. 'date' => '22 July 2010',
  120. 'version' => '5.3.3',
  121. );
  122. $serialized = 'a:4:{s:12:"announcement";b:1;s:6:"source";a:2:{i:0;a:4:{s:8:"filename";s:17:"php-5.3.3.tar.bz2";s:4:"name";s:19:"PHP 5.3.3 (tar.bz2)";s:3:"md5";s:32:"21ceeeb232813c10283a5ca1b4c87b48";s:4:"date";s:12:"22 July 2010";}i:1;a:4:{s:8:"filename";s:16:"php-5.3.3.tar.gz";s:4:"name";s:18:"PHP 5.3.3 (tar.gz)";s:3:"md5";s:32:"5adf1a537895c2ec933fddd48e78d8a2";s:4:"date";s:12:"22 July 2010";}}s:4:"date";s:12:"22 July 2010";s:7:"version";s:5:"5.3.3";}';
  123. $this->assertSame($unserialized, unserialize($serialized));
  124. $this->assertEquals($serialized, serialize($unserialized));
  125. $this->assertSame($unserialized, safe_unserialize($serialized));
  126. $this->assertEquals($serialized, safe_serialize($unserialized));
  127. $this->assertSame($unserialized, safe_unserialize(safe_serialize($unserialized)));
  128. $this->assertEquals($serialized, safe_serialize(safe_unserialize($serialized)));
  129. $a = 'O:31:"Test_Piwik_Cookie_Phantom_Class":0:{}';
  130. $this->assertFalse(safe_unserialize($a), "test: unserializing an object where class not (yet) defined");
  131. $a = 'O:28:"Test_Piwik_Cookie_Mock_Class":0:{}';
  132. $this->assertFalse(safe_unserialize($a), "test: unserializing an object where class is defined");
  133. $a = 'a:1:{i:0;O:28:"Test_Piwik_Cookie_Mock_Class":0:{}}';
  134. $this->assertFalse(safe_unserialize($a), "test: unserializing nested object where class is defined");
  135. $a = 'a:2:{i:0;s:4:"test";i:1;O:28:"Test_Piwik_Cookie_Mock_Class":0:{}}';
  136. $this->assertFalse(safe_unserialize($a), "test: unserializing another nested object where class is defined");
  137. $a = 'O:28:"Test_Piwik_Cookie_Mock_Class":1:{s:34:"' . "\0" . 'Test_Piwik_Cookie_Mock_Class' . "\0" . 'name";s:4:"test";}';
  138. $this->assertFalse(safe_unserialize($a), "test: unserializing object with member where class is defined");
  139. // arrays and objects cannot be used as keys, i.e., generates "Warning: Illegal offset type ..."
  140. $a = 'a:2:{i:0;a:0:{}O:28:"Test_Piwik_Cookie_Mock_Class":0:{}s:4:"test";';
  141. $this->assertFalse(safe_unserialize($a), "test: unserializing with illegal key");
  142. }
  143. }