Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Sep 21, 2023
1 parent a5ee41c commit 8ad4058
Showing 1 changed file with 78 additions and 8 deletions.
86 changes: 78 additions & 8 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/transparency-dev/formats/log"
Expand Down Expand Up @@ -78,21 +79,51 @@ func mustLoadTestCheckpoints() ([][]byte, []log.Checkpoint) {
return raws, cps
}

// testLogFetcher is a fetcher which reads from the checked-in golden test log
// data stored in ../testdata/log
func testLogFetcher(_ context.Context, p string) ([]byte, error) {
path := filepath.Join("../testdata/log", p)
return os.ReadFile(path)
}

// fetchCheckpointShim allows fetcher requests for checkpoints to be intercepted.
type fetchCheckpointShim struct {
// Checkpoints holds raw checkpoints to be returned when the fetcher is asked to retrieve a checkpoint path.
// The zero-th entry will be returned until Advance is called.
Checkpoints [][]byte
}

// Fetcher intercepts requests for the checkpoint file, returning the zero-th
// entry in the Checkpoints field. All other requests are passed through
// to the delegate fetcher.
func (f *fetchCheckpointShim) Fetcher(deleg Fetcher) Fetcher {
return func(ctx context.Context, path string) ([]byte, error) {
if strings.HasSuffix(path, "checkpoint") {
if len(f.Checkpoints) == 0 {
return nil, os.ErrNotExist
}
r := f.Checkpoints[0]
return r, nil
}
return deleg(ctx, path)
}
}

// Advance causes subsequent intercepted checkpoint requests to return
// the next entry in the Checkpoints slice.
func (f *fetchCheckpointShim) Advance() {
f.Checkpoints = f.Checkpoints[1:]
}

func TestCheckLogStateTracker(t *testing.T) {
ctx := context.Background()
h := rfc6962.DefaultHasher

lst := NewLogStateTracker(ctx, testLogFetcher, h, testRawCheckpoints[0])

for _, test := range []struct {
desc string
cpRaws [][]byte
}{} {
desc string
cpRaws [][]byte
wantCpRaws [][]byte
}{
{
desc: "Consistent",
cpRaws: [][]byte{
Expand All @@ -103,6 +134,14 @@ func TestCheckLogStateTracker(t *testing.T) {
testRawCheckpoints[6],
testRawCheckpoints[10],
},
wantCpRaws: [][]byte{
testRawCheckpoints[0],
testRawCheckpoints[2],
testRawCheckpoints[3],
testRawCheckpoints[5],
testRawCheckpoints[6],
testRawCheckpoints[10],
},
}, {
desc: "Identical CP",
cpRaws: [][]byte{
Expand All @@ -111,6 +150,12 @@ func TestCheckLogStateTracker(t *testing.T) {
testRawCheckpoints[0],
testRawCheckpoints[0],
},
wantCpRaws: [][]byte{
testRawCheckpoints[0],
testRawCheckpoints[0],
testRawCheckpoints[0],
testRawCheckpoints[0],
},
}, {
desc: "Identical CP pairs",
cpRaws: [][]byte{
Expand All @@ -119,6 +164,12 @@ func TestCheckLogStateTracker(t *testing.T) {
testRawCheckpoints[5],
testRawCheckpoints[5],
},
wantCpRaws: [][]byte{
testRawCheckpoints[0],
testRawCheckpoints[0],
testRawCheckpoints[5],
testRawCheckpoints[5],
},
}, {
desc: "Out of order",
cpRaws: [][]byte{
Expand All @@ -127,13 +178,32 @@ func TestCheckLogStateTracker(t *testing.T) {
testRawCheckpoints[0],
testRawCheckpoints[3],
},
wantCpRaws: [][]byte{
testRawCheckpoints[5],
testRawCheckpoints[5],
testRawCheckpoints[5],
testRawCheckpoints[5],
},
},
} {
t.Run(test.desc, func(t *testing.T){
for i, cpRaw := range test.cpRaws {
if err := lst.Update(ctx, cpRaw); err != nil {
t.Run(test.desc, func(t *testing.T) {
shim := fetchCheckpointShim{Checkpoints: test.cpRaws}
f := shim.Fetcher(testLogFetcher)
lst, err := NewLogStateTracker(ctx, f, h, testRawCheckpoints[0], testLogVerifier, testOrigin, UnilateralConsensus(f))
if err != nil {
t.Fatalf("NewLogStateTracker: %v", err)
}

for i := range test.cpRaws {
_, _, newCP, err := lst.Update(ctx)
if err != nil {
t.Errorf("Update %d: %v", i, err)
}
if got, want := newCP, test.wantCpRaws[i]; !bytes.Equal(got, want) {
t.Errorf("Update moved to:\n%s\nwant:\n%s", string(got), string(want))
}

shim.Advance()
}
})
}
Expand Down

0 comments on commit 8ad4058

Please sign in to comment.