chore(filter): common filter for all providers

This commit is contained in:
Quentin McGaw
2022-04-18 17:06:57 +00:00
parent ac9571c6b2
commit f5c00c3e2d
54 changed files with 435 additions and 1571 deletions

View File

@@ -1,7 +1,6 @@
package windscribe
import (
"errors"
"math/rand"
"net"
"testing"
@@ -11,8 +10,8 @@ import (
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Windscribe_GetConnection(t *testing.T) {
@@ -22,17 +21,19 @@ func Test_Windscribe_GetConnection(t *testing.T) {
servers []models.Server
selection settings.ServerSelection
connection models.Connection
err error
errWrapped error
errMessage string
}{
"no server available": {
selection: settings.ServerSelection{}.WithDefaults(providers.Windscribe),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
selection: settings.ServerSelection{}.WithDefaults(providers.Windscribe),
errWrapped: utils.ErrNoServerFound,
errMessage: "no server found: for VPN openvpn; protocol udp",
},
"no filter": {
servers: []models.Server{
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{VPN: vpn.OpenVPN, UDP: true, 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)}},
},
selection: settings.ServerSelection{}.WithDefaults(providers.Windscribe),
connection: models.Connection{
@@ -47,9 +48,9 @@ func Test_Windscribe_GetConnection(t *testing.T) {
TargetIP: net.IPv4(2, 2, 2, 2),
}.WithDefaults(providers.Windscribe),
servers: []models.Server{
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: vpn.OpenVPN, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{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},
},
connection: models.Connection{
Type: vpn.OpenVPN,
@@ -63,9 +64,9 @@ func Test_Windscribe_GetConnection(t *testing.T) {
Hostnames: []string{"b"},
}.WithDefaults(providers.Windscribe),
servers: []models.Server{
{VPN: vpn.OpenVPN, Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: vpn.OpenVPN, Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: vpn.OpenVPN, Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{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},
},
connection: models.Connection{
Type: vpn.OpenVPN,
@@ -83,17 +84,14 @@ func Test_Windscribe_GetConnection(t *testing.T) {
randSource := rand.NewSource(0)
m := New(testCase.servers, randSource)
provider := New(testCase.servers, randSource)
connection, err := m.GetConnection(testCase.selection)
connection, err := provider.GetConnection(testCase.selection)
if testCase.err != nil {
require.Error(t, err)
assert.Equal(t, testCase.err.Error(), err.Error())
} else {
assert.NoError(t, err)
assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.connection, connection)
})
}