Skip to content

Commit

Permalink
tests: lots of small asjustement
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Nov 20, 2024
1 parent 2fb5a3d commit aea1880
Show file tree
Hide file tree
Showing 27 changed files with 374 additions and 394 deletions.
112 changes: 45 additions & 67 deletions did/didtest/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
package didtest

import (
"sync"
"fmt"
"testing"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/stretchr/testify/require"

"github.com/ucan-wg/go-ucan/did"
)

const (
alicePrivKeyCfg = "CAESQHdNJLBBiuc1AdwPHBkubB2KS1p0cv2JEF7m8tfwtrcm5ajaYPm+XmVCmtcHOF2lGDlmaiDA7emfwD3IrcyES0M="
bobPrivKeyCfg = "CAESQHBz+AIop1g+9iBDj+ufUc/zm9/ry7c6kDFO8Wl/D0+H63V9hC6s9l4npf3pYEFCjBtlR0AMNWMoFQKSlYNKo20="
carolPrivKeyCfg = "CAESQPrCgkcHnYFXDT9AlAydhPECBEivEuuVx9dJxLjVvDTmJIVNivfzg6H4mAiPfYS+5ryVVUZTHZBzvMuvvvG/Ks0="
danPrivKeyCfg = "CAESQCgNhzofKhC+7hW6x+fNd7iMPtQHeEmKRhhlduf/I7/TeOEFYAEflbJ0sAhMeDJ/HQXaAvsWgHEbJ3ZLhP8q2B0="
erinPrivKeyCfg = "CAESQKhCJo5UBpQcthko8DKMFsbdZ+qqQ5oc01CtLCqrE90dF2GfRlrMmot3WPHiHGCmEYi5ZMEHuiSI095e/6O4Bpw="
frankPrivKeyCfg = "CAESQDlXPKsy3jHh7OWTWQqyZF95Ueac5DKo7xD0NOBE5F2BNr1ZVxRmJ2dBELbOt8KP9sOACcO9qlCB7uMA1UQc7sk="
alicePrivKeyB64 = "CAESQHdNJLBBiuc1AdwPHBkubB2KS1p0cv2JEF7m8tfwtrcm5ajaYPm+XmVCmtcHOF2lGDlmaiDA7emfwD3IrcyES0M="
bobPrivKeyB64 = "CAESQHBz+AIop1g+9iBDj+ufUc/zm9/ry7c6kDFO8Wl/D0+H63V9hC6s9l4npf3pYEFCjBtlR0AMNWMoFQKSlYNKo20="
carolPrivKeyB64 = "CAESQPrCgkcHnYFXDT9AlAydhPECBEivEuuVx9dJxLjVvDTmJIVNivfzg6H4mAiPfYS+5ryVVUZTHZBzvMuvvvG/Ks0="
danPrivKeyB64 = "CAESQCgNhzofKhC+7hW6x+fNd7iMPtQHeEmKRhhlduf/I7/TeOEFYAEflbJ0sAhMeDJ/HQXaAvsWgHEbJ3ZLhP8q2B0="
erinPrivKeyB64 = "CAESQKhCJo5UBpQcthko8DKMFsbdZ+qqQ5oc01CtLCqrE90dF2GfRlrMmot3WPHiHGCmEYi5ZMEHuiSI095e/6O4Bpw="
frankPrivKeyB64 = "CAESQDlXPKsy3jHh7OWTWQqyZF95Ueac5DKo7xD0NOBE5F2BNr1ZVxRmJ2dBELbOt8KP9sOACcO9qlCB7uMA1UQc7sk="
)

// Persona is a generic participant used for cryptographic testing.
type Persona int

// The provided Personas were selected from the first few generic
// The provided Personas were selected from the first few generic
// participants listed in this [table].
//
// [table]: https://en.wikipedia.org/wiki/Alice_and_Bob#Cryptographic_systems
Expand All @@ -37,26 +38,36 @@ const (
PersonaFrank
)

var (
once sync.Once
var privKeys map[Persona]crypto.PrivKey

func init() {
privKeys = make(map[Persona]crypto.PrivKey, 6)
err error
)
for persona, privKeyCfg := range privKeyB64() {
privKeyMar, err := crypto.ConfigDecodeKey(privKeyCfg)
if err != nil {
return
}

// DID returns a did.DID based on the Persona's Ed25519 public key.
func (p Persona) DID(t *testing.T) did.DID {
t.Helper()
privKey, err := crypto.UnmarshalPrivateKey(privKeyMar)
if err != nil {
return
}

did, err := did.FromPrivKey(p.PrivKey(t))
require.NoError(t, err)
privKeys[persona] = privKey
}
}

return did
// DID returns a did.DID based on the Persona's Ed25519 public key.
func (p Persona) DID() did.DID {
d, err := did.FromPrivKey(p.PrivKey())
if err != nil {
panic(err)
}
return d
}

// Name returns the username of the Persona.
func (p Persona) Name(t *testing.T) string {
t.Helper()

func (p Persona) Name() string {
name, ok := map[Persona]string{
PersonaAlice: "Alice",
PersonaBob: "Bob",
Expand All @@ -66,78 +77,45 @@ func (p Persona) Name(t *testing.T) string {
PersonaFrank: "Frank",
}[p]
if !ok {
t.Fatal("Unknown persona:", p)
panic(fmt.Sprintf("Unknown persona: %v", p))
}

return name
}

// PrivKey returns the Ed25519 private key for the Persona.
func (p Persona) PrivKey(t *testing.T) crypto.PrivKey {
t.Helper()

once.Do(func() {
for persona, privKeyCfg := range privKeyCfgs(t) {
privKeyMar, err := crypto.ConfigDecodeKey(privKeyCfg)
if err != nil {
return
}

privKey, err := crypto.UnmarshalPrivateKey(privKeyMar)
if err != nil {
return
}

privKeys[persona] = privKey
}
})
require.NoError(t, err)

func (p Persona) PrivKey() crypto.PrivKey {
return privKeys[p]
}

// PrivKeyConfig returns the marshaled and encoded Ed25519 private key
// for the Persona.
func (p Persona) PrivKeyConfig(t *testing.T) string {
t.Helper()

return privKeyCfgs(t)[p]
}

// PubKey returns the Ed25519 public key for the Persona.
func (p Persona) PubKey(t *testing.T) crypto.PubKey {
t.Helper()

return p.PrivKey(t).GetPublic()
func (p Persona) PubKey() crypto.PubKey {
return p.PrivKey().GetPublic()
}

// PubKeyConfig returns the marshaled and encoded Ed25519 public key
// for the Persona.
func (p Persona) PubKeyConfig(t *testing.T) string {
pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey(t).GetPublic())
pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey().GetPublic())
require.NoError(t, err)

return crypto.ConfigEncodeKey(pubKeyMar)
}

func privKeyCfgs(t *testing.T) map[Persona]string {
t.Helper()

func privKeyB64() map[Persona]string {
return map[Persona]string{
PersonaAlice: alicePrivKeyCfg,
PersonaBob: bobPrivKeyCfg,
PersonaCarol: carolPrivKeyCfg,
PersonaDan: danPrivKeyCfg,
PersonaErin: erinPrivKeyCfg,
PersonaFrank: frankPrivKeyCfg,
PersonaAlice: alicePrivKeyB64,
PersonaBob: bobPrivKeyB64,
PersonaCarol: carolPrivKeyB64,
PersonaDan: danPrivKeyB64,
PersonaErin: erinPrivKeyB64,
PersonaFrank: frankPrivKeyB64,
}
}

// Personas returns an (alphabetically) ordered list of the defined
// Persona values.
func Personas(t *testing.T) []Persona {
t.Helper()

func Personas() []Persona {
return []Persona{
PersonaAlice,
PersonaBob,
Expand Down
20 changes: 0 additions & 20 deletions pkg/policy/policytest/policy.go

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions token/delegation/delegationtest/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//
// Delegation proof-chain names contain each didtest.Persona name in
// order starting with the root delegation (which will always be generated
// by Alice.) This is opposite of the list of cic.Cids that represent the
// by Alice). This is the opposite of the list of cic.Cids that represent the
// proof chain.
//
// For both the generated delegation tokens granted to Carol's Persona and
// the proof chains containing Carol's delegations to Dan, if there is no
// suffix, the proof chain will be deemed valid. If there is a suffix, it
// will consist of either the word "Valid" or "Invalid" and the name of the
// field that has been altered. Only optional fields will generate proof
// field that has been altered. Only optional fields will generate proof
// chains with Valid suffixes.
//
// If changes are made to the list of Personas included in the chain, or
Expand All @@ -25,9 +25,9 @@
// go test . -update
//
// Generated delegation Tokens are stored in the data/ directory and loaded
// into the DelegationLoader on the first call to GetDelegationLoader.
// into the delegation.Loader.
// Generated references to these tokens and the tokens themselves are
// created in the token_gen.go file. See /token/invocation/invocation_test.go
// created in the token_gen.go file. See /token/invocation/invocation_test.go
// for an example of how these delegation tokens and proof-chains can
// be used during testing.
package delegationtest
Loading

0 comments on commit aea1880

Please sign in to comment.