Skip to content

Commit 9d54796

Browse files
committed
🎉 add unit tests
Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
1 parent dcd5d7a commit 9d54796

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

providers/os/connection/ssh/ssh_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
package ssh
55

66
import (
7+
"net"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1012
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
1113
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
1214
)
@@ -37,3 +39,66 @@ func TestSSHAuthError(t *testing.T) {
3739
// local testing without ssh agent
3840
err.Error() == "no authentication method defined")
3941
}
42+
43+
// helper to start a fake SSH server with a custom banner
44+
func startMockSSHServer(t *testing.T, banner string) (addr string, closeFn func()) {
45+
ln, err := net.Listen("tcp", "127.0.0.1:0")
46+
require.Nil(t, err)
47+
48+
go func() {
49+
conn, err := ln.Accept()
50+
if err != nil {
51+
return
52+
}
53+
defer conn.Close()
54+
// simulate SSH banner
55+
conn.Write([]byte(banner + "\r\n"))
56+
}()
57+
58+
return ln.Addr().String(), func() { ln.Close() }
59+
}
60+
61+
func TestServerSupportsHybridKEX(t *testing.T) {
62+
tests := []struct {
63+
name string
64+
banner string
65+
expectHybrid bool
66+
}{
67+
{
68+
name: "OpenSSH 9.9 detected",
69+
banner: "SSH-2.0-OpenSSH_9.9",
70+
expectHybrid: true,
71+
},
72+
{
73+
name: "OpenSSH 9.7 (no hybrid)",
74+
banner: "SSH-2.0-OpenSSH_9.7",
75+
expectHybrid: false,
76+
},
77+
{
78+
name: "Non-OpenSSH server",
79+
banner: "SSH-2.0-CustomSSH_1.0",
80+
expectHybrid: false,
81+
},
82+
{
83+
name: "Malformed banner",
84+
banner: "garbage",
85+
expectHybrid: false,
86+
},
87+
}
88+
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
addr, shutdown := startMockSSHServer(t, tt.banner)
92+
defer shutdown()
93+
94+
got, err := serverSupportsHybridKEX(addr)
95+
require.Nil(t, err)
96+
assert.Equal(t, tt.expectHybrid, got)
97+
})
98+
}
99+
}
100+
101+
func TestServerSupportsHybridKEX_ServerUnreachable(t *testing.T) {
102+
_, err := serverSupportsHybridKEX("127.0.0.1:9")
103+
require.NotNil(t, err)
104+
}

0 commit comments

Comments
 (0)