mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-06 06:24:20 -04:00
testing router info options
This commit is contained in:
@ -204,14 +204,41 @@ func (router_info RouterInfo) Signature() (signature Signature) {
|
|||||||
// Used during parsing to determine where in the RouterInfo the Mapping data begins.
|
// Used during parsing to determine where in the RouterInfo the Mapping data begins.
|
||||||
//
|
//
|
||||||
func (router_info RouterInfo) optionsLocation() (location int) {
|
func (router_info RouterInfo) optionsLocation() (location int) {
|
||||||
location = 9
|
data, remainder, err := ReadRouterIdentity(router_info)
|
||||||
var router_address RouterAddress
|
if err != nil {
|
||||||
remaining := router_info[9:]
|
return
|
||||||
addr_count, _ := router_info.RouterAddressCount()
|
|
||||||
for i := 0; i < addr_count; i++ {
|
|
||||||
router_address, remaining, _ = ReadRouterAddress(remaining)
|
|
||||||
location += len(router_address)
|
|
||||||
}
|
}
|
||||||
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,6 +247,6 @@ func (router_info RouterInfo) optionsLocation() (location int) {
|
|||||||
//
|
//
|
||||||
func (router_info RouterInfo) optionsSize() (size int) {
|
func (router_info RouterInfo) optionsSize() (size int) {
|
||||||
head := router_info.optionsLocation()
|
head := router_info.optionsLocation()
|
||||||
size = Integer(router_info[head:head+1]) + 1
|
size = Integer(router_info[head:head+2]) + 2
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -6,26 +6,39 @@ import (
|
|||||||
"testing"
|
"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 {
|
func buildRouterAddress() RouterAddress {
|
||||||
router_address_bytes := []byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
router_address_bytes := []byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||||
str, _ := ToI2PString("foo")
|
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, []byte(str)...)
|
||||||
router_address_bytes = append(router_address_bytes, mapping...)
|
router_address_bytes = append(router_address_bytes, mappingData()...)
|
||||||
|
|
||||||
return RouterAddress(router_address_bytes)
|
return RouterAddress(router_address_bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildFullRouterInfo() RouterInfo {
|
func buildFullRouterInfo() RouterInfo {
|
||||||
router_info_data := make([]byte, 0)
|
router_info_data := make([]byte, 0)
|
||||||
router_ident_data := make([]byte, 128+256)
|
router_info_data = append(router_info_data, buildRouterIdentity()...)
|
||||||
router_ident_data = append(router_ident_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
|
router_info_data = append(router_info_data, buildDate()...)
|
||||||
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, 0x01)
|
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)
|
return RouterInfo(router_info_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,6 +111,8 @@ func TestRouterAddressesReturnsAddresses(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRouterAddressesReturnsAddressesWithMultiple(t *testing.T) {}
|
||||||
|
|
||||||
func TestPeerSizeIsZero(t *testing.T) {
|
func TestPeerSizeIsZero(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
@ -106,6 +121,20 @@ func TestPeerSizeIsZero(t *testing.T) {
|
|||||||
assert.Equal(0, size, "RouterInfo.PeerSize() did not return 0")
|
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) {}
|
||||||
|
Reference in New Issue
Block a user