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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
package acme
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"errors"
"io"
"reflect"
"strings"
"testing"
)
func TestClient_NewAccount(t *testing.T) {
errorTests := []struct {
Name string
OnlyReturnExisting bool
TermsOfServiceAgreed bool
Contact []string
}{
{
Name: "fetching non-existing account",
OnlyReturnExisting: true,
TermsOfServiceAgreed: true,
},
{
Name: "not agreeing to terms of service",
OnlyReturnExisting: false,
TermsOfServiceAgreed: false,
},
{
Name: "bad contacts",
OnlyReturnExisting: false,
TermsOfServiceAgreed: true,
Contact: []string{"this will fail"},
},
}
for _, currentTest := range errorTests {
key := makePrivateKey(t)
_, err := testClient.NewAccount(key, currentTest.OnlyReturnExisting, currentTest.TermsOfServiceAgreed, currentTest.Contact...)
if err == nil {
t.Fatalf("expected error %s, got none", currentTest.Name)
}
acmeErr, ok := err.(Problem)
if !ok {
t.Fatalf("unknown error %s: %v", currentTest.Name, err)
}
if acmeErr.Type == "" {
t.Fatalf("%s no acme error type present: %+v", currentTest.Name, acmeErr)
}
}
}
func TestClient_NewAccount2(t *testing.T) {
existingKey := makePrivateKey(t)
successTests := []struct {
Name string
Existing bool
Key crypto.Signer
Contact []string
}{
{
Name: "new account without contact",
},
{
Name: "new account with contact",
Contact: []string{"mailto:test@test.com"},
},
{
Name: "new account for fetching existing",
Key: existingKey,
},
{
Name: "fetching existing account",
Key: existingKey,
Existing: true,
},
}
for _, currentTest := range successTests {
var key crypto.Signer
if currentTest.Key != nil {
key = currentTest.Key
} else {
key = makePrivateKey(t)
}
if _, err := testClient.NewAccount(key, currentTest.Existing, true, currentTest.Contact...); err != nil {
t.Fatalf("unexpected error %s: %v", currentTest.Name, err)
}
}
}
func TestClient_UpdateAccount(t *testing.T) {
account := makeAccount(t)
contact := []string{"mailto:test@test.com"}
updatedAccount, err := testClient.UpdateAccount(account, contact...)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(updatedAccount.Contact, contact) {
t.Fatalf("contact mismatch, expected: %v, got: %v", contact, updatedAccount.Contact)
}
}
func TestClient_UpdateAccount2(t *testing.T) {
account := makeAccount(t)
updatedAccount, err := testClient.UpdateAccount(Account{PrivateKey: account.PrivateKey, URL: account.URL})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(account, updatedAccount) {
t.Fatalf("account and updated account mismatch, expected: %+v, got: %+v", account, updatedAccount)
}
_, err = testClient.UpdateAccount(Account{PrivateKey: account.PrivateKey})
if err == nil {
t.Fatalf("expected error, got none")
}
}
type errSigner struct{}
func (es errSigner) Public() crypto.PublicKey {
privKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
return privKey
}
func (es errSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
return nil, errors.New("cannot sign key okeydokey")
}
func TestClient_AccountKeyChange(t *testing.T) {
tests := []struct {
name string
account func() Account
newKey func() crypto.Signer
expectsError bool
errorStr string
}{
{
name: "bad key",
account: func() Account {
return Account{
PrivateKey: errSigner{},
}
},
newKey: func() crypto.Signer {
return nil
},
expectsError: true,
errorStr: "unknown key type",
},
{
name: "success",
account: func() Account { return makeAccount(t) },
newKey: func() crypto.Signer { return makePrivateKey(t) },
},
{
name: "bad signer",
account: func() Account { return makeAccount(t) },
newKey: func() crypto.Signer { return errSigner{} },
expectsError: true,
errorStr: "inner jws",
},
{
name: "bad post",
account: func() Account {
acct := makeAccount(t)
acct.URL = "invalid"
return acct
},
newKey: func() crypto.Signer { return makePrivateKey(t) },
expectsError: true,
errorStr: "malformed",
},
}
for i, ct := range tests {
account := ct.account()
newKey := ct.newKey()
accountNewKey, err := testClient.AccountKeyChange(account, newKey)
if ct.expectsError && err == nil {
t.Errorf("AccountKeyChange test %d %q expected error, got none", i, ct.name)
}
if !ct.expectsError && err != nil {
t.Errorf("AccountKeyChange 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("AccountKeyChange test %d %q error doesnt contain %q: %s", i, ct.name, ct.errorStr, err.Error())
}
if err != nil {
continue
}
if accountNewKey.PrivateKey == account.PrivateKey {
t.Fatalf("UpdateAccount test %d %q account key didnt change", i, ct.name)
}
if accountNewKey.PrivateKey != newKey {
t.Fatalf("UpdateAccount test %d %q new key isnt set", i, ct.name)
}
}
}
func TestClient_DeactivateAccount(t *testing.T) {
account := makeAccount(t)
var err error
account, err = testClient.DeactivateAccount(account)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if account.Status != "deactivated" {
t.Fatalf("expected account deactivated, got: %s", account.Status)
}
}
func TestClient_FetchOrderList(t *testing.T) {
if testClientMeta.Software == clientBoulder {
t.Skip("boulder doesnt support orders list: https://github.com/letsencrypt/boulder/issues/3335")
return
}
tests := []struct {
pre func(acct *Account) bool
post func(*testing.T, Account, OrderList)
expectsError bool
errorStr string
}{
{
pre: func(acct *Account) bool {
acct.Orders = ""
return false
},
expectsError: true,
errorStr: "no order",
},
{
pre: func(acct *Account) bool {
*acct, _, _ = makeOrderFinalised(t, nil)
return true
},
post: func(st *testing.T, account Account, list OrderList) {
if len(list.Orders) != 1 {
st.Fatalf("expected 1 orders, got: %d", len(list.Orders))
}
},
expectsError: false,
},
}
for i, ct := range tests {
acct := makeAccount(t)
if ct.pre != nil {
update := ct.pre(&acct)
if update {
var err error
acct, err = testClient.UpdateAccount(acct)
if err != nil {
panic(err)
}
}
}
list, err := testClient.FetchOrderList(acct)
if ct.expectsError && err == nil {
t.Errorf("order list test %d expected error, got none", i)
}
if !ct.expectsError && err != nil {
t.Errorf("order list test %d expected no error, got: %v", i, err)
}
if err != nil && ct.errorStr != "" && !strings.Contains(err.Error(), ct.errorStr) {
t.Errorf("order list test %d error doesnt contain %q: %s", i, ct.errorStr, err.Error())
}
if ct.post != nil {
ct.post(t, acct, list)
}
}
}
func TestClient_NewAccountOptions(t *testing.T) {
tests := []struct {
name string
options []NewAccountOptionFunc
expectsError bool
errorStr string
}{
{
name: "opt error func",
options: []NewAccountOptionFunc{
func(signer crypto.Signer, account *Account, request *NewAccountRequest, client Client) error {
return errors.New("ALWAYS ERRORS")
},
},
expectsError: true,
errorStr: "ALWAYS",
},
}
for i, ct := range tests {
key := makePrivateKey(t)
_, err := testClient.NewAccountOptions(key, ct.options...)
if ct.expectsError && err == nil {
t.Errorf("NewAccountOptions test %d %q expected error, got none", i, ct.name)
}
if !ct.expectsError && err != nil {
t.Errorf("NewAccountOptions test %d %q expected no error, got: %v", i, ct.name, err)
}
}
}
|