func runServer(t *testing.T, expectedRequest, response []byte) {
	l := prepareTestSocket(t)
	conn, err := l.Accept()
	if err != nil {
		fmt.Printf("Accepting connection should succeed, got: %v\n", err)
		t.Fail()
	}
	expectedRequestLength := len(expectedRequest)
	receivedRequest := make([]byte, expectedRequestLength*2)  
	bytesRead, err := conn.Read(receivedRequest)
	if err != nil {
		fmt.Printf("Reading data from connection should succeed, got: %v\n", err)
		t.Fail()
	}
	if bytesRead != expectedRequestLength {
		fmt.Printf("%d differs from %d\n", bytesRead, expectedRequestLength)
		t.Fail()
	}
	receivedRequest = bytes.TrimRight(receivedRequest, "\x00")
	if !reflect.DeepEqual(receivedRequest, expectedRequest) {
		fmt.Printf("Bad request. expected '%+v', got '%+v'\n", expectedRequest, receivedRequest)
		t.Fail()
	}
	if _, err := conn.Write(response); err != nil {
		fmt.Printf("Writing response should succeed, got: %v\n", err)
		t.Fail()
	}
	if err := conn.Close(); err != nil {
		fmt.Printf("Failed closing connection: %v\n", err)
		t.Fail()
	}
}
nolint:thelper // This function is actually part of the test.
package ssh
import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"reflect"
	"strings"
	"testing"
	"github.com/flexkube/libflexkube/pkg/host/transport"
)
const testServerAddr = "/run/test.sock"
func TestPasswordAuth(t *testing.T) {
	unsetSSHAuthSockEnv(t)
	passwordFilePath := "/home/core/.password"
	pass, err := ioutil.ReadFile(passwordFilePath)
	if err != nil {
		t.Fatalf("Reading password file %q: %v", passwordFilePath, err)
	}
	c := &Config{
		Address:           "localhost",
		User:              "core",
		ConnectionTimeout: "5s",
		RetryTimeout:      "5s",
		RetryInterval:     "1s",
		Port:              Port,
		Password:          strings.TrimSpace(string(pass)),
	}
	s, err := c.New()
	if err != nil {
		t.Fatalf("Creating new SSH object should succeed, got: %v", err)
	}
	if _, err := s.Connect(); err != nil {
		t.Fatalf("Connecting should succeed, got: %v", err)
	}
}
func TestPasswordAuthFail(t *testing.T) {
	unsetSSHAuthSockEnv(t)
	c := &Config{
		Address:           "localhost",
		User:              "core",
		ConnectionTimeout: "5s",
		RetryTimeout:      "5s",
		RetryInterval:     "1s",
		Port:              Port,
		Password:          "badpassword",
	}
	s, err := c.New()
	if err != nil {
		t.Fatalf("Creating new SSH object should succeed, got: %v", err)
	}
	if _, err := s.Connect(); err == nil {
		t.Fatalf("Connecting with bad password should fail")
	}
}
func TestPrivateKeyAuth(t *testing.T) {
	s := withPrivateKey(t)
	if _, err := s.Connect(); err != nil {
		t.Fatalf("Connecting should succeed, got: %v", err)
	}
}
func withPrivateKey(t *testing.T) transport.Interface {
	t.Helper()
	unsetSSHAuthSockEnv(t)
	sshPrivateKeyPath := "/home/core/.ssh/id_rsa"
	key, err := ioutil.ReadFile(sshPrivateKeyPath)
	if err != nil {
		t.Fatalf("Reading SSH private key from %q shouldn't fail, got: %v", sshPrivateKeyPath, err)
	}
	c := &Config{
		Address:           "localhost",
		User:              "core",
		ConnectionTimeout: "5s",
		RetryTimeout:      "5s",
		RetryInterval:     "1s",
		Port:              Port,
		PrivateKey:        string(key),
	}
	ssh, err := c.New()
	if err != nil {
		t.Fatalf("Creating new SSH object should succeed, got: %v", err)
	}
	return ssh
}
func TestForwardUnixSocketFull(t *testing.T) {
	ssh := withPrivateKey(t)
	c, err := ssh.Connect()
	if err != nil {
		t.Fatalf("Connecting should succeed, got: %v", err)
	}
	randomRequest, _ := testMessage(t)
	randomResponse, _ := testMessage(t)
	go runServer(t, randomRequest, randomResponse)
	localSocket, err := c.ForwardUnixSocket(fmt.Sprintf("unix://%s", testServerAddr))
	if err != nil {
		t.Fatalf("Forwarding should succeed, got: %v", err)
	}
	conn, err := net.Dial("unix", strings.ReplaceAll(localSocket, "unix://", ""))
	if err != nil {
		t.Fatalf("Opening connection to %s should succeed, got: %v", localSocket, err)
	}
	if _, err := conn.Write(randomRequest); err != nil {
		t.Fatalf("Writing data to connection: %v", err)
	}
	response, err := ioutil.ReadAll(conn)
	if err != nil {
		t.Fatalf("Reading data from connection should succeed, got: %v", err)
	}
	if !reflect.DeepEqual(response, randomResponse) {
		t.Fatalf("Bad response. expected '%+v', got '%+v'", randomResponse, response)
	}
}
func prepareTestSocket(t *testing.T) net.Listener {
	t.Helper()
	l, err := net.Listen("unix", testServerAddr)
	if err != nil {
		fmt.Printf("Listening on socket should succeed, got: %v\n", err)
		t.Fail()
	}
	t.Cleanup(func() {
		if err := l.Close(); err != nil {
			fmt.Printf("Failed closing local listener: %v\n", err)
		}
		if err := os.Remove(testServerAddr); err != nil {
			fmt.Printf("Failed removing UNIX socket %q: %v\n", testServerAddr, err)
		}
	})
	if err := os.Chmod(testServerAddr, 0o777); err != nil {
		fmt.Printf("Socket chmod should succeed, got: %v\n", err)
		t.Fail()
	}
	return l
}