FilterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. use PrivateBin\Filter;
  3. use Eris\Generator;
  4. class FilterTest extends PHPUnit_Framework_TestCase
  5. {
  6. use Eris\TestTrait;
  7. public function testFilterStripsSlashesDeeply()
  8. {
  9. $this->assertEquals(
  10. array("f'oo", "b'ar", array("fo'o", "b'ar")),
  11. Filter::stripslashesDeep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")))
  12. );
  13. }
  14. public function testFilterMakesTimesHumanlyReadable()
  15. {
  16. $this->forAll(
  17. Generator\nat(),
  18. Generator\oneOf(
  19. 'sec', 'second', 'seconds'
  20. )
  21. )->then(
  22. function ($int, $unit)
  23. {
  24. $suffix = $int === 1 ? '' : 's';
  25. $this->assertEquals($int . ' second' . $suffix, Filter::formatHumanReadableTime($int . $unit));
  26. }
  27. );
  28. $this->forAll(
  29. Generator\nat(),
  30. Generator\oneOf(
  31. 'min', 'minute', 'minutes'
  32. )
  33. )->then(
  34. function ($int, $unit)
  35. {
  36. $suffix = $int === 1 ? '' : 's';
  37. $this->assertEquals($int . ' minute' . $suffix, Filter::formatHumanReadableTime($int . $unit));
  38. }
  39. );
  40. $this->forAll(
  41. Generator\nat(),
  42. Generator\oneOf(
  43. 'hour', 'hours', 'day', 'days', 'week', 'weeks',
  44. 'month', 'months', 'year', 'years'
  45. )
  46. )->then(
  47. function ($int, $unit)
  48. {
  49. $suffix = $int === 1 ? '' : 's';
  50. $this->assertEquals($int . ' ' . rtrim($unit, 's') . $suffix, Filter::formatHumanReadableTime($int . $unit));
  51. }
  52. );
  53. }
  54. /**
  55. * @expectedException Exception
  56. * @expectedExceptionCode 30
  57. */
  58. public function testFilterFailTimesHumanlyReadable()
  59. {
  60. $this->forAll(
  61. Generator\string()
  62. )->then(
  63. function ($string)
  64. {
  65. Filter::formatHumanReadableTime($string);
  66. }
  67. );
  68. }
  69. public function testFilterMakesSizesHumanlyReadable()
  70. {
  71. $this->forAll(
  72. Generator\neg()
  73. )->then(
  74. function ($int)
  75. {
  76. $this->assertEquals(number_format($int, 0, '.', ' ') . ' B', Filter::formatHumanReadableSize($int));
  77. }
  78. );
  79. $from = 0;
  80. $exponent = 1024;
  81. $to = $exponent - 1;
  82. $this->forAll(
  83. Generator\choose($from, $to)
  84. )->then(
  85. function ($int)
  86. {
  87. $this->assertEquals(number_format($int, 0, '.', ' ') . ' B', Filter::formatHumanReadableSize($int));
  88. }
  89. );
  90. $from = $exponent;
  91. $exponent *= 1024;
  92. $to = $exponent - 1;
  93. $this->forAll(
  94. Generator\choose($from, $to),
  95. $from
  96. )->then(
  97. function ($int, $divisor)
  98. {
  99. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' KiB', Filter::formatHumanReadableSize($int));
  100. }
  101. );
  102. $from = $exponent;
  103. $exponent *= 1024;
  104. $to = $exponent - 1;
  105. $this->forAll(
  106. Generator\choose($from, $to),
  107. $from
  108. )->then(
  109. function ($int, $divisor)
  110. {
  111. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' MiB', Filter::formatHumanReadableSize($int));
  112. }
  113. );
  114. $from = $exponent;
  115. $exponent *= 1024;
  116. $to = $exponent - 1;
  117. $this->forAll(
  118. Generator\choose($from, $to),
  119. $from
  120. )->then(
  121. function ($int, $divisor)
  122. {
  123. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' GiB', Filter::formatHumanReadableSize($int));
  124. }
  125. );
  126. $from = $exponent;
  127. $exponent *= 1024;
  128. $to = $exponent - 1;
  129. $this->forAll(
  130. Generator\choose($from, $to),
  131. $from
  132. )->then(
  133. function ($int, $divisor)
  134. {
  135. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' TiB', Filter::formatHumanReadableSize($int));
  136. }
  137. );
  138. $from = $exponent;
  139. $exponent *= 1024;
  140. $to = $exponent - 1;
  141. $this->forAll(
  142. Generator\choose($from, $to),
  143. $from
  144. )->then(
  145. function ($int, $divisor)
  146. {
  147. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' PiB', Filter::formatHumanReadableSize($int));
  148. }
  149. );
  150. $from = $exponent;
  151. $exponent *= 1024;
  152. $to = $exponent - 1;
  153. // on 64bit systems, this gets larger then PHP_INT_MAX, so PHP casts it
  154. // to double and the "choose" generator can't handle it
  155. if ($to > PHP_INT_MAX) {
  156. $this->assertEquals('1.00 EiB', Filter::formatHumanReadableSize($from));
  157. $this->assertEquals('1.23 EiB', Filter::formatHumanReadableSize(1.234 * $from));
  158. $this->assertEquals('1 000.00 EiB', Filter::formatHumanReadableSize(1000 * $from));
  159. $this->assertEquals('1.00 ZiB', Filter::formatHumanReadableSize($exponent));
  160. $this->assertEquals('1.23 ZiB', Filter::formatHumanReadableSize(1.234 * $exponent));
  161. $this->assertEquals('1 000.00 ZiB', Filter::formatHumanReadableSize(1000 * $exponent));
  162. $exponent *= 1024;
  163. $this->assertEquals('1.00 YiB', Filter::formatHumanReadableSize($exponent));
  164. $this->assertEquals('1.23 YiB', Filter::formatHumanReadableSize(1.234 * $exponent));
  165. } else {
  166. $this->forAll(
  167. Generator\choose($from, $to),
  168. $from
  169. )->then(
  170. function ($int, $divisor)
  171. {
  172. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' EiB', Filter::formatHumanReadableSize($int));
  173. }
  174. );
  175. $from = $exponent;
  176. $exponent *= 1024;
  177. $to = $exponent - 1;
  178. $this->forAll(
  179. Generator\choose($from, $to),
  180. $from
  181. )->then(
  182. function ($int, $divisor)
  183. {
  184. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' ZiB', Filter::formatHumanReadableSize($int));
  185. }
  186. );
  187. $from = $exponent;
  188. $exponent *= 1024;
  189. $to = $exponent - 1;
  190. $this->forAll(
  191. Generator\choose($from, $to),
  192. $from
  193. )->then(
  194. function ($int, $divisor)
  195. {
  196. $this->assertEquals(number_format($int / $divisor, 2, '.', ' ') . ' YiB', Filter::formatHumanReadableSize($int));
  197. }
  198. );
  199. }
  200. }
  201. public function testSlowEquals()
  202. {
  203. $this->forAll(
  204. Generator\string()
  205. )->then(
  206. function ($string)
  207. {
  208. $this->assertTrue(Filter::slowEquals($string, $string), 'same string');
  209. }
  210. );
  211. $this->forAll(
  212. Generator\int()
  213. )->then(
  214. function ($int)
  215. {
  216. $this->assertTrue(Filter::slowEquals($int, $int), 'same integer');
  217. }
  218. );
  219. $this->forAll(
  220. Generator\float()
  221. )->then(
  222. function ($float)
  223. {
  224. $this->assertTrue(Filter::slowEquals($float, $float), 'same float');
  225. }
  226. );
  227. $this->forAll(
  228. Generator\string()
  229. )->then(
  230. function ($string)
  231. {
  232. $this->assertFalse(Filter::slowEquals($string, true), 'string and boolean true');
  233. }
  234. );
  235. $this->forAll(
  236. Generator\string()
  237. )->then(
  238. function ($string)
  239. {
  240. // false is casted into an empty string
  241. if ($string !== '') {
  242. $this->assertFalse(Filter::slowEquals($string, false), 'string and boolean false');
  243. }
  244. }
  245. );
  246. $this->forAll(
  247. Generator\string(),
  248. Generator\int()
  249. )->then(
  250. function ($string, $int)
  251. {
  252. $this->assertFalse(Filter::slowEquals($string, $int), 'string and integer');
  253. }
  254. );
  255. $this->forAll(
  256. Generator\string(),
  257. Generator\float()
  258. )->then(
  259. function ($string, $float)
  260. {
  261. $this->assertFalse(Filter::slowEquals($string, $float), 'string and float');
  262. }
  263. );
  264. $this->forAll(
  265. Generator\string(),
  266. Generator\string()
  267. )->then(
  268. function ($string1, $string2)
  269. {
  270. if ($string1 !== $string2) {
  271. $this->assertFalse(Filter::slowEquals($string1, $string2), 'different strings');
  272. }
  273. }
  274. );
  275. $this->forAll(
  276. Generator\string()
  277. )->then(
  278. function ($string)
  279. {
  280. $this->assertFalse(Filter::slowEquals($string, ' ' . $string), 'strings with space');
  281. }
  282. );
  283. $this->forAll(
  284. Generator\float()
  285. )->then(
  286. function ($float)
  287. {
  288. $this->assertFalse(Filter::slowEquals(strval($float), $float . '0'), 'floats as strings');
  289. }
  290. );
  291. $this->forAll(
  292. Generator\int()
  293. )->then(
  294. function ($int)
  295. {
  296. $this->assertFalse(Filter::slowEquals($int . 'e3', $int . '000'), 'integers as strings');
  297. }
  298. );
  299. // these two tests would be compared equal if casted to integers as they are larger then PHP_INT_MAX
  300. $this->assertFalse(Filter::slowEquals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
  301. $this->assertFalse(Filter::slowEquals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
  302. }
  303. }