From 1e3f37eb14d38253d6e7d275fb5549b71a4e0b44 Mon Sep 17 00:00:00 2001 From: AaronH88 Date: Thu, 28 Nov 2024 10:32:08 -0300 Subject: [PATCH] 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") + } } }) } + }