aboutsummaryrefslogtreecommitdiff
path: root/crypto_test.go
blob: cfbb0a857059f6903f3c7e0441ffad6fd8ecbc37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package stfe

import (
	"bytes"
	"crypto"
	"fmt"
	"testing"

	cttestdata "github.com/google/certificate-transparency-go/trillian/testdata"
)

var (
	testLeaf = make([]byte, 64)
)

// TestGenV1Sdi tests that a signature failure works as expected, and that
// the issued SDI (if any) is populated correctly.
func TestGenV1Sdi(t *testing.T) {
	for _, table := range []struct {
		description string
		leaf        []byte
		signer      crypto.Signer
		wantErr     bool
	}{
		{
			description: "signature failure",
			leaf:        testLeaf,
			signer:      cttestdata.NewSignerWithErr(nil, fmt.Errorf("signer failed")),
			wantErr:     true,
		},
		{
			description: "all ok",
			leaf:        testLeaf,
			signer:      cttestdata.NewSignerWithFixedSig(nil, testSignature),
		},
	} {
		item, err := makeTestLogParameters(t, table.signer).genV1Sdi(table.leaf)
		if err != nil && !table.wantErr {
			t.Errorf("signing failed in test %q: %v", table.description, err)
		} else if err == nil && table.wantErr {
			t.Errorf("signing succeeded but wanted failure in test %q", table.description)
		}
		if err != nil || table.wantErr {
			continue
		}
		if want, got := item.Format, StFormatSignedDebugInfoV1; got != want {
			t.Errorf("got format %s, wanted %s in test %q", got, want, table.description)
			continue
		}

		sdi := item.SignedDebugInfoV1
		if got, want := sdi.LogId, testLogId; !bytes.Equal(got, want) {
			t.Errorf("got logId %X, wanted %X in test %q", got, want, table.description)
		}
		if got, want := sdi.Message, []byte("reserved"); !bytes.Equal(got, want) {
			t.Errorf("got message %s, wanted %s in test %q", got, want, table.description)
		}
		if got, want := sdi.Signature, testSignature; !bytes.Equal(got, want) {
			t.Errorf("got signature %X, wanted %X in test %q", got, want, table.description)
		}
	}
}

// TestGenV1Sth tests that a signature failure works as expected, and that
// the issued STH (if any) is populated correctly.
func TestGenV1Sth(t *testing.T) {
	th := NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash))
	for _, table := range []struct {
		description string
		th          *TreeHeadV1
		signer      crypto.Signer
		wantErr     bool
	}{
		{
			description: "marshal failure",
			th:          NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, nil)),
			wantErr:     true,
		},
		{
			description: "signature failure",
			th:          th,
			signer:      cttestdata.NewSignerWithErr(nil, fmt.Errorf("signer failed")),
			wantErr:     true,
		},
		{
			description: "all ok",
			th:          th,
			signer:      cttestdata.NewSignerWithFixedSig(nil, testSignature),
		},
	} {
		item, err := makeTestLogParameters(t, table.signer).genV1Sth(table.th)
		if err != nil && !table.wantErr {
			t.Errorf("signing failed in test %q: %v", table.description, err)
		} else if err == nil && table.wantErr {
			t.Errorf("signing succeeded but wanted failure in test %q", table.description)
		}
		if err != nil || table.wantErr {
			continue
		}
		if want, got := item.Format, StFormatSignedTreeHeadV1; got != want {
			t.Errorf("got format %s, wanted %s in test %q", got, want, table.description)
			continue
		}

		sth := item.SignedTreeHeadV1
		if got, want := sth.LogId, testLogId; !bytes.Equal(got, want) {
			t.Errorf("got logId %X, wanted %X in test %q", got, want, table.description)
		}
		if got, want := sth.Signature, testSignature; !bytes.Equal(got, want) {
			t.Errorf("got signature %X, wanted %X in test %q", got, want, table.description)
		}
		if got, want := sth.TreeHead.Timestamp, th.Timestamp; got != want {
			t.Errorf("got timestamp %d, wanted %d in test %q", got, want, table.description)
		}
		if got, want := sth.TreeHead.TreeSize, th.TreeSize; got != want {
			t.Errorf("got tree size %d, wanted %d in test %q", got, want, table.description)
		}
		if got, want := sth.TreeHead.RootHash.Data, th.RootHash.Data; !bytes.Equal(got, want) {
			t.Errorf("got root hash %X, wanted %X in test %q", got, want, table.description)
		}
		if sth.TreeHead.Extension != nil {
			t.Errorf("got extensions %X, wanted nil in test %q", sth.TreeHead.Extension, table.description)
		}
	}
}