aboutsummaryrefslogtreecommitdiff
path: root/internal/node/secondary/secondary_test.go
blob: 164bdf694f38cb5eb6b215cdafe555e7bf0da6c2 (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
package secondary

import (
	"context"
	"fmt"
	"testing"

	mocksClient "git.sigsum.org/log-go/internal/mocks/client"
	mocksDB "git.sigsum.org/log-go/internal/mocks/db"
	"git.sigsum.org/sigsum-go/pkg/merkle"
	"git.sigsum.org/sigsum-go/pkg/types"
	"github.com/golang/mock/gomock"
)

var (
	testConfig = Config{
		LogID:    fmt.Sprintf("%x", merkle.HashFn([]byte("logid"))[:]),
		TreeID:   0,
		Prefix:   "testonly",
		Deadline: 10,
	}
)

// TestHandlers checks that the expected internal handlers are configured
func TestIntHandlers(t *testing.T) {
	endpoints := map[types.Endpoint]bool{
		types.EndpointGetTreeHeadToCosign: false,
	}
	node := Secondary{
		Config: testConfig,
	}
	for _, handler := range node.InternalHTTPHandlers() {
		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 TestFetchLeavesFromPrimary(t *testing.T) {
	for _, tbl := range []struct {
		desc string
		// client.GetUnsignedTreeHead()
		primaryTHRet types.TreeHead
		primaryTHErr error
		// db.GetTreeHead()
		trillianTHRet *types.TreeHead
		trillianTHErr error
		// client.GetLeaves()
		primaryGetLeavesRet types.Leaves
		primaryGetLeavesErr error
		// db.AddSequencedLeaves()
		trillianAddLeavesExp bool
		trillianAddLeavesErr error
	}{
		{
			desc:         "no tree head from primary",
			primaryTHErr: fmt.Errorf("mocked error"),
		},
		{
			desc:          "no tree head from trillian",
			primaryTHRet:  types.TreeHead{},
			trillianTHErr: fmt.Errorf("mocked error"),
		},
		{
			desc:                "error fetching leaves",
			primaryTHRet:        types.TreeHead{TreeSize: 6},
			trillianTHRet:       &types.TreeHead{TreeSize: 5}, // 6-5 => 1 expected GetLeaves
			primaryGetLeavesErr: fmt.Errorf("mocked error"),
		},
		{
			desc:          "error adding leaves",
			primaryTHRet:  types.TreeHead{TreeSize: 6},
			trillianTHRet: &types.TreeHead{TreeSize: 5}, // 6-5 => 1 expected GetLeaves
			primaryGetLeavesRet: types.Leaves{
				types.Leaf{},
			},
			trillianAddLeavesErr: fmt.Errorf("mocked error"),
		},
		{
			desc:          "success",
			primaryTHRet:  types.TreeHead{TreeSize: 10},
			trillianTHRet: &types.TreeHead{TreeSize: 5},
			primaryGetLeavesRet: types.Leaves{
				types.Leaf{},
			},
			trillianAddLeavesExp: true,
		},
	} {
		func() {
			ctrl := gomock.NewController(t)
			defer ctrl.Finish()

			primaryClient := mocksClient.NewMockClient(ctrl)
			primaryClient.EXPECT().GetUnsignedTreeHead(gomock.Any()).Return(tbl.primaryTHRet, tbl.primaryTHErr)

			trillianClient := mocksDB.NewMockClient(ctrl)
			if tbl.trillianTHErr != nil || tbl.trillianTHRet != nil {
				trillianClient.EXPECT().GetTreeHead(gomock.Any()).Return(tbl.trillianTHRet, tbl.trillianTHErr)
			}

			if tbl.primaryGetLeavesErr != nil || tbl.primaryGetLeavesRet != nil {
				primaryClient.EXPECT().GetLeaves(gomock.Any(), gomock.Any()).Return(tbl.primaryGetLeavesRet, tbl.primaryGetLeavesErr)
				if tbl.trillianAddLeavesExp {
					for i := tbl.trillianTHRet.TreeSize; i < tbl.primaryTHRet.TreeSize-1; i++ {
						primaryClient.EXPECT().GetLeaves(gomock.Any(), gomock.Any()).Return(tbl.primaryGetLeavesRet, tbl.primaryGetLeavesErr)
					}
				}
			}

			if tbl.trillianAddLeavesErr != nil || tbl.trillianAddLeavesExp {
				trillianClient.EXPECT().AddSequencedLeaves(gomock.Any(), gomock.Any(), gomock.Any()).Return(tbl.trillianAddLeavesErr)
				if tbl.trillianAddLeavesExp {
					for i := tbl.trillianTHRet.TreeSize; i < tbl.primaryTHRet.TreeSize-1; i++ {
						trillianClient.EXPECT().AddSequencedLeaves(gomock.Any(), gomock.Any(), gomock.Any()).Return(tbl.trillianAddLeavesErr)
					}
				}
			}

			node := Secondary{
				Config:         testConfig,
				Primary:        primaryClient,
				TrillianClient: trillianClient,
			}

			node.fetchLeavesFromPrimary(context.Background())

			// NOTE: We are not verifying that
			// AddSequencedLeaves() is being called with
			// the right data.
		}()
	}
}