Skip to content

Commit

Permalink
Add traceroute test
Browse files Browse the repository at this point in the history
  • Loading branch information
matoval committed Aug 22, 2023
1 parent 2899f02 commit 7b34873
Showing 1 changed file with 72 additions and 12 deletions.
84 changes: 72 additions & 12 deletions pkg/netceptor/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,20 @@ func setupTest(t *testing.T) (*gomock.Controller, *mock_netceptor.MockNetcForPin
return ctrl, mockNetceptor, mockPacketConn, ctx
}

func teardownTest(t *testing.T, mockNetceptor *mock_netceptor.MockNetcForPing, mockPacketConn *mock_netceptor.MockPacketConner) {

Check warning on line 31 in pkg/netceptor/ping_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
mockPacketConn.EXPECT().SetHopsToLive(gomock.Any()).Times(0)
mockPacketConn.EXPECT().Close().Times(0)
mockNetceptor.EXPECT().NewAddr(gomock.Any(), gomock.Any()).Times(0)
mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Times(0)
mockPacketConn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Times(0)
mockNetceptor.EXPECT().Context().Times(0)
mockPacketConn.EXPECT().ReadFrom(gomock.Any()).Times(0)
mockPacketConn.EXPECT().SubscribeUnreachable(gomock.Any()).Times(0)
}

func TestListenSubscribeUnreachableErr(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

mockUnreachableMessage := netceptor.UnreachableMessage{
Expand Down Expand Up @@ -61,11 +72,14 @@ func TestListenSubscribeUnreachableErr(t *testing.T) {
if subscribeUnreachableError == nil {
t.Fatal("SubscribeUnreachable expected to return error but returned nil")
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestCreatePing(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -91,11 +105,14 @@ func TestCreatePing(t *testing.T) {
if nodeID == expectedNodeID {
t.Errorf("expected node ID %s, got %s", expectedNodeID, nodeID)
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestListenPacketErr(t *testing.T) {
ctrl, mockNetceptor, _, ctx := setupTest(t)
defer ctrl.Finish()
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -104,11 +121,14 @@ func TestListenPacketErr(t *testing.T) {
if listenPacketError == nil {
t.Fatal("ListenPacker expected to return error but returned nil")
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestReadFromErr(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -122,11 +142,14 @@ func TestReadFromErr(t *testing.T) {
if readFromError == nil {
t.Fatal("ReadFrom expected to return error but returned nil")
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestWriteToErr(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -141,11 +164,14 @@ func TestWriteToErr(t *testing.T) {
if writeToError == nil {
t.Fatal("ReadFrom expected to return error but returned nil")
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestTimeOutErr(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -161,11 +187,14 @@ func TestTimeOutErr(t *testing.T) {
if err.Error() != "timeout" {
t.Fatalf("Expected error to be 'timeout' but got %v", err)
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestUserCancel(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
Expand All @@ -174,7 +203,7 @@ func TestUserCancel(t *testing.T) {
mockPacketConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, nil).Times(1)
mockPacketConn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil)
mockNetceptor.EXPECT().Context().DoAndReturn(func() context.Context {
time.Sleep(time.Second * 1)
time.Sleep(time.Second * 2)

return context.Background()
}).Times(2)
Expand All @@ -187,36 +216,44 @@ func TestUserCancel(t *testing.T) {
if err.Error() != "user cancelled" {
t.Fatalf("Expected error to be 'user cancelled' but got %v", err)
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestNetceptorShutdown(t *testing.T) {
ctrl, mockNetceptor, mockPacketConn, ctx := setupTest(t)
defer ctrl.Finish()
// defer ctrl.Finish()
defer ctx.Done()

// Set up the mock behaviours
mockNetceptor.EXPECT().ListenPacket(gomock.Any()).Return(mockPacketConn, nil)
mockPacketConn.EXPECT().SubscribeUnreachable(gomock.Any()).Return(make(chan netceptor.UnreachableNotification))
mockPacketConn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil)
// mockPacketConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, nil).MaxTimes(1)
mockPacketConn.EXPECT().ReadFrom(gomock.Any()).Return(0, nil, nil).MaxTimes(1)
mockNetceptor.EXPECT().Context().DoAndReturn(func() context.Context {
newCtx, ctxCancel := context.WithCancel(context.Background())
ctxCancel()

return newCtx
}).Times(1)
}).Times(2)
time.Sleep(time.Second * 1)

_, _, err := netceptor.CreatePing(ctx, mockNetceptor, "target", 1)
if err.Error() != "netceptor shutdown" {
t.Fatalf("Expected error to be 'netceptor shutdown' but got %v", err)
}

teardownTest(t, mockNetceptor, mockPacketConn)
ctrl.Finish()
}

func TestCreateTraceroute(t *testing.T) {
ctrl := gomock.NewController(t)

mockNetceptor := mock_netceptor.NewMockNetcForTraceroute(ctrl)
ctx := context.Background()
defer ctx.Done()

mockNetceptor.EXPECT().Context().Return(context.Background())
mockNetceptor.EXPECT().MaxForwardingHops().Return(byte(1))
Expand All @@ -228,4 +265,27 @@ func TestCreateTraceroute(t *testing.T) {
t.Fatalf("Unexpected error %v", res.Err)
}
}

ctrl.Finish()
}

func TestCreateTracerouteError(t *testing.T) {
ctrl := gomock.NewController(t)

mockNetceptor := mock_netceptor.NewMockNetcForTraceroute(ctrl)
ctx := context.Background()
defer ctx.Done()

mockNetceptor.EXPECT().Context().Return(context.Background())
mockNetceptor.EXPECT().MaxForwardingHops().Return(byte(1))
mockNetceptor.EXPECT().Ping(ctx, "target", byte(0)).Return(time.Since(time.Now()), "target", errors.New("traceroute error"))

result := netceptor.CreateTraceroute(ctx, mockNetceptor, "target")
for res := range result {
if res.Err.Error() != "traceroute error" {
t.Fatalf("Expected error to be 'traceroute error' but got: %v", res.Err)
}
}

ctrl.Finish()
}

0 comments on commit 7b34873

Please sign in to comment.