Skip to content

Commit

Permalink
Add UDP Outbound tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronH88 committed Nov 28, 2024
1 parent cdc26c4 commit 1e3f37e
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions pkg/services/udp_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,86 @@ func TestUDPProxyServiceInbound(t *testing.T) {
t.Errorf("net UDPProxyServiceInbound fail case error")
}
return

Check failure on line 88 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

return with no blank line before (nlreturn)
} else {

Check failure on line 89 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
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"))

},

Check failure on line 149 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

unnecessary trailing newline (whitespace)
},
}

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

Check failure on line 162 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

return with no blank line before (nlreturn)
} else {

Check failure on line 163 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
if err != nil {
t.Errorf("net UDPProxyServiceOutbound error")
}
}
})
}

}

Check failure on line 171 in pkg/services/udp_proxy_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

unnecessary trailing newline (whitespace)

0 comments on commit 1e3f37e

Please sign in to comment.