From 449a70c2dc1bbf243a2e109b2c87098adf0ddcc5 Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 14 Nov 2024 14:32:09 +0000 Subject: [PATCH 1/6] Add the framework and mocks for testing udp_proxy.go --- .../mock_interfaces/net_interfaces.go | 448 ++++++++++++++++++ pkg/services/interfaces/net_interfaces.go | 41 ++ pkg/services/mock_services/udp_proxy.go | 100 ++++ pkg/services/udp_proxy.go | 82 ++-- 4 files changed, 641 insertions(+), 30 deletions(-) create mode 100644 pkg/services/interfaces/mock_interfaces/net_interfaces.go create mode 100644 pkg/services/interfaces/net_interfaces.go create mode 100644 pkg/services/mock_services/udp_proxy.go diff --git a/pkg/services/interfaces/mock_interfaces/net_interfaces.go b/pkg/services/interfaces/mock_interfaces/net_interfaces.go new file mode 100644 index 000000000..b0b93a2f4 --- /dev/null +++ b/pkg/services/interfaces/mock_interfaces/net_interfaces.go @@ -0,0 +1,448 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: pkg/services/interfaces/net_interfaces.go +// +// Generated by this command: +// +// mockgen -source=pkg/services/interfaces/net_interfaces.go -destination=pkg/services/interfaces/mock_interfaces/net_interfaces.go +// + +// Package mock_net_interface is a generated GoMock package. +package mock_net_interface + +import ( + net "net" + netip "net/netip" + os "os" + reflect "reflect" + syscall "syscall" + time "time" + + net_interface "github.com/ansible/receptor/pkg/services/interfaces" + gomock "go.uber.org/mock/gomock" +) + +// MockNetterUDP is a mock of NetterUDP interface. +type MockNetterUDP struct { + ctrl *gomock.Controller + recorder *MockNetterUDPMockRecorder + isgomock struct{} +} + +// MockNetterUDPMockRecorder is the mock recorder for MockNetterUDP. +type MockNetterUDPMockRecorder struct { + mock *MockNetterUDP +} + +// NewMockNetterUDP creates a new mock instance. +func NewMockNetterUDP(ctrl *gomock.Controller) *MockNetterUDP { + mock := &MockNetterUDP{ctrl: ctrl} + mock.recorder = &MockNetterUDPMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockNetterUDP) EXPECT() *MockNetterUDPMockRecorder { + return m.recorder +} + +// DialUDP mocks base method. +func (m *MockNetterUDP) DialUDP(network string, laddr, raddr *net.UDPAddr) (net_interface.UDPConnInterface, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DialUDP", network, laddr, raddr) + ret0, _ := ret[0].(net_interface.UDPConnInterface) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DialUDP indicates an expected call of DialUDP. +func (mr *MockNetterUDPMockRecorder) DialUDP(network, laddr, raddr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DialUDP", reflect.TypeOf((*MockNetterUDP)(nil).DialUDP), network, laddr, raddr) +} + +// ListenUDP mocks base method. +func (m *MockNetterUDP) ListenUDP(network string, laddr *net.UDPAddr) (net_interface.UDPConnInterface, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListenUDP", network, laddr) + ret0, _ := ret[0].(net_interface.UDPConnInterface) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListenUDP indicates an expected call of ListenUDP. +func (mr *MockNetterUDPMockRecorder) ListenUDP(network, laddr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenUDP", reflect.TypeOf((*MockNetterUDP)(nil).ListenUDP), network, laddr) +} + +// ResolveUDPAddr mocks base method. +func (m *MockNetterUDP) ResolveUDPAddr(network, address string) (*net.UDPAddr, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ResolveUDPAddr", network, address) + ret0, _ := ret[0].(*net.UDPAddr) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ResolveUDPAddr indicates an expected call of ResolveUDPAddr. +func (mr *MockNetterUDPMockRecorder) ResolveUDPAddr(network, address any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveUDPAddr", reflect.TypeOf((*MockNetterUDP)(nil).ResolveUDPAddr), network, address) +} + +// MockUDPConnInterface is a mock of UDPConnInterface interface. +type MockUDPConnInterface struct { + ctrl *gomock.Controller + recorder *MockUDPConnInterfaceMockRecorder + isgomock struct{} +} + +// MockUDPConnInterfaceMockRecorder is the mock recorder for MockUDPConnInterface. +type MockUDPConnInterfaceMockRecorder struct { + mock *MockUDPConnInterface +} + +// NewMockUDPConnInterface creates a new mock instance. +func NewMockUDPConnInterface(ctrl *gomock.Controller) *MockUDPConnInterface { + mock := &MockUDPConnInterface{ctrl: ctrl} + mock.recorder = &MockUDPConnInterfaceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUDPConnInterface) EXPECT() *MockUDPConnInterfaceMockRecorder { + return m.recorder +} + +// Close mocks base method. +func (m *MockUDPConnInterface) Close() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Close") + ret0, _ := ret[0].(error) + return ret0 +} + +// Close indicates an expected call of Close. +func (mr *MockUDPConnInterfaceMockRecorder) Close() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockUDPConnInterface)(nil).Close)) +} + +// File mocks base method. +func (m *MockUDPConnInterface) File() (*os.File, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "File") + ret0, _ := ret[0].(*os.File) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// File indicates an expected call of File. +func (mr *MockUDPConnInterfaceMockRecorder) File() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "File", reflect.TypeOf((*MockUDPConnInterface)(nil).File)) +} + +// LocalAddr mocks base method. +func (m *MockUDPConnInterface) LocalAddr() net.Addr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LocalAddr") + ret0, _ := ret[0].(net.Addr) + return ret0 +} + +// LocalAddr indicates an expected call of LocalAddr. +func (mr *MockUDPConnInterfaceMockRecorder) LocalAddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalAddr", reflect.TypeOf((*MockUDPConnInterface)(nil).LocalAddr)) +} + +// Read mocks base method. +func (m *MockUDPConnInterface) Read(b []byte) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Read", b) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Read indicates an expected call of Read. +func (mr *MockUDPConnInterfaceMockRecorder) Read(b any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockUDPConnInterface)(nil).Read), b) +} + +// ReadFrom mocks base method. +func (m *MockUDPConnInterface) ReadFrom(b []byte) (int, net.Addr, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReadFrom", b) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(net.Addr) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ReadFrom indicates an expected call of ReadFrom. +func (mr *MockUDPConnInterfaceMockRecorder) ReadFrom(b any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadFrom", reflect.TypeOf((*MockUDPConnInterface)(nil).ReadFrom), b) +} + +// ReadFromUDP mocks base method. +func (m *MockUDPConnInterface) ReadFromUDP(b []byte) (int, *net.UDPAddr, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReadFromUDP", b) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(*net.UDPAddr) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ReadFromUDP indicates an expected call of ReadFromUDP. +func (mr *MockUDPConnInterfaceMockRecorder) ReadFromUDP(b any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadFromUDP", reflect.TypeOf((*MockUDPConnInterface)(nil).ReadFromUDP), b) +} + +// ReadFromUDPAddrPort mocks base method. +func (m *MockUDPConnInterface) ReadFromUDPAddrPort(b []byte) (int, netip.AddrPort, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReadFromUDPAddrPort", b) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(netip.AddrPort) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ReadFromUDPAddrPort indicates an expected call of ReadFromUDPAddrPort. +func (mr *MockUDPConnInterfaceMockRecorder) ReadFromUDPAddrPort(b any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadFromUDPAddrPort", reflect.TypeOf((*MockUDPConnInterface)(nil).ReadFromUDPAddrPort), b) +} + +// ReadMsgUDP mocks base method. +func (m *MockUDPConnInterface) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReadMsgUDP", b, oob) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(int) + ret3, _ := ret[3].(*net.UDPAddr) + ret4, _ := ret[4].(error) + return ret0, ret1, ret2, ret3, ret4 +} + +// ReadMsgUDP indicates an expected call of ReadMsgUDP. +func (mr *MockUDPConnInterfaceMockRecorder) ReadMsgUDP(b, oob any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadMsgUDP", reflect.TypeOf((*MockUDPConnInterface)(nil).ReadMsgUDP), b, oob) +} + +// ReadMsgUDPAddrPort mocks base method. +func (m *MockUDPConnInterface) ReadMsgUDPAddrPort(b, oob []byte) (int, int, int, netip.AddrPort, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ReadMsgUDPAddrPort", b, oob) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(int) + ret3, _ := ret[3].(netip.AddrPort) + ret4, _ := ret[4].(error) + return ret0, ret1, ret2, ret3, ret4 +} + +// ReadMsgUDPAddrPort indicates an expected call of ReadMsgUDPAddrPort. +func (mr *MockUDPConnInterfaceMockRecorder) ReadMsgUDPAddrPort(b, oob any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadMsgUDPAddrPort", reflect.TypeOf((*MockUDPConnInterface)(nil).ReadMsgUDPAddrPort), b, oob) +} + +// RemoteAddr mocks base method. +func (m *MockUDPConnInterface) RemoteAddr() net.Addr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemoteAddr") + ret0, _ := ret[0].(net.Addr) + return ret0 +} + +// RemoteAddr indicates an expected call of RemoteAddr. +func (mr *MockUDPConnInterfaceMockRecorder) RemoteAddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoteAddr", reflect.TypeOf((*MockUDPConnInterface)(nil).RemoteAddr)) +} + +// SetDeadline mocks base method. +func (m *MockUDPConnInterface) SetDeadline(t time.Time) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetDeadline", t) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetDeadline indicates an expected call of SetDeadline. +func (mr *MockUDPConnInterfaceMockRecorder) SetDeadline(t any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDeadline", reflect.TypeOf((*MockUDPConnInterface)(nil).SetDeadline), t) +} + +// SetReadBuffer mocks base method. +func (m *MockUDPConnInterface) SetReadBuffer(bytes int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetReadBuffer", bytes) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetReadBuffer indicates an expected call of SetReadBuffer. +func (mr *MockUDPConnInterfaceMockRecorder) SetReadBuffer(bytes any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReadBuffer", reflect.TypeOf((*MockUDPConnInterface)(nil).SetReadBuffer), bytes) +} + +// SetReadDeadline mocks base method. +func (m *MockUDPConnInterface) SetReadDeadline(t time.Time) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetReadDeadline", t) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetReadDeadline indicates an expected call of SetReadDeadline. +func (mr *MockUDPConnInterfaceMockRecorder) SetReadDeadline(t any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReadDeadline", reflect.TypeOf((*MockUDPConnInterface)(nil).SetReadDeadline), t) +} + +// SetWriteBuffer mocks base method. +func (m *MockUDPConnInterface) SetWriteBuffer(bytes int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetWriteBuffer", bytes) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetWriteBuffer indicates an expected call of SetWriteBuffer. +func (mr *MockUDPConnInterfaceMockRecorder) SetWriteBuffer(bytes any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetWriteBuffer", reflect.TypeOf((*MockUDPConnInterface)(nil).SetWriteBuffer), bytes) +} + +// SetWriteDeadline mocks base method. +func (m *MockUDPConnInterface) SetWriteDeadline(t time.Time) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetWriteDeadline", t) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetWriteDeadline indicates an expected call of SetWriteDeadline. +func (mr *MockUDPConnInterfaceMockRecorder) SetWriteDeadline(t any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetWriteDeadline", reflect.TypeOf((*MockUDPConnInterface)(nil).SetWriteDeadline), t) +} + +// SyscallConn mocks base method. +func (m *MockUDPConnInterface) SyscallConn() (syscall.RawConn, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SyscallConn") + ret0, _ := ret[0].(syscall.RawConn) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SyscallConn indicates an expected call of SyscallConn. +func (mr *MockUDPConnInterfaceMockRecorder) SyscallConn() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SyscallConn", reflect.TypeOf((*MockUDPConnInterface)(nil).SyscallConn)) +} + +// Write mocks base method. +func (m *MockUDPConnInterface) Write(b []byte) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Write", b) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Write indicates an expected call of Write. +func (mr *MockUDPConnInterfaceMockRecorder) Write(b any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockUDPConnInterface)(nil).Write), b) +} + +// WriteMsgUDP mocks base method. +func (m *MockUDPConnInterface) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (int, int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WriteMsgUDP", b, oob, addr) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// WriteMsgUDP indicates an expected call of WriteMsgUDP. +func (mr *MockUDPConnInterfaceMockRecorder) WriteMsgUDP(b, oob, addr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteMsgUDP", reflect.TypeOf((*MockUDPConnInterface)(nil).WriteMsgUDP), b, oob, addr) +} + +// WriteMsgUDPAddrPort mocks base method. +func (m *MockUDPConnInterface) WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (int, int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WriteMsgUDPAddrPort", b, oob, addr) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// WriteMsgUDPAddrPort indicates an expected call of WriteMsgUDPAddrPort. +func (mr *MockUDPConnInterfaceMockRecorder) WriteMsgUDPAddrPort(b, oob, addr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteMsgUDPAddrPort", reflect.TypeOf((*MockUDPConnInterface)(nil).WriteMsgUDPAddrPort), b, oob, addr) +} + +// WriteTo mocks base method. +func (m *MockUDPConnInterface) WriteTo(b []byte, addr net.Addr) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WriteTo", b, addr) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// WriteTo indicates an expected call of WriteTo. +func (mr *MockUDPConnInterfaceMockRecorder) WriteTo(b, addr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteTo", reflect.TypeOf((*MockUDPConnInterface)(nil).WriteTo), b, addr) +} + +// WriteToUDP mocks base method. +func (m *MockUDPConnInterface) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WriteToUDP", b, addr) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// WriteToUDP indicates an expected call of WriteToUDP. +func (mr *MockUDPConnInterfaceMockRecorder) WriteToUDP(b, addr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteToUDP", reflect.TypeOf((*MockUDPConnInterface)(nil).WriteToUDP), b, addr) +} + +// WriteToUDPAddrPort mocks base method. +func (m *MockUDPConnInterface) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WriteToUDPAddrPort", b, addr) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// WriteToUDPAddrPort indicates an expected call of WriteToUDPAddrPort. +func (mr *MockUDPConnInterfaceMockRecorder) WriteToUDPAddrPort(b, addr any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteToUDPAddrPort", reflect.TypeOf((*MockUDPConnInterface)(nil).WriteToUDPAddrPort), b, addr) +} diff --git a/pkg/services/interfaces/net_interfaces.go b/pkg/services/interfaces/net_interfaces.go new file mode 100644 index 000000000..38bbc0243 --- /dev/null +++ b/pkg/services/interfaces/net_interfaces.go @@ -0,0 +1,41 @@ +package net_interface + +import ( + "net" + "net/netip" + "os" + "syscall" + "time" +) + +type NetterUDP interface { + ResolveUDPAddr(network string, address string) (*net.UDPAddr, error) + ListenUDP(network string, laddr *net.UDPAddr) (UDPConnInterface, error) + DialUDP(network string, laddr *net.UDPAddr, raddr *net.UDPAddr) (UDPConnInterface, error) +} + +// UDPConnInterface abstracts the methods of net.UDPConn used in the proxy. +type UDPConnInterface interface { + Close() error + File() (f *os.File, err error) + LocalAddr() net.Addr + Read(b []byte) (int, error) + ReadFrom(b []byte) (int, net.Addr, error) + ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) + ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) + ReadMsgUDP(b []byte, oob []byte) (n int, oobn int, flags int, addr *net.UDPAddr, err error) + ReadMsgUDPAddrPort(b []byte, oob []byte) (n int, oobn int, flags int, addr netip.AddrPort, err error) + RemoteAddr() net.Addr + SetDeadline(t time.Time) error + SetReadBuffer(bytes int) error + SetReadDeadline(t time.Time) error + SetWriteBuffer(bytes int) error + SetWriteDeadline(t time.Time) error + SyscallConn() (syscall.RawConn, error) + Write(b []byte) (int, error) + WriteMsgUDP(b []byte, oob []byte, addr *net.UDPAddr) (n int, oobn int, err error) + WriteMsgUDPAddrPort(b []byte, oob []byte, addr netip.AddrPort) (n int, oobn int, err error) + WriteTo(b []byte, addr net.Addr) (int, error) + WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) + WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) +} diff --git a/pkg/services/mock_services/udp_proxy.go b/pkg/services/mock_services/udp_proxy.go new file mode 100644 index 000000000..218681370 --- /dev/null +++ b/pkg/services/mock_services/udp_proxy.go @@ -0,0 +1,100 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: pkg/services/udp_proxy.go +// +// Generated by this command: +// +// mockgen -source=pkg/services/udp_proxy.go -destination=pkg/services/mock_services/udp_proxy.go +// + +// Package mock_services is a generated GoMock package. +package mock_services + +import ( + reflect "reflect" + + logger "github.com/ansible/receptor/pkg/logger" + netceptor "github.com/ansible/receptor/pkg/netceptor" + gomock "go.uber.org/mock/gomock" +) + +// MockNetcForUDPProxy is a mock of NetcForUDPProxy interface. +type MockNetcForUDPProxy struct { + ctrl *gomock.Controller + recorder *MockNetcForUDPProxyMockRecorder + isgomock struct{} +} + +// MockNetcForUDPProxyMockRecorder is the mock recorder for MockNetcForUDPProxy. +type MockNetcForUDPProxyMockRecorder struct { + mock *MockNetcForUDPProxy +} + +// NewMockNetcForUDPProxy creates a new mock instance. +func NewMockNetcForUDPProxy(ctrl *gomock.Controller) *MockNetcForUDPProxy { + mock := &MockNetcForUDPProxy{ctrl: ctrl} + mock.recorder = &MockNetcForUDPProxyMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockNetcForUDPProxy) EXPECT() *MockNetcForUDPProxyMockRecorder { + return m.recorder +} + +// GetLogger mocks base method. +func (m *MockNetcForUDPProxy) GetLogger() *logger.ReceptorLogger { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetLogger") + ret0, _ := ret[0].(*logger.ReceptorLogger) + return ret0 +} + +// GetLogger indicates an expected call of GetLogger. +func (mr *MockNetcForUDPProxyMockRecorder) GetLogger() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogger", reflect.TypeOf((*MockNetcForUDPProxy)(nil).GetLogger)) +} + +// ListenPacket mocks base method. +func (m *MockNetcForUDPProxy) ListenPacket(service string) (netceptor.PacketConner, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListenPacket", service) + ret0, _ := ret[0].(netceptor.PacketConner) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListenPacket indicates an expected call of ListenPacket. +func (mr *MockNetcForUDPProxyMockRecorder) ListenPacket(service any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenPacket", reflect.TypeOf((*MockNetcForUDPProxy)(nil).ListenPacket), service) +} + +// ListenPacketAndAdvertise mocks base method. +func (m *MockNetcForUDPProxy) ListenPacketAndAdvertise(service string, tags map[string]string) (netceptor.PacketConner, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListenPacketAndAdvertise", service, tags) + ret0, _ := ret[0].(netceptor.PacketConner) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListenPacketAndAdvertise indicates an expected call of ListenPacketAndAdvertise. +func (mr *MockNetcForUDPProxyMockRecorder) ListenPacketAndAdvertise(service, tags any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenPacketAndAdvertise", reflect.TypeOf((*MockNetcForUDPProxy)(nil).ListenPacketAndAdvertise), service, tags) +} + +// NewAddr mocks base method. +func (m *MockNetcForUDPProxy) NewAddr(node, service string) netceptor.Addr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewAddr", node, service) + ret0, _ := ret[0].(netceptor.Addr) + return ret0 +} + +// NewAddr indicates an expected call of NewAddr. +func (mr *MockNetcForUDPProxyMockRecorder) NewAddr(node, service any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewAddr", reflect.TypeOf((*MockNetcForUDPProxy)(nil).NewAddr), node, service) +} diff --git a/pkg/services/udp_proxy.go b/pkg/services/udp_proxy.go index 1a9b05094..a224e6926 100644 --- a/pkg/services/udp_proxy.go +++ b/pkg/services/udp_proxy.go @@ -6,23 +6,45 @@ import ( "github.com/ansible/receptor/pkg/logger" "github.com/ansible/receptor/pkg/netceptor" + net_interface "github.com/ansible/receptor/pkg/services/interfaces" "github.com/ansible/receptor/pkg/utils" "github.com/ghjm/cmdline" "github.com/spf13/viper" ) +type NetcForUDPProxy interface { + NewAddr(node string, service string) netceptor.Addr + ListenPacket(service string) (netceptor.PacketConner, error) + GetLogger() *logger.ReceptorLogger + ListenPacketAndAdvertise(service string, tags map[string]string) (netceptor.PacketConner, error) +} + +type Net struct{} + +func (n *Net) ResolveUDPAddr(network string, address string) (*net.UDPAddr, error) { + return net.ResolveUDPAddr(network, address) +} + +func (n *Net) ListenUDP(network string, laddr *net.UDPAddr) (net_interface.UDPConnInterface, error) { + return net.ListenUDP(network, laddr) +} + +func (n *Net) DialUDP(network string, laddr *net.UDPAddr, raddr *net.UDPAddr) (net_interface.UDPConnInterface, error) { + return net.DialUDP(network, laddr, raddr) +} + // UDPProxyServiceInbound listens on a UDP port and forwards packets to a remote Receptor service. -func UDPProxyServiceInbound(s *netceptor.Netceptor, host string, port int, node string, service string) error { +func UDPProxyServiceInbound(s NetcForUDPProxy, host string, port int, node string, service string, nett net_interface.NetterUDP) error { connMap := make(map[string]netceptor.PacketConner) buffer := make([]byte, utils.NormalBufferSize) addrStr := fmt.Sprintf("%s:%d", host, port) - udpAddr, err := net.ResolveUDPAddr("udp", addrStr) + udpAddr, err := nett.ResolveUDPAddr("udp", addrStr) if err != nil { return fmt.Errorf("could not resolve address %s", addrStr) } - uc, err := net.ListenUDP("udp", udpAddr) + uc, err := nett.ListenUDP("udp", udpAddr) if err != nil { return fmt.Errorf("error listening on UDP: %s", err) } @@ -33,7 +55,7 @@ func UDPProxyServiceInbound(s *netceptor.Netceptor, host string, port int, node for { n, addr, err := uc.ReadFrom(buffer) if err != nil { - s.Logger.Error("Error reading from UDP: %s\n", err) + s.GetLogger().Error("Error reading from UDP: %s\n", err) return } @@ -42,22 +64,22 @@ func UDPProxyServiceInbound(s *netceptor.Netceptor, host string, port int, node if !ok { pc, err = s.ListenPacket("") if err != nil { - s.Logger.Error("Error listening on Receptor network: %s\n", err) + s.GetLogger().Error("Error listening on Receptor network: %s\n", err) return } - s.Logger.Debug("Received new UDP connection from %s\n", raddrStr) + s.GetLogger().Debug("Received new UDP connection from %s\n", raddrStr) connMap[raddrStr] = pc - go runNetceptorToUDPInbound(pc, uc, addr, s.NewAddr(node, service), s.Logger) + go runNetceptorToUDPInbound(pc, uc, addr, s.NewAddr(node, service), s.GetLogger()) } wn, err := pc.WriteTo(buffer[:n], ncAddr) if err != nil { - s.Logger.Error("Error sending packet on Receptor network: %s\n", err) + s.GetLogger().Error("Error sending packet on Receptor network: %s\n", err) continue } if wn != n { - s.Logger.Debug("Not all bytes written on Receptor network\n") + s.GetLogger().Debug("Not all bytes written on Receptor network\n") continue } @@ -67,28 +89,28 @@ func UDPProxyServiceInbound(s *netceptor.Netceptor, host string, port int, node return nil } -func runNetceptorToUDPInbound(pc netceptor.PacketConner, uc *net.UDPConn, udpAddr net.Addr, expectedAddr netceptor.Addr, logger *logger.ReceptorLogger) { +func runNetceptorToUDPInbound(pc netceptor.PacketConner, uc net_interface.UDPConnInterface, udpAddr net.Addr, expectedAddr netceptor.Addr, logger *logger.ReceptorLogger) { buf := make([]byte, utils.NormalBufferSize) for { n, addr, err := pc.ReadFrom(buf) if err != nil { - logger.Error("Error reading from Receptor network: %s\n", err) + pc.GetLogger().Error("Error reading from Receptor network: %s\n", err) continue } if addr != expectedAddr { - logger.Debug("Received packet from unexpected source %s\n", addr) + pc.GetLogger().Debug("Received packet from unexpected source %s\n", addr) continue } wn, err := uc.WriteTo(buf[:n], udpAddr) if err != nil { - logger.Error("Error sending packet via UDP: %s\n", err) + pc.GetLogger().Error("Error sending packet via UDP: %s\n", err) continue } if wn != n { - logger.Debug("Not all bytes written via UDP\n") + pc.GetLogger().Debug("Not all bytes written via UDP\n") continue } @@ -96,10 +118,10 @@ func runNetceptorToUDPInbound(pc netceptor.PacketConner, uc *net.UDPConn, udpAdd } // UDPProxyServiceOutbound listens on the Receptor network and forwards packets via UDP. -func UDPProxyServiceOutbound(s *netceptor.Netceptor, service string, address string) error { - connMap := make(map[string]*net.UDPConn) +func UDPProxyServiceOutbound(s NetcForUDPProxy, service string, address string, nett net_interface.NetterUDP) error { + connMap := make(map[string]net_interface.UDPConnInterface) buffer := make([]byte, utils.NormalBufferSize) - udpAddr, err := net.ResolveUDPAddr("udp", address) + udpAddr, err := nett.ResolveUDPAddr("udp", address) if err != nil { return fmt.Errorf("could not resolve UDP address %s", address) } @@ -114,31 +136,31 @@ func UDPProxyServiceOutbound(s *netceptor.Netceptor, service string, address str for { n, addr, err := pc.ReadFrom(buffer) if err != nil { - s.Logger.Error("Error reading from Receptor network: %s\n", err) + s.GetLogger().Error("Error reading from Receptor network: %s\n", err) return } raddrStr := addr.String() uc, ok := connMap[raddrStr] if !ok { - uc, err = net.DialUDP("udp", nil, udpAddr) + uc, err = nett.DialUDP("udp", nil, udpAddr) if err != nil { - s.Logger.Error("Error connecting via UDP: %s\n", err) + s.GetLogger().Error("Error connecting via UDP: %s\n", err) return } - s.Logger.Debug("Opened new UDP connection to %s\n", raddrStr) + s.GetLogger().Debug("Opened new UDP connection to %s\n", raddrStr) connMap[raddrStr] = uc - go runUDPToNetceptorOutbound(uc, pc, addr, s.Logger) + go runUDPToNetceptorOutbound(uc, pc, addr, s.GetLogger()) } wn, err := uc.Write(buffer[:n]) if err != nil { - s.Logger.Error("Error writing to UDP: %s\n", err) + s.GetLogger().Error("Error writing to UDP: %s\n", err) continue } if wn != n { - s.Logger.Debug("Not all bytes written to UDP\n") + s.GetLogger().Debug("Not all bytes written to UDP\n") continue } @@ -148,23 +170,23 @@ func UDPProxyServiceOutbound(s *netceptor.Netceptor, service string, address str return nil } -func runUDPToNetceptorOutbound(uc *net.UDPConn, pc netceptor.PacketConner, addr net.Addr, logger *logger.ReceptorLogger) { +func runUDPToNetceptorOutbound(uc net_interface.UDPConnInterface, pc netceptor.PacketConner, addr net.Addr, logger *logger.ReceptorLogger) { buf := make([]byte, utils.NormalBufferSize) for { n, err := uc.Read(buf) if err != nil { - logger.Error("Error reading from UDP: %s\n", err) + pc.GetLogger().Error("Error reading from UDP: %s\n", err) return } wn, err := pc.WriteTo(buf[:n], addr) if err != nil { - logger.Error("Error writing to the Receptor network: %s\n", err) + pc.GetLogger().Error("Error writing to the Receptor network: %s\n", err) continue } if wn != n { - logger.Debug("Not all bytes written to the Netceptor network\n") + pc.GetLogger().Debug("Not all bytes written to the Netceptor network\n") continue } @@ -183,7 +205,7 @@ type UDPProxyInboundCfg struct { func (cfg UDPProxyInboundCfg) Run() error { netceptor.MainInstance.Logger.Debug("Running UDP inbound proxy service %v\n", cfg) - return UDPProxyServiceInbound(netceptor.MainInstance, cfg.BindAddr, cfg.Port, cfg.RemoteNode, cfg.RemoteService) + return UDPProxyServiceInbound(netceptor.MainInstance, cfg.BindAddr, cfg.Port, cfg.RemoteNode, cfg.RemoteService, &Net{}) } // udpProxyOutboundCfg is the cmdline configuration object for a UDP outbound proxy. @@ -196,7 +218,7 @@ type UDPProxyOutboundCfg struct { func (cfg UDPProxyOutboundCfg) Run() error { netceptor.MainInstance.Logger.Debug("Running UDP outbound proxy service %s\n", cfg) - return UDPProxyServiceOutbound(netceptor.MainInstance, cfg.Service, cfg.Address) + return UDPProxyServiceOutbound(netceptor.MainInstance, cfg.Service, cfg.Address, &Net{}) } func init() { From a90e939bff4ea5e76097ebbd0d5ca29f6e96397d Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 14 Nov 2024 14:43:51 +0000 Subject: [PATCH 2/6] Fix lint error --- pkg/services/interfaces/net_interfaces.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/services/interfaces/net_interfaces.go b/pkg/services/interfaces/net_interfaces.go index 38bbc0243..33f70dcfa 100644 --- a/pkg/services/interfaces/net_interfaces.go +++ b/pkg/services/interfaces/net_interfaces.go @@ -1,4 +1,4 @@ -package net_interface +package netinterface import ( "net" From dac9f0b2651d9259fcb592405f12b8de17eabbdd Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 21 Nov 2024 12:49:52 -0300 Subject: [PATCH 3/6] Add upd_proxy tests --- pkg/services/udp_proxy_test.go | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 pkg/services/udp_proxy_test.go diff --git a/pkg/services/udp_proxy_test.go b/pkg/services/udp_proxy_test.go new file mode 100644 index 000000000..e10da9dce --- /dev/null +++ b/pkg/services/udp_proxy_test.go @@ -0,0 +1,98 @@ +package services + +import ( + "errors" + "testing" + + "github.com/ansible/receptor/pkg/logger" + "github.com/ansible/receptor/pkg/netceptor" + "github.com/ansible/receptor/pkg/netceptor/mock_netceptor" + "github.com/ansible/receptor/pkg/services/mock_services" + + mock_net_interface "github.com/ansible/receptor/pkg/services/interfaces/mock_interfaces" + "go.uber.org/mock/gomock" +) + +func setUpMocks(ctrl *gomock.Controller) (*mock_services.MockNetcForUDPProxy, *mock_net_interface.MockNetterUDP, *mock_net_interface.MockUDPConnInterface, *mock_netceptor.MockPacketConner) { + mockNetceptor := mock_services.NewMockNetcForUDPProxy(ctrl) + mockNetter := mock_net_interface.NewMockNetterUDP(ctrl) + mockUDPConn := mock_net_interface.NewMockUDPConnInterface(ctrl) + mockPacketCon := mock_netceptor.NewMockPacketConner(ctrl) + logger := logger.NewReceptorLogger("") + mockNetceptor.EXPECT().GetLogger().AnyTimes().Return(logger) + + return mockNetceptor, mockNetter, mockUDPConn, mockPacketCon +} + +func TestUDPProxyServiceInbound(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockNetceptor, mockNetter, mockUDPConn, mockPacketCon := setUpMocks(ctrl) + type testCase struct { + name string + host string + port int + node string + service string + expectErr bool + calls func() + } + tests := []testCase{ + { + name: "Fail ResolveUDPAddr", + expectErr: true, + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, errors.New("RecolveUDPAddr error")) + }, + }, + { + name: "Fail ListenUDP", + expectErr: true, + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(nil, errors.New("Listen Udp Error")) + }, + }, + { + name: "Pass Case", + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(mockUDPConn, nil) + mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) + mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil) + mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketCon, nil).Times(1) + mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) + mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil).Times(1) + mockUDPConn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes() + mockPacketCon.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes() + mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, errors.New("Clean Up error")) + mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketCon, errors.New("Clean Up error")) + + }, + }, + // { + // name: "Fail UDP Con Read From", + // calls: func() { + // mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + // mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(mockUDPConn, nil) + // mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) + // mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")) + // }, + // }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + tc.calls() + err := UDPProxyServiceInbound(mockNetceptor, tc.host, tc.port, tc.node, tc.service, mockNetter) + if tc.expectErr { + if err == nil { + t.Errorf("net UDPProxyServiceInbound fail case error") + } + return + } + if err != nil { + t.Errorf("net UDPProxyServiceInbound error") + } + }) + } +} From 84706f5b9e5b1e650b4d67eeb42f2236026071b8 Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Tue, 26 Nov 2024 11:08:47 -0300 Subject: [PATCH 4/6] Update ProxyServieInbound test --- pkg/services/udp_proxy_test.go | 35 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/pkg/services/udp_proxy_test.go b/pkg/services/udp_proxy_test.go index e10da9dce..edf1674ad 100644 --- a/pkg/services/udp_proxy_test.go +++ b/pkg/services/udp_proxy_test.go @@ -27,7 +27,10 @@ func setUpMocks(ctrl *gomock.Controller) (*mock_services.MockNetcForUDPProxy, *m func TestUDPProxyServiceInbound(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockNetceptor, mockNetter, mockUDPConn, mockPacketCon := setUpMocks(ctrl) + var mockNetceptor *mock_services.MockNetcForUDPProxy + var mockNetter *mock_net_interface.MockNetterUDP + var mockUDPConn *mock_net_interface.MockUDPConnInterface + var mockPacketCon *mock_netceptor.MockPacketConner type testCase struct { name string host string @@ -54,34 +57,28 @@ func TestUDPProxyServiceInbound(t *testing.T) { }, }, { - name: "Pass Case", + name: "Fail UDP Con Read From", calls: func() { mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(mockUDPConn, nil) mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) - mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil) - mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketCon, nil).Times(1) + mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")).AnyTimes() + }, + }, + { + name: "Fail Netceptor listen packet", + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(mockUDPConn, nil) mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) - mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil).Times(1) - mockUDPConn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes() - mockPacketCon.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes() - mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, errors.New("Clean Up error")) - mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketCon, errors.New("Clean Up error")) - + mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil).AnyTimes() + mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketCon, errors.New("Clean Up error")).AnyTimes() }, }, - // { - // name: "Fail UDP Con Read From", - // calls: func() { - // mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) - // mockNetter.EXPECT().ListenUDP(gomock.Any(), gomock.Any()).Return(mockUDPConn, nil) - // mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Return(netceptor.Addr{}) - // mockUDPConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")) - // }, - // }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { + mockNetceptor, mockNetter, mockUDPConn, mockPacketCon = setUpMocks(ctrl) tc.calls() err := UDPProxyServiceInbound(mockNetceptor, tc.host, tc.port, tc.node, tc.service, mockNetter) if tc.expectErr { From ad4fa8f11051c4c418911fdc62effa92b6c00873 Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 28 Nov 2024 10:32:08 -0300 Subject: [PATCH 5/6] Add UDP Outbound tests --- pkg/services/udp_proxy_test.go | 80 +++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/pkg/services/udp_proxy_test.go b/pkg/services/udp_proxy_test.go index edf1674ad..91e408ec3 100644 --- a/pkg/services/udp_proxy_test.go +++ b/pkg/services/udp_proxy_test.go @@ -86,10 +86,86 @@ func TestUDPProxyServiceInbound(t *testing.T) { t.Errorf("net UDPProxyServiceInbound fail case error") } return + } else { + if err != nil { + t.Errorf("net UDPProxyServiceInbound error") + } } - if err != nil { - t.Errorf("net UDPProxyServiceInbound error") + }) + } +} + +func TestUDPProxyServiceOutbound(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + var mockNetceptor *mock_services.MockNetcForUDPProxy + var mockNetter *mock_net_interface.MockNetterUDP + var mockPacketCon *mock_netceptor.MockPacketConner + logger := logger.NewReceptorLogger("test") + type testCase struct { + name string + service string + address string + expectErr bool + calls func() + } + + tests := []testCase{ + { + name: "Fail ResolveUDPAddr", + expectErr: true, + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, errors.New("RecolveUDPAddr error")) + }, + }, + { + name: "Fail Listen And Advertive", + expectErr: true, + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetceptor.EXPECT().ListenPacketAndAdvertise(gomock.Any(), gomock.Any()).Return(nil, errors.New("Netceptor Listen Error")) + }, + }, + { + name: "Fail Read From", + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetceptor.EXPECT().ListenPacketAndAdvertise(gomock.Any(), gomock.Any()).Return(mockPacketCon, nil) + mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")) + }, + }, + { + name: "Fail Dial UDP", + calls: func() { + mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) + mockNetceptor.EXPECT().ListenPacketAndAdvertise(gomock.Any(), gomock.Any()).Return(mockPacketCon, nil) + gomock.InOrder( + mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil), + mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")).AnyTimes(), + ) + mockNetceptor.EXPECT().GetLogger().Return(logger).AnyTimes() + mockNetter.EXPECT().DialUDP(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("Dial UDP error")) + + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + mockNetceptor, mockNetter, _, mockPacketCon = setUpMocks(ctrl) + tc.calls() + err := UDPProxyServiceOutbound(mockNetceptor, tc.service, tc.address, mockNetter) + if tc.expectErr { + if err == nil { + t.Errorf("net UDPProxyServiceOutbound fail case error") + } + return + } else { + if err != nil { + t.Errorf("net UDPProxyServiceOutbound error") + } } }) } + } From 9cd5ea83a5f70ac42cd017a74230acfaf9bb0427 Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 28 Nov 2024 10:49:45 -0300 Subject: [PATCH 6/6] Update UDP outbound tests --- pkg/services/udp_proxy_test.go | 35 ++++++++-------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/pkg/services/udp_proxy_test.go b/pkg/services/udp_proxy_test.go index 91e408ec3..c88e3f92c 100644 --- a/pkg/services/udp_proxy_test.go +++ b/pkg/services/udp_proxy_test.go @@ -7,9 +7,8 @@ import ( "github.com/ansible/receptor/pkg/logger" "github.com/ansible/receptor/pkg/netceptor" "github.com/ansible/receptor/pkg/netceptor/mock_netceptor" - "github.com/ansible/receptor/pkg/services/mock_services" - mock_net_interface "github.com/ansible/receptor/pkg/services/interfaces/mock_interfaces" + "github.com/ansible/receptor/pkg/services/mock_services" "go.uber.org/mock/gomock" ) @@ -85,11 +84,10 @@ func TestUDPProxyServiceInbound(t *testing.T) { if err == nil { t.Errorf("net UDPProxyServiceInbound fail case error") } + return - } else { - if err != nil { - t.Errorf("net UDPProxyServiceInbound error") - } + } else if err != nil { + t.Errorf("net UDPProxyServiceInbound error") } }) } @@ -101,7 +99,6 @@ func TestUDPProxyServiceOutbound(t *testing.T) { var mockNetceptor *mock_services.MockNetcForUDPProxy var mockNetter *mock_net_interface.MockNetterUDP var mockPacketCon *mock_netceptor.MockPacketConner - logger := logger.NewReceptorLogger("test") type testCase struct { name string service string @@ -131,21 +128,7 @@ func TestUDPProxyServiceOutbound(t *testing.T) { calls: func() { mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) mockNetceptor.EXPECT().ListenPacketAndAdvertise(gomock.Any(), gomock.Any()).Return(mockPacketCon, nil) - mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")) - }, - }, - { - name: "Fail Dial UDP", - calls: func() { - mockNetter.EXPECT().ResolveUDPAddr(gomock.Any(), gomock.Any()).Return(nil, nil) - mockNetceptor.EXPECT().ListenPacketAndAdvertise(gomock.Any(), gomock.Any()).Return(mockPacketCon, nil) - gomock.InOrder( - mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, netceptor.Addr{}, nil), - mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")).AnyTimes(), - ) - mockNetceptor.EXPECT().GetLogger().Return(logger).AnyTimes() - mockNetter.EXPECT().DialUDP(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("Dial UDP error")) - + mockPacketCon.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, errors.New("Read From error")).AnyTimes() }, }, } @@ -159,13 +142,11 @@ func TestUDPProxyServiceOutbound(t *testing.T) { if err == nil { t.Errorf("net UDPProxyServiceOutbound fail case error") } + return - } else { - if err != nil { - t.Errorf("net UDPProxyServiceOutbound error") - } + } else if err != nil { + t.Errorf("net UDPProxyServiceOutbound error") } }) } - }