mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-04 13:32:52 -04:00
converted existing common tests to testify
This commit is contained in:
@ -3,92 +3,109 @@ package common
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValuesExclusesPairWithBadData(t *testing.T) {
|
func TestValuesExclusesPairWithBadData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
bad_key := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
bad_key := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||||
values, errs := bad_key.Values()
|
values, errs := bad_key.Values()
|
||||||
if len(values) != 1 {
|
|
||||||
t.Fatal("Values did not return valid values when some values had bad key")
|
if assert.Equal(len(values), 1, "Values() did not return valid values when some values had bad key") {
|
||||||
} else {
|
|
||||||
key, _ := values[0][0].Data()
|
key, _ := values[0][0].Data()
|
||||||
val, _ := values[0][1].Data()
|
val, _ := values[0][1].Data()
|
||||||
if key != "a" || val != "b" {
|
assert.Equal(key, "a", "Values() returned by data with invalid key contains incorrect present key")
|
||||||
t.Fatal("Value returned by values when other value had invalid key was incorrect")
|
assert.Equal(val, "b", "Values() returned by data with invalid key contains incorrect present key")
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(errs) != 2 {
|
|
||||||
t.Fatal("Values reported wrong error count when some values had invalid data", errs)
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(len(errs), 2, "Values() reported wrong error count when some values had invalid data")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValuesWarnsMissingData(t *testing.T) {
|
func TestValuesWarnsMissingData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62})
|
||||||
_, errs := mapping.Values()
|
_, errs := mapping.Values()
|
||||||
if len(errs) != 2 || errs[0].Error() != "warning parsing mapping: mapping length exceeds provided data" {
|
|
||||||
t.Fatal("Values reported wrong error when missing data", len(errs), errs)
|
if assert.Equal(len(errs), 2, "Values() reported wrong error count when mapping had missing data") {
|
||||||
|
assert.Equal(errs[0].Error(), "warning parsing mapping: mapping length exceeds provided data", "correct error message should be returned")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValuesWarnsExtraData(t *testing.T) {
|
func TestValuesWarnsExtraData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||||
_, errs := mapping.Values()
|
_, errs := mapping.Values()
|
||||||
if len(errs) != 2 || errs[0].Error() != "warning parsing mapping: data exists beyond length of mapping" {
|
|
||||||
t.Fatal("Values reported wrong error when extra data", len(errs), errs)
|
if assert.Equal(len(errs), 2, "Values() reported wrong error count when mapping had extra data") {
|
||||||
|
assert.Equal(errs[0].Error(), "warning parsing mapping: data exists beyond length of mapping", "correct error message should be returned")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValuesEnforcesEqualDelimitor(t *testing.T) {
|
func TestValuesEnforcesEqualDelimitor(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x30, 0x01, 0x62, 0x3b})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x30, 0x01, 0x62, 0x3b})
|
||||||
values, errs := mapping.Values()
|
values, errs := mapping.Values()
|
||||||
if len(errs) != 1 || errs[0].Error() != "mapping format violation, expected =" {
|
|
||||||
t.Fatal("wrong error reported with equal format error", errs)
|
if assert.Equal(len(errs), 1, "Values() reported wrong error count when mapping had = format error") {
|
||||||
}
|
assert.Equal(errs[0].Error(), "mapping format violation, expected =", "correct error message should be returned")
|
||||||
if len(values) != 0 {
|
|
||||||
t.Fatal("values not empty with invalid data, equal error")
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(len(values), 0, "Values() not empty with invalid data due to = format error")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValuesEnforcedSemicolonDelimitor(t *testing.T) {
|
func TestValuesEnforcedSemicolonDelimitor(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x30})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x30})
|
||||||
values, errs := mapping.Values()
|
values, errs := mapping.Values()
|
||||||
if len(errs) != 1 || errs[0].Error() != "mapping format violation, expected ;" {
|
|
||||||
t.Fatal("wrong error reported with semicolon format error", errs)
|
if assert.Equal(len(errs), 1, "Values() reported wrong error count when mapping had ; format error") {
|
||||||
}
|
assert.Equal(errs[0].Error(), "mapping format violation, expected ;", "correct error message should be returned")
|
||||||
if len(values) != 0 {
|
|
||||||
t.Fatal("values not empty with invalid data, semicolon error")
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(len(values), 0, "Values() not empty with invalid data due to ; format error")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValuesReturnsValues(t *testing.T) {
|
func TestValuesReturnsValues(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||||
_, errs := mapping.Values()
|
values, errs := mapping.Values()
|
||||||
if errs != nil {
|
key, kerr := values[0][0].Data()
|
||||||
t.Fatal("errs when parsing valid mapping values", errs)
|
val, verr := values[0][1].Data()
|
||||||
}
|
|
||||||
|
assert.Nil(errs, "Values() returned a errors with parsing valid data")
|
||||||
|
assert.Nil(kerr)
|
||||||
|
assert.Nil(verr)
|
||||||
|
assert.Equal(key, "a", "Values() did not return key in valid data")
|
||||||
|
assert.Equal(val, "b", "Values() did not return value in valid data")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHasDuplicateKeysTrueWhenDuplicates(t *testing.T) {
|
func TestHasDuplicateKeysTrueWhenDuplicates(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
dups := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
dups := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||||
if dups.HasDuplicateKeys() != true {
|
|
||||||
t.Fatal("HasDuplicateKeys did not report true when duplicate keys present")
|
assert.Equal(dups.HasDuplicateKeys(), true, "HasDuplicateKeys() did not report true when duplicate keys present")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHasDuplicateKeysFalseWithoutDuplicates(t *testing.T) {
|
func TestHasDuplicateKeysFalseWithoutDuplicates(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||||
if mapping.HasDuplicateKeys() != false {
|
|
||||||
t.Fatal("HasDuplicateKeys did not report false when duplicate keys were not present")
|
assert.Equal(mapping.HasDuplicateKeys(), false, "HasDuplicateKeys() did not report false when no duplicate keys present")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGoMapToMappingProducesCorrectMapping(t *testing.T) {
|
func TestGoMapToMappingProducesCorrectMapping(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
gomap := map[string]string{"a": "b"}
|
gomap := map[string]string{"a": "b"}
|
||||||
mapping, err := GoMapToMapping(gomap)
|
mapping, err := GoMapToMapping(gomap)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("GoMapToMapping returned error with valid data", err)
|
assert.Nil(err, "GoMapToMapping() returned error with valid data")
|
||||||
}
|
|
||||||
expected := []byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b}
|
expected := []byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b}
|
||||||
if bytes.Compare(mapping, expected) != 0 {
|
if bytes.Compare(mapping, expected) != 0 {
|
||||||
t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected)
|
t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected)
|
||||||
@ -130,39 +147,41 @@ func TestMappingOrderSortsValuesThenKeys(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStopValueReadTrueWhenCorrectErr(t *testing.T) {
|
func TestStopValueReadTrueWhenCorrectErr(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
status := stopValueRead(errors.New("error parsing string: zero length"))
|
status := stopValueRead(errors.New("error parsing string: zero length"))
|
||||||
if status != true {
|
|
||||||
t.Fatal("stopValueRead not true when String error found")
|
assert.Equal(status, true, "stopValueRead() did not return true when String error found")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStopValueReadFalseWhenWrongErr(t *testing.T) {
|
func TestStopValueReadFalseWhenWrongErr(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
status := stopValueRead(errors.New("something else"))
|
status := stopValueRead(errors.New("something else"))
|
||||||
if status != false {
|
|
||||||
t.Fatal("stopValueRead not false when error not String error")
|
assert.Equal(status, false, "stopValueRead() did not return false when non String error found")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBeginsWithCorrectWhenTrue(t *testing.T) {
|
func TestBeginsWithCorrectWhenTrue(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
slice := []byte{0x41}
|
slice := []byte{0x41}
|
||||||
status := beginsWith(slice, 0x41)
|
|
||||||
if status != true {
|
assert.Equal(beginsWith(slice, 0x41), true, "beginsWith() did not return true when correct")
|
||||||
t.Fatal("beginsWith did not return false on empty slice")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBeginsWithCorrectWhenFalse(t *testing.T) {
|
func TestBeginsWithCorrectWhenFalse(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
slice := []byte{0x00}
|
slice := []byte{0x00}
|
||||||
status := beginsWith(slice, 0x41)
|
|
||||||
if status != false {
|
assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not false when incorrect")
|
||||||
t.Fatal("beginsWith did not return false on empty slice")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBeginsWithCorrectWhenNil(t *testing.T) {
|
func TestBeginsWithCorrectWhenNil(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
slice := make([]byte, 0)
|
slice := make([]byte, 0)
|
||||||
status := beginsWith(slice, 0x41)
|
|
||||||
if status != false {
|
assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not return false on empty slice")
|
||||||
t.Fatal("beginsWith did not return false on empty slice")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,67 +2,71 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCheckValidReportsEmptySlice(t *testing.T) {
|
func TestCheckValidReportsEmptySlice(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
router_address := RouterAddress([]byte{})
|
router_address := RouterAddress([]byte{})
|
||||||
err, exit := router_address.checkValid()
|
err, exit := router_address.checkValid()
|
||||||
if err == nil || err.Error() != "error parsing RouterAddress: no data" {
|
|
||||||
t.Fatal("incorrect error returned by checkValid:", err)
|
if assert.NotNil(err) {
|
||||||
}
|
assert.Equal(err.Error(), "error parsing RouterAddress: no data", "correct error message should be returned")
|
||||||
if exit != true {
|
|
||||||
t.Fatal("checkValid did not indicate to stop parsing on empty slice")
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(exit, true, "checkValid did not indicate to stop parsing on empty slice")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckRouterAddressValidReportsDataMissing(t *testing.T) {
|
func TestCheckRouterAddressValidReportsDataMissing(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
router_address := RouterAddress([]byte{0x01})
|
router_address := RouterAddress([]byte{0x01})
|
||||||
err, exit := router_address.checkValid()
|
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 assert.NotNil(err) {
|
||||||
}
|
assert.Equal(err.Error(), "warning parsing RouterAddress: data too small", "correct error message should be returned")
|
||||||
if exit != false {
|
|
||||||
t.Fatal("checkValid indicated to stop parsing when some fields may be present")
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(exit, false, "checkValid indicates to stop parsing when some fields may be present")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
|
func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
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"})
|
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
|
||||||
router_address = append(router_address, mapping...)
|
router_address = append(router_address, mapping...)
|
||||||
err, exit := router_address.checkValid()
|
err, exit := router_address.checkValid()
|
||||||
if err != nil {
|
|
||||||
t.Fatal("checkValid reported error with valid data:", err)
|
assert.Nil(err, "checkValid() reported error with valid data")
|
||||||
}
|
assert.Equal(exit, false, "checkValid() indicated to stop parsing valid data")
|
||||||
if exit != false {
|
|
||||||
t.Fatal("checkValid indicated to stop parsing valid data")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
|
func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
||||||
cost, err := router_address.Cost()
|
cost, err := router_address.Cost()
|
||||||
if err != nil {
|
|
||||||
t.Fatal("err when calling Cost on valid data:", err)
|
assert.Nil(err, "Cost() returned error with valid data")
|
||||||
}
|
assert.Equal(cost, 6, "Cost() returned wrong cost")
|
||||||
if cost != 6 {
|
|
||||||
t.Fatal("Cost returned wrong cost:", cost)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterAddressExpirationReturnsCorrectData(t *testing.T) {
|
func TestRouterAddressExpirationReturnsCorrectData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
router_address := RouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
|
router_address := RouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
|
||||||
expiration, err := router_address.Expiration()
|
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 {
|
if bytes.Compare(expiration[:], []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) != 0 {
|
||||||
t.Fatal("Expiration did not return correct data:", expiration)
|
t.Fatal("Expiration did not return correct data:", expiration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadRouterAddressReturnsCorrectRemainderWithoutError(t *testing.T) {
|
func TestReadRouterAddressReturnsCorrectRemainderWithoutError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
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"})
|
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, mapping...)
|
||||||
router_address_bytes = append(router_address_bytes, []byte{0x01, 0x02, 0x03}...)
|
router_address_bytes = append(router_address_bytes, []byte{0x01, 0x02, 0x03}...)
|
||||||
router_address, remainder, err := ReadRouterAddress(router_address_bytes)
|
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 {
|
if bytes.Compare(remainder, []byte{0x01, 0x02, 0x03}) != 0 {
|
||||||
t.Fatal("incorrect remainder returned on ReadRouterAddress:", remainder)
|
t.Fatal("incorrect remainder returned on ReadRouterAddress:", remainder)
|
||||||
}
|
}
|
||||||
|
|
||||||
err, exit := router_address.checkValid()
|
err, exit := router_address.checkValid()
|
||||||
if err != nil {
|
|
||||||
t.Fatal("checkValid on address from ReadRouterAddress reported error with valid data:", err)
|
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")
|
||||||
if exit != false {
|
|
||||||
t.Fatal("checkValid on address from ReadRouterAddress indicated to stop parsing valid data")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
//"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
package common
|
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
STRING_MAX_SIZE = 256
|
STRING_MAX_SIZE = 255
|
||||||
)
|
)
|
||||||
|
|
||||||
type String []byte
|
type String []byte
|
||||||
@ -79,7 +79,7 @@ func (str String) Data() (data string, err error) {
|
|||||||
//
|
//
|
||||||
func ToI2PString(data string) (str String, err error) {
|
func ToI2PString(data string) (str String, err error) {
|
||||||
data_len := len(data)
|
data_len := len(data)
|
||||||
if data_len >= STRING_MAX_SIZE {
|
if data_len > STRING_MAX_SIZE {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"string_len": data_len,
|
"string_len": data_len,
|
||||||
"max_len": STRING_MAX_SIZE,
|
"max_len": STRING_MAX_SIZE,
|
||||||
|
@ -1,150 +1,148 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStringReportsCorrectLength(t *testing.T) {
|
func TestStringReportsCorrectLength(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
str_len, err := String([]byte{0x02, 0x00, 0x00}).Length()
|
str_len, err := String([]byte{0x02, 0x00, 0x00}).Length()
|
||||||
if str_len != 2 {
|
|
||||||
t.Fatal("string.Length() did not report correct length")
|
assert.Equal(str_len, 2, "Length() did not report correct length")
|
||||||
}
|
assert.Nil(err, "Length() reported an error on valid string")
|
||||||
if err != nil {
|
|
||||||
t.Fatal("string.Length() reported an error on valid string:", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringReportsLengthZeroError(t *testing.T) {
|
func TestStringReportsLengthZeroError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
str_len, err := String(make([]byte, 0)).Length()
|
str_len, err := String(make([]byte, 0)).Length()
|
||||||
if str_len != 0 {
|
|
||||||
t.Fatal("string.Length() reported non-zero length on empty slice")
|
assert.Equal(str_len, 0, "Length() reported non-zero length on empty slice")
|
||||||
}
|
if assert.NotNil(err) {
|
||||||
if err == nil || err.Error() != "error parsing string: zero length" {
|
assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned")
|
||||||
t.Fatal("string.Length() reported incorrect error on zero length slice:", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringReportsExtraDataError(t *testing.T) {
|
func TestStringReportsExtraDataError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
str_len, err := String([]byte{0x01, 0x00, 0x00}).Length()
|
str_len, err := String([]byte{0x01, 0x00, 0x00}).Length()
|
||||||
if str_len != 1 {
|
|
||||||
t.Fatal("string.Length() reported wrong size when extra data present")
|
assert.Equal(str_len, 1, "Length() reported wrong size when extra data present")
|
||||||
}
|
if assert.NotNil(err) {
|
||||||
if err == nil || err.Error() != "string parsing warning: string contains data beyond length" {
|
assert.Equal(err.Error(), "string parsing warning: string contains data beyond length", "correct error message should be returned")
|
||||||
t.Fatal("string.Length() reported incorrect error on extra data:", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringDataReportsLengthZeroError(t *testing.T) {
|
func TestStringDataReportsLengthZeroError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
str_len, err := String([]byte{0x01}).Length()
|
str_len, err := String([]byte{0x01}).Length()
|
||||||
if str_len != 1 {
|
|
||||||
t.Fatal("string.Length() reported wrong length with missing data", str_len)
|
assert.Equal(str_len, 1, "Length() reported wrong size with missing data")
|
||||||
}
|
if assert.NotNil(err) {
|
||||||
if err == nil || err.Error() != "string parsing warning: string data is shorter than specified by length" {
|
assert.Equal(err.Error(), "string parsing warning: string data is shorter than specified by length", "correct error message should be returned")
|
||||||
t.Fatal("string.Length() reported wrong error when data was missing", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringDataReportsExtraDataError(t *testing.T) {
|
func TestStringDataReportsExtraDataError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
data, err := String([]byte{0x01, 0x00, 0x01}).Data()
|
data, err := String([]byte{0x01, 0x00, 0x01}).Data()
|
||||||
data_len := len(data)
|
data_len := len(data)
|
||||||
if data_len != 1 {
|
|
||||||
t.Fatal("string.Data() returned wrong size data for length with extra data:", data_len)
|
assert.Equal(data_len, 1, "Data() reported wrong size on string with extra data")
|
||||||
}
|
if assert.NotNil(err) {
|
||||||
if err == nil || err.Error() != "string parsing warning: string contains data beyond length" {
|
assert.Equal(err.Error(), "string parsing warning: string contains data beyond length", "correct error message should be returned")
|
||||||
t.Fatal("string.Length() reported wrong error with extra data", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringDataEmptyWhenZeroLength(t *testing.T) {
|
func TestStringDataEmptyWhenZeroLength(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
data, err := String(make([]byte, 0)).Data()
|
data, err := String(make([]byte, 0)).Data()
|
||||||
data_len := len(data)
|
|
||||||
if data_len != 0 {
|
assert.Equal(len(data), 0, "Data() returned data when none was present:")
|
||||||
t.Fatal("string.Data() returned data when none was present:", data_len)
|
if assert.NotNil(err) {
|
||||||
}
|
assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned")
|
||||||
if err == nil || err.Error() != "error parsing string: zero length" {
|
|
||||||
t.Fatal("string.Length() reported wrong error with no data", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringDataErrorWhenNonZeroLengthOnly(t *testing.T) {
|
func TestStringDataErrorWhenNonZeroLengthOnly(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
data, err := String([]byte{0x01}).Data()
|
data, err := String([]byte{0x01}).Data()
|
||||||
data_len := len(data)
|
|
||||||
if data_len != 0 {
|
assert.Equal(len(data), 0, "Data() returned data when only length was present")
|
||||||
t.Fatal("string.Data() returned data when only length was present:", data_len)
|
if assert.NotNil(err) {
|
||||||
}
|
assert.Equal(err.Error(), "string parsing warning: string data is shorter than specified by length", "correct error message should be returned")
|
||||||
if err == nil || err.Error() != "string parsing warning: string data is shorter than specified by length" {
|
|
||||||
t.Fatal("string.Length() reported wrong error with length but no data", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToI2PStringFormatsCorrectly(t *testing.T) {
|
func TestToI2PStringFormatsCorrectly(t *testing.T) {
|
||||||
i2p_string, err := ToI2PString(string([]byte{0x22, 0x33}))
|
assert := assert.New(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("ToI2PString() returned error on valid data:", err)
|
i2p_string, err := ToI2PString(string([]byte{0x08, 0x09}))
|
||||||
}
|
|
||||||
if i2p_string[0] != 0x02 {
|
assert.Nil(err, "ToI2PString() returned error on valid data")
|
||||||
t.Fatal("ToI2PString() did not prepend the correct length")
|
assert.Equal(2, int(i2p_string[0]), "ToI2PString() did not prepend the correct length")
|
||||||
}
|
assert.Equal(8, int(i2p_string[1]), "ToI2PString() did not include string")
|
||||||
if i2p_string[1] != 0x22 && i2p_string[2] != 0x33 {
|
assert.Equal(9, int(i2p_string[2]), "ToI2PString() did not include string")
|
||||||
t.Fatal("ToI2PString() did not preserve string")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToI2PStringReportsOverflows(t *testing.T) {
|
func TestToI2PStringReportsOverflows(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
i2p_string, err := ToI2PString(string(make([]byte, 256)))
|
i2p_string, err := ToI2PString(string(make([]byte, 256)))
|
||||||
if len(i2p_string) != 0 {
|
|
||||||
t.Fatal("ToI2PString() returned data when overflowed")
|
assert.Equal(len(i2p_string), 0, "ToI2PString() returned data when overflowed")
|
||||||
}
|
if assert.NotNil(err) {
|
||||||
if err == nil || err.Error() != "cannot store that much data in I2P string" {
|
assert.Equal(err.Error(), "cannot store that much data in I2P string", "correct error message should be returned")
|
||||||
t.Fatal("ToI2pString() did not report overflow")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = ToI2PString(string(make([]byte, 255)))
|
_, err = ToI2PString(string(make([]byte, 255)))
|
||||||
if err != nil {
|
|
||||||
t.Fatal("ToI2PString() reported error with acceptable size:", err)
|
assert.Nil(err, "ToI2PString() reported error with acceptable size")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadStringReadsLength(t *testing.T) {
|
func TestReadStringReadsLength(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
bytes := []byte{0x01, 0x04, 0x06}
|
bytes := []byte{0x01, 0x04, 0x06}
|
||||||
str, remainder, err := ReadString(bytes)
|
str, remainder, err := ReadString(bytes)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("ReadString() returned error reading string with extra data,", err)
|
assert.Nil(err, "ReadString() returned error reading string with extra data")
|
||||||
}
|
assert.Equal(len(str), 2, "ReadString() did not return correct string length")
|
||||||
if len(str) != 2 {
|
assert.Equal(1, int(str[0]), "ReadString() did not return correct string")
|
||||||
t.Fatal("ReadString() did not return correct string length:", len(str))
|
assert.Equal(4, int(str[1]), "ReadString() did not return correct string")
|
||||||
}
|
assert.Equal(len(remainder), 1, "ReadString() did not return correct remainder length")
|
||||||
if str[0] != 0x01 && str[1] != 0x04 {
|
assert.Equal(6, int(remainder[0]), "ReadString() did not return correct remainder")
|
||||||
t.Fatal("ReadString() did not return correct string")
|
|
||||||
}
|
|
||||||
if len(remainder) != 1 {
|
|
||||||
t.Fatal("ReadString() did not return correct remainder length")
|
|
||||||
}
|
|
||||||
if remainder[0] != 0x06 {
|
|
||||||
t.Fatal("ReadString() did not return correct remainder")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadStringErrWhenEmptySlice(t *testing.T) {
|
func TestReadStringErrWhenEmptySlice(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
bytes := make([]byte, 0)
|
bytes := make([]byte, 0)
|
||||||
_, _, err := ReadString(bytes)
|
_, _, err := ReadString(bytes)
|
||||||
if err == nil || err.Error() != "error parsing string: zero length" {
|
|
||||||
t.Fatal("ReadString() did not report empty slice error", err)
|
if assert.NotNil(err) {
|
||||||
|
assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadStringErrWhenDataTooShort(t *testing.T) {
|
func TestReadStringErrWhenDataTooShort(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
short_str := []byte{0x03, 0x01}
|
short_str := []byte{0x03, 0x01}
|
||||||
str, remainder, err := ReadString(short_str)
|
str, remainder, err := ReadString(short_str)
|
||||||
if err == nil || err.Error() != "string parsing warning: string data is shorter than specified by length" {
|
|
||||||
t.Fatal("ReadString() did not report string too long:", err)
|
if assert.NotNil(err) {
|
||||||
}
|
assert.Equal(err.Error(), "string parsing warning: string data is shorter than specified by length", "correct error message should be returned")
|
||||||
if len(str) != 2 {
|
|
||||||
t.Fatal("ReadString() did not return the slice as string when too long")
|
|
||||||
}
|
|
||||||
if str[0] != 0x03 && str[1] != 0x01 {
|
|
||||||
t.Fatal("ReadString() did not return the correct partial string")
|
|
||||||
}
|
|
||||||
if len(remainder) != 0 {
|
|
||||||
t.Fatal("ReadString() returned a remainder when the string data was too short")
|
|
||||||
}
|
}
|
||||||
|
assert.Equal(len(str), 2, "ReadString() did not return the slice as string when too long")
|
||||||
|
assert.Equal(3, int(str[0]), "ReadString() did not return the correct partial string")
|
||||||
|
assert.Equal(1, int(str[1]), "ReadString() did not return the correct partial string")
|
||||||
|
assert.Equal(len(remainder), 0, "ReadString() returned a remainder when the string data was too short")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user