diff options
author | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-05-21 20:31:09 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-06-21 19:46:54 +0200 |
commit | 97540f9ded30f68f9fda62f66f3006414cbfd5b7 (patch) | |
tree | 329db21d4e63aba70711d8e897fd95713f1cc1f9 | |
parent | 1f9ec26e11052e1ab24b854e997404a42ef5bfd0 (diff) |
move mock signer to internal/
-rw-r--r-- | internal/mocks/signer/signer.go | 22 | ||||
-rw-r--r-- | pkg/types/crypto_test.go | 15 | ||||
-rw-r--r-- | pkg/types/leaf_test.go | 6 | ||||
-rw-r--r-- | pkg/types/tree_head_test.go | 5 |
4 files changed, 29 insertions, 19 deletions
diff --git a/internal/mocks/signer/signer.go b/internal/mocks/signer/signer.go new file mode 100644 index 0000000..a4ec30d --- /dev/null +++ b/internal/mocks/signer/signer.go @@ -0,0 +1,22 @@ +package signer + +import ( + "crypto" + "crypto/ed25519" + "io" +) + +// Signer implements crypto.Signer with fixed outputs. Use for tests only. +type Signer struct { + PublicKey []byte + Signature []byte + Error error +} + +func (s *Signer) Public() crypto.PublicKey { + return ed25519.PublicKey(s.PublicKey[:]) +} + +func (s *Signer) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { + return s.Signature[:], s.Error +} diff --git a/pkg/types/crypto_test.go b/pkg/types/crypto_test.go index d95d5fa..181c8f0 100644 --- a/pkg/types/crypto_test.go +++ b/pkg/types/crypto_test.go @@ -4,24 +4,9 @@ import ( "crypto" "crypto/ed25519" "crypto/rand" - "io" "testing" ) -type testSigner struct { - PublicKey PublicKey - Signature Signature - Error error -} - -func (ts *testSigner) Public() crypto.PublicKey { - return ed25519.PublicKey(ts.PublicKey[:]) -} - -func (ts *testSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { - return ts.Signature[:], ts.Error -} - func newKeyPair(t *testing.T) (crypto.Signer, PublicKey) { vk, sk, err := ed25519.GenerateKey(rand.Reader) if err != nil { diff --git a/pkg/types/leaf_test.go b/pkg/types/leaf_test.go index 645f49e..9eae13c 100644 --- a/pkg/types/leaf_test.go +++ b/pkg/types/leaf_test.go @@ -8,6 +8,8 @@ import ( "reflect" "strings" "testing" + + "git.sigsum.org/sigsum-go/internal/mocks/signer" ) func TestStatementToBinary(t *testing.T) { @@ -28,13 +30,13 @@ func TestStatementSign(t *testing.T) { { desc: "invalid: signer error", stm: validStatement(t), - signer: &testSigner{*newPubBufferInc(t), *newSigBufferInc(t), fmt.Errorf("signing error")}, + signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], fmt.Errorf("signing error")}, wantErr: true, }, { desc: "valid", stm: validStatement(t), - signer: &testSigner{*newPubBufferInc(t), *newSigBufferInc(t), nil}, + signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], nil}, wantSig: newSigBufferInc(t), }, } { diff --git a/pkg/types/tree_head_test.go b/pkg/types/tree_head_test.go index a1ffa6f..aa57a1c 100644 --- a/pkg/types/tree_head_test.go +++ b/pkg/types/tree_head_test.go @@ -8,6 +8,7 @@ import ( "reflect" "testing" + "git.sigsum.org/sigsum-go/internal/mocks/signer" "git.sigsum.org/sigsum-go/pkg/hex" ) @@ -30,13 +31,13 @@ func TestTreeHeadSign(t *testing.T) { { desc: "invalid: signer error", th: validTreeHead(t), - signer: &testSigner{*newPubBufferInc(t), *newSigBufferInc(t), fmt.Errorf("signing error")}, + signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], fmt.Errorf("signing error")}, wantErr: true, }, { desc: "valid", th: validTreeHead(t), - signer: &testSigner{*newPubBufferInc(t), *newSigBufferInc(t), nil}, + signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], nil}, wantSig: newSigBufferInc(t), }, } { |