@@ -7,6 +7,9 @@ package ssh
77import (
88 "bytes"
99 "crypto/rand"
10+ "errors"
11+ "fmt"
12+ "net"
1013 "strings"
1114 "testing"
1215)
@@ -207,9 +210,12 @@ func TestBannerCallback(t *testing.T) {
207210}
208211
209212func TestNewClientConn (t * testing.T ) {
213+ errHostKeyMismatch := errors .New ("host key mismatch" )
214+
210215 for _ , tt := range []struct {
211- name string
212- user string
216+ name string
217+ user string
218+ simulateHostKeyMismatch HostKeyCallback
213219 }{
214220 {
215221 name : "good user field for ConnMetadata" ,
@@ -219,6 +225,13 @@ func TestNewClientConn(t *testing.T) {
219225 name : "empty user field for ConnMetadata" ,
220226 user : "" ,
221227 },
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+ },
222235 } {
223236 t .Run (tt .name , func (t * testing.T ) {
224237 c1 , c2 , err := netPipe ()
@@ -243,8 +256,16 @@ func TestNewClientConn(t *testing.T) {
243256 },
244257 HostKeyCallback : InsecureIgnoreHostKey (),
245258 }
259+
260+ if tt .simulateHostKeyMismatch != nil {
261+ clientConf .HostKeyCallback = tt .simulateHostKeyMismatch
262+ }
263+
246264 clientConn , _ , _ , err := NewClientConn (c2 , "" , clientConf )
247265 if err != nil {
266+ if tt .simulateHostKeyMismatch != nil && errors .Is (err , errHostKeyMismatch ) {
267+ return
268+ }
248269 t .Fatal (err )
249270 }
250271
0 commit comments