Files
gluetun/internal/provider/expressvpn/connection_test.go

101 lines
2.9 KiB
Go
Raw Normal View History

2021-09-23 10:19:30 -07:00
package expressvpn
import (
"math/rand"
"net"
"testing"
"github.com/qdm12/gluetun/internal/configuration/settings"
2021-09-23 10:19:30 -07:00
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/constants/vpn"
2021-09-23 10:19:30 -07:00
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
2021-09-23 10:19:30 -07:00
"github.com/stretchr/testify/assert"
)
func Test_Provider_GetConnection(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
servers []models.Server
selection settings.ServerSelection
2021-09-23 10:19:30 -07:00
connection models.Connection
errWrapped error
errMessage string
2021-09-23 10:19:30 -07:00
}{
"no server": {
selection: settings.ServerSelection{}.WithDefaults(providers.Expressvpn),
errWrapped: utils.ErrNoServer,
errMessage: "no server",
2021-09-23 10:19:30 -07:00
},
"no filter": {
servers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, VPN: vpn.OpenVPN, UDP: true},
{IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, VPN: vpn.OpenVPN, UDP: true},
{IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, VPN: vpn.OpenVPN, UDP: true},
2021-09-23 10:19:30 -07:00
},
selection: settings.ServerSelection{}.WithDefaults(providers.Expressvpn),
2021-09-23 10:19:30 -07:00
connection: models.Connection{
Type: vpn.OpenVPN,
2021-09-23 10:19:30 -07:00
IP: net.IPv4(1, 1, 1, 1),
Port: 1195,
Protocol: constants.UDP,
},
},
"target IP": {
selection: settings.ServerSelection{
2021-09-23 10:19:30 -07:00
TargetIP: net.IPv4(2, 2, 2, 2),
}.WithDefaults(providers.Expressvpn),
servers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, VPN: vpn.OpenVPN, UDP: true},
{IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, VPN: vpn.OpenVPN, UDP: true},
{IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, VPN: vpn.OpenVPN, UDP: true},
2021-09-23 10:19:30 -07:00
},
connection: models.Connection{
Type: vpn.OpenVPN,
2021-09-23 10:19:30 -07:00
IP: net.IPv4(2, 2, 2, 2),
Port: 1195,
Protocol: constants.UDP,
},
},
"with filter": {
selection: settings.ServerSelection{
2021-09-23 10:19:30 -07:00
Hostnames: []string{"b"},
}.WithDefaults(providers.Expressvpn),
servers: []models.Server{
{Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, VPN: vpn.OpenVPN, UDP: true},
{Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, VPN: vpn.OpenVPN, UDP: true},
{Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, VPN: vpn.OpenVPN, UDP: true},
2021-09-23 10:19:30 -07:00
},
connection: models.Connection{
Type: vpn.OpenVPN,
2021-09-23 10:19:30 -07:00
IP: net.IPv4(2, 2, 2, 2),
Port: 1195,
Protocol: constants.UDP,
Hostname: "b",
},
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
randSource := rand.NewSource(0)
m := New(testCase.servers, randSource)
connection, err := m.GetConnection(testCase.selection)
assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
assert.EqualError(t, err, testCase.errMessage)
2021-09-23 10:19:30 -07:00
}
assert.Equal(t, testCase.connection, connection)
})
}
}