diff options
-rw-r--r-- | instance_test.go | 22 | ||||
-rw-r--r-- | reqres_tests.go | 61 |
2 files changed, 74 insertions, 9 deletions
diff --git a/instance_test.go b/instance_test.go index 6ce31a9..40a0c57 100644 --- a/instance_test.go +++ b/instance_test.go @@ -11,7 +11,13 @@ import ( ) var ( - testHashLen = 31 + testHashLen = 31 + testMaxRange = int64(3) + testMaxChain = int64(3) + testTreeId = int64(0) + testPrefix = "/test" + testHashType = crypto.SHA256 + testExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageAny} ) func makeTestLogParameters(t *testing.T, signer crypto.Signer) *LogParameters { @@ -23,15 +29,15 @@ func makeTestLogParameters(t *testing.T, signer crypto.Signer) *LogParameters { t.Fatalf("must have %d trust anchor(s), got %d", want, got) } return &LogParameters{ - LogId: make([]byte, 32), - TreeId: 0, - Prefix: "/test", - MaxRange: 3, - MaxChain: 3, + LogId: testLogId, + TreeId: testTreeId, + Prefix: testPrefix, + MaxRange: testMaxRange, + MaxChain: testMaxChain, AnchorPool: x509util.NewCertPool(anchorList), AnchorList: anchorList, - KeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageAny}, + KeyUsage: testExtKeyUsage, Signer: signer, - HashType: crypto.SHA256, + HashType: testHashType, } } diff --git a/reqres_tests.go b/reqres_tests.go index 7f74bcd..a41c98f 100644 --- a/reqres_tests.go +++ b/reqres_tests.go @@ -1,15 +1,74 @@ package stfe import ( + "fmt" "testing" + + "net/http" ) // TODO: TestNewAddEntryRequest func TestNewAddEntryRequest(t *testing.T) { } -// TODO: TestNewGetEntriesRequest func TestNewGetEntriesRequest(t *testing.T) { + lp := makeTestLogParameters(t, nil) + for _, table := range []struct { + description string + start string + end string + wantErr bool + }{ + { + description: "bad request: start must be an integer", + start: "start", + end: "10", + wantErr: true, + }, + { + description: "bad request: end must be an integer", + start: "10", + end: "end", + wantErr: true, + }, + { + description: "bad request: start must not be negative", + start: "-1", + end: "10", + wantErr: true, + }, + { + description: "bad request: start must be larger than end", + start: "1", + end: "0", + wantErr: true, + }, + { + description: "ok request but bad response: expected truncated", + start: "0", + end: fmt.Sprintf("%d", testMaxRange), + }, + { + description: "ok request and response", + start: "0", + end: fmt.Sprintf("%d", testMaxRange-1), + }, + } { + req, err := http.NewRequest("GET", "http://example.com/"+lp.Prefix+"/get-entries", nil) + q := req.URL.Query() + q.Add("start", table.start) + q.Add("end", table.end) + req.URL.RawQuery = q.Encode() + + rsp, err := lp.newGetEntriesRequest(req) + if got, want := err != nil, table.wantErr; got != want { + t.Errorf("got error is %v but wanted %v in test %q: %v", got, want, table.description, err) + } + + if n := rsp.End - rsp.Start + 1; n > int64(testMaxRange) { + t.Errorf("get-entries range is too large in test %q: %d > %d", table.description, n, testMaxRange) + } + } } // TODO: TestNewGetProofByHashRequest |