Skip to content

Commit

Permalink
Add gettls null backend test (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Sep 6, 2024
1 parent 07f101e commit 4413a87
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on: # yamllint disable-line rule:truthy

env:
DESIRED_GO_VERSION: '1.20'
DESIRED_GOLANGCI_LINT_VERSION: 'v1.56'
DESIRED_GOLANGCI_LINT_VERSION: 'v1.60'
DESIRED_PYTHON_VERSION: '3.12'

jobs:
Expand Down
4 changes: 4 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sonar.python.version=3.8, 3.9, 3.11, 3.12
sonar.sources=.
sonar.test.inclusions=**/*_test.go, receptorctl/tests/
sonar.tests=.
41 changes: 38 additions & 3 deletions pkg/backends/null_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package backends
package backends_test

import "testing"
import (
"crypto/tls"
"reflect"
"testing"

"github.com/ansible/receptor/pkg/backends"
)

func TestNullBackendCfgGetAddr(t *testing.T) {
type fields struct {
Expand All @@ -21,7 +27,7 @@ func TestNullBackendCfgGetAddr(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &NullBackendCfg{
cfg := &backends.NullBackendCfg{
Local: tt.fields.Local,
}
if got := cfg.GetAddr(); got != tt.want {
Expand All @@ -30,3 +36,32 @@ func TestNullBackendCfgGetAddr(t *testing.T) {
})
}
}

func TestNullBackendCfgGetTLS(t *testing.T) {
type fields struct {
Local bool
}
tests := []struct {
name string
fields fields
want *tls.Config
}{
{
name: "Positive",
fields: fields{
Local: true,
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &backends.NullBackendCfg{
Local: tt.fields.Local,
}
if got := cfg.GetTLS(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NullBackendCfg.GetTLS() = %v, want %v", got, tt.want)
}
})
}
}
78 changes: 43 additions & 35 deletions pkg/backends/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import (
"github.com/golang/mock/gomock"
)

const (
expectedWebsocketListenerError = "Expected Websocket Listener, got nil"
newWebsocketDialerError = "NewWebsockerDialer return error: %+v"
websocketListenerExpectedError = "Websocket listener expected, nil returned"
websocketsTestNewWebsocketDialer = "websockets_test.go>TestNewWebsocketDialer"
wssTestTesting = "wss://test.testing"
)

func setupTLSCfg(t *testing.T) tls.Certificate {
// Create a server TLS certificate for "localhost"
key, err := rsa.GenerateKey(rand.Reader, 2048)
Expand Down Expand Up @@ -66,15 +74,15 @@ func TestNewWebsocketDialer(t *testing.T) {
}{
{
name: "NewWebsocketDialer wss Success ",
address: "wss://test.testing",
address: wssTestTesting,
redial: false,
tlscfg: &tls.Config{
Certificates: []tls.Certificate{setupTLSCfg(t)},
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
},
extraHeader: "",
logger: logger.NewReceptorLogger("websockets_test.go>TestNewWebsocketDialer"),
logger: logger.NewReceptorLogger(websocketsTestNewWebsocketDialer),
expectedErr: "",
failedTestString: "Expected no error, but got: %v",
},
Expand All @@ -88,7 +96,7 @@ func TestNewWebsocketDialer(t *testing.T) {
PreferServerCipherSuites: true,
},
extraHeader: "",
logger: logger.NewReceptorLogger("websockets_test.go>TestNewWebsocketDialer"),
logger: logger.NewReceptorLogger(websocketsTestNewWebsocketDialer),
expectedErr: "",
failedTestString: "Expected no error, but got: %v",
},
Expand Down Expand Up @@ -130,9 +138,9 @@ func TestWebsocketDialerStart(t *testing.T) {
ctx := context.Background()
defer ctx.Done()

wd, wdErr := backends.NewWebsocketDialer("wss://test.testing", &tls.Config{}, "", false, logger.NewReceptorLogger("websockets_test.go>TestNewWebsocketDialer"), mockWebsocketDialer)
wd, wdErr := backends.NewWebsocketDialer(wssTestTesting, &tls.Config{}, "", false, logger.NewReceptorLogger(websocketsTestNewWebsocketDialer), mockWebsocketDialer)
if wdErr != nil {
t.Errorf("NewWebsockerDialer return error: %v", wdErr)
t.Errorf(newWebsocketDialerError, wdErr)
}
resp := &http.Response{
Body: io.NopCloser(bytes.NewBufferString("Hello World")),
Expand All @@ -158,7 +166,7 @@ func TestWebsocketDialerStart(t *testing.T) {
mockWebsocketConner.EXPECT().ReadMessage().Return(0, []byte{}, nil).AnyTimes()
sess, err := wd.Start(ctx, &sync.WaitGroup{})
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
s := <-sess

Expand All @@ -175,10 +183,10 @@ func TestWebsocketDialerStart(t *testing.T) {
func TestWebsocketDialerGetAddr(t *testing.T) {
_, mockWebsocketDialer := setUpDialer(t)

address := "wss://test.testing"
wd, wdErr := backends.NewWebsocketDialer(address, &tls.Config{}, "", false, logger.NewReceptorLogger("websockets_test.go>TestNewWebsocketDialer"), mockWebsocketDialer)
address := wssTestTesting
wd, wdErr := backends.NewWebsocketDialer(address, &tls.Config{}, "", false, logger.NewReceptorLogger(websocketsTestNewWebsocketDialer), mockWebsocketDialer)
if wdErr != nil {
t.Errorf("NewWebsockerDialer return error: %v", wdErr)
t.Errorf(newWebsocketDialerError, wdErr)
}
add := wd.GetAddr()
if add != address {
Expand All @@ -190,9 +198,9 @@ func TestWebsocketDialerGetTLS(t *testing.T) {
_, mockWebsocketDialer := setUpDialer(t)

blankTLS := &tls.Config{}
wd, wdErr := backends.NewWebsocketDialer("wss://test.testing", blankTLS, "", false, logger.NewReceptorLogger("websockets_test.go>TestNewWebsocketDialer"), mockWebsocketDialer)
wd, wdErr := backends.NewWebsocketDialer(wssTestTesting, blankTLS, "", false, logger.NewReceptorLogger(websocketsTestNewWebsocketDialer), mockWebsocketDialer)
if wdErr != nil {
t.Errorf("NewWebsockerDialer return error: %v", wdErr)
t.Errorf(newWebsocketDialerError, wdErr)
}
TLS := wd.GetTLS()
if TLS != blankTLS {
Expand All @@ -214,11 +222,11 @@ func TestNewWebsocketListener(t *testing.T) {

wi, err := backends.NewWebsocketListener("address", &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}

if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}
}

Expand All @@ -227,11 +235,11 @@ func TestWebsocketListenerSetandGetPath(t *testing.T) {

wi, err := backends.NewWebsocketListener("address", &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}

if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}
pathName := "Test Path"

Expand All @@ -251,10 +259,10 @@ func TestWebsocketListenerStart(t *testing.T) {

wi, err := backends.NewWebsocketListener("localhost:21700", &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}

mockWebsocketUpgrader.EXPECT().Upgrade(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockWebsocketConner, nil).AnyTimes()
Expand All @@ -264,10 +272,10 @@ func TestWebsocketListenerStart(t *testing.T) {

bs, err := wi.Start(ctx, &sync.WaitGroup{})
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if bs == nil {
t.Errorf("Expected Websocket Listener, got nil")
t.Error(expectedWebsocketListenerError)
}
}

Expand All @@ -278,10 +286,10 @@ func TestWebsocketListenerStartUpgradeError(t *testing.T) {

wi, err := backends.NewWebsocketListener("localhost:21701", &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}
returnError := errors.New("Upgrade Error")

Expand All @@ -295,7 +303,7 @@ func TestWebsocketListenerStartUpgradeError(t *testing.T) {
t.Errorf("Expected error %v, got %v instead", nil, err)
}
if bs == nil {
t.Errorf("Expected Websocket Listener, got nil")
t.Error(expectedWebsocketListenerError)
}
}

Expand All @@ -309,17 +317,17 @@ func TestWebsocketListenerStartNetError(t *testing.T) {
badAddress := "127.0.0.1:80"
wi, err := backends.NewWebsocketListener(badAddress, &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}

mockWebsocketUpgrader.EXPECT().Upgrade(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockWebsocketConner, nil).AnyTimes()

bs, err := wi.Start(ctx, &sync.WaitGroup{})
if !strings.Contains(err.Error(), "listen tcp 127.0.0.1:80: bind: permission denied") {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if bs != nil {
t.Errorf("Expected Websocket Listener to be nil")
Expand All @@ -335,10 +343,10 @@ func TestWebsocketListenerStartTLSNil(t *testing.T) {

wi, err := backends.NewWebsocketListener("localhost:21702", nil, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}

mockWebsocketUpgrader.EXPECT().Upgrade(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockWebsocketConner, nil).AnyTimes()
Expand All @@ -347,7 +355,7 @@ func TestWebsocketListenerStartTLSNil(t *testing.T) {

bs, err := wi.Start(ctx, &sync.WaitGroup{})
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if bs == nil {
t.Errorf("Expected Websocket Listener not be nil")
Expand All @@ -364,10 +372,10 @@ func TestWebsocketListenerGetAddr(t *testing.T) {

wi, err := backends.NewWebsocketListener(address, &tls.Config{}, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}

mockWebsocketUpgrader.EXPECT().Upgrade(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockWebsocketConner, nil).AnyTimes()
Expand All @@ -377,10 +385,10 @@ func TestWebsocketListenerGetAddr(t *testing.T) {

bs, err := wi.Start(ctx, &sync.WaitGroup{})
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if bs == nil {
t.Errorf("Expected Websocket Listener, got nil")
t.Error(expectedWebsocketListenerError)
}

add := wi.GetAddr()
Expand All @@ -395,10 +403,10 @@ func TestWebsocketListenerGetTLS(t *testing.T) {
blankTLS := &tls.Config{}
wi, err := backends.NewWebsocketListener("127.0.0.1:21704", blankTLS, logger.NewReceptorLogger("test"), mockWebsocketUpgrader, mockServer)
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
if wi == nil {
t.Errorf("Websocket listener expected, nil returned")
t.Error(websocketListenerExpectedError)
}

TLS := wi.GetTLS()
Expand Down Expand Up @@ -442,6 +450,6 @@ func TestWebsocketListenerCfg(t *testing.T) {

err := wlc.Prepare()
if err != nil {
t.Errorf(err.Error())
t.Errorf(err.Error()) //nolint:govet,staticcheck
}
}
2 changes: 1 addition & 1 deletion pkg/controlsvc/controlsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (cfg CmdlineConfigUnix) Run() error {
}
}
err = MainInstance.RunControlSvc(context.Background(), cfg.Service, tlscfg, cfg.Filename,
os.FileMode(cfg.Permissions), cfg.TCPListen, tcptls)
os.FileMode(cfg.Permissions), cfg.TCPListen, tcptls) //nolint:gosec
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/framer/framer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New() Framer {
// SendData takes a data buffer and returns a framed buffer.
func (f *framer) SendData(data []byte) []byte {
buf := make([]byte, len(data)+2)
binary.LittleEndian.PutUint16(buf[0:2], uint16(len(data)))
binary.LittleEndian.PutUint16(buf[0:2], uint16(len(data))) //nolint:gosec
copy(buf[2:], data)

return buf
Expand Down
6 changes: 3 additions & 3 deletions pkg/netceptor/netceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ func ReceptorVerifyFunc(tlscfg *tls.Config, pinnedFingerprints [][]byte, expecte
if err != nil {
logger.Error("RVF failed to parse: %s", err)

return fmt.Errorf("failed to parse certificate from server: " + err.Error())
return fmt.Errorf("failed to parse certificate from server: " + err.Error()) //nolint:govet
}
certs[i] = cert
}
Expand Down Expand Up @@ -1383,7 +1383,7 @@ func (s *Netceptor) printRoutingTable() {
_, _ = fmt.Fprintf(sb, "%s(%.2f) ", peer, s.knownConnectionCosts[conn][peer])
}
_, _ = fmt.Fprintf(sb, "\n")
s.Logger.Log(logLevel, sb.String())
s.Logger.Log(logLevel, sb.String()) //nolint:govet
}
s.Logger.Log(logLevel, "Routing Table:\n")
for node := range s.routingTable {
Expand Down Expand Up @@ -1656,7 +1656,7 @@ func (s *Netceptor) handleMessageData(md *MessageData) error {
if !ok || pc.context.Err() != nil {
s.listenerLock.RUnlock()
if md.FromNode == s.nodeID {
return fmt.Errorf(ProblemServiceUnknown)
return fmt.Errorf(ProblemServiceUnknown) //nolint:staticcheck
}
_ = s.sendUnreachable(md.FromNode, &UnreachableMessage{
FromNode: md.FromNode,
Expand Down
2 changes: 1 addition & 1 deletion pkg/netceptor/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func SendPing(ctx context.Context, s NetcForPing, target string, hopsToLive byte
go func() {
for msg := range unrCh {
errorChan <- errorResult{
err: fmt.Errorf(msg.Problem),
err: fmt.Errorf(msg.Problem), //nolint:govet
fromNode: msg.ReceivedFromNode,
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/unix_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (cfg UnixProxyInboundCfg) Run() error {
return err
}

return UnixProxyServiceInbound(netceptor.MainInstance, cfg.Filename, os.FileMode(cfg.Permissions),
return UnixProxyServiceInbound(netceptor.MainInstance, cfg.Filename, os.FileMode(cfg.Permissions), //nolint:gosec
cfg.RemoteNode, cfg.RemoteService, tlscfg)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/error_kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
goodErrorString string = "unit was already started"
)

var errUnitWasAlreadyStarted error = fmt.Errorf(goodErrorString)
var errUnitWasAlreadyStarted error = fmt.Errorf(goodErrorString) //nolint:staticcheck

func TestErrorWithKind_Error(t *testing.T) {
type fields struct {
Expand Down
Loading

0 comments on commit 4413a87

Please sign in to comment.