Skip to content

Commit

Permalink
Fix ProtobufBundle.MinVersion (#228)
Browse files Browse the repository at this point in the history
Fixes #227

Signed-off-by: Cody Soyland <[email protected]>
  • Loading branch information
codysoyland authored Jul 11, 2024
1 parent a0f4538 commit 47683f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,18 @@ func (b *ProtobufBundle) Timestamps() ([][]byte, error) {
return signedTimestamps, nil
}

func (b *ProtobufBundle) MinVersion(version string) bool {
mediaTypeParts := strings.Split(b.Bundle.MediaType, "version=")
if len(mediaTypeParts) < 2 {
// MinVersion returns true if the bundle version is greater than or equal to the expected version.
func (b *ProtobufBundle) MinVersion(expectVersion string) bool {
version, err := getBundleVersion(b.Bundle.MediaType)
if err != nil {
return false
}

return semver.Compare("v"+mediaTypeParts[1], "v"+version) >= 0
if !strings.HasPrefix(expectVersion, "v") {
expectVersion = "v" + expectVersion
}

return semver.Compare(version, expectVersion) >= 0
}

func parseEnvelope(input *protodsse.Envelope) (*Envelope, error) {
Expand Down
33 changes: 33 additions & 0 deletions pkg/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ func Test_getBundleVersion(t *testing.T) {
}
}

func TestMinVersion(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
mediaType string
expectedVersion string
ret bool
}{
{"old-format", "application/vnd.dev.sigstore.bundle+json;version=0.1", "v0.1", true},
{"old-format-unexpected", "application/vnd.dev.sigstore.bundle+json;version=0.1", "v0.2", false},
{"old-format-without-v", "application/vnd.dev.sigstore.bundle+json;version=0.1", "0.1", true},
{"new-format", "application/vnd.dev.sigstore.bundle.v0.3+json", "v0.1", true},
{"new-format-exact", "application/vnd.dev.sigstore.bundle.v0.3+json", "v0.3", true},
{"new-format-unexpected", "application/vnd.dev.sigstore.bundle.v0.2+json", "v0.3", false},
{"new-format-without-v", "application/vnd.dev.sigstore.bundle.v0.3+json", "0.3", true},
{"new-format-without-v-unexpected", "application/vnd.dev.sigstore.bundle.v0.2+json", "0.3", false},
{"blank", "", "", false},
{"invalid", "garbage", "v0.1", false},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
b := &ProtobufBundle{Bundle: &protobundle.Bundle{
MediaType: tc.mediaType,
}}
ret := b.MinVersion(tc.expectedVersion)
if tc.ret != ret {
t.Fatalf("expected %v, got %v", tc.ret, ret)
}
})
}
}

func TestMediaTypeString(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
Expand Down

0 comments on commit 47683f5

Please sign in to comment.