Minor improvements

This commit is contained in:
TwinProduction 2021-09-30 20:45:47 -04:00
parent 30cb7b6ec8
commit bc25fea1c0
2 changed files with 55 additions and 63 deletions

View File

@ -40,7 +40,7 @@ func CanPerformStartTLS(address string, config *Config) (connected bool, certifi
} }
conn, err := net.DialTimeout("tcp", address, config.Timeout) conn, err := net.DialTimeout("tcp", address, config.Timeout)
if err != nil { if err != nil {
return return
} }
smtpClient, err := smtp.NewClient(conn, hostAndPort[0]) smtpClient, err := smtp.NewClient(conn, hostAndPort[0])
if err != nil { if err != nil {
@ -63,24 +63,16 @@ func CanPerformStartTLS(address string, config *Config) (connected bool, certifi
// CanPerformTLS checks whether a connection can be established to an address using the TLS protocol // CanPerformTLS checks whether a connection can be established to an address using the TLS protocol
func CanPerformTLS(address string, config *Config) (connected bool, certificate *x509.Certificate, err error) { func CanPerformTLS(address string, config *Config) (connected bool, certificate *x509.Certificate, err error) {
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: config.Timeout}, "tcp", address, nil) connection, err := tls.DialWithDialer(&net.Dialer{Timeout: config.Timeout}, "tcp", address, nil)
if err != nil { if err != nil {
return return
} }
defer conn.Close() defer connection.Close()
verifiedChains := connection.ConnectionState().VerifiedChains
verifiedChains := conn.ConnectionState().VerifiedChains if len(verifiedChains) == 0 || len(verifiedChains[0]) == 0 {
if len(verifiedChains) == 0 { return
return
} }
return true, verifiedChains[0][0], nil
chain := verifiedChains[0] // VerifiedChains[0] == PeerCertificates[0]
if len(chain) == 0 {
return
}
certificate = chain[0]
return true, certificate, nil
} }
// Ping checks if an address can be pinged and returns the round-trip time if the address can be pinged // Ping checks if an address can be pinged and returns the round-trip time if the address can be pinged

View File

@ -92,53 +92,53 @@ func TestCanPerformStartTLS(t *testing.T) {
} }
func TestCanPerformTLS(t *testing.T) { func TestCanPerformTLS(t *testing.T) {
type args struct { type args struct {
address string address string
insecure bool insecure bool
} }
tests := []struct { tests := []struct {
name string name string
args args args args
wantConnected bool wantConnected bool
wantErr bool wantErr bool
}{ }{
{ {
name: "invalid address", name: "invalid address",
args: args{ args: args{
address: "test", address: "test",
}, },
wantConnected: false, wantConnected: false,
wantErr: true, wantErr: true,
}, },
{ {
name: "error dial", name: "error dial",
args: args{ args: args{
address: "test:1234", address: "test:1234",
}, },
wantConnected: false, wantConnected: false,
wantErr: true, wantErr: true,
}, },
{ {
name: "valid tls", name: "valid tls",
args: args{ args: args{
address: "smtp.gmail.com:465", address: "smtp.gmail.com:465",
}, },
wantConnected: true, wantConnected: true,
wantErr: false, wantErr: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
connected, _, err := CanPerformTLS(tt.args.address, &Config{Insecure: tt.args.insecure, Timeout: 5 * time.Second}) connected, _, err := CanPerformTLS(tt.args.address, &Config{Insecure: tt.args.insecure, Timeout: 5 * time.Second})
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("CanPerformTLS() err=%v, wantErr=%v", err, tt.wantErr) t.Errorf("CanPerformTLS() err=%v, wantErr=%v", err, tt.wantErr)
return return
} }
if connected != tt.wantConnected { if connected != tt.wantConnected {
t.Errorf("CanPerformTLS() connected=%v, wantConnected=%v", connected, tt.wantConnected) t.Errorf("CanPerformTLS() connected=%v, wantConnected=%v", connected, tt.wantConnected)
} }
}) })
} }
} }
func TestCanCreateTCPConnection(t *testing.T) { func TestCanCreateTCPConnection(t *testing.T) {