| 1 |
package signer_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"sort" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/theupdateframework/go-tuf/data" |
| 9 |
"github.com/theupdateframework/go-tuf/internal/signer" |
| 10 |
"github.com/theupdateframework/go-tuf/pkg/keys" |
| 11 |
) |
| 12 |
|
| 13 |
type mockSigner struct { |
| 14 |
value json.RawMessage |
| 15 |
} |
| 16 |
|
| 17 |
func (s *mockSigner) MarshalPrivateKey() (*data.PrivateKey, error) { |
| 18 |
panic("not implemented") |
| 19 |
} |
| 20 |
|
| 21 |
func (s *mockSigner) UnmarshalPrivateKey(key *data.PrivateKey) error { |
| 22 |
panic("not implemented") |
| 23 |
} |
| 24 |
|
| 25 |
func (s *mockSigner) PublicData() *data.PublicKey { |
| 26 |
return &data.PublicKey{ |
| 27 |
Type: "mock", |
| 28 |
Scheme: "mock", |
| 29 |
Algorithms: []data.HashAlgorithm{"mock"}, |
| 30 |
Value: s.value, |
| 31 |
} |
| 32 |
} |
| 33 |
func (s *mockSigner) SignMessage(message []byte) ([]byte, error) { |
| 34 |
panic("not implemented") |
| 35 |
} |
| 36 |
|
| 37 |
func TestSignerSortByIDs(t *testing.T) { |
| 38 |
s1 := &mockSigner{ |
| 39 |
value: json.RawMessage(`{"mock": 1}`), |
| 40 |
} |
| 41 |
s2 := &mockSigner{ |
| 42 |
value: json.RawMessage(`{"mock": 2}`), |
| 43 |
} |
| 44 |
s3 := &mockSigner{ |
| 45 |
value: json.RawMessage(`{"mock": 3}`), |
| 46 |
} |
| 47 |
s4 := &mockSigner{ |
| 48 |
value: json.RawMessage(`{"mock": 4}`), |
| 49 |
} |
| 50 |
s5 := &mockSigner{ |
| 51 |
value: json.RawMessage(`{"mock": 5}`), |
| 52 |
} |
| 53 |
|
| 54 |
s := []keys.Signer{ |
| 55 |
s1, s2, s3, s4, s5, |
| 56 |
} |
| 57 |
|
| 58 |
sort.Sort(signer.ByIDs(s)) |
| 59 |
|
| 60 |
signerIDs := []string{} |
| 61 |
|
| 62 |
for i, signer := range s { |
| 63 |
ids := signer.PublicData().IDs() |
| 64 |
if len(ids) != 1 { |
| 65 |
t.Errorf("Signer %v IDs %v should have length 1", i, ids) |
| 66 |
} |
| 67 |
signerIDs = append(signerIDs, ids[0]) |
| 68 |
} |
| 69 |
|
| 70 |
if !sort.StringsAreSorted(signerIDs) { |
| 71 |
t.Errorf("Signers incorrectly sorted: %+v", signerIDs) |
| 72 |
} |
| 73 |
} |