Skip to content

Commit 50bd9d7

Browse files
[Delegations prereq] Move set utils to an internal helper package (#191)
Splitting up https://github.com/theupdateframework/go-tuf/pull/175/files
1 parent 13f0687 commit 50bd9d7

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

client/client_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
tuf "github.com/theupdateframework/go-tuf"
2020
"github.com/theupdateframework/go-tuf/data"
21+
"github.com/theupdateframework/go-tuf/internal/sets"
2122
"github.com/theupdateframework/go-tuf/pkg/keys"
2223
"github.com/theupdateframework/go-tuf/sign"
2324
"github.com/theupdateframework/go-tuf/util"
@@ -362,7 +363,7 @@ func (s *ClientSuite) TestNewRoot(c *C) {
362363
}
363364
role := client.db.GetRole(name)
364365
c.Assert(role, NotNil)
365-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(ids))
366+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(ids))
366367
}
367368
}
368369

@@ -602,7 +603,7 @@ func (s *ClientSuite) TestNewTimestampKey(c *C) {
602603
}
603604
role := client.db.GetRole("timestamp")
604605
c.Assert(role, NotNil)
605-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(newIDs))
606+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
606607
}
607608

608609
func (s *ClientSuite) TestNewSnapshotKey(c *C) {
@@ -642,7 +643,7 @@ func (s *ClientSuite) TestNewSnapshotKey(c *C) {
642643
}
643644
role := client.db.GetRole("snapshot")
644645
c.Assert(role, NotNil)
645-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(newIDs))
646+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
646647
}
647648

648649
func (s *ClientSuite) TestNewTargetsKey(c *C) {
@@ -685,7 +686,7 @@ func (s *ClientSuite) TestNewTargetsKey(c *C) {
685686
}
686687
role := client.db.GetRole("targets")
687688
c.Assert(role, NotNil)
688-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(newIDs))
689+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
689690
}
690691

691692
func (s *ClientSuite) TestLocalExpired(c *C) {

internal/sets/strings.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sets
2+
3+
func StringSliceToSet(items []string) map[string]struct{} {
4+
s := map[string]struct{}{}
5+
for _, item := range items {
6+
s[item] = struct{}{}
7+
}
8+
return s
9+
}
10+
11+
func StringSetToSlice(items map[string]struct{}) []string {
12+
ret := []string{}
13+
14+
for k := range items {
15+
ret = append(ret, k)
16+
}
17+
18+
return ret
19+
}
20+
21+
func DeduplicateStrings(items []string) []string {
22+
s := StringSliceToSet(items)
23+
return StringSetToSlice(s)
24+
}

internal/sets/strings_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sets
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestStringSliceToSet(t *testing.T) {
10+
assert.Equal(t,
11+
map[string]struct{}{
12+
"a": {},
13+
"b": {},
14+
"c": {},
15+
},
16+
StringSliceToSet([]string{"a", "c", "b", "c", "b"}))
17+
}
18+
19+
func TestStringSetToSlice(t *testing.T) {
20+
assert.ElementsMatch(t,
21+
[]string{"a", "b", "c"},
22+
StringSetToSlice(map[string]struct{}{
23+
"a": {},
24+
"b": {},
25+
"c": {},
26+
}),
27+
)
28+
}
29+
30+
func TestDeduplicateStrings(t *testing.T) {
31+
assert.ElementsMatch(t,
32+
[]string{"a", "b", "c"},
33+
DeduplicateStrings([]string{"a", "c", "b", "c", "b"}),
34+
)
35+
}

repo_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/secure-systems-lab/go-securesystemslib/cjson"
1919
"github.com/theupdateframework/go-tuf/data"
2020
"github.com/theupdateframework/go-tuf/encrypted"
21+
"github.com/theupdateframework/go-tuf/internal/sets"
2122
"github.com/theupdateframework/go-tuf/pkg/keys"
2223
"github.com/theupdateframework/go-tuf/util"
2324
"github.com/theupdateframework/go-tuf/verify"
@@ -218,7 +219,7 @@ func (rs *RepoSuite) TestGenKey(c *C) {
218219
c.Assert(err, IsNil)
219220
c.Assert(rootKey.MarshalPublicKey().IDs(), DeepEquals, ids)
220221
role := db.GetRole("root")
221-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(ids))
222+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(ids))
222223

223224
// check the key was saved correctly
224225
localKeys, err := local.GetSigners("root")
@@ -381,7 +382,7 @@ func (rs *RepoSuite) TestAddPrivateKey(c *C) {
381382
c.Assert(err, IsNil)
382383
c.Assert(rootKey.MarshalPublicKey().IDs(), DeepEquals, ids)
383384
role := db.GetRole("root")
384-
c.Assert(role.KeyIDs, DeepEquals, util.StringSliceToSet(ids))
385+
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(ids))
385386

386387
// check the key was saved correctly
387388
localKeys, err := local.GetSigners("root")

util/util.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,6 @@ func HashedPaths(p string, hashes data.Hashes) []string {
266266
return paths
267267
}
268268

269-
func StringSliceToSet(items []string) map[string]struct{} {
270-
s := make(map[string]struct{})
271-
for _, item := range items {
272-
s[item] = struct{}{}
273-
}
274-
return s
275-
}
276-
277269
func AtomicallyWriteFile(filename string, data []byte, perm os.FileMode) error {
278270
dir, name := filepath.Split(filename)
279271
f, err := ioutil.TempFile(dir, name)

0 commit comments

Comments
 (0)