FilterTest.php 9.8 KB

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