2016-02-05 02:23:11 -08:00
|
|
|
package common
|
2016-02-12 00:21:27 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-03-29 23:27:58 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-02-12 00:21:27 -08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckValidReportsEmptySlice(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
router_address := RouterAddress([]byte{})
|
|
|
|
err, exit := router_address.checkValid()
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
if assert.NotNil(err) {
|
|
|
|
assert.Equal(err.Error(), "error parsing RouterAddress: no data", "correct error message should be returned")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
2016-03-29 23:27:58 -07:00
|
|
|
assert.Equal(exit, true, "checkValid did not indicate to stop parsing on empty slice")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckRouterAddressValidReportsDataMissing(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
router_address := RouterAddress([]byte{0x01})
|
|
|
|
err, exit := router_address.checkValid()
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
if assert.NotNil(err) {
|
|
|
|
assert.Equal(err.Error(), "warning parsing RouterAddress: data too small", "correct error message should be returned")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
2016-03-29 23:27:58 -07:00
|
|
|
assert.Equal(exit, false, "checkValid indicates to stop parsing when some fields may be present")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
|
|
|
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
|
|
|
|
router_address = append(router_address, mapping...)
|
|
|
|
err, exit := router_address.checkValid()
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "checkValid() reported error with valid data")
|
|
|
|
assert.Equal(exit, false, "checkValid() indicated to stop parsing valid data")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
|
|
|
cost, err := router_address.Cost()
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "Cost() returned error with valid data")
|
|
|
|
assert.Equal(cost, 6, "Cost() returned wrong cost")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterAddressExpirationReturnsCorrectData(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
router_address := RouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
|
|
|
|
expiration, err := router_address.Expiration()
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "Expiration() returned error with valid data")
|
2016-02-12 00:21:27 -08:00
|
|
|
if bytes.Compare(expiration[:], []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) != 0 {
|
|
|
|
t.Fatal("Expiration did not return correct data:", expiration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadRouterAddressReturnsCorrectRemainderWithoutError(t *testing.T) {
|
2016-03-29 23:27:58 -07:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
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, []byte{0x01, 0x02, 0x03}...)
|
|
|
|
router_address, remainder, err := ReadRouterAddress(router_address_bytes)
|
2016-03-29 23:27:58 -07:00
|
|
|
|
|
|
|
assert.Nil(err, "ReadRouterAddress() reported error with valid data:")
|
2016-06-22 21:06:03 -07:00
|
|
|
assert.Equal(0, bytes.Compare(remainder, []byte{0x01, 0x02, 0x03}))
|
2016-03-29 23:27:58 -07:00
|
|
|
|
2016-02-12 00:21:27 -08:00
|
|
|
err, exit := router_address.checkValid()
|
2016-03-29 23:27:58 -07:00
|
|
|
assert.Nil(err, "checkValid() on address from ReadRouterAddress() reported error with valid data")
|
|
|
|
assert.Equal(exit, false, "checkValid() on address from ReadRouterAddress() indicated to stop parsing valid data")
|
2016-02-12 00:21:27 -08:00
|
|
|
}
|
2017-04-16 21:02:47 -07:00
|
|
|
|
|
|
|
func TestCorrectsFuzzCrasher1(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
assert.Equal(nil, r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
router_address_bytes := []byte{0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x30, 0x30}
|
|
|
|
ReadRouterAddress(router_address_bytes)
|
|
|
|
}
|