aboutsummaryrefslogtreecommitdiff
path: root/log_parameters_test.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
commitc05c22ddbc771e7713849cae40f9d91bfafa0503 (patch)
treeb97d11ab2a914806e6f671f9aff1cab9767b2eab /log_parameters_test.go
parentc9b4b43654f0ff26207cc63449f13298cd3c56e8 (diff)
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.
Diffstat (limited to 'log_parameters_test.go')
-rw-r--r--log_parameters_test.go98
1 files changed, 98 insertions, 0 deletions
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)
+ }
+ }
+}