Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func BenchmarkCompareWithScheme(b *testing.B) {
{name: "APK", a: "1.2.3_rc1", other: "1.2.3", scheme: "apk"},
{name: "IntDot", a: "1.2.3", other: "1.2.4", scheme: "intdot"},
{name: "IntDotUneven", a: "1.2.3.4.5.6.7.8.9", other: "1.2.3.4.5.6.7.8", scheme: "intdot"},
{name: "OpenSSL", a: "1.1.1w", other: "1.1.1k", scheme: "openssl"},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
Expand Down
2 changes: 2 additions & 0 deletions ecosystem_extra_parity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func TestEcosystemComparatorsDoNotAllocate(t *testing.T) {
{name: "IntDot", compare: compareIntDot, a: "1.2.3", b: "1.2.4"},
{name: "IntDot uneven", compare: compareIntDot, a: "1.2.3.4.5.6.7.8.9", b: "1.2.3.4.5.6.7.8"},
{name: "IntDot suffix", compare: compareIntDot, a: "10.0.1-rc1", b: "9.9.9"},
{name: "OpenSSL letter patch", compare: compareOpenSSL, a: "1.1.1w", b: "1.1.1k"},
{name: "OpenSSL prerelease", compare: compareOpenSSL, a: "1.1.1-alpha1", b: "1.1.1"},
}

for _, tt := range tests {
Expand Down
44 changes: 44 additions & 0 deletions openssl_parity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package vers

import "testing"

func openSSLCorpus() []string {
return append(parityCorpus(),
"1.1.1", "1.1.1w", "1.1.1k", "1.0.2u", "0.9.8zh", "1.1.0l", "1.1.1-alpha1", "1.1.1-beta2",
"1.1.1-pre9", "3.0.0", "3.0.13", "3.2.1", "3.0.0-alpha17", "3.0.0-beta1", "10.20.30x",
"1.1.", "1..1", ".1.1", "..", "1.1.x", "a.1.1", "1.a.1", "1.1.1.1", "1.1.1a.2",
"1.1.1 ", " 1.1.1", "1.1.1-", "01.1.1", "1.01.1", "1.1.01a", "1.1", "1", "1.1.١",
)
}

func assertOpenSSLParity(t *testing.T, versions []string) {
t.Helper()
for _, s := range versions {
got, okGot := parseOpenSSLVersion(s)
want, okWant := refParseOpenSSLVersion(s)
if got != want || okGot != okWant {
t.Fatalf("parseOpenSSLVersion(%q) = %+v, %v; reference implementation returns %+v, %v", s, got, okGot, want, okWant)
}
}
}

func TestParseOpenSSLVersionMatchesReference(t *testing.T) {
assertOpenSSLParity(t, openSSLCorpus())
}

func TestParseOpenSSLVersionMatchesReferenceOnFixtures(t *testing.T) {
assertOpenSSLParity(t, fixtureVersions(t))
}

func FuzzParseOpenSSLVersionMatchesReference(f *testing.F) {
for _, s := range []string{"", ".", "..", "1.1", "1.1.1", "1.1.1w", "0.9.8zh", "1.1.1-alpha1", "3.0.13", "1.1.1.1", "a.1.1"} {
f.Add(s)
}
f.Fuzz(func(t *testing.T, s string) {
got, okGot := parseOpenSSLVersion(s)
want, okWant := refParseOpenSSLVersion(s)
if got != want || okGot != okWant {
t.Fatalf("parseOpenSSLVersion(%q) = %+v, %v; reference implementation returns %+v, %v", s, got, okGot, want, okWant)
}
})
}
28 changes: 28 additions & 0 deletions openssl_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vers

// This file is a mechanically renamed copy of parseOpenSSLVersion from before it
// stopped splitting the version with strings.SplitN. It exists so the
// differential tests in openssl_parity_test.go can assert that the
// allocation-free rewrite parses every input exactly the way the previous code
// did. Do not edit it to fix a behavior difference; fix the real
// implementation instead.

import "strings"

func refParseOpenSSLVersion(s string) (opensslVersion, bool) {
var v opensslVersion
parts := strings.SplitN(s, ".", 3) //nolint:mnd
if len(parts) != 3 || !isDigits(parts[0]) || !isDigits(parts[1]) {
return v, false
}
i := 0
for i < len(parts[2]) && isASCIIDigit(parts[2][i]) {
i++
}
if i == 0 {
return v, false
}
v.core = [3]string{parts[0], parts[1], parts[2][:i]}
v.patch = parts[2][i:]
return v, true
}
11 changes: 6 additions & 5 deletions schemes.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,19 +670,20 @@ type opensslVersion struct {

func parseOpenSSLVersion(s string) (opensslVersion, bool) {
var v opensslVersion
parts := strings.SplitN(s, ".", 3) //nolint:mnd
if len(parts) != 3 || !isDigits(parts[0]) || !isDigits(parts[1]) {
major, rest, _ := nextDotPart(s)
minor, rest, _ := nextDotPart(rest)
if !isDigits(major) || !isDigits(minor) {
return v, false
}
i := 0
for i < len(parts[2]) && isASCIIDigit(parts[2][i]) {
for i < len(rest) && isASCIIDigit(rest[i]) {
i++
}
if i == 0 {
return v, false
}
v.core = [3]string{parts[0], parts[1], parts[2][:i]}
v.patch = parts[2][i:]
v.core = [3]string{major, minor, rest[:i]}
v.patch = rest[i:]
return v, true
}

Expand Down