From c05c22ddbc771e7713849cae40f9d91bfafa0503 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 25 Feb 2021 14:36:35 +0100 Subject: major refactor based on README.md and TODOs Updated types, improved units tests, isolated most test data to have it in one place, renamed and created new files to improve readability, and fixed a bunch of minor TODOs. --- log_parameters_test.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 log_parameters_test.go (limited to 'log_parameters_test.go') diff --git a/log_parameters_test.go b/log_parameters_test.go new file mode 100644 index 0000000..cec7674 --- /dev/null +++ b/log_parameters_test.go @@ -0,0 +1,98 @@ +package stfe + +import ( + "crypto" + "fmt" + "reflect" + "testing" + + cttestdata "github.com/google/certificate-transparency-go/trillian/testdata" + "github.com/system-transparency/stfe/testdata" + "github.com/system-transparency/stfe/types" +) + +// newLogParameters must create new log parameters with an optional log signer +// based on the parameters in "github.com/system-transparency/stfe/testdata". +// The log's namespace is initialized with testdata.LogEd25519Vk, the submmiter +// namespace list is initialized with testdata.SubmmiterEd25519, and the witness +// namespace list is initialized with testdata.WitnessEd25519Vk. +func newLogParameters(t *testing.T, signer crypto.Signer) *LogParameters { + t.Helper() + logId := testdata.NewNamespace(t, testdata.Ed25519VkLog) + witnessPool := testdata.NewNamespacePool(t, []*types.Namespace{ + testdata.NewNamespace(t, testdata.Ed25519VkWitness), + }) + submitPool := testdata.NewNamespacePool(t, []*types.Namespace{ + testdata.NewNamespace(t, testdata.Ed25519VkSubmitter), + }) + lp, err := NewLogParameters(signer, logId, testdata.TreeId, testdata.Prefix, submitPool, witnessPool, testdata.MaxRange, testdata.Interval, testdata.Deadline) + if err != nil { + t.Fatalf("must create new log parameters: %v", err) + } + return lp +} + +func TestNewLogParameters(t *testing.T) { + for _, table := range []struct { + description string + logId *types.Namespace + wantErr bool + }{ + { + description: "invalid: cannot marshal log id", + logId: &types.Namespace{ + Format: types.NamespaceFormatReserved, + }, + wantErr: true, + }, + { + description: "valid", + logId: testdata.NewNamespace(t, testdata.Ed25519VkLog), + }, + } { + _, err := NewLogParameters(nil, table.logId, testdata.TreeId, testdata.Prefix, nil, nil, testdata.MaxRange, testdata.Interval, testdata.Deadline) + if got, want := err != nil, table.wantErr; got != want { + t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) + } + } +} + +func TestSignTreeHeadV1(t *testing.T) { + for _, table := range []struct { + description string + th *types.TreeHeadV1 + signer crypto.Signer + wantErr bool + wantSth *types.StItem + }{ + { + description: "invalid: marshal failure", + th: types.NewTreeHeadV1(testdata.Timestamp, testdata.TreeSize, nil, testdata.Extension), + wantErr: true, + }, + { + description: "invalid: signature failure", + th: types.NewTreeHeadV1(testdata.Timestamp, testdata.TreeSize, testdata.NodeHash, testdata.Extension), + signer: cttestdata.NewSignerWithErr(nil, fmt.Errorf("signer failed")), + wantErr: true, + }, + { + description: "valid", + th: testdata.DefaultTh(t), + signer: cttestdata.NewSignerWithFixedSig(nil, testdata.Signature), + wantSth: testdata.DefaultSth(t, testdata.Ed25519VkLog), + }, + } { + sth, err := newLogParameters(t, table.signer).SignTreeHeadV1(table.th) + if got, want := err != nil, table.wantErr; got != want { + t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) + } + if err != nil { + continue + } + + if got, want := sth, table.wantSth; !reflect.DeepEqual(got, want) { + t.Errorf("got \n%v\n\tbut wanted\n%v\n\tin test %q", got, want, table.description) + } + } +} -- cgit v1.2.3