@@ -43,21 +43,160 @@ public function testClosingStreamEmitsErrorForCurrentCommand()
43
43
$ this ->assertEquals (defined ('SOCKET_ECONNABORTED ' ) ? SOCKET_ECONNABORTED : 103 , $ error ->getCode ());
44
44
}
45
45
46
+ public function testParseValidAuthPluginWillSendAuthResponse ()
47
+ {
48
+ $ stream = new ThroughStream ();
49
+
50
+ $ outgoing = new ThroughStream ();
51
+ $ outgoing ->on ('data ' , $ this ->expectCallableOnceWith ("\x08\0\0\x01" . "response " ));
52
+
53
+ $ command = $ this ->getMockBuilder (AuthenticateCommand::class)->disableOriginalConstructor ()->getMock ();
54
+ $ command ->expects ($ this ->once ())->method ('authenticatePacket ' )->with ($ this ->anything (), 'caching_sha2_password ' )->willReturn ('response ' );
55
+
56
+ $ executor = new Executor ();
57
+ $ executor ->enqueue ($ command );
58
+
59
+ $ parser = new Parser (new CompositeStream ($ stream , $ outgoing ), $ executor );
60
+ $ parser ->start ();
61
+
62
+ $ stream ->write ("\x49\0\0\0\x0a\x38\x2e\x34\x2e\x35\0\x5e\0\0\0\x08\x0c\x41\x44\x12\x5e\x69\x59\0\xff\xff\xff\x02\0\xff\xdf\x15\0\0\0\0\0\0\0\0\0\0\x3c\x2c\x5e\x54\x06\x04\x01\x61\x01\x20\x79\x1b\0\x63\x61\x63\x68\x69\x6e\x67\x5f\x73\x68\x61\x32\x5f\x70\x61\x73\x73\x77\x6f\x72\x64\0" );
63
+
64
+ $ ref = new \ReflectionProperty ($ parser , 'authPlugin ' );
65
+ $ ref ->setAccessible (true );
66
+ $ this ->assertEquals ('caching_sha2_password ' , $ ref ->getValue ($ parser ));
67
+ }
68
+
46
69
public function testUnexpectedAuthPluginShouldEmitErrorOnAuthenticateCommandAndCloseStream ()
47
70
{
48
71
$ stream = new ThroughStream ();
49
72
$ stream ->on ('close ' , $ this ->expectCallableOnce ());
50
73
51
74
$ command = new AuthenticateCommand ('root ' , '' , 'test ' , 'utf8mb4 ' );
52
- $ command ->on ('error ' , $ this ->expectCallableOnceWith (new \UnexpectedValueException ('Unknown authentication plugin "caching_sha2_password " requested by server ' )));
75
+ $ command ->on ('error ' , $ this ->expectCallableOnceWith (new \UnexpectedValueException ('Unknown authentication plugin "sha256_password " requested by server ' )));
53
76
54
77
$ executor = new Executor ();
55
78
$ executor ->enqueue ($ command );
56
79
57
80
$ parser = new Parser ($ stream , $ executor );
58
81
$ parser ->start ();
59
82
60
- $ stream ->write ("\x49\0\0\0\x0a\x38\x2e\x34\x2e\x35\0\x5e\0\0\0\x08\x0c\x41\x44\x12\x5e\x69\x59\0\xff\xff\xff\x02\0\xff\xdf\x15\0\0\0\0\0\0\0\0\0\0\x3c\x2c\x5e\x54\x06\x04\x01\x61\x01\x20\x79\x1b\0\x63\x61\x63\x68\x69\x6e\x67\x5f\x73\x68\x61\x32\x5f\x70\x61\x73\x73\x77\x6f\x72\x64\0" );
83
+ $ stream ->write ("\x43\0\0\0\x0a\x38\x2e\x34\x2e\x35\0\x5e\0\0\0\x08\x0c\x41\x44\x12\x5e\x69\x59\0\xff\xff\xff\x02\0\xff\xdf\x15\0\0\0\0\0\0\0\0\0\0\x3c\x2c\x5e\x54\x06\x04\x01\x61\x01\x20\x79\x1b\0\x73\x68\x61\x32\x35\x36\x5f\x70\x61\x73\x73\x77\x6f\x72\x64\0" );
84
+ }
85
+
86
+ public function testParseAuthMoreDataWithFastAuthSuccessWillPrintDebugLogAndWaitForOkPacketWithoutSendingPacket ()
87
+ {
88
+ $ stream = new ThroughStream ();
89
+ $ stream ->on ('close ' , $ this ->expectCallableNever ());
90
+
91
+ $ outgoing = new ThroughStream ();
92
+ $ outgoing ->on ('data ' , $ this ->expectCallableNever ());
93
+
94
+ $ executor = new Executor ();
95
+
96
+ $ parser = new Parser (new CompositeStream ($ stream , $ outgoing ), $ executor );
97
+ $ parser ->start ();
98
+
99
+ $ ref = new \ReflectionProperty ($ parser , 'debug ' );
100
+ $ ref ->setAccessible (true );
101
+ $ ref ->setValue ($ parser , true );
102
+
103
+ $ ref = new \ReflectionProperty ($ parser , 'phase ' );
104
+ $ ref ->setAccessible (true );
105
+ $ ref ->setValue ($ parser , Parser::PHASE_AUTH_SENT );
106
+
107
+ $ ref = new \ReflectionProperty ($ parser , 'authPlugin ' );
108
+ $ ref ->setAccessible (true );
109
+ $ ref ->setValue ($ parser , 'caching_sha2_password ' );
110
+
111
+ $ this ->expectOutputRegex ('/Fast auth success\n$/ ' );
112
+ $ stream ->write ("\x02\0\0\0" . "\x01\x03" );
113
+ }
114
+
115
+ public function testParseAuthMoreDataWithFastAuthFailureWillSendCertificateRequest ()
116
+ {
117
+ $ stream = new ThroughStream ();
118
+ $ stream ->on ('close ' , $ this ->expectCallableNever ());
119
+
120
+ $ outgoing = new ThroughStream ();
121
+ $ outgoing ->on ('data ' , $ this ->expectCallableOnceWith ("\x01\0\0\x01" . "\x02" ));
122
+
123
+ $ executor = new Executor ();
124
+
125
+ $ parser = new Parser (new CompositeStream ($ stream , $ outgoing ), $ executor );
126
+ $ parser ->start ();
127
+
128
+ $ ref = new \ReflectionProperty ($ parser , 'phase ' );
129
+ $ ref ->setAccessible (true );
130
+ $ ref ->setValue ($ parser , Parser::PHASE_AUTH_SENT );
131
+
132
+ $ ref = new \ReflectionProperty ($ parser , 'authPlugin ' );
133
+ $ ref ->setAccessible (true );
134
+ $ ref ->setValue ($ parser , 'caching_sha2_password ' );
135
+
136
+ $ stream ->write ("\x02\0\0\0" . "\x01\x04" );
137
+ }
138
+
139
+ public function testParseAuthMoreDataWithCertificateWillSendEncryptedPassword ()
140
+ {
141
+ $ stream = new ThroughStream ();
142
+ $ stream ->on ('close ' , $ this ->expectCallableNever ());
143
+
144
+ $ outgoing = new ThroughStream ();
145
+ $ outgoing ->on ('data ' , $ this ->expectCallableOnceWith ("\x09\0\0\x01" . "encrypted " ));
146
+
147
+ $ command = $ this ->getMockBuilder (AuthenticateCommand::class)->disableOriginalConstructor ()->getMock ();
148
+ $ command ->expects ($ this ->once ())->method ('authSha256 ' )->with ('' , '--- ' )->willReturn ('encrypted ' );
149
+
150
+ $ executor = new Executor ();
151
+
152
+ $ parser = new Parser (new CompositeStream ($ stream , $ outgoing ), $ executor );
153
+ $ parser ->start ();
154
+
155
+ $ ref = new \ReflectionProperty ($ parser , 'phase ' );
156
+ $ ref ->setAccessible (true );
157
+ $ ref ->setValue ($ parser , Parser::PHASE_AUTH_SENT );
158
+
159
+ $ ref = new \ReflectionProperty ($ parser , 'authPlugin ' );
160
+ $ ref ->setAccessible (true );
161
+ $ ref ->setValue ($ parser , 'caching_sha2_password ' );
162
+
163
+ $ ref = new \ReflectionProperty ($ parser , 'currCommand ' );
164
+ $ ref ->setAccessible (true );
165
+ $ ref ->setValue ($ parser , $ command );
166
+
167
+ $ stream ->write ("\x04\0\0\0" . "\x01--- " );
168
+ }
169
+
170
+ public function testAuthMoreDataWithCertificateWillEmitErrorAndCloseConnectionWhenEncryptingPasswordThrows ()
171
+ {
172
+ $ stream = new ThroughStream ();
173
+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
174
+
175
+ $ outgoing = new ThroughStream ();
176
+ $ outgoing ->on ('data ' , $ this ->expectCallableNever ());
177
+
178
+ $ command = $ this ->getMockBuilder (AuthenticateCommand::class)->disableOriginalConstructor ()->getMock ();
179
+ $ command ->expects ($ this ->once ())->method ('authSha256 ' )->with ('' , '--- ' )->willThrowException (new \UnexpectedValueException ('Error ' ));
180
+ $ command ->expects ($ this ->once ())->method ('emit ' )->with ('error ' , [new \UnexpectedValueException ('Error ' )]);
181
+
182
+ $ executor = new Executor ();
183
+
184
+ $ parser = new Parser (new CompositeStream ($ stream , $ outgoing ), $ executor );
185
+ $ parser ->start ();
186
+
187
+ $ ref = new \ReflectionProperty ($ parser , 'phase ' );
188
+ $ ref ->setAccessible (true );
189
+ $ ref ->setValue ($ parser , Parser::PHASE_AUTH_SENT );
190
+
191
+ $ ref = new \ReflectionProperty ($ parser , 'authPlugin ' );
192
+ $ ref ->setAccessible (true );
193
+ $ ref ->setValue ($ parser , 'caching_sha2_password ' );
194
+
195
+ $ ref = new \ReflectionProperty ($ parser , 'currCommand ' );
196
+ $ ref ->setAccessible (true );
197
+ $ ref ->setValue ($ parser , $ command );
198
+
199
+ $ stream ->write ("\x04\0\0\0" . "\x01--- " );
61
200
}
62
201
63
202
public function testUnexpectedErrorWithoutCurrentCommandWillBeIgnored ()
0 commit comments