410 lines
9.2 KiB
Go
410 lines
9.2 KiB
Go
package custom
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_ProcessCustomConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
file, err := os.CreateTemp("", "")
|
|
require.NoError(t, err)
|
|
defer removeFile(t, file.Name())
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString("remote 1.9.8.7\nkeep me\ncipher remove")
|
|
require.NoError(t, err)
|
|
|
|
err = file.Close()
|
|
require.NoError(t, err)
|
|
|
|
settings := configuration.OpenVPN{
|
|
Cipher: "cipher",
|
|
MSSFix: 999,
|
|
Config: file.Name(),
|
|
}
|
|
|
|
lines, connection, err := ProcessCustomConfig(settings)
|
|
assert.NoError(t, err)
|
|
|
|
expectedLines := []string{
|
|
"keep me",
|
|
"proto udp",
|
|
"remote 1.9.8.7 1194",
|
|
"mute-replay-warnings",
|
|
"auth-nocache",
|
|
"pull-filter ignore \"auth-token\"",
|
|
"auth-retry nointeract",
|
|
"suppress-timestamps",
|
|
"verb 0",
|
|
"data-ciphers-fallback cipher",
|
|
"data-ciphers cipher",
|
|
"mssfix 999",
|
|
"pull-filter ignore \"route-ipv6\"",
|
|
"pull-filter ignore \"ifconfig-ipv6\"",
|
|
"user ",
|
|
}
|
|
assert.Equal(t, expectedLines, lines)
|
|
|
|
expectedConnection := models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 9, 8, 7),
|
|
Port: 1194,
|
|
Protocol: constants.UDP,
|
|
}
|
|
assert.Equal(t, expectedConnection, connection)
|
|
}
|
|
|
|
func Test_readCustomConfigLines(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
file, err := os.CreateTemp("", "")
|
|
require.NoError(t, err)
|
|
defer removeFile(t, file.Name())
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString("line one\nline two\nline three\n")
|
|
require.NoError(t, err)
|
|
|
|
err = file.Close()
|
|
require.NoError(t, err)
|
|
|
|
lines, err := readCustomConfigLines(file.Name())
|
|
assert.NoError(t, err)
|
|
|
|
expectedLines := []string{
|
|
"line one", "line two", "line three", "",
|
|
}
|
|
assert.Equal(t, expectedLines, lines)
|
|
}
|
|
|
|
func removeFile(t *testing.T, filename string) {
|
|
t.Helper()
|
|
err := os.RemoveAll(filename)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func Test_modifyCustomConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := map[string]struct {
|
|
lines []string
|
|
settings configuration.OpenVPN
|
|
connection models.OpenVPNConnection
|
|
modified []string
|
|
}{
|
|
"mixed": {
|
|
lines: []string{
|
|
"up bla",
|
|
"proto tcp",
|
|
"remote 5.5.5.5",
|
|
"cipher bla",
|
|
"tun-ipv6",
|
|
"keep me here",
|
|
"auth bla",
|
|
},
|
|
settings: configuration.OpenVPN{
|
|
User: "user",
|
|
Cipher: "cipher",
|
|
Auth: "auth",
|
|
MSSFix: 1000,
|
|
ProcUser: "procuser",
|
|
},
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 1194,
|
|
Protocol: constants.UDP,
|
|
},
|
|
modified: []string{
|
|
"keep me here",
|
|
"proto udp",
|
|
"remote 1.2.3.4 1194",
|
|
"mute-replay-warnings",
|
|
"auth-nocache",
|
|
"pull-filter ignore \"auth-token\"",
|
|
"auth-retry nointeract",
|
|
"suppress-timestamps",
|
|
"auth-user-pass /etc/openvpn/auth.conf",
|
|
"verb 0",
|
|
"data-ciphers-fallback cipher",
|
|
"data-ciphers cipher",
|
|
"auth auth",
|
|
"mssfix 1000",
|
|
"pull-filter ignore \"route-ipv6\"",
|
|
"pull-filter ignore \"ifconfig-ipv6\"",
|
|
"user procuser",
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, testCase := range testCases {
|
|
testCase := testCase
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
modified := modifyCustomConfig(testCase.lines,
|
|
testCase.settings, testCase.connection)
|
|
|
|
assert.Equal(t, testCase.modified, modified)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_extractConnectionFromLines(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := map[string]struct {
|
|
lines []string
|
|
connection models.OpenVPNConnection
|
|
err error
|
|
}{
|
|
"success": {
|
|
lines: []string{"bla bla", "proto tcp", "remote 1.2.3.4 1194 tcp"},
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 1194,
|
|
Protocol: constants.TCP,
|
|
},
|
|
},
|
|
"extraction error": {
|
|
lines: []string{"bla bla", "proto bad", "remote 1.2.3.4 1194 tcp"},
|
|
err: errors.New("on line 2: failed extracting protocol from proto line: network protocol not supported: bad"),
|
|
},
|
|
"only use first values found": {
|
|
lines: []string{"proto udp", "proto tcp", "remote 1.2.3.4 443 tcp", "remote 5.2.3.4 1194 udp"},
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 443,
|
|
Protocol: constants.UDP,
|
|
},
|
|
},
|
|
"no IP found": {
|
|
lines: []string{"proto tcp"},
|
|
connection: models.OpenVPNConnection{
|
|
Protocol: constants.TCP,
|
|
},
|
|
err: errRemoteLineNotFound,
|
|
},
|
|
"default TCP port": {
|
|
lines: []string{"remote 1.2.3.4", "proto tcp"},
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 443,
|
|
Protocol: constants.TCP,
|
|
},
|
|
},
|
|
"default UDP port": {
|
|
lines: []string{"remote 1.2.3.4", "proto udp"},
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 1194,
|
|
Protocol: constants.UDP,
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, testCase := range testCases {
|
|
testCase := testCase
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
connection, err := extractConnectionFromLines(testCase.lines)
|
|
|
|
if testCase.err != nil {
|
|
require.Error(t, err)
|
|
assert.Equal(t, testCase.err.Error(), err.Error())
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
assert.Equal(t, testCase.connection, connection)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_extractConnectionFromLine(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := map[string]struct {
|
|
line string
|
|
connection models.OpenVPNConnection
|
|
isErr error
|
|
}{
|
|
"irrelevant line": {
|
|
line: "bla bla",
|
|
},
|
|
"extract proto error": {
|
|
line: "proto bad",
|
|
isErr: errExtractProto,
|
|
},
|
|
"extract proto success": {
|
|
line: "proto tcp",
|
|
connection: models.OpenVPNConnection{
|
|
Protocol: constants.TCP,
|
|
},
|
|
},
|
|
"extract remote error": {
|
|
line: "remote bad",
|
|
isErr: errExtractRemote,
|
|
},
|
|
"extract remote success": {
|
|
line: "remote 1.2.3.4 1194 udp",
|
|
connection: models.OpenVPNConnection{
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
Port: 1194,
|
|
Protocol: constants.UDP,
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, testCase := range testCases {
|
|
testCase := testCase
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
connection, err := extractConnectionFromLine(testCase.line)
|
|
|
|
if testCase.isErr != nil {
|
|
assert.ErrorIs(t, err, testCase.isErr)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
assert.Equal(t, testCase.connection, connection)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_extractProto(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := map[string]struct {
|
|
line string
|
|
protocol string
|
|
err error
|
|
}{
|
|
"fields error": {
|
|
line: "proto one two",
|
|
err: errors.New("proto line has not 2 fields as expected: proto one two"),
|
|
},
|
|
"bad protocol": {
|
|
line: "proto bad",
|
|
err: errors.New("network protocol not supported: bad"),
|
|
},
|
|
"udp": {
|
|
line: "proto udp",
|
|
protocol: constants.UDP,
|
|
},
|
|
"tcp": {
|
|
line: "proto tcp",
|
|
protocol: constants.TCP,
|
|
},
|
|
}
|
|
|
|
for name, testCase := range testCases {
|
|
testCase := testCase
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
protocol, err := extractProto(testCase.line)
|
|
|
|
if testCase.err != nil {
|
|
require.Error(t, err)
|
|
assert.Equal(t, testCase.err.Error(), err.Error())
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
assert.Equal(t, testCase.protocol, protocol)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_extractRemote(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := map[string]struct {
|
|
line string
|
|
ip net.IP
|
|
port uint16
|
|
protocol string
|
|
err error
|
|
}{
|
|
"not enough fields": {
|
|
line: "remote",
|
|
err: errors.New("remote line has not 2 fields as expected: remote"),
|
|
},
|
|
"too many fields": {
|
|
line: "remote one two three four",
|
|
err: errors.New("remote line has not 2 fields as expected: remote one two three four"),
|
|
},
|
|
"host is not an IP": {
|
|
line: "remote somehost.com",
|
|
err: errors.New("host is not an an IP address: somehost.com"),
|
|
},
|
|
"only IP host": {
|
|
line: "remote 1.2.3.4",
|
|
ip: net.IPv4(1, 2, 3, 4),
|
|
},
|
|
"port not an integer": {
|
|
line: "remote 1.2.3.4 bad",
|
|
err: errors.New("port is not valid: remote 1.2.3.4 bad"),
|
|
},
|
|
"port is zero": {
|
|
line: "remote 1.2.3.4 0",
|
|
err: errors.New("port is not valid: not between 1 and 65535: 0"),
|
|
},
|
|
"port is minus one": {
|
|
line: "remote 1.2.3.4 -1",
|
|
err: errors.New("port is not valid: not between 1 and 65535: -1"),
|
|
},
|
|
"port is over 65535": {
|
|
line: "remote 1.2.3.4 65536",
|
|
err: errors.New("port is not valid: not between 1 and 65535: 65536"),
|
|
},
|
|
"IP host and port": {
|
|
line: "remote 1.2.3.4 8000",
|
|
ip: net.IPv4(1, 2, 3, 4),
|
|
port: 8000,
|
|
},
|
|
"invalid protocol": {
|
|
line: "remote 1.2.3.4 8000 bad",
|
|
err: errors.New("network protocol not supported: bad"),
|
|
},
|
|
"IP host and port and protocol": {
|
|
line: "remote 1.2.3.4 8000 udp",
|
|
ip: net.IPv4(1, 2, 3, 4),
|
|
port: 8000,
|
|
protocol: constants.UDP,
|
|
},
|
|
}
|
|
|
|
for name, testCase := range testCases {
|
|
testCase := testCase
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ip, port, protocol, err := extractRemote(testCase.line)
|
|
|
|
if testCase.err != nil {
|
|
require.Error(t, err)
|
|
assert.Equal(t, testCase.err.Error(), err.Error())
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
assert.Equal(t, testCase.ip, ip)
|
|
assert.Equal(t, testCase.port, port)
|
|
assert.Equal(t, testCase.protocol, protocol)
|
|
})
|
|
}
|
|
}
|