2021-08-23 16:07:47 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2021-08-23 16:07:47 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2022-04-18 09:15:20 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants/vpn"
|
2021-08-23 16:07:47 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_GetProtocol(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection settings.ServerSelection
|
2021-08-23 16:07:47 +00:00
|
|
|
protocol string
|
|
|
|
|
}{
|
|
|
|
|
"default": {
|
|
|
|
|
protocol: constants.UDP,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN UDP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(false),
|
|
|
|
|
},
|
2021-08-23 16:07:47 +00:00
|
|
|
},
|
|
|
|
|
protocol: constants.UDP,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN TCP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(true),
|
2021-08-23 16:07:47 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
protocol: constants.TCP,
|
|
|
|
|
},
|
|
|
|
|
"Wireguard": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.Wireguard,
|
2021-08-23 16:07:47 +00:00
|
|
|
},
|
|
|
|
|
protocol: constants.UDP,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, testCase := range testCases {
|
|
|
|
|
testCase := testCase
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
protocol := GetProtocol(testCase.selection)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, testCase.protocol, protocol)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-23 20:16:29 +00:00
|
|
|
|
|
|
|
|
func Test_FilterByProtocol(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection settings.ServerSelection
|
2021-08-23 20:16:29 +00:00
|
|
|
serverTCP bool
|
|
|
|
|
serverUDP bool
|
|
|
|
|
filtered bool
|
|
|
|
|
}{
|
|
|
|
|
"Wireguard and server has UDP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.Wireguard,
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
serverUDP: true,
|
|
|
|
|
filtered: false,
|
|
|
|
|
},
|
|
|
|
|
"Wireguard and server has not UDP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.Wireguard,
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
serverUDP: false,
|
|
|
|
|
filtered: true,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN UDP and server has UDP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(false),
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
serverUDP: true,
|
|
|
|
|
filtered: false,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN UDP and server has not UDP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(false),
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
serverUDP: false,
|
|
|
|
|
filtered: true,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN TCP and server has TCP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(true),
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
serverTCP: true,
|
|
|
|
|
filtered: false,
|
|
|
|
|
},
|
|
|
|
|
"OpenVPN TCP and server has not TCP": {
|
2022-01-06 06:40:23 -05:00
|
|
|
selection: settings.ServerSelection{
|
2022-04-18 09:15:20 +00:00
|
|
|
VPN: vpn.OpenVPN,
|
2022-01-06 06:40:23 -05:00
|
|
|
OpenVPN: settings.OpenVPNSelection{
|
|
|
|
|
TCP: boolPtr(true),
|
2021-08-23 20:16:29 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
serverTCP: false,
|
|
|
|
|
filtered: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, testCase := range testCases {
|
|
|
|
|
testCase := testCase
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
filtered := FilterByProtocol(testCase.selection,
|
|
|
|
|
testCase.serverTCP, testCase.serverUDP)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, testCase.filtered, filtered)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|