Skip to content

Commit

Permalink
Tests for RouteDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Nov 13, 2019
1 parent 68a9d30 commit 4f0e72b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 88 deletions.
96 changes: 96 additions & 0 deletions pkg/routing/route_descriptor.go
Original file line number Diff line number Diff line change
@@ -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())
}
31 changes: 31 additions & 0 deletions pkg/routing/route_descriptor_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
88 changes: 0 additions & 88 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4f0e72b

Please sign in to comment.