From 4f0e72b03ea4a7b73fa5c4639710ecc638a15b95 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 13 Nov 2019 19:28:52 +0300 Subject: [PATCH] Tests for RouteDescriptor --- pkg/routing/route_descriptor.go | 96 ++++++++++++++++++++++++++++ pkg/routing/route_descriptor_test.go | 31 +++++++++ pkg/routing/rule.go | 88 ------------------------- 3 files changed, 127 insertions(+), 88 deletions(-) create mode 100644 pkg/routing/route_descriptor.go create mode 100644 pkg/routing/route_descriptor_test.go diff --git a/pkg/routing/route_descriptor.go b/pkg/routing/route_descriptor.go new file mode 100644 index 0000000000..33e5906732 --- /dev/null +++ b/pkg/routing/route_descriptor.go @@ -0,0 +1,96 @@ +package routing + +import ( + "encoding/binary" + "fmt" + + "github.com/SkycoinProject/dmsg/cipher" +) + +// RouteDescriptor describes a route (from the perspective of the source and destination edges). +type RouteDescriptor [routeDescriptorSize]byte + +// NewRouteDescriptor returns a new RouteDescriptor. +func NewRouteDescriptor(srcPK, dstPK cipher.PubKey, srcPort, dstPort Port) RouteDescriptor { + var desc RouteDescriptor + + // p := copy(desc[:pkSize], srcPK[:]) + // p = copy(desc[p:p+pkSize], dstPK[:]) + // binary.BigEndian.PutUint16(desc[p:p+2], uint16(srcPort)) + // binary.BigEndian.PutUint16(desc[p+2:p+2*2], uint16(dstPort)) + desc.setSrcPK(srcPK) + desc.setDstPK(dstPK) + desc.setSrcPort(srcPort) + desc.setDstPort(dstPort) + + return desc +} + +// Src returns source Addr from RouteDescriptor. +func (rd RouteDescriptor) Src() Addr { + return Addr{ + PubKey: rd.SrcPK(), + Port: rd.SrcPort(), + } +} + +// Dst returns destination Addr from RouteDescriptor. +func (rd RouteDescriptor) Dst() Addr { + return Addr{ + PubKey: rd.DstPK(), + Port: rd.DstPort(), + } +} + +// SrcPK returns source public key from RouteDescriptor. +func (rd RouteDescriptor) SrcPK() cipher.PubKey { + var pk cipher.PubKey + copy(pk[:], rd[0:pkSize]) + return pk +} + +// setSrcPK sets source public key of a rule. +func (rd *RouteDescriptor) setSrcPK(pk cipher.PubKey) { + copy(rd[:pkSize], pk[:]) +} + +// DstPK returns destination public key from RouteDescriptor. +func (rd RouteDescriptor) DstPK() cipher.PubKey { + var pk cipher.PubKey + copy(pk[:], rd[pkSize:pkSize*2]) + return pk +} + +// setDstPK sets destination public key of a rule. +func (rd *RouteDescriptor) setDstPK(pk cipher.PubKey) { + copy(rd[pkSize:pkSize*2], pk[:]) +} + +// SrcPort returns source port from RouteDescriptor. +func (rd RouteDescriptor) SrcPort() Port { + return Port(binary.BigEndian.Uint16(rd[pkSize*2 : pkSize*2+2])) +} + +// setSrcPort sets source port of a rule. +func (rd *RouteDescriptor) setSrcPort(port Port) { + binary.BigEndian.PutUint16(rd[pkSize*2:pkSize*2+2], uint16(port)) +} + +// DstPort returns destination port from RouteDescriptor. +func (rd RouteDescriptor) DstPort() Port { + return Port(binary.BigEndian.Uint16(rd[pkSize*2+2 : pkSize*2+2*2])) +} + +// setDstPort sets destination port of a rule. +func (rd *RouteDescriptor) setDstPort(port Port) { + binary.BigEndian.PutUint16(rd[pkSize*2+2:pkSize*2+2*2], uint16(port)) +} + +// Invert inverts source and destination. +func (rd RouteDescriptor) Invert() RouteDescriptor { + return NewRouteDescriptor(rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPort()) +} + +func (rd RouteDescriptor) String() string { + return fmt.Sprintf("rPK:%s, lPK:%s, rPort:%d, lPort:%d", rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPK()) +} diff --git a/pkg/routing/route_descriptor_test.go b/pkg/routing/route_descriptor_test.go new file mode 100644 index 0000000000..61ac184bd8 --- /dev/null +++ b/pkg/routing/route_descriptor_test.go @@ -0,0 +1,31 @@ +package routing + +import ( + "testing" + + "github.com/SkycoinProject/dmsg/cipher" + "github.com/stretchr/testify/require" +) + +func TestRouteDescriptor(t *testing.T) { + pk1, _ := cipher.GenerateKeyPair() + pk2, _ := cipher.GenerateKeyPair() + port1 := Port(1) + port2 := Port(2) + + rd := NewRouteDescriptor(pk1, pk2, port1, port2) + + require.Len(t, rd, routeDescriptorSize) + require.Equal(t, pk1, rd.SrcPK()) + require.Equal(t, pk2, rd.DstPK()) + require.Equal(t, port1, rd.SrcPort()) + require.Equal(t, port2, rd.DstPort()) + + inverted := rd.Invert() + + require.Len(t, inverted, routeDescriptorSize) + require.Equal(t, pk2, inverted.SrcPK()) + require.Equal(t, pk1, inverted.DstPK()) + require.Equal(t, port2, inverted.SrcPort()) + require.Equal(t, port1, inverted.DstPort()) +} diff --git a/pkg/routing/rule.go b/pkg/routing/rule.go index fe5ddece7c..78ca9521de 100644 --- a/pkg/routing/rule.go +++ b/pkg/routing/rule.go @@ -241,94 +241,6 @@ func (r Rule) setDstPort(port Port) { } } -// RouteDescriptor describes a route (from the perspective of the source and destination edges). -type RouteDescriptor [routeDescriptorSize]byte - -// NewRouteDescriptor returns a new RouteDescriptor. -func NewRouteDescriptor(srcPK, dstPK cipher.PubKey, srcPort, dstPort Port) RouteDescriptor { - var desc RouteDescriptor - - p := copy(desc[:pkSize], srcPK[:]) - p = copy(desc[p:p+pkSize], dstPK[:]) - binary.BigEndian.PutUint16(desc[p:p+2], uint16(srcPort)) - binary.BigEndian.PutUint16(desc[p+2:p+2*2], uint16(dstPort)) - /*desc.setSrcPK(srcPK) - desc.setDstPK(dstPK) - desc.setSrcPort(srcPort) - desc.setDstPort(dstPort)*/ - - return desc -} - -// Src returns source Addr from RouteDescriptor. -func (rd RouteDescriptor) Src() Addr { - return Addr{ - PubKey: rd.SrcPK(), - Port: rd.SrcPort(), - } -} - -// Dst returns destination Addr from RouteDescriptor. -func (rd RouteDescriptor) Dst() Addr { - return Addr{ - PubKey: rd.DstPK(), - Port: rd.DstPort(), - } -} - -// SrcPK returns source public key from RouteDescriptor. -func (rd RouteDescriptor) SrcPK() cipher.PubKey { - var pk cipher.PubKey - copy(pk[:], rd[0:pkSize]) - return pk -} - -// setSrcPK sets source public key of a rule. -func (rd RouteDescriptor) setSrcPK(pk cipher.PubKey) { - copy(rd[:pkSize], pk[:]) -} - -// DstPK returns destination public key from RouteDescriptor. -func (rd RouteDescriptor) DstPK() cipher.PubKey { - var pk cipher.PubKey - copy(pk[:], rd[pkSize:pkSize*2]) - return pk -} - -// setDstPK sets destination public key of a rule. -func (rd RouteDescriptor) setDstPK(pk cipher.PubKey) { - copy(rd[pkSize:pkSize*2], pk[:]) -} - -// SrcPort returns source port from RouteDescriptor. -func (rd RouteDescriptor) SrcPort() Port { - return Port(binary.BigEndian.Uint16(rd[pkSize*2 : pkSize*2+2])) -} - -// setSrcPort sets source port of a rule. -func (rd RouteDescriptor) setSrcPort(port Port) { - binary.BigEndian.PutUint16(rd[pkSize*2:pkSize*2+2], uint16(port)) -} - -// DstPort returns destination port from RouteDescriptor. -func (rd RouteDescriptor) DstPort() Port { - return Port(binary.BigEndian.Uint16(rd[pkSize*2+2 : pkSize*2+2*2])) -} - -// setDstPort sets destination port of a rule. -func (rd RouteDescriptor) setDstPort(port Port) { - binary.BigEndian.PutUint16(rd[pkSize*2+2:pkSize*2+2*2], uint16(port)) -} - -// Invert inverts source and destination. -func (rd RouteDescriptor) Invert() RouteDescriptor { - return NewRouteDescriptor(rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPort()) -} - -func (rd RouteDescriptor) String() string { - return fmt.Sprintf("rPK:%s, lPK:%s, rPort:%d, lPort:%d", rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPK()) -} - // String returns rule's string representation. func (r Rule) String() string { switch t := r.Type(); t {