Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix IsTopLevelManifest calculation for versioned manifests #381

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions internal/roles/roles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package roles

import (
"fmt"
"strconv"
"strings"
)
Expand All @@ -22,6 +23,13 @@ func IsDelegatedTargetsRole(name string) bool {
}

func IsTopLevelManifest(name string) bool {
if IsVersionedManifest(name) {
var found bool
_, name, found = strings.Cut(name, ".")
if !found {
panic(fmt.Sprint("expected a versioned manifest of the form x.role.json"))
}
}
return IsTopLevelRole(strings.TrimSuffix(name, ".json"))
}

Expand Down
6 changes: 6 additions & 0 deletions internal/roles/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ func TestIsDelegatedTargetsRole(t *testing.T) {

func TestIsTopLevelManifest(t *testing.T) {
assert.True(t, IsTopLevelManifest("root.json"))
assert.True(t, IsTopLevelManifest("1.root.json"))
assert.True(t, IsTopLevelManifest("targets.json"))
assert.True(t, IsTopLevelManifest("timestamp.json"))
assert.True(t, IsTopLevelManifest("snapshot.json"))
assert.True(t, IsTopLevelManifest("2.snapshot.json"))
assert.False(t, IsTopLevelManifest("bins.json"))
assert.False(t, IsTopLevelManifest("3.bins.json"))
}

func TestIsDelegatedTargetsManifest(t *testing.T) {
assert.False(t, IsDelegatedTargetsManifest("root.json"))
assert.False(t, IsDelegatedTargetsManifest("1.root.json"))
assert.False(t, IsDelegatedTargetsManifest("targets.json"))
assert.False(t, IsDelegatedTargetsManifest("2.targets.json"))
assert.False(t, IsDelegatedTargetsManifest("timestamp.json"))
assert.False(t, IsDelegatedTargetsManifest("snapshot.json"))
assert.True(t, IsDelegatedTargetsManifest("bins.json"))
assert.True(t, IsDelegatedTargetsManifest("2.bins.json"))
}

func TestIsVersionedManifest(t *testing.T) {
Expand Down