ssh: return missing user field in NewClientConn

Fixes golang/go#45249
This commit is contained in:
povsister 2021-03-29 13:04:32 +08:00
Родитель 0c34fe9e7d
Коммит 9f631b8092
2 изменённых файлов: 50 добавлений и 1 удалений

Просмотреть файл

@ -77,7 +77,7 @@ func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan
}
conn := &connection{
sshConn: sshConn{conn: c},
sshConn: sshConn{conn: c, user: fullConf.User},
}
if err := conn.clientHandshake(addr, &fullConf); err != nil {

Просмотреть файл

@ -164,3 +164,52 @@ func TestBannerCallback(t *testing.T) {
t.Fatalf("got %s; want %s", receivedBanner, expected)
}
}
func TestNewClientConn(t *testing.T) {
for _, tt := range []struct {
name string
user string
}{
{
name: "good user field for ConnMetadata",
user: "testuser",
},
{
name: "empty user field for ConnMetadata",
user: "",
},
} {
t.Run(tt.name, func(t *testing.T) {
c1, c2, err := netPipe()
if err != nil {
t.Fatalf("netPipe: %v", err)
}
defer c1.Close()
defer c2.Close()
serverConf := &ServerConfig{
PasswordCallback: func(conn ConnMetadata, password []byte) (*Permissions, error) {
return &Permissions{}, nil
},
}
serverConf.AddHostKey(testSigners["rsa"])
go NewServerConn(c1, serverConf)
clientConf := &ClientConfig{
User: tt.user,
Auth: []AuthMethod{
Password("testpw"),
},
HostKeyCallback: InsecureIgnoreHostKey(),
}
clientConn, _, _, err := NewClientConn(c2, "", clientConf)
if err != nil {
t.Fatal(err)
}
if userGot := clientConn.User(); userGot != tt.user {
t.Errorf("got user %q; want user %q", userGot, tt.user)
}
})
}
}