File: sth_test.go

package info (click to toggle)
golang-github-google-certificate-transparency 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,764 kB
  • sloc: sh: 606; makefile: 103; sql: 16
file content (311 lines) | stat: -rw-r--r-- 9,503 bytes parent folder | download | duplicates (3)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctfe

import (
	"context"
	"crypto"
	"errors"
	"io"
	"reflect"
	"strings"
	"testing"

	"github.com/golang/mock/gomock"
	ct "github.com/google/certificate-transparency-go"
	"github.com/google/certificate-transparency-go/tls"
	"github.com/google/certificate-transparency-go/trillian/mockclient"
	"github.com/google/trillian"
	"github.com/google/trillian/types"
)

type testCase struct {
	desc     string
	ctxSetup func(ctx context.Context) context.Context
	ms       MirrorSTHStorage // Only set for mirror getter tests.
	slr      *trillian.GetLatestSignedLogRootResponse
	slrErr   error
	sig      []byte // Only set (and sigErr) for log getter tests.
	sigErr   error
	wantSTH  *ct.SignedTreeHead
	errStr   string
}

// commonTests are valid for both cases, mostly basic parameter checks
// and type / error handling.
func commonTests(t *testing.T) []testCase {
	t.Helper()
	return []testCase{
		{
			desc: "bad quota value",
			ctxSetup: func(ctx context.Context) context.Context {
				return context.WithValue(ctx, remoteQuotaCtxKey, []byte("not a string value"))
			},
			errStr: "incorrect quota",
		},
		{
			desc:   "latest root RPC fails",
			slrErr: errors.New("slr failed"),
			errStr: "slr failed",
		},
		{
			desc:   "nil slr",
			slr:    &trillian.GetLatestSignedLogRootResponse{},
			errStr: "no log root",
		},
		{
			desc:   "bad slr",
			slr:    &trillian.GetLatestSignedLogRootResponse{SignedLogRoot: &trillian.SignedLogRoot{LogRoot: []byte("not tls encoded")}},
			errStr: "unmarshal root: log_root:\"not tls",
		},
		{
			desc: "bad hash",
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{RootHash: []byte("not a 32 byte hash")}),
			},
			errStr: "bad hash size",
		},
	}
}

// logTests apply only to the LogSTHGetter where things are signed.
func logTests(t *testing.T) []testCase {
	t.Helper()
	return []testCase{
		{
			desc: "signer error",
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{RootHash: []byte("12345678123456781234567812345678")}),
			},
			sigErr: errors.New("not signing that"),
			errStr: "sign tree head: not signing",
		},
		{
			desc: "empty sig",
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{RootHash: []byte("12345678123456781234567812345678")}),
			},
			sig:    []byte{},
			errStr: "sign tree head: <nil>",
		},
		{
			desc: "ok",
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{
						// Ensure response contains all fields needed for the CT STH.
						TreeSize:       12345,
						TimestampNanos: 987654321,
						RootHash:       []byte("12345678123456781234567812345678")}),
			},
			sig: []byte("signedit"),
			wantSTH: &ct.SignedTreeHead{
				Timestamp:      987,
				SHA256RootHash: hashFromString("12345678123456781234567812345678"),
				TreeHeadSignature: ct.DigitallySigned{
					Algorithm: tls.SignatureAndHashAlgorithm{
						Hash:      tls.SHA256,
						Signature: tls.SignatureAlgorithmFromPubKey(tls.Anonymous),
					},
					Signature: []byte("signedit"),
				},
				TreeSize: 12345,
			},
		},
	}
}

// mirrorTests apply only to the MirrorSTHGetter where sth is read from a store.
func mirrorTests(t *testing.T) []testCase {
	t.Helper()
	return []testCase{
		{
			desc: "bad mirror storage",
			ms: &fakeMirrorSTHStorage{
				err: errors.New("mirror store failed"),
			},
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{
						// Ensure response contains all fields needed for the CT STH.
						TreeSize:       12345,
						TimestampNanos: 987654321,
						RootHash:       []byte("12345678123456781234567812345678")}),
			},
			errStr: "mirror store failed",
		},
		{
			desc: "ok",
			ms: &fakeMirrorSTHStorage{
				sth: &ct.SignedTreeHead{
					Timestamp:      987,
					SHA256RootHash: hashFromString("12345678123456781234567812345678"),
					TreeHeadSignature: ct.DigitallySigned{
						Algorithm: tls.SignatureAndHashAlgorithm{
							Hash:      tls.SHA256,
							Signature: tls.SignatureAlgorithmFromPubKey(tls.Anonymous),
						},
						Signature: []byte("signedit"),
					},
					TreeSize: 12345,
				},
			},
			slr: &trillian.GetLatestSignedLogRootResponse{
				SignedLogRoot: mustMarshalRoot(t,
					&types.LogRootV1{
						// Ensure response contains all fields needed for the CT STH.
						TreeSize:       12345,
						TimestampNanos: 987654321,
						RootHash:       []byte("12345678123456781234567812345678")}),
			},
			wantSTH: &ct.SignedTreeHead{
				Timestamp:      987,
				SHA256RootHash: hashFromString("12345678123456781234567812345678"),
				TreeHeadSignature: ct.DigitallySigned{
					Algorithm: tls.SignatureAndHashAlgorithm{
						Hash:      tls.SHA256,
						Signature: tls.SignatureAlgorithmFromPubKey(tls.Anonymous),
					},
					Signature: []byte("signedit"),
				},
				TreeSize: 12345,
			},
		},
	}
}

func TestLogSTHGetter(t *testing.T) {
	// Note: Does not test signature cache interaction as this is inside
	// signV1TreeHead and covered by other tests.
	tests := make([]testCase, 0, 30)
	tests = append(tests, commonTests(t)...)
	tests = append(tests, logTests(t)...)

	for _, tc := range tests {
		t.Run(tc.desc, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			rpcCl := mockclient.NewMockTrillianLogClient(ctrl)
			if tc.slr != nil || tc.slrErr != nil {
				rpcCl.EXPECT().GetLatestSignedLogRoot(gomock.Any(), cmpMatcher{&trillian.GetLatestSignedLogRootRequest{LogId: 99}}).Return(tc.slr, tc.slrErr)
			}

			sthg := LogSTHGetter{li: &logInfo{rpcClient: rpcCl, logID: 99, signer: &fakeSigner{sig: tc.sig, err: tc.sigErr}}}
			ctx := context.Background()
			if tc.ctxSetup != nil {
				ctx = tc.ctxSetup(ctx)
			}

			sth, err := sthg.GetSTH(ctx)
			if len(tc.errStr) > 0 {
				if err == nil || !strings.Contains(err.Error(), tc.errStr) {
					t.Errorf("GetSTH()=%v, %v want: nil, err containing %s", sth, err, tc.errStr)
				}
			} else {
				if err != nil || !reflect.DeepEqual(sth, tc.wantSTH) {
					t.Errorf("GetSTH()=%v, %v, want: %v, nil", sth, err, tc.wantSTH)
				}
			}
			ctrl.Finish()
		})
	}
}

func TestMirrorSTHGetter(t *testing.T) {
	// Note: This does not test the operation of MirrorSTHStorage. Implementations
	// of this need their own tests.
	tests := make([]testCase, 0, 30)
	tests = append(tests, commonTests(t)...)
	tests = append(tests, mirrorTests(t)...)

	for _, tc := range tests {
		t.Run(tc.desc, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			rpcCl := mockclient.NewMockTrillianLogClient(ctrl)
			if tc.slr != nil || tc.slrErr != nil {
				rpcCl.EXPECT().GetLatestSignedLogRoot(gomock.Any(), cmpMatcher{&trillian.GetLatestSignedLogRootRequest{LogId: 99}}).Return(tc.slr, tc.slrErr)
			}

			sthg := MirrorSTHGetter{li: &logInfo{rpcClient: rpcCl, logID: 99}, st: tc.ms}
			ctx := context.Background()
			if tc.ctxSetup != nil {
				ctx = tc.ctxSetup(ctx)
			}

			sth, err := sthg.GetSTH(ctx)
			if len(tc.errStr) > 0 {
				if err == nil || !strings.Contains(err.Error(), tc.errStr) {
					t.Errorf("GetSTH()=%v, %v want: nil, err containing %s", sth, err, tc.errStr)
				}
			} else {
				if err != nil || !reflect.DeepEqual(sth, tc.wantSTH) {
					t.Errorf("GetSTH()=%v, %v, want: %v, nil", sth, err, tc.wantSTH)
				}
			}
			ctrl.Finish()
		})
	}
}

func TestFrozenSTHGetter(t *testing.T) {
	sth := &ct.SignedTreeHead{TreeSize: 123, Version: 1}
	f := FrozenSTHGetter{sth: sth}
	// This should always return its canned value and never an error.
	if sth2, err := f.GetSTH(context.Background()); sth2 != sth || err != nil {
		t.Fatalf("FrozenSTHGetter.GetSTH()=%v, %v, want: %v, nil", sth2, err, sth)
	}
}

func TestDefaultMirrorSTHStorage(t *testing.T) {
	s, err := DefaultMirrorSTHFactory{}.NewStorage([32]byte{})
	if err != nil {
		t.Fatalf("NewStorage()=%v, %v, want: no err", s, err)
	}
	// We expect a "not implemented" error from this and nil sth.
	sth, err := s.GetMirrorSTH(context.Background(), 9999)
	if sth != nil || err == nil || !strings.Contains(err.Error(), "not impl") {
		t.Fatalf("MirrorSTHStorage.GetMirrorSTH(): got: %v, %v, want: nil, err containing 'not impl'", sth, err)
	}
}

type fakeSigner struct {
	sig []byte
	err error
}

func (f *fakeSigner) Public() crypto.PublicKey {
	return []byte("this key is public") // This will map to tls.Anonymous.
}

func (f *fakeSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
	return f.sig, f.err
}

type fakeMirrorSTHStorage struct {
	sth *ct.SignedTreeHead
	err error
}

func (f *fakeMirrorSTHStorage) GetMirrorSTH(ctx context.Context, maxTreeSize int64) (*ct.SignedTreeHead, error) {
	return f.sth, f.err
}

func hashFromString(str string) [32]byte {
	var hash = [32]byte{}
	copy(hash[:], []byte(str))
	return hash
}