aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-18 19:30:17 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-18 19:30:17 +0100
commitf93ca569f1c892217786302e6a46682f015fda57 (patch)
treed0bc7ca392d29f0eed7c516c4b04443c9401532a
parent55a0a1c9c2a6df4f05d539f0fc67a8c7052a338f (diff)
added TestNewActiveSthSource
-rw-r--r--sth.go4
-rw-r--r--sth_test.go57
2 files changed, 59 insertions, 2 deletions
diff --git a/sth.go b/sth.go
index 5cf3413..810bd58 100644
--- a/sth.go
+++ b/sth.go
@@ -51,7 +51,7 @@ func NewActiveSthSource(cli trillian.TrillianLogClient, lp *LogParameters) (*Act
if err != nil {
return nil, fmt.Errorf("Latest: %v", err)
}
- // TODO: load peristed cosigned STH?
+ // TODO: load persisted cosigned STH?
s.currSth = NewCosignedTreeHeadV1(sth.SignedTreeHeadV1, nil)
s.nextSth = NewCosignedTreeHeadV1(sth.SignedTreeHeadV1, nil)
s.cosignatureFrom = make(map[string]bool)
@@ -93,7 +93,7 @@ func (s *ActiveSthSource) Cosigned(_ context.Context) (*StItem, error) {
func (s *ActiveSthSource) AddCosignature(_ context.Context, costh *StItem) error {
s.mutex.Lock()
defer s.mutex.Unlock()
- if !reflect.DeepEqual(s.nextSth.CosignedTreeHeadV1.SignedTreeHeadV1, costh.CosignedTreeHeadV1.SignedTreeHeadV1) {
+ if !reflect.DeepEqual(s.nextSth.CosignedTreeHeadV1.SignedTreeHeadV1, costh.CosignedTreeHeadV1.SignedTreeHeadV1) {
return fmt.Errorf("cosignature covers a different tree head")
}
witness := costh.CosignedTreeHeadV1.SignatureV1[0].Namespace.String()
diff --git a/sth_test.go b/sth_test.go
index 3b84b8c..c174672 100644
--- a/sth_test.go
+++ b/sth_test.go
@@ -452,3 +452,60 @@ func TestRotate(t *testing.T) {
}
}
}
+
+func TestNewActiveSthSource(t *testing.T) {
+ for _, table := range []struct {
+ description string
+ signer crypto.Signer
+ trsp *trillian.GetLatestSignedLogRootResponse
+ terr error
+ wantErr bool
+ wantCosi *StItem // current cosigned sth
+ wantStable *StItem // next stable sth that signatures are collected for
+ }{
+ {
+ description: "invalid trillian response",
+ signer: cttestdata.NewSignerWithFixedSig(nil, testSignature),
+ terr: fmt.Errorf("internal server error"),
+ wantErr: true,
+ },
+ {
+ description: "valid",
+ signer: cttestdata.NewSignerWithFixedSig(nil, testSignature),
+ trsp: makeLatestSignedLogRootResponse(t, testTimestamp, testTreeSize, testNodeHash),
+ wantCosi: NewCosignedTreeHeadV1(NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, testSignature).SignedTreeHeadV1, nil),
+ wantStable: NewCosignedTreeHeadV1(NewSignedTreeHeadV1(NewTreeHeadV1(makeTrillianLogRoot(t, testTimestamp, testTreeSize, testNodeHash)), testLogId, testSignature).SignedTreeHeadV1, nil),
+ },
+ } {
+ func() { // run deferred functions at the end of each iteration
+ th := newTestHandler(t, table.signer, nil)
+ defer th.mockCtrl.Finish()
+ th.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr)
+ source, err := NewActiveSthSource(th.client, th.instance.LogParameters)
+ 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 {
+ return
+ }
+
+ if got, want := source.currSth, table.wantCosi; !reflect.DeepEqual(got, want) {
+ t.Errorf("got cosigned sth %v but wanted %v in test %q", got, want, table.description)
+ }
+ if got, want := source.nextSth, table.wantStable; !reflect.DeepEqual(got, want) {
+ t.Errorf("got stable sth %v but wanted %v in test %q", got, want, table.description)
+ }
+ cosignatureFrom := make(map[string]bool)
+ for _, wit := range table.wantStable.CosignedTreeHeadV1.SignatureV1 {
+ cosignatureFrom[wit.Namespace.String()] = true
+ }
+ if got, want := source.cosignatureFrom, cosignatureFrom; !reflect.DeepEqual(got, want) {
+ if got == nil {
+ t.Errorf("got uninitialized witness map %v but wanted %v in test %q", nil, want, table.description)
+ } else {
+ t.Errorf("got witness map %v but wanted %v in test %q", got, want, table.description)
+ }
+ }
+ }()
+ }
+}