// 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.

package client_test

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"
	"github.com/google/certificate-transparency-go/client"
	"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/x509"
	"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 (
	ValidSTHResponseTreeSize          = 3721782
	ValidSTHResponseTimestamp         = 1396609800587
	ValidSTHResponseSHA256RootHash    = "SxKOxksguvHPyUaKYKXoZHzXl91Q257+JQ0AUMlFfeo="
	ValidSTHResponseTreeHeadSignature = "BAMARjBEAiBUYO2tODlUUw4oWGiVPUHqZadRRyXs9T2rSXchA79VsQIgLASkQv3cu4XdPFCZbgFkIUefniNPCpO3LzzHX53l+wg="

	PrecertEntryB64          = "AAAAAAFLSYHwyAABN2DieQ8zpJj5tsFJ/s/KOZOVS1NvvzatRdCoQVt5M30ABHowggR2oAMCAQICEAUyKYw5aj4l/KoZd+gntfMwDQYJKoZIhvcNAQELBQAwbTELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUdlb1RydXN0IEluYy4xHzAdBgNVBAsTFkZPUiBURVNUIFBVUlBPU0VTIE9OTFkxJTAjBgNVBAMTHEdlb1RydXN0IEVWIFN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VgzQ1JMLmNybDAdBgNVHQ4EFgQU+3hPEvlgFYMsnxd/NBmzLjbqQYkwDQYJKoZIhvcNAQELBQADggEBAA0YAeLXOklx4hhCikUUl+BdnFfn1g0W5AiQLVNIOL6PnqXu0wjnhNyhqdwnfhYMnoy4idRh4lB6pz8Gf9pnlLd/DnWSV3gS+/I/mAl1dCkKby6H2V790e6IHmIK2KYm3jm+U++FIdGpBdsQTSdmiX/rAyuxMDM0adMkNBwTfQmZQCz6nGHw1QcSPZMvZpsC8SkvekzxsjF1otOrMUPNPQvtTWrVx8GlR2qfx/4xbQa1v2frNvFBCmO59goz+jnWvfTtj2NjwDZ7vlMBsPm16dbKYC840uvRoZjxqsdc3ChCZjqimFqlNG/xoPA8+dTicZzCXE9ijPIcvW6y1aa3bGw="
	AddJSONResp       = `{
	   "sct_version":0,
	   "id":"KHYaGJAn++880NYaAY12sFBXKcenQRvMvfYE9F1CYVM=",
	   "timestamp":1337,
	   "extensions":"",
	   "signature":"BAMARjBEAiAIc21J5ZbdKZHw5wLxCP+MhBEsV5+nfvGyakOIv6FOvAIgWYMZb6Pw///uiNM7QTg2Of1OqmK1GbeGuEl9VJN8v8c="
	}`
	ProofByHashResp = `
	{
		"leaf_index": 3,
		"audit_path": [
		"pMumx96PIUB3TX543ljlpQ/RgZRqitRfykupIZrXq0Q=",
		"5s2NQWkjmesu+Kqgp70TCwVLwq8obpHw/JyMGwN56pQ=",
		"7VelXijfmGFSl62BWIsG8LRmxJGBq9XP8FxmszuT2Cg="
		]
	}`
	GetRootsResp = `
	{
		"certificates":[
		"MIIFLjCCAxagAwIBAgIQNgEiBHAkH6lLUWKp42Ob1DANBgkqhkiG9w0BAQ0FADAWMRQwEgYDVQQDEwtlc2lnbml0Lm9yZzAeFw0xNDA2MjAxODM3NTRaFw0zMDA2MjAxODQ3NDZaMBYxFDASBgNVBAMTC2VzaWduaXQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtylZx/zTLxRDsok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ouMXtWLkwKaZwimmgJlyeL264ayNshOFGOpg2gkSliheLQYIy9C3gCFt+BzhS/EdWKCeb7WCLrKOBszCBsDAPBgNVHQ8BAf8EBQMDB/mAMIGLBgNVHQ4EgYMEgYBPRBC+90lR8pRLbTi3ID4j0WRzjoJOT3MGkKko87o8z6gEifk9zCwOiHeIgclTA0ZUTxXMRI5r+nUY0frjRCWZu4uthPlE90iJM+RyjcNTwDJGu2StvLnJ8y4t5fdnwdGssncXiBQMuM7/1eMEwAOfHgTFzJ0UBC2Umztl0hul3zAPBgNVHSMECDAGgAQBAgMEMAoGCCqGSM49BAMCA0gAMEUCIQCrwywGKvyt/BwR+e7yDs78qt4sSEVJltv7Y0W6gOI5awIgQ+IAjejYivLEfqNufFRezCBWHWhbq/HHGdNQtv6EArkAAA==",
		"extra_data": "RXh0cmEK",
		"audit_path": [
		"pMumx96PIUB3TX543ljlpQ/RgZRqitRfykupIZrXq0Q=",
		"5s2NQWkjmesu+Kqgp70TCwVLwq8obpHw/JyMGwN56pQ=",
		"7VelXijfmGFSl62BWIsG8LRmxJGBq9XP8FxmszuT2Cg="
		]
  }`
)

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) {
		if _, err := fmt.Fprint(w, rsp); err != nil {
			t.Fatal(err)
		}
	})
}

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)
		}
		if _, err := w.Write(data); err != nil {
			t.Fatal(err)
		}
	})
}

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])
		}
		_, err := fmt.Fprintf(w, `{"entries":[{"leaf_input": "%s","extra_data": "%s"},{"leaf_input": "%s","extra_data": "%s"}]}`,
			PrecertEntryB64,
			PrecertEntryExtraDataB64,
			CertEntryB64,
			CertEntryExtraDataB64)
		if err != nil {
			t.Fatal(err)
		}
	})
	defer ts.Close()
	lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}
	leaves, err := lc.GetEntries(context.Background(), 0, 1)
	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 {
		desc       string
		start, end int64
		rsp, want  string
	}{
		{desc: "eof", start: 1, end: 2, rsp: "", want: "EOF"},
		{desc: "negative end", start: 0, end: -1, want: "end should be >= 0"},
		{desc: "invalid range", start: 3, end: 2, want: "start should be <= end"},
		{desc: "non json input", start: 4, end: 5, rsp: "not-json", want: "invalid"},
		{desc: "leaf_input not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"Z29vZA=="}]}`, want: "illegal base64"},
		{desc: "extra_data not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"Z29vZA","extra_data":"bogus"}]}`, want: "illegal base64"},
		{desc: "both not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"bogus"}]}`, want: "illegal base64"},
		{desc: "bad json", start: 6, end: 7, rsp: `{"entries":[{"leaf_input":"bbbb","extra_data":"bbbb"}]}`, want: "failed to unmarshal"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-entries", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetEntries(ctx, test.start, test.end)
			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 {
		desc       string
		start, end int64
		rsp, want  string
	}{
		{desc: "empty", start: 1, end: 2, rsp: "", want: "EOF"},
		{desc: "negative end", start: 0, end: -1, want: "end should be >= 0"},
		{desc: "bad range", start: 3, end: 2, want: "start should be <= end"},
		{desc: "invalid json", start: 4, end: 5, rsp: "not-json", want: "invalid"},
		{desc: "leaf_input not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"Z29vZA=="}]}`, want: "illegal base64"},
		{desc: "extra_data not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"Z29vZA==","extra_data":"bogus"}]}`, want: "illegal base64"},
		{desc: "both not base64", start: 5, end: 6, rsp: `{"entries":[{"leaf_input":"bogus","extra_data":"bogus"}]}`, want: "illegal base64"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-entries", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetRawEntries(ctx, test.start, test.end)
			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
				if rspErr, ok := err.(client.RspError); !ok {
					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()
	lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}
	sth, err := lc.GetSTH(context.Background())
	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.Equal(sth.TreeHeadSignature.Signature, wantDS.Signature) {
		t.Errorf("GetSTH().TreeHeadSignature.Signature=%v; want %v", wantDS.Signature, sth.TreeHeadSignature.Signature)
	}
}

func TestGetSTHErrors(t *testing.T) {
	ctx := context.Background()
	var tests = []struct {
		desc      string
		rsp, want string
	}{
		{desc: "empty", rsp: "", want: "EOF"},
		{desc: "bad json", rsp: "not-json", want: "invalid"},
		{desc: "root not base64", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"bogus","tree_head_signature":"Z29vZA=="}`, want: "illegal base64"},
		{desc: "sig not base64", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"Z29vZA==","tree_head_signature":"bogus"}`, want: "illegal base64"},
		{desc: "both not base64", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"bogus","tree_head_signature":"bogus"}`, want: "illegal base64"},
		{desc: "invalid hash len", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"bbbb","tree_head_signature":"bbbb"}`, want: "hash is invalid length"},
		{desc: "bad tls data", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"tncuLXiPAo711IOxjaYTwLmwbSyyE8hEcRhaOXvFb3g=","tree_head_signature":"Z29vZA=="}`, want: "tls: syntax error"},
		{desc: "trailing junk", rsp: `{"tree_size":228163,"timestamp":1507127718502,"sha256_root_hash":"tncuLXiPAo711IOxjaYTwLmwbSyyE8hEcRhaOXvFb3g=","tree_head_signature":"BAMARjBEAiAi5045/h8Yvs1mNlsYskWvuFbu2A6hO2J45KDFfOR1OwIgZ2jq8iFCwKuTbcIgsBB1ibHEupv97CeAQynK0Dw2PT8bbbb="}`, want: "trailing data"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-sth", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetSTH(ctx)
			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
				if rspErr, ok := err.(client.RspError); !ok {
					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 {
		desc                  string
		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
	}{
		{
			desc:                  "success first time",
			deadlineLength:        -1,
			expected:              1 * time.Millisecond,
			retryAfter:            0,
			failuresBeforeSuccess: 0,
			success:               true,
		},
		{
			desc:                  "multiple attempts 503",
			deadlineLength:        -1,
			expected:              7 * time.Second, // 1 + 2 + 4
			retryAfter:            -1,
			failuresBeforeSuccess: 3,
			success:               true,
		},
		{
			desc:                  "one retry",
			deadlineLength:        6 * time.Second,
			expected:              5 * time.Second,
			retryAfter:            5 * time.Second,
			failuresBeforeSuccess: 1,
			success:               true,
		},
		{
			desc:                  "fail after one retry",
			deadlineLength:        5 * time.Second,
			expected:              5 * time.Second,
			retryAfter:            10 * time.Second,
			failuresBeforeSuccess: 1,
			success:               false,
		},
		{
			desc:                  "multiple retries",
			deadlineLength:        10 * time.Second,
			expected:              5 * time.Second,
			retryAfter:            1 * time.Second,
			failuresBeforeSuccess: 5,
			success:               true,
		},
		{
			desc:                  "immediate 10 retries",
			deadlineLength:        1 * time.Second,
			expected:              10 * time.Millisecond,
			retryAfter:            0,
			failuresBeforeSuccess: 10,
			success:               true,
		},
	}

	for i, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			deadline := context.Background()
			lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
			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()
			sct, err := lc.AddChain(deadline, chain)
			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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{PublicKey: testdata.LogPublicKeyPEM})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}

	cert, err := x509util.CertificateFromPEM([]byte(testdata.TestCertPEM))
	if x509.IsFatal(err) {
		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}}
	_, err = lc.AddChain(context.Background(), chain)
	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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{PublicKey: testdata.LogPublicKeyPEM})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}

	cert, err := x509util.CertificateFromPEM([]byte(testdata.TestPreCertPEM))
	if x509.IsFatal(err) {
		t.Fatalf("Failed to parse pre-certificate from PEM: %v", err)
	}
	issuer, err := x509util.CertificateFromPEM([]byte(testdata.CACertPEM))
	if x509.IsFatal(err) {
		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}}
	_, err = lc.AddPreChain(context.Background(), chain)
	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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
	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 {
		sct, err := lc.AddJSON(context.Background(), test.data)
		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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
	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 {
		proof, err := lc.GetSTHConsistency(context.Background(), test.first, test.second)
		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 {
		desc          string
		first, second uint64
		rsp, want     string
	}{
		{desc: "empty", first: 1, second: 2, rsp: "", want: "EOF"},
		{desc: "invalid json", first: 1, second: 2, rsp: "not-json", want: "invalid"},
		{desc: "not base64", first: 1, second: 2, rsp: `{"consistency":["bogus"]}`, want: "illegal base64"},
		{desc: "bad proof", first: 1, second: 2, rsp: `{"consistency":["2SyPbmCNzn9l7dhWVz1uz6nW7DB7p0EkSsfH9M+qU5E=",]}`, want: "invalid"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-sth-consistency", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetSTHConsistency(ctx, test.first, test.second)
			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
				if rspErr, ok := err.(client.RspError); !ok {
					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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}

	tests := []struct {
		desc     string
		hash     []byte
		treeSize uint64
	}{
		{desc: "ok", hash: dh("4a9e8edbe5ce2d2da69d483edb45186675d4be37b649d40923b156a7d1277463"), treeSize: 5},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			resp, err := lc.GetProofByHash(context.Background(), test.hash, test.treeSize)
			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 {
		desc      string
		rsp, want string
	}{
		{desc: "empty", rsp: "", want: "EOF"},
		{desc: "bad json", rsp: "not-json", want: "invalid"},
		{desc: "path not base64", rsp: `{"leaf_index": 17, "audit_path":["bogus"]}`, want: "illegal base64"},
		{desc: "malformed json", rsp: `{"leaf_index": 17, "audit_path":["Z29vZA==",]}`, want: "invalid"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-proof-by-hash", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetProofByHash(ctx, aHash, 100)
			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
				if rspErr, ok := err.(client.RspError); !ok {
					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()
	lc, err := client.New(hs.URL, &http.Client{}, jsonclient.Options{})
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}

	certs, err := lc.GetAcceptedRoots(context.Background())
	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 {
		desc      string
		rsp, want string
	}{
		{desc: "empty", rsp: "", want: "EOF"},
		{desc: "not json", rsp: "not-json", want: "invalid"},
		{desc: "not base64", rsp: `{"certificates":["bogus"]}`, want: "illegal base64"},
		{desc: "malformed json", rsp: `{"certificates":["bbbb",]}`, want: "invalid"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			ts := serveRspAt(t, "/ct/v1/get-roots", test.rsp)
			defer ts.Close()
			lc, err := client.New(ts.URL, &http.Client{}, jsonclient.Options{})
			if err != nil {
				t.Fatalf("Failed to create client: %v", err)
			}
			got, err := lc.GetAcceptedRoots(ctx)
			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
				if rspErr, ok := err.(client.RspError); !ok {
					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)
				}
			}
		})
	}
}

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 {
		desc      string
		rsp, want string
	}{
		{desc: "empty", rsp: "", want: "EOF"},
		{desc: "bad json", rsp: "not-json", want: "invalid"},
		{desc: "leaf_input not base64", rsp: `{"leaf_input": "bogus", "extra_data": "Z29vZAo=", "audit_path": ["Z29vZAo="]}`, want: "illegal base64"},
		{desc: "extra_data not base64", rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "bogus", "audit_path": ["Z29vZAo="]}`, want: "illegal base64"},
		{desc: "audit_path no base64", rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "Z29vZAo=", "audit_path": ["bogus"]}`, want: "illegal base64"},
		{desc: "malformed json", rsp: `{"leaf_input": "Z29vZAo=", "extra_data": "Z29vZAo=", "audit_path": ["bbbb",]}`, want: "invalid"},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			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.Fatalf("Failed to create client: %v", err)
			}
			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)
				}
			}
		})
	}
}
