@@ -7,6 +7,9 @@ package ssh
7
7
import (
8
8
"bytes"
9
9
"crypto/rand"
10
+ "errors"
11
+ "fmt"
12
+ "net"
10
13
"strings"
11
14
"testing"
12
15
)
@@ -207,9 +210,12 @@ func TestBannerCallback(t *testing.T) {
207
210
}
208
211
209
212
func TestNewClientConn (t * testing.T ) {
213
+ errHostKeyMismatch := errors .New ("host key mismatch" )
214
+
210
215
for _ , tt := range []struct {
211
- name string
212
- user string
216
+ name string
217
+ user string
218
+ simulateHostKeyMismatch HostKeyCallback
213
219
}{
214
220
{
215
221
name : "good user field for ConnMetadata" ,
@@ -219,6 +225,13 @@ func TestNewClientConn(t *testing.T) {
219
225
name : "empty user field for ConnMetadata" ,
220
226
user : "" ,
221
227
},
228
+ {
229
+ name : "host key mismatch" ,
230
+ user : "testuser" ,
231
+ simulateHostKeyMismatch : func (hostname string , remote net.Addr , key PublicKey ) error {
232
+ return fmt .Errorf ("%w: %s" , errHostKeyMismatch , bytes .TrimSpace (MarshalAuthorizedKey (key )))
233
+ },
234
+ },
222
235
} {
223
236
t .Run (tt .name , func (t * testing.T ) {
224
237
c1 , c2 , err := netPipe ()
@@ -243,8 +256,16 @@ func TestNewClientConn(t *testing.T) {
243
256
},
244
257
HostKeyCallback : InsecureIgnoreHostKey (),
245
258
}
259
+
260
+ if tt .simulateHostKeyMismatch != nil {
261
+ clientConf .HostKeyCallback = tt .simulateHostKeyMismatch
262
+ }
263
+
246
264
clientConn , _ , _ , err := NewClientConn (c2 , "" , clientConf )
247
265
if err != nil {
266
+ if tt .simulateHostKeyMismatch != nil && errors .Is (err , errHostKeyMismatch ) {
267
+ return
268
+ }
248
269
t .Fatal (err )
249
270
}
250
271
0 commit comments