IpBlackListComponentTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Ratchet\Server;
  3. use Ratchet\Server\IpBlackList;
  4. /**
  5. * @covers Ratchet\Server\IpBlackList
  6. */
  7. class IpBlackListTest extends \PHPUnit_Framework_TestCase {
  8. protected $blocker;
  9. protected $mock;
  10. public function setUp() {
  11. $this->mock = $this->getMock('\\Ratchet\\MessageComponentInterface');
  12. $this->blocker = new IpBlackList($this->mock);
  13. }
  14. public function testOnOpen() {
  15. $this->mock->expects($this->exactly(3))->method('onOpen');
  16. $conn1 = $this->newConn();
  17. $conn2 = $this->newConn();
  18. $conn3 = $this->newConn();
  19. $this->blocker->onOpen($conn1);
  20. $this->blocker->onOpen($conn3);
  21. $this->blocker->onOpen($conn2);
  22. }
  23. public function testBlockDoesNotTriggerOnOpen() {
  24. $conn = $this->newConn();
  25. $this->blocker->blockAddress($conn->remoteAddress);
  26. $this->mock->expects($this->never())->method('onOpen');
  27. $ret = $this->blocker->onOpen($conn);
  28. }
  29. public function testBlockDoesNotTriggerOnClose() {
  30. $conn = $this->newConn();
  31. $this->blocker->blockAddress($conn->remoteAddress);
  32. $this->mock->expects($this->never())->method('onClose');
  33. $ret = $this->blocker->onOpen($conn);
  34. }
  35. public function testOnMessageDecoration() {
  36. $conn = $this->newConn();
  37. $msg = 'Hello not being blocked';
  38. $this->mock->expects($this->once())->method('onMessage')->with($conn, $msg);
  39. $this->blocker->onMessage($conn, $msg);
  40. }
  41. public function testOnCloseDecoration() {
  42. $conn = $this->newConn();
  43. $this->mock->expects($this->once())->method('onClose')->with($conn);
  44. $this->blocker->onClose($conn);
  45. }
  46. public function testBlockClosesConnection() {
  47. $conn = $this->newConn();
  48. $this->blocker->blockAddress($conn->remoteAddress);
  49. $conn->expects($this->once())->method('close');
  50. $this->blocker->onOpen($conn);
  51. }
  52. public function testAddAndRemoveWithFluentInterfaces() {
  53. $blockOne = '127.0.0.1';
  54. $blockTwo = '192.168.1.1';
  55. $unblock = '75.119.207.140';
  56. $this->blocker
  57. ->blockAddress($unblock)
  58. ->blockAddress($blockOne)
  59. ->unblockAddress($unblock)
  60. ->blockAddress($blockTwo)
  61. ;
  62. $this->assertEquals(array($blockOne, $blockTwo), $this->blocker->getBlockedAddresses());
  63. }
  64. public function testDecoratorPassesErrors() {
  65. $conn = $this->newConn();
  66. $e = new \Exception('I threw an error');
  67. $this->mock->expects($this->once())->method('onError')->with($conn, $e);
  68. $this->blocker->onError($conn, $e);
  69. }
  70. public function addressProvider() {
  71. return array(
  72. array('127.0.0.1', '127.0.0.1')
  73. , array('localhost', 'localhost')
  74. , array('fe80::1%lo0', 'fe80::1%lo0')
  75. , array('127.0.0.1', '127.0.0.1:6392')
  76. );
  77. }
  78. /**
  79. * @dataProvider addressProvider
  80. */
  81. public function testFilterAddress($expected, $input) {
  82. $this->assertEquals($expected, $this->blocker->filterAddress($input));
  83. }
  84. public function testUnblockingSilentlyFails() {
  85. $this->assertInstanceOf('\\Ratchet\\Server\\IpBlackList', $this->blocker->unblockAddress('localhost'));
  86. }
  87. protected function newConn() {
  88. $conn = $this->getMock('\\Ratchet\\ConnectionInterface');
  89. $conn->remoteAddress = '127.0.0.1';
  90. return $conn;
  91. }
  92. }