summaryrefslogtreecommitdiff
path: root/internal/control/ssh_test.go
blob: 8b7afd0faa028693376f35a4131db3809623d5b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package control

import (
	"context"
	"os"
	"path/filepath"
	"testing"
)

func TestValidateNodeSSHPasswordAuth(t *testing.T) {
	t.Parallel()

	node := Node{
		ID:       "pw-01",
		Name:     "Password Node",
		Provider: "custom-vps",
		Region:   "nl",
		Host:     "203.0.113.20",
		Enabled:  true,
		SSH: SSHConfig{
			User:        "root",
			Port:        22,
			Auth:        "password",
			PasswordEnv: "VPNEM_TEST_PASSWORD",
		},
		Protocols: []ProtocolProfile{
			{Type: "socks5", Enabled: true, Port: 1080},
		},
	}

	if err := ValidateNode(node); err != nil {
		t.Fatalf("ValidateNode() error = %v", err)
	}
}

func TestWrapWithPasswordUsesSSHPass(t *testing.T) {
	t.Setenv("VPNEM_TEST_PASSWORD", "secret")
	node := Node{
		ID:   "pw-01",
		Name: "Password Node",
		SSH: SSHConfig{
			User:        "root",
			Port:        22,
			Auth:        "password",
			PasswordEnv: "VPNEM_TEST_PASSWORD",
		},
	}

	cmd, err := wrapWithPassword(context.Background(), node, "ssh", "-V")
	if err != nil {
		t.Fatalf("wrapWithPassword() error = %v", err)
	}
	if got := filepath.Base(cmd.Path); got != "sshpass" {
		t.Fatalf("filepath.Base(cmd.Path) = %q, want sshpass", got)
	}
	if len(cmd.Args) < 4 {
		t.Fatalf("cmd.Args too short: %#v", cmd.Args)
	}
	if cmd.Args[1] != "-p" || cmd.Args[2] != "secret" || cmd.Args[3] != "ssh" {
		t.Fatalf("unexpected cmd.Args: %#v", cmd.Args)
	}
}

func TestWrapWithPasswordRequiresEnv(t *testing.T) {
	_ = os.Unsetenv("VPNEM_TEST_PASSWORD_MISSING")
	node := Node{
		ID:   "pw-01",
		Name: "Password Node",
		SSH: SSHConfig{
			User:        "root",
			Port:        22,
			Auth:        "password",
			PasswordEnv: "VPNEM_TEST_PASSWORD_MISSING",
		},
	}

	if _, err := wrapWithPassword(context.Background(), node, "ssh", "-V"); err == nil {
		t.Fatal("expected error for missing password env")
	}
}