4
4
5
5
use React \Socket \SecureServer ;
6
6
use React \Socket \TcpServer ;
7
+ use React \Promise \Promise ;
7
8
8
9
class SecureServerTest extends TestCase
9
10
{
@@ -74,14 +75,14 @@ public function testCloseWillBePassedThroughToTcpServer()
74
75
$ server ->close ();
75
76
}
76
77
77
- public function testConnectionWillBeEndedWithErrorIfItIsNotAStream ()
78
+ public function testConnectionWillBeClosedWithErrorIfItIsNotAStream ()
78
79
{
79
80
$ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
80
81
81
82
$ tcp = new TcpServer (0 , $ loop );
82
83
83
84
$ connection = $ this ->getMockBuilder ('React\Socket\ConnectionInterface ' )->getMock ();
84
- $ connection ->expects ($ this ->once ())->method ('end ' );
85
+ $ connection ->expects ($ this ->once ())->method ('close ' );
85
86
86
87
$ server = new SecureServer ($ tcp , $ loop , array ());
87
88
@@ -90,6 +91,74 @@ public function testConnectionWillBeEndedWithErrorIfItIsNotAStream()
90
91
$ tcp ->emit ('connection ' , array ($ connection ));
91
92
}
92
93
94
+ public function testConnectionWillTryToEnableEncryptionAndWaitForHandshake ()
95
+ {
96
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
97
+
98
+ $ tcp = new TcpServer (0 , $ loop );
99
+
100
+ $ connection = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->getMock ();
101
+ $ connection ->expects ($ this ->once ())->method ('getRemoteAddress ' )->willReturn ('tcp://127.0.0.1:1234 ' );
102
+ $ connection ->expects ($ this ->never ())->method ('close ' );
103
+
104
+ $ server = new SecureServer ($ tcp , $ loop , array ());
105
+
106
+ $ pending = new Promise (function () { });
107
+
108
+ $ encryption = $ this ->getMockBuilder ('React\Socket\StreamEncryption ' )->disableOriginalConstructor ()->getMock ();
109
+ $ encryption ->expects ($ this ->once ())->method ('enable ' )->willReturn ($ pending );
110
+
111
+ $ ref = new \ReflectionProperty ($ server , 'encryption ' );
112
+ $ ref ->setAccessible (true );
113
+ $ ref ->setValue ($ server , $ encryption );
114
+
115
+ $ ref = new \ReflectionProperty ($ server , 'context ' );
116
+ $ ref ->setAccessible (true );
117
+ $ ref ->setValue ($ server , array ());
118
+
119
+ $ server ->on ('error ' , $ this ->expectCallableNever ());
120
+ $ server ->on ('connection ' , $ this ->expectCallableNever ());
121
+
122
+ $ tcp ->emit ('connection ' , array ($ connection ));
123
+ }
124
+
125
+ public function testConnectionWillBeClosedWithErrorIfEnablingEncryptionFails ()
126
+ {
127
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
128
+
129
+ $ tcp = new TcpServer (0 , $ loop );
130
+
131
+ $ connection = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->getMock ();
132
+ $ connection ->expects ($ this ->once ())->method ('getRemoteAddress ' )->willReturn ('tcp://127.0.0.1:1234 ' );
133
+ $ connection ->expects ($ this ->once ())->method ('close ' );
134
+
135
+ $ server = new SecureServer ($ tcp , $ loop , array ());
136
+
137
+ $ error = new \RuntimeException ('Original ' );
138
+
139
+ $ encryption = $ this ->getMockBuilder ('React\Socket\StreamEncryption ' )->disableOriginalConstructor ()->getMock ();
140
+ $ encryption ->expects ($ this ->once ())->method ('enable ' )->willReturn (\React \Promise \reject ($ error ));
141
+
142
+ $ ref = new \ReflectionProperty ($ server , 'encryption ' );
143
+ $ ref ->setAccessible (true );
144
+ $ ref ->setValue ($ server , $ encryption );
145
+
146
+ $ ref = new \ReflectionProperty ($ server , 'context ' );
147
+ $ ref ->setAccessible (true );
148
+ $ ref ->setValue ($ server , array ());
149
+
150
+ $ error = null ;
151
+ $ server ->on ('error ' , $ this ->expectCallableOnce ());
152
+ $ server ->on ('error ' , function ($ e ) use (&$ error ) {
153
+ $ error = $ e ;
154
+ });
155
+
156
+ $ tcp ->emit ('connection ' , array ($ connection ));
157
+
158
+ $ this ->assertInstanceOf ('RuntimeException ' , $ error );
159
+ $ this ->assertEquals ('Connection from tcp://127.0.0.1:1234 failed during TLS handshake: Original ' , $ error ->getMessage ());
160
+ }
161
+
93
162
public function testSocketErrorWillBeForwarded ()
94
163
{
95
164
$ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
0 commit comments