blob: 2290a758656f44f098140f42f76f0fc38b41fde6 [file] [log] [blame]
package data
import (
"encoding/json"
. "gopkg.in/check.v1"
)
const (
// This public key is from the TUF specs:
//
// https://github.com/theupdateframework/specification
//
// Unfortunately there was a bug in the 1.0 spec, which reused the 0.9
// key ids. This patch fixes it:
//
// https://github.com/theupdateframework/specification/pull/43
public = `"72378e5bc588793e58f81c8533da64a2e8f1565c1fcc7f253496394ffc52542c"`
keyid09 = "1a2b4110927d4cba257262f614896179ff85ca1f1353a41b5224ac474ca71cb4"
keyid10 = "1bf1c6e3cdd3d3a8420b19199e27511999850f4b376c4547b2f32fba7e80fca3"
keyid10algos = "8e1824bd4e2de736e1388208c41e439fa1cfa19f4852f9ca80015e1da981cad5"
)
type TypesSuite struct{}
var _ = Suite(&TypesSuite{})
func (TypesSuite) TestKeyIDs(c *C) {
var hexbytes HexBytes
err := json.Unmarshal([]byte(public), &hexbytes)
c.Assert(err, IsNil)
key := Key{
Type: KeyTypeEd25519,
Value: KeyValue{Public: hexbytes},
}
c.Assert(key.IDs(), DeepEquals, []string{keyid09})
key = Key{
Type: KeyTypeEd25519,
Scheme: KeySchemeEd25519,
Value: KeyValue{Public: hexbytes},
}
c.Assert(key.IDs(), DeepEquals, []string{keyid10, keyid09})
key = Key{
Type: KeyTypeEd25519,
Scheme: KeySchemeEd25519,
Algorithms: KeyAlgorithms,
Value: KeyValue{Public: hexbytes},
}
c.Assert(key.IDs(), DeepEquals, []string{keyid10algos, keyid09})
}
func (TypesSuite) TestRootAddKey(c *C) {
var hexbytes HexBytes
err := json.Unmarshal([]byte(public), &hexbytes)
c.Assert(err, IsNil)
key := &Key{
Type: KeyTypeEd25519,
Scheme: KeySchemeEd25519,
Value: KeyValue{Public: hexbytes},
}
root := NewRoot()
c.Assert(root.AddKey(key), Equals, true)
c.Assert(root.AddKey(key), Equals, false)
}
func (TypesSuite) TestRoleAddKeyIDs(c *C) {
var hexbytes HexBytes
err := json.Unmarshal([]byte(public), &hexbytes)
c.Assert(err, IsNil)
key := &Key{
Type: KeyTypeEd25519,
Scheme: KeySchemeEd25519,
Value: KeyValue{Public: hexbytes},
}
role := &Role{}
c.Assert(role.KeyIDs, HasLen, 0)
c.Assert(role.AddKeyIDs(key.IDs()), Equals, true)
c.Assert(role.KeyIDs, DeepEquals, []string{keyid10, keyid09})
// Adding the key again doesn't modify the array.
c.Assert(role.AddKeyIDs(key.IDs()), Equals, false)
c.Assert(role.KeyIDs, DeepEquals, []string{keyid10, keyid09})
// Add another key.
key = &Key{
Type: KeyTypeEd25519,
Scheme: KeySchemeEd25519,
Algorithms: KeyAlgorithms,
Value: KeyValue{Public: hexbytes},
}
// Adding the key again doesn't modify the array.
c.Assert(role.AddKeyIDs(key.IDs()), Equals, true)
c.Assert(role.KeyIDs, DeepEquals, []string{keyid10, keyid09, keyid10algos})
}