From d8950a4e5c2f46a476483354135c2501e7f9aef5 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Wed, 11 Nov 2020 16:48:36 +0100 Subject: move test helpers to testdata package --- server/testdata/data.go | 4 ++-- server/testdata/helper.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ server/testdata/type.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 server/testdata/helper.go create mode 100644 server/testdata/type.go (limited to 'server') diff --git a/server/testdata/data.go b/server/testdata/data.go index dc61bb0..306b11f 100644 --- a/server/testdata/data.go +++ b/server/testdata/data.go @@ -1,6 +1,6 @@ - package testdata +package testdata - var ( +var ( PemAnchors = []byte(` -----BEGIN CERTIFICATE----- MIIB/TCCAa+gAwIBAgIUDYJzaC5VSkKwiLVAxO5MyphAkN8wBQYDK2VwMGwxCzAJ diff --git a/server/testdata/helper.go b/server/testdata/helper.go new file mode 100644 index 0000000..6874616 --- /dev/null +++ b/server/testdata/helper.go @@ -0,0 +1,47 @@ +package testdata + +import ( + "testing" + + "github.com/google/trillian" + "github.com/google/trillian/types" +) + +// NewGetLatestSignedLogRootResponse creates a new trillian STH. Revision, +// Metadata, Proof, KeyHint, and LogRootSignature are unsset. +func NewGetLatestSignedLogRootResponse(t *testing.T, timestamp, size uint64, hash []byte) *trillian.GetLatestSignedLogRootResponse { + t.Helper() + return &trillian.GetLatestSignedLogRootResponse{ + SignedLogRoot: marshalSignedLogRoot(t, &types.LogRootV1{ + TreeSize: size, + RootHash: hash, + TimestampNanos: timestamp, + Revision: 0, // not used by stfe + Metadata: nil, // not used by stfe + }), + Proof: nil, // not used by stfe + } +} + +// TruncatedSignedLogRootResponse creates a truncated signed log root response +// that cannot be unmarshalled, i.e., SignedLogRoot.LogRoot is invalid. +func TruncatedSignedLogRootResponse(t *testing.T) *trillian.GetLatestSignedLogRootResponse { + t.Helper() + slrr := NewGetLatestSignedLogRootResponse(t, 0, 0, make([]byte, 32)) + slrr.SignedLogRoot.LogRoot = slrr.SignedLogRoot.LogRoot[1:] + return slrr +} + +// marshalSignedLogRoot must marshal a signed log root +func marshalSignedLogRoot(t *testing.T, lr *types.LogRootV1) *trillian.SignedLogRoot { + t.Helper() + rootBytes, err := lr.MarshalBinary() + if err != nil { + t.Fatalf("failed to marshal root in test: %v", err) + } + return &trillian.SignedLogRoot{ + KeyHint: nil, // not used by stfe + LogRoot: rootBytes, + LogRootSignature: nil, // not used by stfe + } +} diff --git a/server/testdata/type.go b/server/testdata/type.go new file mode 100644 index 0000000..93041c9 --- /dev/null +++ b/server/testdata/type.go @@ -0,0 +1,32 @@ +package testdata + +import ( + "context" + "fmt" + + "github.com/golang/mock/gomock" +) + +// DeadlineMatcher implements gomock.Matcher, such that an error is raised if +// there is no context.Context deadline set +type DeadlineMatcher struct{} + +// NewDeadlineMatcher returns a new DeadlineMatcher +func NewDeadlineMatcher() gomock.Matcher { + return &DeadlineMatcher{} +} + +// Matches returns true if the passed interface is a context with a deadline +func (dm *DeadlineMatcher) Matches(i interface{}) bool { + ctx, ok := i.(context.Context) + if !ok { + return false + } + _, ok = ctx.Deadline() + return ok +} + +// String is needed to implement gomock.Matcher +func (dm *DeadlineMatcher) String() string { + return fmt.Sprintf("deadlineMatcher{}") +} -- cgit v1.2.3