Skip to content

Commit 76bf537

Browse files
committed
Add unit tests for parsing OpenSSH version strings
Signed-off-by: Jan Dubois <[email protected]>
1 parent 40c5c77 commit 76bf537

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/opencontainers/go-digest v1.0.0
2525
github.com/sirupsen/logrus v1.8.1
2626
github.com/spf13/cobra v1.2.1
27+
github.com/stretchr/testify v1.7.0
2728
github.com/yalue/native_endian v1.0.1
2829
golang.org/x/sys v0.0.0-20210818153620-00dd8d7831e7
2930
gopkg.in/yaml.v2 v2.4.0
@@ -32,6 +33,7 @@ require (
3233

3334
require (
3435
github.com/VividCortex/ewma v1.1.1 // indirect
36+
github.com/davecgh/go-spew v1.1.1 // indirect
3537
github.com/digitalocean/go-libvirt v0.0.0-20201209184759-e2a69bcd5bd1 // indirect
3638
github.com/fatih/color v1.10.0 // indirect
3739
github.com/fsnotify/fsnotify v1.4.9 // indirect
@@ -46,6 +48,7 @@ require (
4648
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
4749
github.com/pkg/errors v0.9.1 // indirect
4850
github.com/pkg/sftp v1.13.3 // indirect
51+
github.com/pmezard/go-difflib v1.0.0 // indirect
4952
github.com/rivo/uniseg v0.2.0 // indirect
5053
github.com/spf13/pflag v1.0.5 // indirect
5154
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
@@ -58,4 +61,5 @@ require (
5861
google.golang.org/protobuf v1.26.0 // indirect
5962
gopkg.in/djherbis/times.v1 v1.2.0 // indirect
6063
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
64+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
6165
)

pkg/sshutil/sshutil.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ func SSHArgsFromOpts(opts []string) []string {
231231
return args
232232
}
233233

234+
func ParseOpenSSHVersion(version []byte) *semver.Version {
235+
regex := regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)
236+
matches := regex.FindSubmatch(version)
237+
if len(matches) == 3 {
238+
if len(matches[2]) == 0 {
239+
matches[2] = []byte("0")
240+
}
241+
return semver.New(fmt.Sprintf("%s.%s", matches[1], matches[2]))
242+
}
243+
return &semver.Version{}
244+
}
245+
234246
func detectOpenSSHVersion() semver.Version {
235247
var (
236248
v semver.Version
@@ -241,15 +253,8 @@ func detectOpenSSHVersion() semver.Version {
241253
if err := cmd.Run(); err != nil {
242254
logrus.Warnf("failed to run %v: stderr=%q", cmd.Args, stderr.String())
243255
} else {
244-
regex := regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)
245-
matches := regex.FindSubmatch(stderr.Bytes())
246-
if len(matches) == 3 {
247-
if len(matches[2]) == 0 {
248-
matches[2] = []byte("0")
249-
}
250-
v = *semver.New(fmt.Sprintf("%s.%s", matches[1], matches[2]))
251-
}
256+
v = *ParseOpenSSHVersion(stderr.Bytes())
257+
logrus.Debugf("OpenSSH version %s detected", v)
252258
}
253-
logrus.Debugf("OpenSSH version %s detected", v)
254259
return v
255260
}

pkg/sshutil/sshutil_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package sshutil
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/coreos/go-semver/semver"
7+
"github.com/stretchr/testify/assert"
8+
)
49

510
func TestDefaultPubKeys(t *testing.T) {
611
keys, _ := DefaultPubKeys(true)
@@ -9,3 +14,12 @@ func TestDefaultPubKeys(t *testing.T) {
914
t.Logf("%s: %q", key.Filename, key.Content)
1015
}
1116
}
17+
18+
func TestParseOpenSSHVersion(t *testing.T) {
19+
assert.True(t, ParseOpenSSHVersion([]byte("OpenSSH_8.4p1 Ubuntu")).Equal(semver.Version{8,4,1, "", ""}))
20+
assert.True(t, ParseOpenSSHVersion([]byte("OpenSSH_7.6p1 Ubuntu")).LessThan(*semver.New("8.0.0")))
21+
// macOS 10.15
22+
assert.True(t, ParseOpenSSHVersion([]byte("OpenSSH_8.1p1, LibreSSL 2.7.3")).Equal(semver.Version{8,1,1, "", ""}))
23+
// OpenBSD 5.8
24+
assert.True(t, ParseOpenSSHVersion([]byte("OpenSSH_7.0, LibreSSL")).Equal(*semver.New("7.0.0")))
25+
}

0 commit comments

Comments
 (0)