github.com/glycerine/xcryptossh@v7.0.4+incompatible/client_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssh 6 7 import ( 8 "context" 9 "net" 10 "strings" 11 "testing" 12 ) 13 14 func testClientVersion(t *testing.T, config *ClientConfig, expected string) { 15 clientConn, serverConn := net.Pipe() 16 defer clientConn.Close() 17 receivedVersion := make(chan string, 1) 18 config.HostKeyCallback = InsecureIgnoreHostKey() 19 go func() { 20 version, err := readVersion(serverConn) 21 if err != nil { 22 receivedVersion <- "" 23 } else { 24 receivedVersion <- string(version) 25 } 26 serverConn.Close() 27 }() 28 config.Halt = NewHalter() 29 30 ctx := context.Background() 31 32 NewClientConn(ctx, clientConn, "", config) 33 defer config.Halt.RequestStop() 34 actual := <-receivedVersion 35 if actual != expected { 36 t.Fatalf("got %s; want %s", actual, expected) 37 } 38 } 39 40 func TestCustomClientVersion(t *testing.T) { 41 defer xtestend(xtestbegin(t)) 42 43 version := "Test-Client-Version-0.0" 44 testClientVersion(t, &ClientConfig{ClientVersion: version}, version) 45 } 46 47 func TestDefaultClientVersion(t *testing.T) { 48 defer xtestend(xtestbegin(t)) 49 50 testClientVersion(t, &ClientConfig{}, packageVersion) 51 } 52 53 func TestHostKeyCheck(t *testing.T) { 54 defer xtestend(xtestbegin(t)) 55 56 for _, tt := range []struct { 57 name string 58 wantError string 59 key PublicKey 60 }{ 61 {"no callback", "must specify HostKeyCallback", nil}, 62 {"correct key", "", testSigners["rsa"].PublicKey()}, 63 {"mismatch", "mismatch", testSigners["ecdsa"].PublicKey()}, 64 } { 65 c1, c2, err := netPipe() 66 if err != nil { 67 t.Fatalf("netPipe: %v", err) 68 } 69 defer c1.Close() 70 defer c2.Close() 71 serverConf := &ServerConfig{ 72 NoClientAuth: true, 73 Config: Config{ 74 Halt: NewHalter(), 75 }, 76 } 77 serverConf.AddHostKey(testSigners["rsa"]) 78 ctx := context.Background() 79 80 go NewServerConn(ctx, c1, serverConf) 81 defer serverConf.Halt.RequestStop() 82 83 clientConf := ClientConfig{ 84 User: "user", 85 Config: Config{ 86 Halt: NewHalter(), 87 }, 88 } 89 if tt.key != nil { 90 clientConf.HostKeyCallback = FixedHostKey(tt.key) 91 } 92 93 _, _, _, err = NewClientConn(ctx, c2, "", &clientConf) 94 defer clientConf.Halt.RequestStop() 95 96 if err != nil { 97 if tt.wantError == "" || !strings.Contains(err.Error(), tt.wantError) { 98 t.Errorf("%s: got error %q, missing %q", tt.name, err.Error(), tt.wantError) 99 } 100 } else if tt.wantError != "" { 101 t.Errorf("%s: succeeded, but want error string %q", tt.name, tt.wantError) 102 } 103 } 104 }