starting delivery instruction testing

This commit is contained in:
Hayden Parker
2016-12-28 22:47:52 +00:00
parent 1b94d85a05
commit a0bbfac457
2 changed files with 61 additions and 4 deletions

View File

@ -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")

View File

@ -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)
}