more router info tests

This commit is contained in:
Hayden Parker
2016-07-02 21:58:34 -07:00
parent b4728abdee
commit dc5349b094

View File

@ -2,6 +2,7 @@ package common
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
@ -17,16 +18,16 @@ func buildDate() []byte {
return date_data
}
func mappingData() Mapping {
func buildMapping() Mapping {
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
return mapping
}
func buildRouterAddress() RouterAddress {
func buildRouterAddress(transport string) RouterAddress {
router_address_bytes := []byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
str, _ := ToI2PString("foo")
str, _ := ToI2PString(transport)
router_address_bytes = append(router_address_bytes, []byte(str)...)
router_address_bytes = append(router_address_bytes, mappingData()...)
router_address_bytes = append(router_address_bytes, buildMapping()...)
return RouterAddress(router_address_bytes)
}
@ -35,9 +36,9 @@ func buildFullRouterInfo() RouterInfo {
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, buildRouterAddress()...)
router_info_data = append(router_info_data, buildRouterAddress("foo")...)
router_info_data = append(router_info_data, 0x00)
router_info_data = append(router_info_data, mappingData()...)
router_info_data = append(router_info_data, buildMapping()...)
router_info_data = append(router_info_data, make([]byte, 40)...)
return RouterInfo(router_info_data)
}
@ -104,14 +105,45 @@ func TestRouterAddressesReturnsAddresses(t *testing.T) {
assert.Equal(
0,
bytes.Compare(
[]byte(buildRouterAddress()),
[]byte(buildRouterAddress("foo")),
[]byte(router_addresses[0]),
),
)
}
}
func TestRouterAddressesReturnsAddressesWithMultiple(t *testing.T) {}
func TestRouterAddressesReturnsAddressesWithMultiple(t *testing.T) {
assert := assert.New(t)
router_info_data := make([]byte, 0)
router_info_data = append(router_info_data, buildRouterIdentity()...)
router_info_data = append(router_info_data, buildDate()...)
router_info_data = append(router_info_data, 0x03)
router_info_data = append(router_info_data, buildRouterAddress("foo0")...)
router_info_data = append(router_info_data, buildRouterAddress("foo1")...)
router_info_data = append(router_info_data, buildRouterAddress("foo2")...)
router_info_data = append(router_info_data, 0x00)
router_info_data = append(router_info_data, buildMapping()...)
router_info_data = append(router_info_data, make([]byte, 40)...)
router_info := RouterInfo(router_info_data)
count, err := router_info.RouterAddressCount()
if assert.Equal(3, count) && assert.Nil(err) {
router_addresses, err := router_info.RouterAddresses()
if assert.Nil(err) {
for i := 0; i < 3; i++ {
assert.Equal(
0,
bytes.Compare(
[]byte(buildRouterAddress(fmt.Sprintf("foo%d", i))),
[]byte(router_addresses[i]),
),
)
}
}
}
}
func TestPeerSizeIsZero(t *testing.T) {
assert := assert.New(t)
@ -129,7 +161,7 @@ func TestOptionsAreCorrect(t *testing.T) {
assert.Equal(
0,
bytes.Compare(
[]byte(mappingData()),
[]byte(buildMapping()),
[]byte(options),
),
)
@ -143,4 +175,17 @@ func TestSignatureIsCorrectSize(t *testing.T) {
assert.Equal(40, len(signature))
}
func TestRouterIdentityIsCorrect(t *testing.T) {}
func TestRouterIdentityIsCorrect(t *testing.T) {
assert := assert.New(t)
router_info := buildFullRouterInfo()
router_identity, err := router_info.RouterIdentity()
assert.Nil(err)
assert.Equal(
0,
bytes.Compare(
[]byte(buildRouterIdentity()),
[]byte(router_identity),
),
)
}