testing router info options

This commit is contained in:
Hayden Parker
2016-07-02 17:22:14 -07:00
parent 712f0ca0ae
commit c12a16109c
2 changed files with 75 additions and 19 deletions

View File

@ -204,14 +204,41 @@ func (router_info RouterInfo) Signature() (signature Signature) {
// Used during parsing to determine where in the RouterInfo the Mapping data begins.
//
func (router_info RouterInfo) optionsLocation() (location int) {
location = 9
var router_address RouterAddress
remaining := router_info[9:]
addr_count, _ := router_info.RouterAddressCount()
for i := 0; i < addr_count; i++ {
router_address, remaining, _ = ReadRouterAddress(remaining)
location += len(router_address)
data, remainder, err := ReadRouterIdentity(router_info)
if err != nil {
return
}
location += len(data)
remainder_len := len(remainder)
if remainder_len < 9 {
log.WithFields(log.Fields{
"at": "(RouterInfo) optionsLocation",
"data_len": remainder_len,
"required_len": 9,
"reason": "not enough data",
}).Error("error parsing router info")
err = errors.New("error parsing router addresses: not enough data")
return
}
location += 9
remaining := remainder[9:]
var router_address RouterAddress
var router_addresses []RouterAddress
addr_count, cerr := router_info.RouterAddressCount()
if cerr != nil {
err = cerr
return
}
for i := 0; i < addr_count; i++ {
router_address, remaining, err = ReadRouterAddress(remaining)
if err == nil {
location += len(router_address)
router_addresses = append(router_addresses, router_address)
}
}
location += 1
return
}
@ -220,6 +247,6 @@ func (router_info RouterInfo) optionsLocation() (location int) {
//
func (router_info RouterInfo) optionsSize() (size int) {
head := router_info.optionsLocation()
size = Integer(router_info[head:head+1]) + 1
size = Integer(router_info[head:head+2]) + 2
return
}

View File

@ -6,26 +6,39 @@ import (
"testing"
)
func buildRouterIdentity() RouterIdentity {
router_ident_data := make([]byte, 128+256)
router_ident_data = append(router_ident_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
return RouterIdentity(router_ident_data)
}
func buildDate() []byte {
date_data := []byte{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
return date_data
}
func mappingData() Mapping {
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
return mapping
}
func buildRouterAddress() RouterAddress {
router_address_bytes := []byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
str, _ := ToI2PString("foo")
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
router_address_bytes = append(router_address_bytes, []byte(str)...)
router_address_bytes = append(router_address_bytes, mapping...)
router_address_bytes = append(router_address_bytes, mappingData()...)
return RouterAddress(router_address_bytes)
}
func buildFullRouterInfo() RouterInfo {
router_info_data := make([]byte, 0)
router_ident_data := make([]byte, 128+256)
router_ident_data = append(router_ident_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
router_info_data = append(router_info_data, router_ident_data...)
date_data := []byte{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
router_info_data = append(router_info_data, date_data...)
router_info_data = append(router_info_data, buildRouterIdentity()...)
router_info_data = append(router_info_data, buildDate()...)
router_info_data = append(router_info_data, 0x01)
router_info_data = append(router_info_data, []byte(buildRouterAddress())...)
router_info_data = append(router_info_data, buildRouterAddress()...)
router_info_data = append(router_info_data, 0x00)
router_info_data = append(router_info_data, mappingData()...)
router_info_data = append(router_info_data, make([]byte, 40)...)
return RouterInfo(router_info_data)
}
@ -98,6 +111,8 @@ func TestRouterAddressesReturnsAddresses(t *testing.T) {
}
}
func TestRouterAddressesReturnsAddressesWithMultiple(t *testing.T) {}
func TestPeerSizeIsZero(t *testing.T) {
assert := assert.New(t)
@ -106,6 +121,20 @@ func TestPeerSizeIsZero(t *testing.T) {
assert.Equal(0, size, "RouterInfo.PeerSize() did not return 0")
}
func TestSignatureIsCorrectSize(t *testing.T) {
func TestOptionsAreCorrect(t *testing.T) {
assert := assert.New(t)
router_info := buildFullRouterInfo()
options := router_info.Options()
assert.Equal(
0,
bytes.Compare(
[]byte(mappingData()),
[]byte(options),
),
)
}
func TestSignatureIsCorrectSize(t *testing.T) {}
func TestRouterIdentityIsCorrect(t *testing.T) {}