aboutsummaryrefslogtreecommitdiff
path: root/instance_test.go
blob: 02424c66c9f76d5cb3aad414a523706a0fbd9c4d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package stfe

import (
	"bytes"
	"testing"

	"crypto"
	"crypto/sha256"
	"crypto/x509"

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

var (
	testHashLen     = 31
	testMaxRange    = int64(3)
	testMaxChain    = int64(3)
	testTreeId      = int64(0)
	testPrefix      = "test"
	testHashType    = crypto.SHA256
	testExtKeyUsage = []x509.ExtKeyUsage{}
)

func TestNewLogParameters(t *testing.T) {
	anchors, err := x509util.NewCertificateList(testdata.TrustAnchors)
	if err != nil {
		t.Fatalf("must decode trust anchors: %v", err)
	}
	signer, err := x509util.NewEd25519PrivateKey(testdata.LogPrivateKey)
	if err != nil {
		t.Fatalf("must decode private key: %v", err)
	}
	pub, err := x509.MarshalPKIXPublicKey(signer.Public())
	if err != nil {
		t.Fatalf("must encode public key: %v", err)
	}
	hasher := sha256.New()
	hasher.Write(pub)
	logId := hasher.Sum(nil)
	for _, table := range []struct {
		description string
		treeId      int64
		prefix      string
		maxRange    int64
		maxChain    int64
		anchors     []*x509.Certificate
		signer      crypto.Signer
		wantErr     bool
	}{
		{
			description: "invalid signer: nil",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    0,
			maxChain:    testMaxChain,
			anchors:     anchors,
			signer:      nil,
			wantErr:     true,
		},
		{
			description: "no trust anchors",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    testMaxRange,
			maxChain:    testMaxChain,
			anchors:     []*x509.Certificate{},
			signer:      signer,
			wantErr:     true,
		},
		{
			description: "invalid max range",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    0,
			maxChain:    testMaxChain,
			anchors:     anchors,
			signer:      signer,
			wantErr:     true,
		},
		{
			description: "invalid max chain",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    testMaxRange,
			maxChain:    0,
			anchors:     anchors,
			signer:      signer,
			wantErr:     true,
		},
		{
			description: "public key marshal failure",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    testMaxRange,
			maxChain:    testMaxChain,
			anchors:     []*x509.Certificate{},
			signer:      cttestdata.NewSignerWithFixedSig("no pub", testSignature),
			wantErr:     true,
		},
		{
			description: "valid log parameters",
			treeId:      testTreeId,
			prefix:      testPrefix,
			maxRange:    testMaxRange,
			maxChain:    testMaxChain,
			anchors:     anchors,
			signer:      signer,
		},
	} {
		lp, err := NewLogParameters(table.treeId, table.prefix, table.anchors, table.signer, table.maxRange, table.maxChain)
		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 := lp.LogId, logId; !bytes.Equal(got, want) {
			t.Errorf("got log id %X but wanted %X in test %q", got, want, table.description)
		}
		if got, want := lp.TreeId, testTreeId; got != want {
			t.Errorf("got tree id %d but wanted %d in test %q", got, want, table.description)
		}
		if got, want := lp.Prefix, testPrefix; got != want {
			t.Errorf("got prefix %s but wanted %s in test %q", got, want, table.description)
		}
		if got, want := lp.MaxRange, testMaxRange; got != want {
			t.Errorf("got max range %d but wanted %d in test %q", got, want, table.description)
		}
		if got, want := lp.MaxChain, testMaxChain; got != want {
			t.Errorf("got max chain %d but wanted %d in test %q", got, want, table.description)
		}
		if got, want := lp.MaxChain, testMaxChain; got != want {
			t.Errorf("got max chain %d but wanted %d in test %q", got, want, table.description)
		}
		if got, want := len(lp.AnchorList), len(anchors); got != want {
			t.Errorf("got %d anchors but wanted %d in test %q", got, want, table.description)
		}
		if got, want := len(lp.AnchorPool.Subjects()), len(anchors); got != want {
			t.Errorf("got %d anchors in pool but wanted %d in test %q", got, want, table.description)
		}
	}
}

// TestHandlers checks that we configured all endpoints and that there are no
// unexpected ones.
func TestHandlers(t *testing.T) {
	endpoints := map[Endpoint]bool{
		EndpointAddEntry:            false,
		EndpointGetEntries:          false,
		EndpointGetSth:              false,
		EndpointGetProofByHash:      false,
		EndpointGetConsistencyProof: false,
		EndpointGetAnchors:          false,
	}
	i := NewInstance(makeTestLogParameters(t, nil), nil, testDeadline)
	for _, handler := range i.Handlers() {
		if _, ok := endpoints[handler.endpoint]; !ok {
			t.Errorf("got unexpected endpoint: %s", handler.endpoint)
		}
		endpoints[handler.endpoint] = true
	}
	for endpoint, ok := range endpoints {
		if !ok {
			t.Errorf("endpoint %s is not configured", endpoint)
		}
	}
}

func TestEndpointPath(t *testing.T) {
	base, prefix := "http://example.com", "test"
	for _, table := range []struct {
		endpoint Endpoint
		want     string
	}{
		{
			endpoint: EndpointAddEntry,
			want:     "http://example.com/test/add-entry",
		},
		{
			endpoint: EndpointGetEntries,
			want:     "http://example.com/test/get-entries",
		},
		{
			endpoint: EndpointGetProofByHash,
			want:     "http://example.com/test/get-proof-by-hash",
		},
		{
			endpoint: EndpointGetConsistencyProof,
			want:     "http://example.com/test/get-consistency-proof",
		},
		{
			endpoint: EndpointGetSth,
			want:     "http://example.com/test/get-sth",
		},
		{
			endpoint: EndpointGetAnchors,
			want:     "http://example.com/test/get-anchors",
		},
	} {
		if got, want := table.endpoint.Path(base, prefix), table.want; got != want {
			t.Errorf("got %s but wanted %s with multiple components", got, want)
		}
		if got, want := table.endpoint.Path(base+"/"+prefix), table.want; got != want {
			t.Errorf("got %s but wanted %s with one component", got, want)
		}
	}
}

// makeTestLogParameters makes a collection of test log parameters that
// correspond to testLogId, testTreeId, testPrefix, testMaxRange, testMaxChain,
// the anchors in testdata.TrustAnchors, testHashType, and an optional signer.
func makeTestLogParameters(t *testing.T, signer crypto.Signer) *LogParameters {
	anchors, err := x509util.NewCertificateList(testdata.TrustAnchors)
	if err != nil {
		t.Fatalf("must decode trust anchors: %v", err)
	}
	return &LogParameters{
		LogId:      testLogId,
		TreeId:     testTreeId,
		Prefix:     testPrefix,
		MaxRange:   testMaxRange,
		MaxChain:   testMaxChain,
		AnchorPool: x509util.NewCertPool(anchors),
		AnchorList: anchors,
		KeyUsage:   testExtKeyUsage,
		Signer:     signer,
		HashType:   testHashType,
	}
}