From a0bbfac457d4b851162706e8840200e63a3e6f11 Mon Sep 17 00:00:00 2001 From: Hayden Parker Date: Wed, 28 Dec 2016 22:47:52 +0000 Subject: [PATCH] starting delivery instruction testing --- lib/tunnel/delivery.go | 9 +++--- lib/tunnel/delivery_test.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 lib/tunnel/delivery_test.go diff --git a/lib/tunnel/delivery.go b/lib/tunnel/delivery.go index 3616ceb..c733783 100644 --- a/lib/tunnel/delivery.go +++ b/lib/tunnel/delivery.go @@ -337,7 +337,7 @@ func (delivery_instructions DeliveryInstructions) Hash() (hash common.Hash, err err = errors.New("DeliveryInstructions is invalid, not contain enough data for hash given type DT_ROUTER") } } else { - err = errors.New("No Hash on DeliveryInstructions not of type DT_TUNNEL or DT_ROUTER") + //err = errors.New("No Hash on DeliveryInstructions not of type DT_TUNNEL or DT_ROUTER") } return } @@ -679,6 +679,10 @@ func maybeAppendExtendedOptions(di_flag DeliveryInstructions, data, current []by func maybeAppendSize(di_flag DeliveryInstructions, di_type int, data, current []byte) (now []byte, err error) { if di_type == FIRST_FRAGMENT { + if index, err := DeliveryInstructions(data).extended_options_index(); err != nil { + extended_options_length := common.Integer([]byte{data[index]}) + now = append(current, data[index+extended_options_length:index+extended_options_length+2]...) + } } else if di_type == FOLLOW_ON_FRAGMENT { if len(data) < 7 { err = errors.New("data is too short to contain size data") @@ -689,9 +693,6 @@ func maybeAppendSize(di_flag DeliveryInstructions, di_type int, data, current [] return } -//delivery_type, _ := di_flag.DeliveryType() -//has_tunnel_id, _ := di_flag.HasTunnelID() - func readDeliveryInstructions(data []byte) (instructions DeliveryInstructions, remainder []byte, err error) { if len(data) < 1 { err = errors.New("no data provided") diff --git a/lib/tunnel/delivery_test.go b/lib/tunnel/delivery_test.go new file mode 100644 index 0000000..e87e1a7 --- /dev/null +++ b/lib/tunnel/delivery_test.go @@ -0,0 +1,56 @@ +package tunnel + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +type DeliveryInstructionsFlags struct { + FirstFragment bool + Type byte + Delay bool +} + +func (dif DeliveryInstructionsFlags) FlagByte() byte { + flag := byte(0x00) + if !dif.FirstFragment { + flag |= 0x01 + } + flag |= dif.Type + if dif.Delay { + flag |= 0x10 + } + return byte(flag) +} + +func validFirstFragmentDeliveryInstructions() []byte { + data := []byte{} + + flag := DeliveryInstructionsFlags{ + FirstFragment: true, + Type: 0x02, + Delay: false, + }.FlagByte() + data = append(data, flag) + + tunnel_id := []byte{0x00, 0x00, 0x00, 0x01} + data = append(data, tunnel_id...) + + hash := make([]byte, 32) + data = append(data, hash...) + + // Add 0 delay + data = append(data, 0) + + message_id := []byte{0x00, 0x00, 0x00, 0x02} + data = append(data, message_id...) + + return data +} + +func TestReadDeliveryInstructions(t *testing.T) { + assert := assert.New(t) + + _, _, err := readDeliveryInstructions(validFirstFragmentDeliveryInstructions()) + assert.Nil(err) +}