Skip to content

Commit

Permalink
Implement DialRoutes test for router
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 12, 2019
1 parent b0115ce commit 5b7a985
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
6 changes: 5 additions & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Config struct {
TransportManager *transport.Manager
RoutingTable routing.Table
RouteFinder rfclient.Client
RouteGroupDialer setupclient.RouteGroupDialer
SetupNodes []cipher.PubKey
}

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
61 changes: 59 additions & 2 deletions pkg/setup/setupclient/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

0 comments on commit 5b7a985

Please sign in to comment.