2021-05-21 12:16:07 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddress_TLSEnabled(t *testing.T) {
|
|
|
|
testCases := [...]struct {
|
|
|
|
input string
|
|
|
|
wantTLS bool
|
|
|
|
}{
|
|
|
|
{"/dns4/localhost/tcp/8080", false},
|
|
|
|
{"/dns4/localhost/tcp/8080/tls", true},
|
|
|
|
{"/tls/dns4/localhost/tcp/8080", true},
|
2021-06-24 12:25:11 +00:00
|
|
|
{"grpc://localhost:8080", false},
|
|
|
|
{"grpcs://localhost:8080", true},
|
2021-05-21 12:16:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 12:25:11 +00:00
|
|
|
var addr Address
|
|
|
|
|
2021-05-21 12:16:07 +00:00
|
|
|
for _, test := range testCases {
|
2021-06-24 12:25:11 +00:00
|
|
|
err := addr.FromString(test.input)
|
|
|
|
require.NoError(t, err)
|
2021-05-21 12:16:07 +00:00
|
|
|
|
2023-06-26 12:00:44 +00:00
|
|
|
require.Equal(t, test.wantTLS, addr.IsTLSEnabled(), test.input)
|
2021-05-21 12:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 15:46:07 +00:00
|
|
|
|
|
|
|
func BenchmarkAddressTLSEnabled(b *testing.B) {
|
|
|
|
var addr Address
|
|
|
|
err := addr.FromString("/dns4/localhost/tcp/8080/tls")
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
var enabled bool
|
2024-08-30 16:20:55 +00:00
|
|
|
for range b.N {
|
2024-03-21 15:46:07 +00:00
|
|
|
enabled = addr.IsTLSEnabled()
|
|
|
|
}
|
|
|
|
require.True(b, enabled)
|
|
|
|
}
|