From 66128786d63cbe277d9747275663f711b7242d2e Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Thu, 12 Dec 2019 21:42:00 +0300 Subject: [PATCH] Implement tests for Close and KeepAlive packet types --- pkg/router/router_test.go | 109 +++++++++++++++----------------------- 1 file changed, 44 insertions(+), 65 deletions(-) diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 2df277ea2..fcff49c22 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -239,71 +239,50 @@ func TestRouter_Serve(t *testing.T) { assert.Equal(t, dstRtIDs[1], recvPacket.RouteID()) }) - // TODO(evanlinjin): I'm having so much trouble with this I officially give up. - // t.Run("handlePacket_appRule", func(t *testing.T) { - // const duration = 10 * time.Second - // // time.AfterFunc(duration, func() { - // // panic("timeout") - // // }) - // - // defer clearRules(r0, r1) - // - // // prepare mock-app - // localPort := routing.Port(9) - // cConn, sConn := net.Pipe() - // - // // mock-app config - // // appConf := &app.Config{ - // // AppName: "test_app", - // // AppVersion: "1.0", - // // ProtocolVersion: supportedProtocolVersion, - // // } - // - // // serve mock-app - // // sErrCh := make(chan error, 1) - // go func() { - // // sErrCh <- r0.ServeApp(sConn, localPort, appConf) - // _ = r0.ServeApp(sConn, localPort, appConf) - // // close(sErrCh) - // }() - // // defer func() { - // // assert.NoError(t, cConn.Close()) - // // assert.NoError(t, <-sErrCh) - // // }() - // - // // a, err := app.New(cConn, appConf) - // // require.NoError(t, err) - // // cErrCh := make(chan error, 1) - // go func() { - // conn, err := a.Accept() - // if err == nil { - // fmt.Println("ACCEPTED:", conn.RemoteAddr()) - // } - // fmt.Println("FAILED TO ACCEPT") - // // cErrCh <- err - // // close(cErrCh) - // }() - // a.Dial(a.Addr().(routing.Addr)) - // // defer func() { - // // assert.NoError(t, <-cErrCh) - // // }() - // - // // Add a APP rule for r0. - // // port8 := routing.Port(8) - // appRtID := routing.RouteID(7) - // appRule := routing.ConsumeRule(10*time.Minute, appRtID, keys[1].PK, localPort, localPort) - // require.NoError(t, r0.rt.SaveRule(appRule)) - // - // // Call handleTransportPacket for r0. - // - // // payload is prepended with two bytes to satisfy app.Proto. - // // payload[0] = frame type, payload[1] = id - // rAddr := routing.Addr{PubKey: keys[1].PK, Port: localPort} - // rawRAddr, _ := json.Marshal(rAddr) - // // payload := append([]byte{byte(app.FrameClose), 0}, rawRAddr...) - // packet := routing.MakeDataPacket(appRtID, rawRAddr) - // require.NoError(t, r0.handleTransportPacket(context.TODO(), packet)) - // }) + t.Run("handlePacket_close", func(t *testing.T) { + defer clearRules(r0, r1) + defer clearRouteGroups(r0, r1) + + rtIDs, err := r0.ReserveKeys(1) + require.NoError(t, err) + + cnsmRule := routing.ConsumeRule(1*time.Hour, rtIDs[0], keys[1].PK, keys[0].PK, 0, 0) + err = r0.rt.SaveRule(cnsmRule) + require.NoError(t, err) + require.Len(t, r0.rt.AllRules(), 1) + + packet := routing.MakeClosePacket(rtIDs[0], routing.CloseRequested) + require.NoError(t, r0.handleTransportPacket(context.TODO(), packet)) + + require.Len(t, r0.rt.AllRules(), 0) + }) + + t.Run("handlePacket_keepalive", func(t *testing.T) { + defer clearRules(r0, r1) + defer clearRouteGroups(r0, r1) + + rtIDs, err := r0.ReserveKeys(1) + require.NoError(t, err) + + rtID := rtIDs[0] + + cnsmRule := routing.ConsumeRule(100*time.Millisecond, rtID, keys[1].PK, keys[0].PK, 0, 0) + err = r0.rt.SaveRule(cnsmRule) + require.NoError(t, err) + require.Len(t, r0.rt.AllRules(), 1) + + time.Sleep(50 * time.Millisecond) + + packet := routing.MakeKeepAlivePacket(rtIDs[0]) + require.NoError(t, r0.handleTransportPacket(context.TODO(), packet)) + + require.Len(t, r0.rt.AllRules(), 1) + time.Sleep(50 * time.Millisecond) + require.Len(t, r0.rt.AllRules(), 1) + + time.Sleep(100 * time.Millisecond) + require.Len(t, r0.rt.AllRules(), 0) + }) } // TODO (Darkren): fix tests