2019-03-11 16:56:48 +00:00
|
|
|
package acmedns
|
2018-07-09 17:28:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
"github.com/cpu/goacmedns"
|
2018-07-09 17:28:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// errorClientErr is used by the Client mocks that return an error.
|
|
|
|
errorClientErr = errors.New("errorClient always errors")
|
|
|
|
// errorStorageErr is used by the Storage mocks that return an error.
|
|
|
|
errorStorageErr = errors.New("errorStorage always errors")
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
2018-07-09 17:28:01 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
const (
|
2018-07-09 17:28:01 +00:00
|
|
|
// Fixed test data for unit tests.
|
2022-09-19 09:21:35 +00:00
|
|
|
egDomain = "example.com"
|
2018-07-09 17:28:01 +00:00
|
|
|
egFQDN = "_acme-challenge." + egDomain + "."
|
|
|
|
egKeyAuth = "⚷"
|
|
|
|
)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
var egTestAccount = goacmedns.Account{
|
|
|
|
FullDomain: "acme-dns." + egDomain,
|
|
|
|
SubDomain: "random-looking-junk." + egDomain,
|
|
|
|
Username: "spooky.mulder",
|
|
|
|
Password: "trustno1",
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:28:01 +00:00
|
|
|
// mockClient is a mock implementing the acmeDNSClient interface that always
|
|
|
|
// returns a fixed goacmedns.Account from calls to Register.
|
|
|
|
type mockClient struct {
|
|
|
|
mockAccount goacmedns.Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTXTRecord does nothing.
|
|
|
|
func (c mockClient) UpdateTXTRecord(_ goacmedns.Account, _ string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterAccount returns c.mockAccount and no errors.
|
|
|
|
func (c mockClient) RegisterAccount(_ []string) (goacmedns.Account, error) {
|
|
|
|
return c.mockAccount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockUpdateClient is a mock implementing the acmeDNSClient interface that
|
|
|
|
// tracks the calls to UpdateTXTRecord in the records map.
|
|
|
|
type mockUpdateClient struct {
|
|
|
|
mockClient
|
|
|
|
records map[goacmedns.Account]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTXTRecord saves a record value to c.records for the given acct.
|
|
|
|
func (c mockUpdateClient) UpdateTXTRecord(acct goacmedns.Account, value string) error {
|
|
|
|
c.records[acct] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-13 21:10:59 +00:00
|
|
|
// errorUpdateClient is a mock implementing the acmeDNSClient interface that always
|
2018-07-09 17:28:01 +00:00
|
|
|
// returns errors from errorUpdateClient.
|
|
|
|
type errorUpdateClient struct {
|
|
|
|
mockClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTXTRecord always returns an error.
|
|
|
|
func (c errorUpdateClient) UpdateTXTRecord(_ goacmedns.Account, _ string) error {
|
|
|
|
return errorClientErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorRegisterClient is a mock implementing the acmeDNSClient interface that always
|
|
|
|
// returns errors from RegisterAccount.
|
|
|
|
type errorRegisterClient struct {
|
|
|
|
mockClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterAccount always returns an error.
|
|
|
|
func (c errorRegisterClient) RegisterAccount(_ []string) (goacmedns.Account, error) {
|
|
|
|
return goacmedns.Account{}, errorClientErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockStorage is a mock implementing the goacmedns.Storage interface that
|
|
|
|
// returns static account data and ignores Save.
|
|
|
|
type mockStorage struct {
|
|
|
|
accounts map[string]goacmedns.Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save does nothing.
|
|
|
|
func (m mockStorage) Save() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores an account for the given domain in m.accounts.
|
|
|
|
func (m mockStorage) Put(domain string, acct goacmedns.Account) error {
|
|
|
|
m.accounts[domain] = acct
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch retrieves an account for the given domain from m.accounts or returns
|
|
|
|
// goacmedns.ErrDomainNotFound.
|
|
|
|
func (m mockStorage) Fetch(domain string) (goacmedns.Account, error) {
|
|
|
|
if acct, ok := m.accounts[domain]; ok {
|
|
|
|
return acct, nil
|
|
|
|
}
|
|
|
|
return goacmedns.Account{}, goacmedns.ErrDomainNotFound
|
|
|
|
}
|
|
|
|
|
2021-01-06 11:47:20 +00:00
|
|
|
// FetchAll returns all of m.accounts.
|
|
|
|
func (m mockStorage) FetchAll() map[string]goacmedns.Account {
|
|
|
|
return m.accounts
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:28:01 +00:00
|
|
|
// errorPutStorage is a mock implementing the goacmedns.Storage interface that
|
|
|
|
// always returns errors from Put.
|
|
|
|
type errorPutStorage struct {
|
|
|
|
mockStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put always errors.
|
|
|
|
func (e errorPutStorage) Put(_ string, _ goacmedns.Account) error {
|
|
|
|
return errorStorageErr
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// errorSaveStorage is a mock implementing the goacmedns.Storage interface that
|
2018-07-09 17:28:01 +00:00
|
|
|
// always returns errors from Save.
|
|
|
|
type errorSaveStorage struct {
|
|
|
|
mockStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save always errors.
|
|
|
|
func (e errorSaveStorage) Save() error {
|
|
|
|
return errorStorageErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorFetchStorage is a mock implementing the goacmedns.Storage interface that
|
|
|
|
// always returns errors from Fetch.
|
|
|
|
type errorFetchStorage struct {
|
|
|
|
mockStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch always errors.
|
|
|
|
func (e errorFetchStorage) Fetch(_ string) (goacmedns.Account, error) {
|
|
|
|
return goacmedns.Account{}, errorStorageErr
|
|
|
|
}
|
|
|
|
|
2021-01-06 11:47:20 +00:00
|
|
|
// FetchAll is a nop for errorFetchStorage.
|
|
|
|
func (e errorFetchStorage) FetchAll() map[string]goacmedns.Account {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:28:01 +00:00
|
|
|
// TestPresent tests that the ACME-DNS Present function for updating a DNS-01
|
|
|
|
// challenge response TXT record works as expected.
|
|
|
|
func TestPresent(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
// validAccountStorage is a mockStorage configured to return the egTestAccount.
|
2018-07-09 17:28:01 +00:00
|
|
|
validAccountStorage := mockStorage{
|
|
|
|
map[string]goacmedns.Account{
|
2018-12-06 21:50:17 +00:00
|
|
|
egDomain: egTestAccount,
|
2018-07-09 17:28:01 +00:00
|
|
|
},
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
// validUpdateClient is a mockClient configured with the egTestAccount that will
|
2018-07-09 17:28:01 +00:00
|
|
|
// track TXT updates in a map.
|
|
|
|
validUpdateClient := mockUpdateClient{
|
2018-12-06 21:50:17 +00:00
|
|
|
mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
make(map[goacmedns.Account]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Client acmeDNSClient
|
|
|
|
Storage goacmedns.Storage
|
|
|
|
ExpectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "present when client storage returns unexpected error",
|
2018-12-06 21:50:17 +00:00
|
|
|
Client: mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
Storage: errorFetchStorage{},
|
|
|
|
ExpectedError: errorStorageErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "present when client storage returns ErrDomainNotFound",
|
2018-12-06 21:50:17 +00:00
|
|
|
Client: mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
ExpectedError: ErrCNAMERequired{
|
|
|
|
Domain: egDomain,
|
|
|
|
FQDN: egFQDN,
|
2018-12-06 21:50:17 +00:00
|
|
|
Target: egTestAccount.FullDomain,
|
2018-07-09 17:28:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "present when client UpdateTXTRecord returns unexpected error",
|
|
|
|
Client: errorUpdateClient{},
|
|
|
|
Storage: validAccountStorage,
|
|
|
|
ExpectedError: errorClientErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "present when everything works",
|
|
|
|
Storage: validAccountStorage,
|
|
|
|
Client: validUpdateClient,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
|
|
dp, err := NewDNSProviderClient(test.Client, mockStorage{make(map[string]goacmedns.Account)})
|
2018-07-09 17:28:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// override the storage mock if required by the test case.
|
|
|
|
if test.Storage != nil {
|
|
|
|
dp.storage = test.Storage
|
2018-07-09 17:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// call Present. The token argument can be garbage because the ACME-DNS
|
|
|
|
// provider does not use it.
|
|
|
|
err = dp.Present(egDomain, "foo", egKeyAuth)
|
2018-12-06 21:50:17 +00:00
|
|
|
if test.ExpectedError != nil {
|
|
|
|
assert.Equal(t, test.ExpectedError, err)
|
2018-07-09 17:28:01 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// Check that the success test case set a record.
|
2018-07-09 17:28:01 +00:00
|
|
|
assert.Len(t, validUpdateClient.records, 1)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// Check that the success test case set the right record for the right account.
|
|
|
|
assert.Len(t, validUpdateClient.records[egTestAccount], 43)
|
2018-07-09 17:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestRegister tests that the ACME-DNS register function works correctly.
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Client acmeDNSClient
|
|
|
|
Storage goacmedns.Storage
|
|
|
|
Domain string
|
|
|
|
FQDN string
|
|
|
|
ExpectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "register when acme-dns client returns an error",
|
|
|
|
Client: errorRegisterClient{},
|
|
|
|
ExpectedError: errorClientErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "register when acme-dns storage put returns an error",
|
2018-12-06 21:50:17 +00:00
|
|
|
Client: mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
Storage: errorPutStorage{mockStorage{make(map[string]goacmedns.Account)}},
|
|
|
|
ExpectedError: errorStorageErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "register when acme-dns storage save returns an error",
|
2018-12-06 21:50:17 +00:00
|
|
|
Client: mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
Storage: errorSaveStorage{mockStorage{make(map[string]goacmedns.Account)}},
|
|
|
|
ExpectedError: errorStorageErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "register when everything works",
|
2018-12-06 21:50:17 +00:00
|
|
|
Client: mockClient{egTestAccount},
|
2018-07-09 17:28:01 +00:00
|
|
|
ExpectedError: ErrCNAMERequired{
|
|
|
|
Domain: egDomain,
|
|
|
|
FQDN: egFQDN,
|
2018-12-06 21:50:17 +00:00
|
|
|
Target: egTestAccount.FullDomain,
|
2018-07-09 17:28:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
|
|
dp, err := NewDNSProviderClient(test.Client, mockStorage{make(map[string]goacmedns.Account)})
|
2018-07-09 17:28:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// override the storage mock if required by the testcase.
|
2018-12-06 21:50:17 +00:00
|
|
|
if test.Storage != nil {
|
|
|
|
dp.storage = test.Storage
|
2018-07-09 17:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call register for the example domain/fqdn.
|
|
|
|
err = dp.register(egDomain, egFQDN)
|
2018-12-06 21:50:17 +00:00
|
|
|
if test.ExpectedError != nil {
|
|
|
|
assert.Equal(t, test.ExpectedError, err)
|
2018-07-09 17:28:01 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|