2020-02-06 20:42:46 -05:00
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2020-12-29 00:55:31 +00:00
|
|
|
"io"
|
2020-02-08 17:47:25 +00:00
|
|
|
"net"
|
2020-02-06 20:42:46 -05:00
|
|
|
"testing"
|
|
|
|
|
|
2020-04-12 18:09:46 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2020-04-12 18:09:46 +00:00
|
|
|
"github.com/qdm12/golibs/logging/mock_logging"
|
2021-01-02 01:57:00 +00:00
|
|
|
"github.com/qdm12/golibs/os"
|
|
|
|
|
"github.com/qdm12/golibs/os/mock_os"
|
2020-02-06 20:42:46 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-08 17:47:25 +00:00
|
|
|
func Test_UseDNSSystemWide(t *testing.T) {
|
2020-02-06 20:42:46 -05:00
|
|
|
t.Parallel()
|
2021-01-06 21:57:16 +00:00
|
|
|
|
2020-02-06 20:42:46 -05:00
|
|
|
tests := map[string]struct {
|
2021-01-03 16:38:46 +00:00
|
|
|
ip net.IP
|
|
|
|
|
keepNameserver bool
|
|
|
|
|
data []byte
|
2021-01-06 21:57:16 +00:00
|
|
|
firstOpenErr error
|
2021-01-03 16:38:46 +00:00
|
|
|
readErr error
|
2021-01-06 21:57:16 +00:00
|
|
|
firstCloseErr error
|
|
|
|
|
secondOpenErr error
|
|
|
|
|
writtenData string
|
2021-01-03 16:38:46 +00:00
|
|
|
writeErr error
|
2021-01-06 21:57:16 +00:00
|
|
|
secondCloseErr error
|
2021-01-03 16:38:46 +00:00
|
|
|
err error
|
2020-02-06 20:42:46 -05:00
|
|
|
}{
|
|
|
|
|
"no data": {
|
2021-01-03 16:38:46 +00:00
|
|
|
ip: net.IP{127, 0, 0, 1},
|
2020-12-30 20:48:41 +00:00
|
|
|
writtenData: "nameserver 127.0.0.1\n",
|
2020-12-29 00:55:31 +00:00
|
|
|
},
|
2021-01-06 21:57:16 +00:00
|
|
|
"first open error": {
|
|
|
|
|
ip: net.IP{127, 0, 0, 1},
|
|
|
|
|
firstOpenErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
2020-02-06 20:42:46 -05:00
|
|
|
},
|
|
|
|
|
"read error": {
|
|
|
|
|
readErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
|
},
|
2021-01-06 21:57:16 +00:00
|
|
|
"first close error": {
|
|
|
|
|
firstCloseErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
|
},
|
|
|
|
|
"second open error": {
|
|
|
|
|
ip: net.IP{127, 0, 0, 1},
|
|
|
|
|
secondOpenErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
|
},
|
2020-02-06 20:42:46 -05:00
|
|
|
"write error": {
|
2021-01-03 16:38:46 +00:00
|
|
|
ip: net.IP{127, 0, 0, 1},
|
2020-12-30 20:48:41 +00:00
|
|
|
writtenData: "nameserver 127.0.0.1\n",
|
2020-02-06 20:42:46 -05:00
|
|
|
writeErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
|
},
|
2021-01-06 21:57:16 +00:00
|
|
|
"second close error": {
|
|
|
|
|
ip: net.IP{127, 0, 0, 1},
|
|
|
|
|
writtenData: "nameserver 127.0.0.1\n",
|
|
|
|
|
secondCloseErr: fmt.Errorf("error"),
|
|
|
|
|
err: fmt.Errorf("error"),
|
|
|
|
|
},
|
2020-02-06 20:42:46 -05:00
|
|
|
"lines without nameserver": {
|
2021-01-03 16:38:46 +00:00
|
|
|
ip: net.IP{127, 0, 0, 1},
|
2020-02-06 20:42:46 -05:00
|
|
|
data: []byte("abc\ndef\n"),
|
2021-01-03 16:38:46 +00:00
|
|
|
writtenData: "nameserver 127.0.0.1\nabc\ndef\n",
|
2020-02-06 20:42:46 -05:00
|
|
|
},
|
|
|
|
|
"lines with nameserver": {
|
2021-01-03 16:38:46 +00:00
|
|
|
ip: net.IP{127, 0, 0, 1},
|
2020-02-06 20:42:46 -05:00
|
|
|
data: []byte("abc\nnameserver abc def\ndef\n"),
|
2021-01-03 16:38:46 +00:00
|
|
|
writtenData: "nameserver 127.0.0.1\nabc\ndef\n",
|
|
|
|
|
},
|
|
|
|
|
"keep nameserver": {
|
|
|
|
|
ip: net.IP{127, 0, 0, 1},
|
|
|
|
|
keepNameserver: true,
|
|
|
|
|
data: []byte("abc\nnameserver abc def\ndef\n"),
|
|
|
|
|
writtenData: "nameserver 127.0.0.1\nabc\nnameserver abc def\ndef\n",
|
2020-02-06 20:42:46 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
|
tc := tc
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2020-04-12 18:09:46 +00:00
|
|
|
mockCtrl := gomock.NewController(t)
|
2020-12-29 00:55:31 +00:00
|
|
|
|
2021-01-06 21:57:16 +00:00
|
|
|
type fileCall struct {
|
|
|
|
|
path string
|
|
|
|
|
flag int
|
|
|
|
|
perm os.FileMode
|
|
|
|
|
file os.File
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileCalls []fileCall
|
|
|
|
|
|
|
|
|
|
readOnlyFile := mock_os.NewMockFile(mockCtrl)
|
|
|
|
|
|
|
|
|
|
if tc.firstOpenErr == nil {
|
|
|
|
|
firstReadCall := readOnlyFile.EXPECT().
|
2020-12-29 00:55:31 +00:00
|
|
|
Read(gomock.AssignableToTypeOf([]byte{})).
|
|
|
|
|
DoAndReturn(func(b []byte) (int, error) {
|
|
|
|
|
copy(b, tc.data)
|
|
|
|
|
return len(tc.data), nil
|
|
|
|
|
})
|
|
|
|
|
readErr := tc.readErr
|
|
|
|
|
if readErr == nil {
|
|
|
|
|
readErr = io.EOF
|
|
|
|
|
}
|
2021-01-06 21:57:16 +00:00
|
|
|
finalReadCall := readOnlyFile.EXPECT().
|
2020-12-29 00:55:31 +00:00
|
|
|
Read(gomock.AssignableToTypeOf([]byte{})).
|
|
|
|
|
Return(0, readErr).After(firstReadCall)
|
2021-01-06 21:57:16 +00:00
|
|
|
readOnlyFile.EXPECT().Close().
|
|
|
|
|
Return(tc.firstCloseErr).
|
|
|
|
|
After(finalReadCall)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileCalls = append(fileCalls, fileCall{
|
|
|
|
|
path: string(constants.ResolvConf),
|
|
|
|
|
flag: os.O_RDONLY,
|
|
|
|
|
perm: 0,
|
|
|
|
|
file: readOnlyFile,
|
|
|
|
|
err: tc.firstOpenErr,
|
|
|
|
|
}) // always return readOnlyFile
|
|
|
|
|
|
|
|
|
|
if tc.firstOpenErr == nil && tc.readErr == nil && tc.firstCloseErr == nil {
|
|
|
|
|
writeOnlyFile := mock_os.NewMockFile(mockCtrl)
|
|
|
|
|
if tc.secondOpenErr == nil {
|
|
|
|
|
writeCall := writeOnlyFile.EXPECT().
|
|
|
|
|
WriteString(tc.writtenData).
|
|
|
|
|
Return(0, tc.writeErr)
|
|
|
|
|
writeOnlyFile.EXPECT().
|
|
|
|
|
Close().
|
|
|
|
|
Return(tc.secondCloseErr).
|
|
|
|
|
After(writeCall)
|
2020-12-29 00:55:31 +00:00
|
|
|
}
|
2021-01-06 21:57:16 +00:00
|
|
|
fileCalls = append(fileCalls, fileCall{
|
|
|
|
|
path: string(constants.ResolvConf),
|
|
|
|
|
flag: os.O_WRONLY | os.O_TRUNC,
|
|
|
|
|
perm: os.FileMode(0644),
|
|
|
|
|
file: writeOnlyFile,
|
|
|
|
|
err: tc.secondOpenErr,
|
|
|
|
|
})
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
|
2021-01-06 21:57:16 +00:00
|
|
|
fileCallIndex := 0
|
2020-12-29 00:55:31 +00:00
|
|
|
openFile := func(name string, flag int, perm os.FileMode) (os.File, error) {
|
2021-01-06 21:57:16 +00:00
|
|
|
fileCall := fileCalls[fileCallIndex]
|
|
|
|
|
fileCallIndex++
|
|
|
|
|
assert.Equal(t, fileCall.path, name)
|
|
|
|
|
assert.Equal(t, fileCall.flag, flag)
|
|
|
|
|
assert.Equal(t, fileCall.perm, perm)
|
|
|
|
|
return fileCall.file, fileCall.err
|
2020-12-29 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 18:09:46 +00:00
|
|
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
2021-01-03 16:38:46 +00:00
|
|
|
logger.EXPECT().Info("using DNS address %s system wide", tc.ip.String())
|
2021-01-06 21:57:16 +00:00
|
|
|
|
2020-02-06 20:42:46 -05:00
|
|
|
c := &configurator{
|
2020-12-29 00:55:31 +00:00
|
|
|
openFile: openFile,
|
|
|
|
|
logger: logger,
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
2021-01-03 16:38:46 +00:00
|
|
|
err := c.UseDNSSystemWide(tc.ip, tc.keepNameserver)
|
2020-02-06 20:42:46 -05:00
|
|
|
if tc.err != nil {
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
assert.Equal(t, tc.err.Error(), err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|