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