From 5b7a985871e5e2daef8c08dc06abff552865a169 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Thu, 12 Dec 2019 23:55:42 +0300 Subject: [PATCH] Implement DialRoutes test for router --- pkg/router/router.go | 6 ++- pkg/router/router_test.go | 58 +++++++++++++++++++++++++++++ pkg/setup/node_test.go | 2 +- pkg/setup/setupclient/wrappers.go | 61 ++++++++++++++++++++++++++++++- 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/pkg/router/router.go b/pkg/router/router.go index efbe6809e..c07d8d0eb 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -46,6 +46,7 @@ type Config struct { TransportManager *transport.Manager RoutingTable routing.Table RouteFinder rfclient.Client + RouteGroupDialer setupclient.RouteGroupDialer SetupNodes []cipher.PubKey } @@ -54,6 +55,9 @@ func (c *Config) SetDefaults() { if c.Logger == nil { c.Logger = logging.MustGetLogger("router") } + if c.RouteGroupDialer == nil { + c.RouteGroupDialer = setupclient.NewSetupNodeDialer() + } } // DialOptions describes dial options. @@ -189,7 +193,7 @@ func (r *router) DialRoutes( Reverse: reversePath, } - rules, err := setupclient.DialRouteGroup(ctx, r.logger, r.n, r.conf.SetupNodes, req) + rules, err := r.conf.RouteGroupDialer.Dial(ctx, r.logger, r.n, r.conf.SetupNodes, req) if err != nil { r.logger.WithError(err).Error("Error dialing route group") return nil, err diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 034455fa7..829d486ca 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -19,6 +19,7 @@ import ( "github.com/SkycoinProject/skywire-mainnet/pkg/routefinder/rfclient" "github.com/SkycoinProject/skywire-mainnet/pkg/routing" + "github.com/SkycoinProject/skywire-mainnet/pkg/setup/setupclient" "github.com/SkycoinProject/skywire-mainnet/pkg/snet" "github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest" "github.com/SkycoinProject/skywire-mainnet/pkg/transport" @@ -40,6 +41,63 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func Test_router_DialRoutes(t *testing.T) { + // We are generating two key pairs - one for the a `Router`, the other to send packets to `Router`. + keys := snettest.GenKeyPairs(3) + + // create test env + nEnv := snettest.NewEnv(t, keys, []string{dmsg.Type}) + defer nEnv.Teardown() + + rEnv := NewTestEnv(t, nEnv.Nets) + defer rEnv.Teardown() + + // Create routers + r0Ifc, err := New(nEnv.Nets[0], rEnv.GenRouterConfig(0)) + require.NoError(t, err) + + r0, ok := r0Ifc.(*router) + require.True(t, ok) + + r1Ifc, err := New(nEnv.Nets[1], rEnv.GenRouterConfig(1)) + require.NoError(t, err) + + r1, ok := r1Ifc.(*router) + require.True(t, ok) + + r0.conf.RouteGroupDialer = setupclient.NewMockDialer() + r1.conf.RouteGroupDialer = setupclient.NewMockDialer() + + // prepare loop creation (client_1 will use this to request loop creation with setup node). + desc := routing.NewRouteDescriptor(r0.conf.PubKey, r1.conf.PubKey, 1, 1) + + forwardHops := []routing.Hop{ + {From: r0.conf.PubKey, To: r1.conf.PubKey, TpID: uuid.New()}, + } + + reverseHops := []routing.Hop{ + {From: r1.conf.PubKey, To: r0.conf.PubKey, TpID: uuid.New()}, + } + + route := routing.BidirectionalRoute{ + Desc: desc, + KeepAlive: 1 * time.Hour, + Forward: forwardHops, + Reverse: reverseHops, + } + + ctx := context.Background() + testLogger := logging.MustGetLogger("setupclient_test") + pks := []cipher.PubKey{r1.conf.PubKey} + + _, err = r0.conf.RouteGroupDialer.Dial(ctx, testLogger, nEnv.Nets[2], pks, route) + require.NoError(t, err) + rg, err := r0.DialRoutes(context.Background(), r0.conf.PubKey, 0, 0, nil) + + require.NoError(t, err) + require.NotNil(t, rg) +} + func Test_router_Introduce_AcceptRoutes(t *testing.T) { // We are generating two key pairs - one for the a `Router`, the other to send packets to `Router`. keys := snettest.GenKeyPairs(2) diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index b8eb7866e..588932cdc 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -240,7 +240,7 @@ func TestNode(t *testing.T) { testLogger := logging.MustGetLogger("setupclient_test") pks := []cipher.PubKey{clients[0].Addr.PK} - gotEdgeRules, err := setupclient.DialRouteGroup(ctx, testLogger, nEnv.Nets[1], pks, route) + gotEdgeRules, err := setupclient.NewSetupNodeDialer().Dial(ctx, testLogger, nEnv.Nets[1], pks, route) require.NoError(t, err) require.Equal(t, wantEdgeRules, gotEdgeRules) diff --git a/pkg/setup/setupclient/wrappers.go b/pkg/setup/setupclient/wrappers.go index 49237e72d..6d2816632 100644 --- a/pkg/setup/setupclient/wrappers.go +++ b/pkg/setup/setupclient/wrappers.go @@ -3,16 +3,37 @@ package setupclient import ( "context" "fmt" + "time" "github.com/SkycoinProject/dmsg/cipher" "github.com/SkycoinProject/skycoin/src/util/logging" + "github.com/google/uuid" "github.com/SkycoinProject/skywire-mainnet/pkg/routing" "github.com/SkycoinProject/skywire-mainnet/pkg/snet" + "github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest" ) -// DialRouteGroup is a wrapper for (*Client).DialRouteGroup. -func DialRouteGroup( +// RouteGroupDialer is an interface for RouteGroup dialers +type RouteGroupDialer interface { + Dial( + ctx context.Context, + log *logging.Logger, + n *snet.Network, + setupNodes []cipher.PubKey, + req routing.BidirectionalRoute, + ) (routing.EdgeRules, error) +} + +type setupNodeDialer struct{} + +// NewSetupNodeDialer returns a wrapper for (*Client).DialRouteGroup. +func NewSetupNodeDialer() RouteGroupDialer { + return new(setupNodeDialer) +} + +// Dial dials RouteGroup. +func (d *setupNodeDialer) Dial( ctx context.Context, log *logging.Logger, n *snet.Network, @@ -37,3 +58,39 @@ func DialRouteGroup( return resp, nil } + +type mockDialer struct{} + +// NewMockDialer returns a mock for (*Client).DialRouteGroup. +func NewMockDialer() RouteGroupDialer { + return new(mockDialer) +} + +// Dial dials RouteGroup. +func (d *mockDialer) Dial( + context.Context, + *logging.Logger, + *snet.Network, + []cipher.PubKey, + routing.BidirectionalRoute, +) (routing.EdgeRules, error) { + keys := snettest.GenKeyPairs(2) + + srcPK, _ := cipher.GenerateKeyPair() + dstPK, _ := cipher.GenerateKeyPair() + + var srcPort, dstPort routing.Port = 1, 2 + + desc := routing.NewRouteDescriptor(srcPK, dstPK, srcPort, dstPort) + + fwdRule := routing.ForwardRule(1*time.Hour, 1, routing.RouteID(3), uuid.UUID{}, keys[0].PK, keys[1].PK, 4, 5) + cnsmRule := routing.ConsumeRule(1*time.Hour, 2, keys[1].PK, keys[0].PK, 5, 4) + + rules := routing.EdgeRules{ + Desc: desc, + Forward: fwdRule, + Reverse: cnsmRule, + } + + return rules, nil +}