7dbbe7e55a
Auto-encrypt meant to fallback to the default port when it wasn't provided, but it hadn't been because of an issue with the error handling. We were checking against an incomplete error value: "missing port in address" vs "address $HOST: missing port in address" Additionally, all RPCs to AutoEncrypt.Sign were using a.config.ServerPort, so those were updated to use ports resolved by resolveAddrs, if they are available.
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
package consul
|
|
|
|
import (
|
|
"github.com/stretchr/testify/require"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestAutoEncrypt_resolveAddr(t *testing.T) {
|
|
type args struct {
|
|
rawHost string
|
|
defaultPort int
|
|
logger *log.Logger
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
ips []net.IP
|
|
port int
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "host without port",
|
|
args: args{
|
|
"127.0.0.1",
|
|
8300,
|
|
log.New(os.Stderr, "", log.LstdFlags),
|
|
},
|
|
ips: []net.IP{net.IPv4(127, 0, 0, 1)},
|
|
port: 8300,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "host with port",
|
|
args: args{
|
|
"127.0.0.1:1234",
|
|
8300,
|
|
log.New(os.Stderr, "", log.LstdFlags),
|
|
},
|
|
ips: []net.IP{net.IPv4(127, 0, 0, 1)},
|
|
port: 1234,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "host with broken port",
|
|
args: args{
|
|
"127.0.0.1:xyz",
|
|
8300,
|
|
log.New(os.Stderr, "", log.LstdFlags),
|
|
},
|
|
ips: []net.IP{net.IPv4(127, 0, 0, 1)},
|
|
port: 8300,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "not an address",
|
|
args: args{
|
|
"abc",
|
|
8300,
|
|
log.New(os.Stderr, "", log.LstdFlags),
|
|
},
|
|
ips: nil,
|
|
port: 8300,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ips, port, err := resolveAddr(tt.args.rawHost, tt.args.defaultPort, tt.args.logger)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("resolveAddr error: %v, wantErr: %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
require.Equal(t, tt.ips, ips)
|
|
require.Equal(t, tt.port, port)
|
|
})
|
|
}
|
|
}
|