From 57cbb3ca933b3f9e7bb3552cf3777b4a86239d13 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 24 Nov 2020 20:34:51 +0100 Subject: added isolated trillian proof tests --- handler.go | 8 +++--- trillian.go | 14 +++++----- trillian_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/handler.go b/handler.go index f06bd74..00fd686 100644 --- a/handler.go +++ b/handler.go @@ -134,8 +134,8 @@ func getProofByHash(ctx context.Context, i *Instance, w http.ResponseWriter, r * TreeSize: req.TreeSize, OrderBySequence: true, }) - if status, errInner := checkGetInclusionProofByHash(i.LogParameters, trsp, err); errInner != nil { - return status, fmt.Errorf("bad GetInclusionProofByHashResponse: %v", errInner) + if errInner := checkGetInclusionProofByHash(i.LogParameters, trsp, err); errInner != nil { + return http.StatusInternalServerError, fmt.Errorf("bad GetInclusionProofByHashResponse: %v", errInner) } rsp, err := NewInclusionProofV1(i.LogParameters.LogId, uint64(req.TreeSize), uint64(trsp.Proof[0].LeafIndex), trsp.Proof[0].Hashes).MarshalB64() @@ -161,8 +161,8 @@ func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter FirstTreeSize: int64(req.First), SecondTreeSize: int64(req.Second), }) - if status, errInner := checkGetConsistencyProof(i.LogParameters, trsp, err); errInner != nil { - return status, fmt.Errorf("bad GetConsistencyProofResponse: %v", errInner) + if errInner := checkGetConsistencyProof(i.LogParameters, trsp, err); errInner != nil { + return http.StatusInternalServerError, fmt.Errorf("bad GetConsistencyProofResponse: %v", errInner) } rsp, err := NewConsistencyProofV1(i.LogParameters.LogId, uint64(req.First), uint64(req.Second), trsp.Proof.Hashes).MarshalB64() diff --git a/trillian.go b/trillian.go index 5fab6f5..c20901f 100644 --- a/trillian.go +++ b/trillian.go @@ -48,16 +48,16 @@ func checkGetLeavesByRange(req *GetEntriesRequest, rsp *trillian.GetLeavesByRang return 0, nil } -func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) (int, error) { +func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) error { if err != nil || rsp == nil || len(rsp.Proof) == 0 || rsp.Proof[0] == nil { - return http.StatusInternalServerError, fmt.Errorf("%v", err) + return fmt.Errorf("%v", err) } return checkHashPath(lp.HashType.Size(), rsp.Proof[0].Hashes) } -func checkGetConsistencyProof(lp *LogParameters, rsp *trillian.GetConsistencyProofResponse, err error) (int, error) { +func checkGetConsistencyProof(lp *LogParameters, rsp *trillian.GetConsistencyProofResponse, err error) error { if err != nil || rsp == nil || rsp.Proof == nil { - return http.StatusInternalServerError, fmt.Errorf("%v", err) + return fmt.Errorf("%v", err) } return checkHashPath(lp.HashType.Size(), rsp.Proof.Hashes) } @@ -75,11 +75,11 @@ func checkGetLatestSignedLogRoot(lp *LogParameters, rsp *trillian.GetLatestSigne return nil } -func checkHashPath(hashSize int, path [][]byte) (int, error) { +func checkHashPath(hashSize int, path [][]byte) error { for _, hash := range path { if len(hash) != hashSize { - return http.StatusInternalServerError, fmt.Errorf("invalid proof: %v", path) + return fmt.Errorf("invalid proof: %v", path) } } - return 0, nil + return nil } diff --git a/trillian_test.go b/trillian_test.go index 6c9b7af..bbbfff3 100644 --- a/trillian_test.go +++ b/trillian_test.go @@ -141,12 +141,91 @@ func TestCheckGetLeavesByRange(t *testing.T) { } } -// TODO: TestCheckGetInclusionProofByHash func TestCheckGetInclusionProofByHash(t *testing.T) { + lp := makeTestLogParameters(t, nil) + for _, table := range []struct { + description string + rsp *trillian.GetInclusionProofByHashResponse + err error + wantErr bool + }{ + { + description: "bad response: trillian error", + err: fmt.Errorf("backend failure"), + wantErr: true, + }, + { + description: "bad response: empty", + wantErr: true, + }, + { + description: "bad response: no proofs", + rsp: &trillian.GetInclusionProofByHashResponse{}, + wantErr: true, + }, + { + description: "bad response: no proof", + rsp: func(rsp *trillian.GetInclusionProofByHashResponse) *trillian.GetInclusionProofByHashResponse { + rsp.Proof[0] = nil + return rsp + }(makeTrillianGetInclusionProofByHashResponse(t, int64(testIndex), testProof)), + wantErr: true, + }, + { + description: "bad response: proof with invalid node hash", + rsp: makeTrillianGetInclusionProofByHashResponse(t, int64(testIndex), [][]byte{make([]byte, 31)}), + wantErr: true, + }, + { + description: "ok response", + rsp: makeTrillianGetInclusionProofByHashResponse(t, int64(testIndex), testProof), + }, + } { + if err := checkGetInclusionProofByHash(lp, table.rsp, table.err); (err != nil) != table.wantErr { + t.Errorf("got error %v, but wanted error %v in test %q", err, table.wantErr, table.description) + } + } } +// TODO: fix hardcoded assumed test hash value len of 32? + // TODO: TestGetConsistencyProof func TestCheckGetConsistencyProof(t *testing.T) { + lp := makeTestLogParameters(t, nil) + for _, table := range []struct { + description string + rsp *trillian.GetConsistencyProofResponse + err error + wantErr bool + }{ + { + description: "bad response: trillian error", + err: fmt.Errorf("backend failure"), + wantErr: true, + }, + { + description: "bad response: empty", + wantErr: true, + }, + { + description: "bad response: no proof", + rsp: &trillian.GetConsistencyProofResponse{}, + wantErr: true, + }, + { + description: "bad response: proof with invalid node hash", + rsp: makeTrillianGetConsistencyProofResponse(t, [][]byte{make([]byte, 31)}), + wantErr: true, + }, + { + description: "ok response", + rsp: makeTrillianGetConsistencyProofResponse(t, testProof), + }, + } { + if err := checkGetConsistencyProof(lp, table.rsp, table.err); (err != nil) != table.wantErr { + t.Errorf("got error %v, but wanted error %v in test %q", err, table.wantErr, table.description) + } + } } func TestCheckGetLatestSignedLogRoot(t *testing.T) { -- cgit v1.2.3