github.com/devops-filetransfer/sshego@v7.0.4+incompatible/_vendor/golang.org/x/crypto/ssh/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  	"net"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func testClientVersion(t *testing.T, config *ClientConfig, expected string) {
    14  	clientConn, serverConn := net.Pipe()
    15  	defer clientConn.Close()
    16  	receivedVersion := make(chan string, 1)
    17  	config.HostKeyCallback = InsecureIgnoreHostKey()
    18  	go func() {
    19  		version, err := readVersion(serverConn)
    20  		if err != nil {
    21  			receivedVersion <- ""
    22  		} else {
    23  			receivedVersion <- string(version)
    24  		}
    25  		serverConn.Close()
    26  	}()
    27  	NewClientConn(clientConn, "", config)
    28  	actual := <-receivedVersion
    29  	if actual != expected {
    30  		t.Fatalf("got %s; want %s", actual, expected)
    31  	}
    32  }
    33  
    34  func TestCustomClientVersion(t *testing.T) {
    35  	version := "Test-Client-Version-0.0"
    36  	testClientVersion(t, &ClientConfig{ClientVersion: version}, version)
    37  }
    38  
    39  func TestDefaultClientVersion(t *testing.T) {
    40  	testClientVersion(t, &ClientConfig{}, packageVersion)
    41  }
    42  
    43  func TestHostKeyCheck(t *testing.T) {
    44  	for _, tt := range []struct {
    45  		name      string
    46  		wantError string
    47  		key       PublicKey
    48  	}{
    49  		{"no callback", "must specify HostKeyCallback", nil},
    50  		{"correct key", "", testSigners["rsa"].PublicKey()},
    51  		{"mismatch", "mismatch", testSigners["ecdsa"].PublicKey()},
    52  	} {
    53  		c1, c2, err := netPipe()
    54  		if err != nil {
    55  			t.Fatalf("netPipe: %v", err)
    56  		}
    57  		defer c1.Close()
    58  		defer c2.Close()
    59  		serverConf := &ServerConfig{
    60  			NoClientAuth: true,
    61  		}
    62  		serverConf.AddHostKey(testSigners["rsa"])
    63  
    64  		go NewServerConn(c1, serverConf)
    65  		clientConf := ClientConfig{
    66  			User: "user",
    67  		}
    68  		if tt.key != nil {
    69  			clientConf.HostKeyCallback = FixedHostKey(tt.key)
    70  		}
    71  
    72  		_, _, _, err = NewClientConn(c2, "", &clientConf)
    73  		if err != nil {
    74  			if tt.wantError == "" || !strings.Contains(err.Error(), tt.wantError) {
    75  				t.Errorf("%s: got error %q, missing %q", tt.name, err.Error(), tt.wantError)
    76  			}
    77  		} else if tt.wantError != "" {
    78  			t.Errorf("%s: succeeded, but want error string %q", tt.name, tt.wantError)
    79  		}
    80  	}
    81  }