Files
gluetun/internal/provider/utils/protocol_test.go
Quentin McGaw 7d824a5179 chore(settings): refactor settings processing (#756)
- Better settings tree structure logged using `qdm12/gotree`
- Read settings from environment variables, then files, then secret files
- Settings methods to default them, merge them and override them
- `DNS_PLAINTEXT_ADDRESS` default changed to `127.0.0.1` to use DoT. Warning added if set to something else.
- `HTTPPROXY_LISTENING_ADDRESS` instead of `HTTPPROXY_PORT` (with retro-compatibility)
2022-01-06 06:40:23 -05:00

136 lines
2.8 KiB
Go

package utils
import (
"testing"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/stretchr/testify/assert"
)
func Test_GetProtocol(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
selection settings.ServerSelection
protocol string
}{
"default": {
protocol: constants.UDP,
},
"OpenVPN UDP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(false),
},
},
protocol: constants.UDP,
},
"OpenVPN TCP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(true),
},
},
protocol: constants.TCP,
},
"Wireguard": {
selection: settings.ServerSelection{
VPN: constants.Wireguard,
},
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)
})
}
}
func Test_FilterByProtocol(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
selection settings.ServerSelection
serverTCP bool
serverUDP bool
filtered bool
}{
"Wireguard and server has UDP": {
selection: settings.ServerSelection{
VPN: constants.Wireguard,
},
serverUDP: true,
filtered: false,
},
"Wireguard and server has not UDP": {
selection: settings.ServerSelection{
VPN: constants.Wireguard,
},
serverUDP: false,
filtered: true,
},
"OpenVPN UDP and server has UDP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(false),
},
},
serverUDP: true,
filtered: false,
},
"OpenVPN UDP and server has not UDP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(false),
},
},
serverUDP: false,
filtered: true,
},
"OpenVPN TCP and server has TCP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(true),
},
},
serverTCP: true,
filtered: false,
},
"OpenVPN TCP and server has not TCP": {
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(true),
},
},
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)
})
}
}