converted existing common tests to testify

This commit is contained in:
Hayden Parker
2016-03-29 23:27:58 -07:00
parent fc9b661ce6
commit 7e00424a83
6 changed files with 193 additions and 175 deletions

View File

@ -2,67 +2,71 @@ package common
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCheckValidReportsEmptySlice(t *testing.T) {
assert := assert.New(t)
router_address := RouterAddress([]byte{})
err, exit := router_address.checkValid()
if err == nil || err.Error() != "error parsing RouterAddress: no data" {
t.Fatal("incorrect error returned by checkValid:", err)
}
if exit != true {
t.Fatal("checkValid did not indicate to stop parsing on empty slice")
if assert.NotNil(err) {
assert.Equal(err.Error(), "error parsing RouterAddress: no data", "correct error message should be returned")
}
assert.Equal(exit, true, "checkValid did not indicate to stop parsing on empty slice")
}
func TestCheckRouterAddressValidReportsDataMissing(t *testing.T) {
assert := assert.New(t)
router_address := RouterAddress([]byte{0x01})
err, exit := router_address.checkValid()
if err == nil || err.Error() != "warning parsing RouterAddress: data too small" {
t.Fatal("incorrect error returned by checkValid:", err)
}
if exit != false {
t.Fatal("checkValid indicated to stop parsing when some fields may be present")
if assert.NotNil(err) {
assert.Equal(err.Error(), "warning parsing RouterAddress: data too small", "correct error message should be returned")
}
assert.Equal(exit, false, "checkValid indicates to stop parsing when some fields may be present")
}
func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
assert := assert.New(t)
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()
if err != nil {
t.Fatal("checkValid reported error with valid data:", err)
}
if exit != false {
t.Fatal("checkValid indicated to stop parsing valid data")
}
assert.Nil(err, "checkValid() reported error with valid data")
assert.Equal(exit, false, "checkValid() indicated to stop parsing valid data")
}
func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
assert := assert.New(t)
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
cost, err := router_address.Cost()
if err != nil {
t.Fatal("err when calling Cost on valid data:", err)
}
if cost != 6 {
t.Fatal("Cost returned wrong cost:", cost)
}
assert.Nil(err, "Cost() returned error with valid data")
assert.Equal(cost, 6, "Cost() returned wrong cost")
}
func TestRouterAddressExpirationReturnsCorrectData(t *testing.T) {
assert := assert.New(t)
router_address := RouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
expiration, err := router_address.Expiration()
if err != nil {
t.Fatal("err when calling Expiration on valid data:", err)
}
assert.Nil(err, "Expiration() returned error with valid data")
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) {
assert := assert.New(t)
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"})
@ -70,17 +74,14 @@ func TestReadRouterAddressReturnsCorrectRemainderWithoutError(t *testing.T) {
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)
if err != nil {
t.Fatal("ReadRouterAddress reported error with valid data:", err)
}
assert.Nil(err, "ReadRouterAddress() reported error with valid data:")
if bytes.Compare(remainder, []byte{0x01, 0x02, 0x03}) != 0 {
t.Fatal("incorrect remainder returned on ReadRouterAddress:", remainder)
}
err, exit := router_address.checkValid()
if err != nil {
t.Fatal("checkValid on address from ReadRouterAddress reported error with valid data:", err)
}
if exit != false {
t.Fatal("checkValid on address from ReadRouterAddress indicated to stop parsing valid data")
}
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")
}