File: ssh_test.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (117 lines) | stat: -rw-r--r-- 3,763 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ssh

import (
	"net/url"
	"os"
	"testing"

	"github.com/stretchr/testify/require"
)

// these tests cannot check for "true" functionality
// in order to do that, you need two machines and a place to connect to/from
// these will error but we can check the error message to make sure it is an ssh error message
// not one for a segfault or parsing error

func TestCreate(t *testing.T) {
	options := ConnectionCreateOptions{
		Port:    22,
		Path:    "localhost",
		Name:    "testing",
		Socket:  "/run/user/foo/podman/podman.sock",
		Default: false,
	}
	err := Create(&options, NativeMode)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	err = Create(&options, GolangMode)
	// the error with golang should be nil, we want this to work if we are given a socket path
	// that is the current podman behavior
	require.Nil(t, err)
}

func TestExec(t *testing.T) {
	options := ConnectionExecOptions{
		Port: 22,
		Host: "localhost",
		Args: []string{"ls", "/"},
	}

	_, err := Exec(&options, NativeMode)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	_, err = Exec(&options, GolangMode)
	require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}

func TestExecWithInput(t *testing.T) {
	options := ConnectionExecOptions{
		Port: 22,
		Host: "localhost",
		Args: []string{"md5sum"},
	}

	input, err := os.Open("/etc/fstab")
	require.NoError(t, err)
	defer input.Close()

	_, err = ExecWithInput(&options, NativeMode, input)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	_, err = ExecWithInput(&options, GolangMode, input)
	require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}

func TestDial(t *testing.T) {
	options := ConnectionDialOptions{
		Port: 22,
		Host: "localhost",
	}

	_, err := Dial(&options, NativeMode)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	_, err = Dial(&options, GolangMode)
	require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")

	// Test again without specifying sshd port, and code should default to port 22
	options = ConnectionDialOptions{
		Host: "localhost",
	}

	_, err = Dial(&options, NativeMode)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	_, err = Dial(&options, GolangMode)
	require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}

func TestScp(t *testing.T) {
	f, err := os.CreateTemp(t.TempDir(), "")
	require.Nil(t, err)

	options := ConnectionScpOptions{
		User:        &url.Userinfo{},
		Source:      f.Name(),
		Destination: "localhost:/does/not/exist",
		Port:        22,
	}

	_, err = Scp(&options, NativeMode)
	// exit status 255 is what you get when ssh is not enabled or the connection failed
	// this means up to that point, everything worked
	require.Error(t, err, "exit status 255")

	_, err = Scp(&options, GolangMode)
	require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}