2018-06-17 07:32:44 +00:00
|
|
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-03 07:25:07 +00:00
|
|
|
package client_test
|
2018-06-17 07:32:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ct "github.com/google/certificate-transparency-go"
|
2018-07-03 07:25:07 +00:00
|
|
|
"github.com/google/certificate-transparency-go/client"
|
2018-06-17 07:32:44 +00:00
|
|
|
"github.com/google/certificate-transparency-go/jsonclient"
|
|
|
|
"github.com/google/certificate-transparency-go/testdata"
|
|
|
|
"github.com/google/certificate-transparency-go/tls"
|
|
|
|
"github.com/google/certificate-transparency-go/x509util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dh(s string) []byte {
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ValidSTHResponse = `{"tree_size":3721782,"timestamp":1396609800587,
|
|
|
|
"sha256_root_hash":"SxKOxksguvHPyUaKYKXoZHzXl91Q257+JQ0AUMlFfeo=",
|
|
|
|
"tree_head_signature":"BAMARjBEAiBUYO2tODlUUw4oWGiVPUHqZadRRyXs9T2rSXchA79VsQIgLASkQv3cu4XdPFCZbgFkIUefniNPCpO3LzzHX53l+wg="}`
|
|
|
|
ValidSTHResponseTreeSize = 3721782
|
|
|
|
ValidSTHResponseTimestamp = 1396609800587
|
|
|
|
ValidSTHResponseSHA256RootHash = "SxKOxksguvHPyUaKYKXoZHzXl91Q257+JQ0AUMlFfeo="
|
|
|
|
ValidSTHResponseTreeHeadSignature = "BAMARjBEAiBUYO2tODlUUw4oWGiVPUHqZadRRyXs9T2rSXchA79VsQIgLASkQv3cu4XdPFCZbgFkIUefniNPCpO3LzzHX53l+wg="
|
|
|
|
|
|
|
|
PrecertEntryB64 = "AAAAAAFLSYHwyAABN2DieQ8zpJj5tsFJ/s/KOZOVS1NvvzatRdCoQVt5M30ABHowggR2oAMCAQICEAUyKYw5aj4l/KoZd+gntfMwDQYJKoZIhvcNAQELBQAwbTELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUdlb1RydXN0IEluYy4xHzAdBgNVBAsTFkZPUiBURVNUIFBVUlBPU0VTIE9OTFkxJTAjBgNVBAMTHEdlb1RydXN0IEVWIFNTTCBURVNUIENBIC0gRzQwHhcNMTUwMjAyMDAwMDAwWhcNMTYwMjI3MjM1OTU5WjCBwzETMBEGCysGAQQBgjc8AgEDEwJHQjEbMBkGCysGAQQBgjc8AgECFApDYWxpZm9ybmlhMR4wHAYLKwYBBAGCNzwCAQEMDU1vdW50YWluIFZpZXcxCzAJBgNVBAYTAkdCMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1Nb3VudGFpbiBWaWV3MR0wGwYDVQQKDBRTeW1hbnRlYyBDb3Jwb3JhdGlvbjEWMBQGA1UEAwwNc2RmZWRzZi50cnVzdDCCASIwDQYJKo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YaGJAn++880NYaAY12sFBXKcenQRvMvfYE9F1CYVM=",
|
|
|
|
"timestamp":1337,
|
|
|
|
"extensions":"",
|
|
|
|
"signature":"BAMARjBEAiAIc21J5ZbdKZHw5wLxCP+MhBEsV5+nfvGyakOIv6FOvAIgWYMZb6Pw///uiNM7QTg2Of1OqmK1GbeGuEl9VJN8v8c="
|
|
|
|
}`
|
|
|
|
ProofByHashResp = `
|
|
|
|
{
|
|
|
|
"leaf_index": 3,
|
|
|
|
"audit_path": [
|
|
|
|
"pMumx96PIUB3TX543ljlpQ/RgZRqitRfykupIZrXq0Q=",
|
|
|
|
"5s2NQWkjmesu+Kqgp70TCwVLwq8obpHw/JyMGwN56pQ=",
|
|
|
|
"7VelXijfmGFSl62BWIsG8LRmxJGBq9XP8FxmszuT2Cg="
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
GetRootsResp = `
|
|
|
|
{
|
|
|
|
"certificates":[
|
|
|
|
"MIIFLjCCAxagAwIBAgIQNgEiBHAkH6lLUWKp42Ob1DANBgkqhkiG9w0BAQ0FADAWMRQwEgYDVQQDEwtlc2lnbml0Lm9yZzAeFw0xNDA2MjAxODM3NTRaFw0zMDA2MjAxODQ3NDZaMBYxFDASBgNVBAMTC2VzaWduaXQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtylZx/zTLxRDsok14XO0Z3PvWMIY4HWro0YLgCF8dYv3tUaNkmN3ghlQvY8UcByH2LMOBGiQAcMHxgEJ53cnWRyc2DjoGhkDkiPdS2JttNEB0B/XTaGvaHwJh2CSgIBbpZpWTaqGywbe7AgJQ81L8h7tZ4E6W8ZM0vt4mnzqkPBT+BmyjTXG/McGhYTQAsmdsYZDBAdB2Y4X1/RAyL0e9MHdSboRofhg+8d5MeC0VEIgHXU/R4f4wz/pSw0FI9xxWJR3UUK/qOWqNsVYZfmCu6+ksDQtezxSTAuymoL094Dwn+hnXb8RS6dEbIQ+b0bIHxxpypcxH7rBMIpQcbZ8JSq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Umztl0hul3zAPBgNVHSMECDAGgAQBAgMEMAoGCCqGSM49BAMCA0gAMEUCIQCrwywGKvyt/BwR+e7yDs78qt4sSEVJltv7Y0W6gOI5awIgQ+IAjejYivLEfqNufFRezCBWHWhbq/HHGdNQtv6EArkAAA==",
|
|
|
|
"extra_data": "RXh0cmEK",
|
|
|
|
"audit_path": [
|
|
|
|
"pMumx96PIUB3TX543ljlpQ/RgZRqitRfykupIZrXq0Q=",
|
|
|
|
"5s2NQWkjmesu+Kqgp70TCwVLwq8obpHw/JyMGwN56pQ=",
|
|
|
|
"7VelXijfmGFSl62BWIsG8LRmxJGBq9XP8FxmszuT2Cg="
|
|
|
|
]
|
|
|
|
}`
|
2018-06-17 07:32:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func b64(s string) []byte {
|
|
|
|
b, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveHandlerAt returns a test HTTP server that only expects requests at the given path, and invokes
|
|
|
|
// the provided handler for that path.
|
|
|
|
func serveHandlerAt(t *testing.T, path string, handler func(http.ResponseWriter, *http.Request)) *httptest.Server {
|
|
|
|
t.Helper()
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == path {
|
|
|
|
handler(w, r)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Incorrect URL path: %s", r.URL.Path)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveRspAt returns a test HTTP server that returns a canned response body rsp for a given path.
|
|
|
|
func serveRspAt(t *testing.T, path, rsp string) *httptest.Server {
|
|
|
|
t.Helper()
|
|
|
|
return serveHandlerAt(t, path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, rsp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sctToJSON(rawSCT []byte) ([]byte, error) {
|
|
|
|
var sct ct.SignedCertificateTimestamp
|
|
|
|
_, err := tls.Unmarshal(rawSCT, &sct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to tls-unmarshal test certificate proof: %v", err)
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(sct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to json-marshal test certificate proof: %v", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveSCTAt returns a test HTTP server that returns the given SCT as a canned response for
|
|
|
|
// a given path.
|
|
|
|
func serveSCTAt(t *testing.T, path string, rawSCT []byte) *httptest.Server {
|
|
|
|
t.Helper()
|
|
|
|
return serveHandlerAt(t, path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := sctToJSON(rawSCT)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEntries(t *testing.T) {
|
|
|
|
ts := serveHandlerAt(t, "/ct/v1/get-entries", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
q := r.URL.Query()
|
|
|
|
numRE := regexp.MustCompile("[0-9]+")
|
|
|
|
if !numRE.MatchString(q["start"][0]) || !numRE.MatchString(q["end"][0]) {
|
|
|
|
t.Fatalf("Invalid parameter: start=%q, end=%q", q["start"][0], q["end"][0])
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, `{"entries":[{"leaf_input": "%s","extra_data": "%s"},{"leaf_input": "%s","extra_data": "%s"}]}`,
|
|
|
|
PrecertEntryB64,
|
|
|
|
PrecertEntryExtraDataB64,
|
|
|
|
CertEntryB64,
|
|
|
|
CertEntryExtraDataB64)
|
|
|
|
})
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
leaves, err := lc.GetEntries(context.Background(), 0, 1)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetEntries(0,1)=nil,%v; want 2 leaves,nil", err)
|
|
|
|
} else if len(leaves) != 2 {
|
|
|
|
t.Errorf("GetEntries(0,1)=%d leaves,nil; want 2 leaves,nil", len(leaves))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEntriesErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
start, end int64
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{start: 1, end: 2, rsp: "", want: "EOF"},
|
|
|
|
{start: 0, end: -1, want: "end should be >= 0"},
|
|
|
|
{start: 3, end: 2, want: "start should be <= end"},
|
|
|
|
{start: 4, end: 5, rsp: "not-json", want: "invalid"},
|
|
|
|
{start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"bogus"}]}`, want: "illegal base64"},
|
|
|
|
{start: 6, end: 7, rsp: `{"entries":[{"leaf_input":"bbbb","extra_data":"bbbb"}]}`, want: "failed to unmarshal"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-entries", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetEntries(ctx, test.start, test.end)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetEntries(%d, %d)=%+v, nil; want nil, %q", test.start, test.end, got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetEntries(%d, %d)=nil, %q; want nil, %q", test.start, test.end, err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetEntries(%d, %d)=%+v, _; want nil, _", test.start, test.end, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRawEntriesErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
start, end int64
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{start: 1, end: 2, rsp: "", want: "EOF"},
|
|
|
|
{start: 0, end: -1, want: "end should be >= 0"},
|
|
|
|
{start: 3, end: 2, want: "start should be <= end"},
|
|
|
|
{start: 4, end: 5, rsp: "not-json", want: "invalid"},
|
|
|
|
{start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"bogus"}]}`, want: "illegal base64"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-entries", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetRawEntries(ctx, test.start, test.end)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetRawEntries(%d, %d)=%+v, nil; want nil, %q", test.start, test.end, got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetRawEntries(%d, %d)=nil, %q; want nil, %q", test.start, test.end, err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetRawEntries(%d, %d)=%+v, _; want nil, _", test.start, test.end, got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
2018-07-03 07:25:07 +00:00
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
2018-06-17 07:32:44 +00:00
|
|
|
t.Errorf("GetRawEntries(%d, %d)=nil, .(%T); want nil, .(RspError)", test.start, test.end, err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetRawEntries(%d, %d)=nil, .Body=%q; want nil, .Body=%q", test.start, test.end, rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSTH(t *testing.T) {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-sth",
|
|
|
|
fmt.Sprintf(`{"tree_size": %d, "timestamp": %d, "sha256_root_hash": "%s", "tree_head_signature": "%s"}`,
|
|
|
|
ValidSTHResponseTreeSize,
|
|
|
|
int64(ValidSTHResponseTimestamp),
|
|
|
|
ValidSTHResponseSHA256RootHash,
|
|
|
|
ValidSTHResponseTreeHeadSignature))
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
sth, err := lc.GetSTH(context.Background())
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if sth.TreeSize != ValidSTHResponseTreeSize {
|
|
|
|
t.Errorf("GetSTH().TreeSize=%d; want %d", sth.TreeSize, ValidSTHResponseTreeSize)
|
|
|
|
}
|
|
|
|
if sth.Timestamp != ValidSTHResponseTimestamp {
|
|
|
|
t.Errorf("GetSTH().Timestamp=%v; want %v", sth.Timestamp, ValidSTHResponseTimestamp)
|
|
|
|
}
|
|
|
|
if sth.SHA256RootHash.Base64String() != ValidSTHResponseSHA256RootHash {
|
|
|
|
t.Errorf("GetSTH().SHA256RootHash=%v; want %v", sth.SHA256RootHash.Base64String(), ValidSTHResponseSHA256RootHash)
|
|
|
|
}
|
|
|
|
wantRawSignature, err := base64.StdEncoding.DecodeString(ValidSTHResponseTreeHeadSignature)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't b64 decode 'correct' STH signature: %v", err)
|
|
|
|
}
|
|
|
|
var wantDS ct.DigitallySigned
|
|
|
|
if _, err := tls.Unmarshal(wantRawSignature, &wantDS); err != nil {
|
|
|
|
t.Fatalf("Couldn't unmarshal DigitallySigned: %v", err)
|
|
|
|
}
|
|
|
|
if sth.TreeHeadSignature.Algorithm.Hash != wantDS.Algorithm.Hash {
|
|
|
|
t.Errorf("GetSTH().TreeHeadSignature.Algorithm.Hash=%v; %v", wantDS.Algorithm.Hash, sth.TreeHeadSignature.Algorithm.Hash)
|
|
|
|
}
|
|
|
|
if sth.TreeHeadSignature.Algorithm.Signature != wantDS.Algorithm.Signature {
|
|
|
|
t.Errorf("GetSTH().TreeHeadSignature.Algorithm.Signature=%v; want %v", wantDS.Algorithm.Signature, sth.TreeHeadSignature.Algorithm.Signature)
|
|
|
|
}
|
|
|
|
if bytes.Compare(sth.TreeHeadSignature.Signature, wantDS.Signature) != 0 {
|
|
|
|
t.Errorf("GetSTH().TreeHeadSignature.Signature=%v; want %v", wantDS.Signature, sth.TreeHeadSignature.Signature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSTHErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{rsp: "", want: "EOF"},
|
|
|
|
{rsp: "not-json", want: "invalid"},
|
|
|
|
{rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"bogus","tree_head_signature":"bogus"}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"bbbb","tree_head_signature":"bbbb"}`, want: "hash is invalid length"},
|
|
|
|
{rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"tncuLXiPAo711IOxjaYTwLmwbSyyE8hEcRhaOXvFb3g=","tree_head_signature":"bbbb"}`, want: "syntax error"},
|
|
|
|
{rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"tncuLXiPAo711IOxjaYTwLmwbSyyE8hEcRhaOXvFb3g=","tree_head_signature":"BAMARjBEAiAi5045/h8Yvs1mNlsYskWvuFbu2A6hO2J45KDFfOR1OwIgZ2jq8iFCwKuTbcIgsBB1ibHEupv97CeAQynK0Dw2PT8bbbb="}`, want: "trailing data"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-sth", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetSTH(ctx)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetSTH()=%+v, nil; want nil, %q", got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetSTH()=nil, %q; want nil, %q", err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetSTH()=%+v, _; want nil, _", got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
2018-07-03 07:25:07 +00:00
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
2018-06-17 07:32:44 +00:00
|
|
|
t.Errorf("GetSTH()=nil, .(%T); want nil, .(RspError)", err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetSTH()=nil, .Body=%q; want nil, .Body=%q", rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddChainRetries(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping retry test in short mode")
|
|
|
|
}
|
|
|
|
retryAfter := 0 * time.Second
|
|
|
|
currentFailures := 0
|
|
|
|
failuresBeforeSuccess := 0
|
|
|
|
hs := serveHandlerAt(t, "/ct/v1/add-chain", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if failuresBeforeSuccess > 0 && currentFailures < failuresBeforeSuccess {
|
|
|
|
currentFailures++
|
|
|
|
if retryAfter != 0 {
|
|
|
|
if retryAfter > 0 {
|
|
|
|
w.Header().Add("Retry-After", strconv.Itoa(int(retryAfter.Seconds())))
|
|
|
|
}
|
|
|
|
w.WriteHeader(503)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(408)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err := w.Write([]byte(AddJSONResp))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer hs.Close()
|
|
|
|
|
|
|
|
certBytes, err := base64.StdEncoding.DecodeString(SubmissionCertB64)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to decode chain array B64: %s", err)
|
|
|
|
}
|
|
|
|
chain := []ct.ASN1Cert{{Data: certBytes}}
|
|
|
|
|
|
|
|
const leeway = time.Millisecond * 100
|
|
|
|
const leewayRatio = 0.2 // 20%
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
deadlineLength time.Duration // -1 indicates no deadline
|
|
|
|
expected time.Duration
|
|
|
|
retryAfter time.Duration // -1 indicates: generate 503 with no Retry-After
|
|
|
|
failuresBeforeSuccess int
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
deadlineLength: -1,
|
|
|
|
expected: 1 * time.Millisecond,
|
|
|
|
retryAfter: 0,
|
|
|
|
failuresBeforeSuccess: 0,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deadlineLength: -1,
|
|
|
|
expected: 7 * time.Second, // 1 + 2 + 4
|
|
|
|
retryAfter: -1,
|
|
|
|
failuresBeforeSuccess: 3,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deadlineLength: 6 * time.Second,
|
|
|
|
expected: 5 * time.Second,
|
|
|
|
retryAfter: 5 * time.Second,
|
|
|
|
failuresBeforeSuccess: 1,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deadlineLength: 5 * time.Second,
|
|
|
|
expected: 5 * time.Second,
|
|
|
|
retryAfter: 10 * time.Second,
|
|
|
|
failuresBeforeSuccess: 1,
|
|
|
|
success: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deadlineLength: 10 * time.Second,
|
|
|
|
expected: 5 * time.Second,
|
|
|
|
retryAfter: 1 * time.Second,
|
|
|
|
failuresBeforeSuccess: 5,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deadlineLength: 1 * time.Second,
|
|
|
|
expected: 10 * time.Millisecond,
|
|
|
|
retryAfter: 0,
|
|
|
|
failuresBeforeSuccess: 10,
|
|
|
|
success: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
deadline := context.Background()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
if test.deadlineLength >= 0 {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
deadline, cancel = context.WithDeadline(context.Background(), time.Now().Add(test.deadlineLength))
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
retryAfter = test.retryAfter
|
|
|
|
failuresBeforeSuccess = test.failuresBeforeSuccess
|
|
|
|
currentFailures = 0
|
|
|
|
|
|
|
|
started := time.Now()
|
2018-07-03 07:25:07 +00:00
|
|
|
sct, err := lc.AddChain(deadline, chain)
|
2018-06-17 07:32:44 +00:00
|
|
|
took := time.Since(started)
|
|
|
|
delta := math.Abs(float64(took - test.expected))
|
|
|
|
ratio := delta / float64(test.expected)
|
|
|
|
if delta > float64(leeway) && ratio > leewayRatio {
|
|
|
|
t.Errorf("#%d Submission took an unexpected length of time: %s, expected ~%s", i, took, test.expected)
|
|
|
|
}
|
|
|
|
if test.success && err != nil {
|
|
|
|
t.Errorf("#%d Failed to submit chain: %s", i, err)
|
|
|
|
} else if !test.success && err == nil {
|
|
|
|
t.Errorf("#%d Expected AddChain to fail", i)
|
|
|
|
}
|
|
|
|
if test.success && sct == nil {
|
|
|
|
t.Errorf("#%d Nil SCT returned", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddChain(t *testing.T) {
|
|
|
|
hs := serveSCTAt(t, "/ct/v1/add-chain", testdata.TestCertProof)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{PublicKey: testdata.LogPublicKeyPEM})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-03 07:25:07 +00:00
|
|
|
cert, err := x509util.CertificateFromPEM([]byte(testdata.TestCertPEM))
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse certificate from PEM: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddChain will verify the signature because the client has a public key.
|
|
|
|
chain := []ct.ASN1Cert{{Data: cert.Raw}}
|
2018-07-03 07:25:07 +00:00
|
|
|
_, err = lc.AddChain(context.Background(), chain)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("AddChain()=nil,%v; want sct,nil", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPreChain(t *testing.T) {
|
|
|
|
hs := serveSCTAt(t, "/ct/v1/add-pre-chain", testdata.TestPreCertProof)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{PublicKey: testdata.LogPublicKeyPEM})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-03 07:25:07 +00:00
|
|
|
cert, err := x509util.CertificateFromPEM([]byte(testdata.TestPreCertPEM))
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse pre-certificate from PEM: %v", err)
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
issuer, err := x509util.CertificateFromPEM([]byte(testdata.CACertPEM))
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse issuer certificate from PEM: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPreChain will verify the signature because the client has a public key.
|
|
|
|
chain := []ct.ASN1Cert{{Data: cert.Raw}, {Data: issuer.Raw}}
|
2018-07-03 07:25:07 +00:00
|
|
|
_, err = lc.AddPreChain(context.Background(), chain)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("AddPreChain()=nil,%v; want sct,nil", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddJSON(t *testing.T) {
|
|
|
|
hs := serveRspAt(t, "/ct/v1/add-json", AddJSONResp)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
success bool
|
|
|
|
data interface{}
|
|
|
|
}{
|
|
|
|
{true, struct{ hi string }{"bob"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-07-03 07:25:07 +00:00
|
|
|
sct, err := lc.AddJSON(context.Background(), test.data)
|
2018-06-17 07:32:44 +00:00
|
|
|
if test.success && err != nil {
|
|
|
|
t.Errorf("AddJSON(%v)=nil,%v; want sct,nil", test.data, err)
|
|
|
|
} else if !test.success && err == nil {
|
|
|
|
t.Errorf("AddJSON(%v)=sct,nil; want nil,error", test.data)
|
|
|
|
}
|
|
|
|
if test.success && sct == nil {
|
|
|
|
t.Errorf("AddJSON(%v)=nil,%v; want sct,nil", test.data, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSTHConsistency(t *testing.T) {
|
|
|
|
hs := serveRspAt(t, "/ct/v1/get-sth-consistency", GetSTHConsistencyResp)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
first uint64
|
|
|
|
second uint64
|
|
|
|
proof [][]byte
|
|
|
|
}{
|
|
|
|
{1, 3, [][]byte{
|
|
|
|
b64("IqlrapPQKtmCY1jCr8+lpCtscRyjjZAA7nyadtFPRFQ="),
|
|
|
|
b64("ytf6K2GnSRZ3Au+YkivCb7N1DygfKyZmE4aEs9OXl/8="),
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-07-03 07:25:07 +00:00
|
|
|
proof, err := lc.GetSTHConsistency(context.Background(), test.first, test.second)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=nil,%v; want proof,nil", test.first, test.second, err)
|
|
|
|
} else if !reflect.DeepEqual(proof, test.proof) {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=%v,nil; want %v,nil", test.first, test.second, proof, test.proof)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSTHConsistencyErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
first, second uint64
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{first: 1, second: 2, rsp: "", want: "EOF"},
|
|
|
|
{first: 1, second: 2, rsp: "not-json", want: "invalid"},
|
|
|
|
{first: 1, second: 2, rsp: `{"consistency":["bogus"]}`, want: "illegal base64"},
|
|
|
|
{first: 1, second: 2, rsp: `{"consistency":["2SyPbmCNzn9l7dhWVz1uz6nW7DB7p0EkSsfH9M+qU5E=",]}`, want: "invalid"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-sth-consistency", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetSTHConsistency(ctx, test.first, test.second)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=%+v, nil; want nil, %q", test.first, test.second, got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=nil, %q; want nil, %q", test.first, test.second, err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=%+v, _; want nil, _", test.first, test.second, got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
2018-07-03 07:25:07 +00:00
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
2018-06-17 07:32:44 +00:00
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=nil, .(%T); want nil, .(RspError)", test.first, test.second, err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetSTHConsistency(%d, %d)=nil, .Body=%q; want nil, .Body=%q", test.first, test.second, rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProofByHash(t *testing.T) {
|
|
|
|
hs := serveRspAt(t, "/ct/v1/get-proof-by-hash", ProofByHashResp)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
hash []byte
|
|
|
|
treesize uint64
|
|
|
|
}{
|
|
|
|
{dh("4a9e8edbe5ce2d2da69d483edb45186675d4be37b649d40923b156a7d1277463"), 5},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-07-03 07:25:07 +00:00
|
|
|
resp, err := lc.GetProofByHash(context.Background(), test.hash, test.treesize)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetProofByHash(%v, %v)=nil,%v; want proof,nil", test.hash, test.treesize, err)
|
|
|
|
} else if got := len(resp.AuditPath); got < 1 {
|
|
|
|
t.Errorf("len(GetProofByHash(%v, %v)): %v; want > 1", test.hash, test.treesize, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProofByHashErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
aHash := dh("4a9e8edbe5ce2d2da69d483edb45186675d4be37b649d40923b156a7d1277463")
|
|
|
|
var tests = []struct {
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{rsp: "", want: "EOF"},
|
|
|
|
{rsp: "not-json", want: "invalid"},
|
|
|
|
{rsp: `{"leaf_index": 17, "audit_path":["bogus"]}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"leaf_index": 17, "audit_path":["bbbb",]}`, want: "invalid"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-proof-by-hash", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetProofByHash(ctx, aHash, 100)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetProofByHash()=%+v, nil; want nil, %q", got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetProofByHash()=nil, %q; want nil, %q", err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetProofByHash()=%+v, _; want nil, _", got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
2018-07-03 07:25:07 +00:00
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
2018-06-17 07:32:44 +00:00
|
|
|
t.Errorf("GetProofByHash()=nil, .(%T); want nil, .(RspError)", err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetProofByHash()=nil, .Body=%q; want nil, .Body=%q", rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAcceptedRoots(t *testing.T) {
|
|
|
|
hs := serveRspAt(t, "/ct/v1/get-roots", GetRootsResp)
|
|
|
|
defer hs.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-03 07:25:07 +00:00
|
|
|
certs, err := lc.GetAcceptedRoots(context.Background())
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetAcceptedRoots()=nil,%q; want roots,nil", err.Error())
|
|
|
|
} else if len(certs) < 1 {
|
|
|
|
t.Errorf("len(GetAcceptedRoots())=0; want > 1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAcceptedRootsErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{rsp: "", want: "EOF"},
|
|
|
|
{rsp: "not-json", want: "invalid"},
|
|
|
|
{rsp: `{"certificates":["bogus"]}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"certificates":["bbbb",]}`, want: "invalid"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-roots", test.rsp)
|
|
|
|
defer ts.Close()
|
2018-07-03 07:25:07 +00:00
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
2018-06-17 07:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
got, err := lc.GetAcceptedRoots(ctx)
|
2018-06-17 07:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetAcceptedRoots()=%+v, nil; want nil, %q", got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetAcceptedRoots()=nil, %q; want nil, %q", err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetAcceptedRoots()=%+v, _; want nil, _", got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
2018-07-03 07:25:07 +00:00
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
2018-06-17 07:32:44 +00:00
|
|
|
t.Errorf("GetAcceptedRoots()=nil, .(%T); want nil, .(RspError)", err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetAcceptedRoots()=nil, .Body=%q; want nil, .Body=%q", rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 07:25:07 +00:00
|
|
|
|
|
|
|
func TestGetEntryAndProof(t *testing.T) {
|
|
|
|
hs := serveRspAt(t, "/ct/v1/get-entry-and-proof", GetEntryAndProofResp)
|
|
|
|
defer hs.Close()
|
|
|
|
lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
index uint64
|
|
|
|
treesize uint64
|
|
|
|
}{
|
|
|
|
{1000, 2000},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
resp, err := lc.GetEntryAndProof(context.Background(), test.index, test.treesize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetEntryAndProof(%v, %v)=nil,%v; want proof,nil", test.index, test.treesize, err)
|
|
|
|
} else if got := len(resp.AuditPath); got < 1 {
|
|
|
|
t.Errorf("len(GetEntryAndProof(%v, %v)): %v; want > 1", test.index, test.treesize, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEntryAndProofErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
var tests = []struct {
|
|
|
|
rsp, want string
|
|
|
|
}{
|
|
|
|
{rsp: "", want: "EOF"},
|
|
|
|
{rsp: "not-json", want: "invalid"},
|
|
|
|
{rsp: `{"leaf_input": "bogus", "extra_data": "Z29vZAo=", "audit_path": ["Z29vZAo="]}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "bogus", "audit_path": ["Z29vZAo="]}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "Z29vZAo=", "audit_path": ["bogus"]}`, want: "illegal base64"},
|
|
|
|
{rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "Z29vZAo=", "audit_path": ["bbbb",]}`, want: "invalid"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ts := serveRspAt(t, "/ct/v1/get-entry-and-proof", test.rsp)
|
|
|
|
defer ts.Close()
|
|
|
|
lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create client: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, err := lc.GetEntryAndProof(ctx, 99, 100)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("GetEntryAndProof()=%+v, nil; want nil, %q", got, test.want)
|
|
|
|
} else if !strings.Contains(err.Error(), test.want) {
|
|
|
|
t.Errorf("GetEntryAndProof()=nil, %q; want nil, %q", err, test.want)
|
|
|
|
}
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("GetEntryAndProof()=%+v, _; want nil, _", got)
|
|
|
|
}
|
|
|
|
if len(test.rsp) > 0 {
|
|
|
|
// Expect the error to include the HTTP response
|
|
|
|
if rspErr, ok := err.(client.RspError); !ok {
|
|
|
|
t.Errorf("GetEntryAndProof()=nil, .(%T); want nil, .(RspError)", err)
|
|
|
|
} else if string(rspErr.Body) != test.rsp {
|
|
|
|
t.Errorf("GetEntryAndProof()=nil, .Body=%q; want nil, .Body=%q", rspErr.Body, test.rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|