Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 9, 2019
1 parent 8595ca9 commit b920770
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 45 deletions.
6 changes: 3 additions & 3 deletions pkg/router/managed_routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
func TestManagedRoutingTableCleanup(t *testing.T) {
rt := manageRoutingTable(routing.InMemoryRoutingTable())

_, err := rt.AddRule(routing.ForwardRule(time.Now().Add(time.Hour), 3, uuid.New()))
_, err := rt.AddRule(routing.ForwardRule(time.Now().Add(time.Hour), 3, uuid.New(), 1))
require.NoError(t, err)

id, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New()))
id, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New(), 2))
require.NoError(t, err)

id2, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New()))
id2, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New(), 3))
require.NoError(t, err)

assert.Equal(t, 3, rt.Count())
Expand Down
32 changes: 20 additions & 12 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ func (rm *routeManager) Serve(rw io.ReadWriter) error {
var respBody interface{}
switch t {
case setup.PacketAddRules:
respBody, err = rm.addRoutingRules(body)
err = rm.setRoutingRules(body)
case setup.PacketDeleteRules:
respBody, err = rm.deleteRoutingRules(body)
case setup.PacketConfirmLoop:
err = rm.confirmLoop(body)
case setup.PacketLoopClosed:
err = rm.loopClosed(body)
case setup.PacketRequestRouteID:
respBody, err = rm.bookRouteID()
default:
err = errors.New("unknown foundation packet")
}
Expand All @@ -101,27 +103,24 @@ func (rm *routeManager) Serve(rw io.ReadWriter) error {
}

return proto.WritePacket(setup.RespSuccess, respBody)

}

func (rm *routeManager) addRoutingRules(data []byte) ([]routing.RouteID, error) {
func (rm *routeManager) setRoutingRules(data []byte) error {
var rules []routing.Rule
if err := json.Unmarshal(data, &rules); err != nil {
return nil, err
return err
}

res := make([]routing.RouteID, len(rules))
for idx, rule := range rules {
routeID, err := rm.rt.AddRule(rule)
if err != nil {
return nil, fmt.Errorf("routing table: %s", err)
for _, rule := range rules {
routeID := rule.RegistrationID()
if err := rm.rt.SetRule(routeID, rule); err != nil {
return fmt.Errorf("routing table: %s", err)
}

res[idx] = routeID
rm.Logger.Infof("Added new Routing Rule with ID %d %s", routeID, rule)
rm.Logger.Infof("Set new Routing Rule with ID %d %s", routeID, rule)
}

return res, nil
return nil
}

func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, error) {
Expand Down Expand Up @@ -197,3 +196,12 @@ func (rm *routeManager) loopClosed(data []byte) error {

return rm.callbacks.LoopClosed(ld.Loop)
}

func (rm *routeManager) bookRouteID() ([]routing.RouteID, error) {
routeID, err := rm.rt.AddRule(nil)
if err != nil {
return nil, err
}

return []routing.RouteID{routeID}, nil
}
27 changes: 16 additions & 11 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestRouteManagerGetRule(t *testing.T) {
rt := manageRoutingTable(routing.InMemoryRoutingTable())
rm := &routeManager{logging.MustGetLogger("routesetup"), rt, nil}

expiredRule := routing.ForwardRule(time.Now().Add(-10*time.Minute), 3, uuid.New())
expiredRule := routing.ForwardRule(time.Now().Add(-10*time.Minute), 3, uuid.New(), 1)
expiredID, err := rt.AddRule(expiredRule)
require.NoError(t, err)

rule := routing.ForwardRule(time.Now().Add(10*time.Minute), 3, uuid.New())
rule := routing.ForwardRule(time.Now().Add(10*time.Minute), 3, uuid.New(), 2)
id, err := rt.AddRule(rule)
require.NoError(t, err)

Expand All @@ -43,7 +43,7 @@ func TestRouteManagerRemoveLoopRule(t *testing.T) {
rm := &routeManager{logging.MustGetLogger("routesetup"), rt, nil}

pk, _ := cipher.GenerateKeyPair()
rule := routing.AppRule(time.Now(), 3, pk, 3, 2)
rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 1)
_, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -73,15 +73,19 @@ func TestRouteManagerAddRemoveRule(t *testing.T) {
rm := &routeManager{logging.MustGetLogger("routesetup"), rt, nil}

in, out := net.Pipe()
errCh := make(chan error)
errCh := make(chan error, 2)
go func() {
errCh <- rm.Serve(out)
errCh <- rm.Serve(out)
}()

proto := setup.NewSetupProtocol(in)

rule := routing.ForwardRule(time.Now(), 3, uuid.New())
id, err := setup.AddRule(proto, rule)
id, err := setup.RequestRouteID(proto)
require.NoError(t, err)

rule := routing.ForwardRule(time.Now(), 3, uuid.New(), id)
err = setup.AddRule(proto, rule)
require.NoError(t, err)
assert.Equal(t, routing.RouteID(1), id)

Expand All @@ -92,6 +96,7 @@ func TestRouteManagerAddRemoveRule(t *testing.T) {

require.NoError(t, in.Close())
require.NoError(t, <-errCh)
require.NoError(t, <-errCh)
}

func TestRouteManagerDeleteRules(t *testing.T) {
Expand All @@ -106,7 +111,7 @@ func TestRouteManagerDeleteRules(t *testing.T) {

proto := setup.NewSetupProtocol(in)

rule := routing.ForwardRule(time.Now(), 3, uuid.New())
rule := routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
id, err := rt.AddRule(rule)
require.NoError(t, err)
assert.Equal(t, 1, rt.Count())
Expand Down Expand Up @@ -139,10 +144,10 @@ func TestRouteManagerConfirmLoop(t *testing.T) {

proto := setup.NewSetupProtocol(in)
pk, _ := cipher.GenerateKeyPair()
rule := routing.AppRule(time.Now(), 3, pk, 3, 2)
rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 2)
require.NoError(t, rt.SetRule(2, rule))

rule = routing.ForwardRule(time.Now(), 3, uuid.New())
rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
require.NoError(t, rt.SetRule(1, rule))

ld := routing.LoopData{
Expand Down Expand Up @@ -189,10 +194,10 @@ func TestRouteManagerLoopClosed(t *testing.T) {

pk, _ := cipher.GenerateKeyPair()

rule := routing.AppRule(time.Now(), 3, pk, 3, 2)
rule := routing.AppRule(time.Now(), 3, pk, 3, 2, 0)
require.NoError(t, rt.SetRule(2, rule))

rule = routing.ForwardRule(time.Now(), 3, uuid.New())
rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
require.NoError(t, rt.SetRule(1, rule))

ld := routing.LoopData{
Expand Down
25 changes: 17 additions & 8 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestRouterForwarding(t *testing.T) {
tr3, err := m3.CreateDataTransport(context.TODO(), pk2, "mock2", true)
require.NoError(t, err)

rule := routing.ForwardRule(time.Now().Add(time.Hour), 4, tr3.Entry.ID)
rule := routing.ForwardRule(time.Now().Add(time.Hour), 4, tr3.Entry.ID, 1)
routeID, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -207,7 +207,7 @@ func TestRouterApp(t *testing.T) {
tr, err := m1.CreateDataTransport(context.TODO(), pk2, "mock", true)
require.NoError(t, err)

rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk2, 5, 6)
rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk2, 5, 6, 1)
routeID, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -397,7 +397,10 @@ func TestRouterSetup(t *testing.T) {

var routeID routing.RouteID
t.Run("add rule", func(t *testing.T) {
routeID, err = setup.AddRule(sProto, routing.ForwardRule(time.Now().Add(time.Hour), 2, trID))
routeID, err := setup.RequestRouteID(sProto)
require.NoError(t, err)

err = setup.AddRule(sProto, routing.ForwardRule(time.Now().Add(time.Hour), 2, trID, routeID))
require.NoError(t, err)

rule, err := rt.Rule(routeID)
Expand All @@ -407,7 +410,10 @@ func TestRouterSetup(t *testing.T) {
})

t.Run("confirm loop - responder", func(t *testing.T) {
appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2))
appRouteID, err := setup.RequestRouteID(sProto)
require.NoError(t, err)

err = setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2, appRouteID))
require.NoError(t, err)

err = setup.ConfirmLoop(sProto, routing.LoopData{
Expand Down Expand Up @@ -449,7 +455,10 @@ func TestRouterSetup(t *testing.T) {

require.NoError(t, r.pm.SetLoop(4, routing.Addr{PubKey: pk2, Port: 3}, &loop{}))

appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 3, 4))
appRouteID, err := setup.RequestRouteID(sProto)
require.NoError(t, err)

err = setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 3, 4, appRouteID))
require.NoError(t, err)

err = setup.ConfirmLoop(sProto, routing.LoopData{
Expand Down Expand Up @@ -677,7 +686,7 @@ func TestRouterCloseLoop(t *testing.T) {
}()

rt := routing.InMemoryRoutingTable()
rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5)
rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5, 1)
routeID, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -780,7 +789,7 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) {
}()

rt := routing.InMemoryRoutingTable()
rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5)
rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5, 1)
routeID, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -865,7 +874,7 @@ func TestRouterRouteExpiration(t *testing.T) {
require.NoError(t, err)

rt := routing.InMemoryRoutingTable()
_, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5))
_, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5, 1))
require.NoError(t, err)
assert.Equal(t, 1, rt.Count())

Expand Down
4 changes: 2 additions & 2 deletions pkg/routing/routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
func RoutingTableSuite(t *testing.T, tbl Table) {
t.Helper()

rule := ForwardRule(time.Now(), 2, uuid.New())
rule := ForwardRule(time.Now(), 2, uuid.New(), 1)
id, err := tbl.AddRule(rule)
require.NoError(t, err)

Expand All @@ -39,7 +39,7 @@ func RoutingTableSuite(t *testing.T, tbl Table) {
require.NoError(t, err)
assert.Equal(t, rule, r)

rule2 := ForwardRule(time.Now(), 3, uuid.New())
rule2 := ForwardRule(time.Now(), 3, uuid.New(), 2)
id2, err := tbl.AddRule(rule2)
require.NoError(t, err)

Expand Down
6 changes: 3 additions & 3 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r Rule) TransportID() uuid.UUID {
if r.Type() != RuleForward {
panic("invalid rule")
}
return uuid.Must(uuid.FromBytes(r[13:]))
return uuid.Must(uuid.FromBytes(r[13:29]))
}

// RemotePK returns remove PK for an app rule.
Expand Down Expand Up @@ -183,7 +183,7 @@ func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remo
rule[8] = byte(RuleApp)
binary.BigEndian.PutUint32(rule[9:], uint32(respRoute))
rule = append(rule, remotePK[:]...)
rule = append(rule, 0, 0, 0, 0)
rule = append(rule, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint16(rule[46:], uint16(remotePort))
binary.BigEndian.PutUint16(rule[48:], uint16(localPort))
binary.BigEndian.PutUint32(rule[50:], uint32(registrationID))
Expand All @@ -202,7 +202,7 @@ func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID, regi
rule[8] = byte(RuleForward)
binary.BigEndian.PutUint32(rule[9:], uint32(nextRoute))
rule = append(rule, nextTrID[:]...)
rule = append(rule, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
rule = append(rule, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint32(rule[50:], uint32(registrationID))
return Rule(rule)
}
4 changes: 2 additions & 2 deletions pkg/routing/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestAppRule(t *testing.T) {
expireAt := time.Now().Add(2 * time.Minute)
pk, _ := cipher.GenerateKeyPair()
rule := AppRule(expireAt, 2, pk, 3, 4)
rule := AppRule(expireAt, 2, pk, 3, 4, 1)

assert.Equal(t, expireAt.Unix(), rule.Expiry().Unix())
assert.Equal(t, RuleApp, rule.Type())
Expand All @@ -28,7 +28,7 @@ func TestAppRule(t *testing.T) {
func TestForwardRule(t *testing.T) {
trID := uuid.New()
expireAt := time.Now().Add(2 * time.Minute)
rule := ForwardRule(expireAt, 2, trID)
rule := ForwardRule(expireAt, 2, trID, 1)

assert.Equal(t, expireAt.Unix(), rule.Expiry().Unix())
assert.Equal(t, RuleForward, rule.Type())
Expand Down
2 changes: 2 additions & 0 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func (sp PacketType) String() string {
return "Success"
case RespFailure:
return "Failure"
case PacketRequestRouteID:
return "RequestRouteID"
}
return fmt.Sprintf("Unknown(%d)", sp)
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,22 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
}
lp := routing.Port(binary.BigEndian.Uint16(lpRaw[:]))
rp := routing.Port(binary.BigEndian.Uint16(rpRaw[:]))
fwdRule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New())
fwdRID, err := rt.AddRule(fwdRule)
fwdRID, err := rt.AddRule(nil)
if err != nil {
panic(err)
}
appRule := routing.AppRule(ruleExp, fwdRID, remotePK, rp, lp)
appRID, err := rt.AddRule(appRule)
fwdRule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New(), fwdRID)
if err := rt.SetRule(fwdRID, fwdRule); err != nil {
panic(err)
}
appRID, err := rt.AddRule(nil)
if err != nil {
panic(err)
}
appRule := routing.AppRule(ruleExp, fwdRID, remotePK, rp, lp, appRID)
if err := rt.SetRule(appRID, appRule); err != nil {
panic(err)
}
log.Infof("rt[%2da]: %v %v", i, fwdRID, fwdRule.Summary().ForwardFields)
log.Infof("rt[%2db]: %v %v", i, appRID, appRule.Summary().AppFields)
}
Expand Down

0 comments on commit b920770

Please sign in to comment.