1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
package acme
import (
"crypto"
"net/http"
"reflect"
"strings"
"testing"
"time"
)
func TestWithHTTPTimeout(t *testing.T) {
acmeClient := Client{httpClient: http.DefaultClient}
timeout := 30 * time.Second
opt := WithHTTPTimeout(timeout)
if err := opt(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if timeout != acmeClient.httpClient.Timeout {
t.Fatalf("timeout not set, expected %v, got %v", timeout, acmeClient.httpClient.Timeout)
}
}
func TestWithInsecureSkipVerify(t *testing.T) {
acmeClient := Client{httpClient: http.DefaultClient}
opt := WithInsecureSkipVerify()
if err := opt(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
tr := acmeClient.httpClient.Transport.(*http.Transport)
if !tr.TLSClientConfig.InsecureSkipVerify {
t.Fatalf("InsecureSkipVerify not set")
}
}
func TestWithAcceptLanguage(t *testing.T) {
acmeClient := Client{httpClient: http.DefaultClient}
acceptLanguage := "de"
opt := WithAcceptLanguage(acceptLanguage)
if err := opt(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if acceptLanguage != acmeClient.acceptLanguage {
t.Fatalf("accept language not set, expected %v, got %v", acceptLanguage, acmeClient.acceptLanguage)
}
}
func TestWithRetryCount(t *testing.T) {
acmeClient := Client{httpClient: http.DefaultClient}
retryCount := 10
opt := WithRetryCount(retryCount)
if err := opt(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if retryCount != acmeClient.retryCount {
t.Fatalf("retry count not set, expected %v, got %v", retryCount, acmeClient.retryCount)
}
opt2 := WithRetryCount(-100)
if err := opt2(&acmeClient); err == nil {
t.Fatal("expected error, got none")
}
}
func TestWithUserAgentSuffix(t *testing.T) {
acmeClient := Client{httpClient: http.DefaultClient}
suffix := "hi2u"
opt := WithUserAgentSuffix(suffix)
if err := opt(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if suffix != acmeClient.userAgentSuffix {
t.Fatalf("user agent suffix not set, expected %v, got %v", suffix, acmeClient.userAgentSuffix)
}
}
func TestWithHTTPClient(t *testing.T) {
acmeClient := Client{}
opt1 := WithHTTPClient(nil)
if err := opt1(&acmeClient); err == nil {
t.Fatal("expected error, got none")
}
opt2 := WithHTTPClient(http.DefaultClient)
suffix := &http.Client{}
if err := opt2(&acmeClient); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if reflect.TypeOf(suffix).Kind() != reflect.TypeOf(acmeClient.httpClient).Kind() {
t.Fatalf("http client suffix not set, expected %v, got %v", suffix, acmeClient.httpClient)
}
}
func TestNewAcctOptOnlyReturnExisting(t *testing.T) {
r := NewAccountRequest{}
f := NewAcctOptOnlyReturnExisting()
err := f(nil, nil, &r, Client{})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if !r.OnlyReturnExisting {
t.Fatal("OnlyReturnExisting not set")
}
}
func TestNewAcctOptAgreeTOS(t *testing.T) {
r := NewAccountRequest{}
f := NewAcctOptAgreeTOS()
err := f(nil, nil, &r, Client{})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if !r.TermsOfServiceAgreed {
t.Fatal("TermsOfServiceAgreed not set")
}
}
func TestNewAcctOptWithContacts(t *testing.T) {
r := NewAccountRequest{}
f := NewAcctOptWithContacts("hello")
err := f(nil, nil, &r, Client{})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if len(r.Contact) != 1 && r.Contact[0] != "hello" {
t.Fatalf(`expected contact "hello" got: %v`, r.Contact[0])
}
}
func TestNewAcctOptExternalAccountBinding(t *testing.T) {
tests := []struct {
name string
binding ExternalAccountBinding
signer crypto.Signer
account *Account
request *NewAccountRequest
client Client
expectsError bool
errorStr string
}{
{
name: "empty binding",
expectsError: true,
errorStr: "no KeyIdentifier set",
},
{
name: "empty mac",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
},
expectsError: true,
errorStr: "no MacKey set",
},
{
name: "empty algo",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
MacKey: "rubbish",
},
expectsError: true,
errorStr: "no Algorithm set",
},
{
name: "empty hashfunc",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
MacKey: "rubbish",
Algorithm: "rubbish",
},
expectsError: true,
errorStr: "no HashFunc set",
},
{
name: "unknown key type",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
MacKey: "rubbish",
Algorithm: "rubbish",
HashFunc: crypto.SHA256,
},
signer: errSigner{},
expectsError: true,
errorStr: "unknown key type",
},
{
name: "invalid mac",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
MacKey: "!!!!",
Algorithm: "rubbish",
HashFunc: crypto.SHA256,
},
signer: makePrivateKey(t),
expectsError: true,
errorStr: "error decoding mac",
},
{
name: "ok",
binding: ExternalAccountBinding{
KeyIdentifier: "rubbish",
MacKey: "rubbish",
Algorithm: "rubbish",
HashFunc: crypto.SHA256,
},
signer: makePrivateKey(t),
account: &Account{},
request: &NewAccountRequest{},
},
}
for i, ct := range tests {
f := NewAcctOptExternalAccountBinding(ct.binding)
err := f(ct.signer, ct.account, ct.request, ct.client)
if ct.expectsError && err == nil {
t.Errorf("decodeCertificateChain test %d %q expected error, got none", i, ct.name)
}
if !ct.expectsError && err != nil {
t.Errorf("decodeCertificateChain test %d %q expected no error, got: %v", i, ct.name, err)
}
if err != nil && ct.errorStr != "" && !strings.Contains(err.Error(), ct.errorStr) {
t.Errorf("AccoudecodeCertificateChainntKeyChange test %d %q error doesnt contain %q: %s", i, ct.name, ct.errorStr, err.Error())
}
}
}
|