-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Delegations prereq] Add roles helpers
Splitting up #175
- Loading branch information
1 parent
9e07992
commit a8a6feb
Showing
5 changed files
with
101 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package roles | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var TopLevelRoles = map[string]struct{}{ | ||
"root": {}, | ||
"targets": {}, | ||
"snapshot": {}, | ||
"timestamp": {}, | ||
} | ||
|
||
func IsTopLevelRole(name string) bool { | ||
_, ok := TopLevelRoles[name] | ||
return ok | ||
} | ||
|
||
func IsDelegatedTargetsRole(name string) bool { | ||
return !IsTopLevelRole(name) | ||
} | ||
|
||
func IsTopLevelManifest(name string) bool { | ||
return IsTopLevelRole(strings.TrimSuffix(name, ".json")) | ||
} | ||
|
||
func IsDelegatedTargetsManifest(name string) bool { | ||
return !IsTopLevelManifest(name) | ||
} | ||
|
||
func IsVersionedManifest(name string) bool { | ||
parts := strings.Split(name, ".") | ||
// Versioned manifests have the form "x.role.json" | ||
if len(parts) < 3 { | ||
return false | ||
} | ||
|
||
_, err := strconv.Atoi(parts[0]) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package roles | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsTopLevelRole(t *testing.T) { | ||
assert.True(t, IsTopLevelRole("root")) | ||
assert.True(t, IsTopLevelRole("targets")) | ||
assert.True(t, IsTopLevelRole("timestamp")) | ||
assert.True(t, IsTopLevelRole("snapshot")) | ||
assert.False(t, IsTopLevelRole("bins")) | ||
} | ||
|
||
func TestIsDelegatedTargetsRole(t *testing.T) { | ||
assert.False(t, IsDelegatedTargetsRole("root")) | ||
assert.False(t, IsDelegatedTargetsRole("targets")) | ||
assert.False(t, IsDelegatedTargetsRole("timestamp")) | ||
assert.False(t, IsDelegatedTargetsRole("snapshot")) | ||
assert.True(t, IsDelegatedTargetsRole("deleg")) | ||
} | ||
|
||
func TestIsTopLevelManifest(t *testing.T) { | ||
assert.True(t, IsTopLevelManifest("root.json")) | ||
assert.True(t, IsTopLevelManifest("targets.json")) | ||
assert.True(t, IsTopLevelManifest("timestamp.json")) | ||
assert.True(t, IsTopLevelManifest("snapshot.json")) | ||
assert.False(t, IsTopLevelManifest("bins.json")) | ||
} | ||
|
||
func TestIsDelegatedTargetsManifest(t *testing.T) { | ||
assert.False(t, IsDelegatedTargetsManifest("root.json")) | ||
assert.False(t, IsDelegatedTargetsManifest("targets.json")) | ||
assert.False(t, IsDelegatedTargetsManifest("timestamp.json")) | ||
assert.False(t, IsDelegatedTargetsManifest("snapshot.json")) | ||
assert.True(t, IsDelegatedTargetsManifest("bins.json")) | ||
} | ||
|
||
func TestIsVersionedManifest(t *testing.T) { | ||
assert.False(t, IsVersionedManifest("a.b")) | ||
assert.False(t, IsVersionedManifest("a.b.c")) | ||
assert.False(t, IsVersionedManifest("a.b.json")) | ||
assert.False(t, IsVersionedManifest("1.a")) | ||
assert.True(t, IsVersionedManifest("1.a.json")) | ||
assert.True(t, IsVersionedManifest("2.a.json")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters