From 7e00424a83c52c6a87e14134c6c52fdea3ffba10 Mon Sep 17 00:00:00 2001 From: Hayden Parker Date: Tue, 29 Mar 2016 23:27:58 -0700 Subject: [PATCH] converted existing common tests to testify --- lib/common/mapping_test.go | 127 +++++++++++++---------- lib/common/router_address_test.go | 69 +++++++------ lib/common/router_info_test.go | 1 + lib/common/signature_test.go | 1 - lib/common/string.go | 4 +- lib/common/string_test.go | 166 +++++++++++++++--------------- 6 files changed, 193 insertions(+), 175 deletions(-) delete mode 100644 lib/common/signature_test.go diff --git a/lib/common/mapping_test.go b/lib/common/mapping_test.go index 7a46d45..a8ac0f0 100644 --- a/lib/common/mapping_test.go +++ b/lib/common/mapping_test.go @@ -3,92 +3,109 @@ package common import ( "bytes" "errors" + "github.com/stretchr/testify/assert" "testing" ) func TestValuesExclusesPairWithBadData(t *testing.T) { + assert := assert.New(t) + bad_key := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00}) values, errs := bad_key.Values() - if len(values) != 1 { - t.Fatal("Values did not return valid values when some values had bad key") - } else { + + if assert.Equal(len(values), 1, "Values() did not return valid values when some values had bad key") { key, _ := values[0][0].Data() val, _ := values[0][1].Data() - if key != "a" || val != "b" { - t.Fatal("Value returned by values when other value had invalid key was incorrect") - } - } - if len(errs) != 2 { - t.Fatal("Values reported wrong error count when some values had invalid data", errs) + assert.Equal(key, "a", "Values() returned by data with invalid key contains incorrect present key") + assert.Equal(val, "b", "Values() returned by data with invalid key contains incorrect present key") } + assert.Equal(len(errs), 2, "Values() reported wrong error count when some values had invalid data") } func TestValuesWarnsMissingData(t *testing.T) { + assert := assert.New(t) + mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62}) _, 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) { + assert := assert.New(t) + mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00}) _, 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) { + assert := assert.New(t) + mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x30, 0x01, 0x62, 0x3b}) 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 len(values) != 0 { - t.Fatal("values not empty with invalid data, equal error") + + 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") } + assert.Equal(len(values), 0, "Values() not empty with invalid data due to = format error") } func TestValuesEnforcedSemicolonDelimitor(t *testing.T) { + assert := assert.New(t) + mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x30}) 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 len(values) != 0 { - t.Fatal("values not empty with invalid data, semicolon error") + + 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") } + assert.Equal(len(values), 0, "Values() not empty with invalid data due to ; format error") } func TestValuesReturnsValues(t *testing.T) { + assert := assert.New(t) + mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b}) - _, errs := mapping.Values() - if errs != nil { - t.Fatal("errs when parsing valid mapping values", errs) - } + values, errs := mapping.Values() + key, kerr := values[0][0].Data() + 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) { + assert := assert.New(t) + 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) { + assert := assert.New(t) + 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) { + assert := assert.New(t) + gomap := map[string]string{"a": "b"} 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} if bytes.Compare(mapping, expected) != 0 { t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected) @@ -130,39 +147,41 @@ func TestMappingOrderSortsValuesThenKeys(t *testing.T) { } func TestStopValueReadTrueWhenCorrectErr(t *testing.T) { + assert := assert.New(t) + 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) { + assert := assert.New(t) + 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) { + assert := assert.New(t) + slice := []byte{0x41} - status := beginsWith(slice, 0x41) - if status != true { - t.Fatal("beginsWith did not return false on empty slice") - } + + assert.Equal(beginsWith(slice, 0x41), true, "beginsWith() did not return true when correct") } func TestBeginsWithCorrectWhenFalse(t *testing.T) { + assert := assert.New(t) + slice := []byte{0x00} - status := beginsWith(slice, 0x41) - if status != false { - t.Fatal("beginsWith did not return false on empty slice") - } + + assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not false when incorrect") } func TestBeginsWithCorrectWhenNil(t *testing.T) { + assert := assert.New(t) + slice := make([]byte, 0) - status := beginsWith(slice, 0x41) - if status != false { - t.Fatal("beginsWith did not return false on empty slice") - } + + assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not return false on empty slice") } diff --git a/lib/common/router_address_test.go b/lib/common/router_address_test.go index 3b05e85..158b9e3 100644 --- a/lib/common/router_address_test.go +++ b/lib/common/router_address_test.go @@ -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") } diff --git a/lib/common/router_info_test.go b/lib/common/router_info_test.go index 11379ec..f18fe03 100644 --- a/lib/common/router_info_test.go +++ b/lib/common/router_info_test.go @@ -1,6 +1,7 @@ package common import ( + //"github.com/stretchr/testify/assert" "testing" ) diff --git a/lib/common/signature_test.go b/lib/common/signature_test.go deleted file mode 100644 index 805d0c7..0000000 --- a/lib/common/signature_test.go +++ /dev/null @@ -1 +0,0 @@ -package common diff --git a/lib/common/string.go b/lib/common/string.go index 9d70be3..58d2d82 100644 --- a/lib/common/string.go +++ b/lib/common/string.go @@ -12,7 +12,7 @@ import ( ) const ( - STRING_MAX_SIZE = 256 + STRING_MAX_SIZE = 255 ) type String []byte @@ -79,7 +79,7 @@ func (str String) Data() (data string, err error) { // func ToI2PString(data string) (str String, err error) { data_len := len(data) - if data_len >= STRING_MAX_SIZE { + if data_len > STRING_MAX_SIZE { log.WithFields(log.Fields{ "string_len": data_len, "max_len": STRING_MAX_SIZE, diff --git a/lib/common/string_test.go b/lib/common/string_test.go index ae97b71..75bc434 100644 --- a/lib/common/string_test.go +++ b/lib/common/string_test.go @@ -1,150 +1,148 @@ package common import ( + "github.com/stretchr/testify/assert" "testing" ) func TestStringReportsCorrectLength(t *testing.T) { + assert := assert.New(t) + str_len, err := String([]byte{0x02, 0x00, 0x00}).Length() - if str_len != 2 { - t.Fatal("string.Length() did not report correct length") - } - if err != nil { - t.Fatal("string.Length() reported an error on valid string:", err) - } + + assert.Equal(str_len, 2, "Length() did not report correct length") + assert.Nil(err, "Length() reported an error on valid string") } func TestStringReportsLengthZeroError(t *testing.T) { + assert := assert.New(t) + str_len, err := String(make([]byte, 0)).Length() - if str_len != 0 { - t.Fatal("string.Length() reported non-zero length on empty slice") - } - if err == nil || err.Error() != "error parsing string: zero length" { - t.Fatal("string.Length() reported incorrect error on zero length slice:", err) + + assert.Equal(str_len, 0, "Length() reported non-zero length on empty slice") + if assert.NotNil(err) { + assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned") } } func TestStringReportsExtraDataError(t *testing.T) { + assert := assert.New(t) + str_len, err := String([]byte{0x01, 0x00, 0x00}).Length() - if str_len != 1 { - t.Fatal("string.Length() reported wrong size when extra data present") - } - if err == nil || err.Error() != "string parsing warning: string contains data beyond length" { - t.Fatal("string.Length() reported incorrect error on extra data:", err) + + assert.Equal(str_len, 1, "Length() reported wrong size when extra data present") + if assert.NotNil(err) { + assert.Equal(err.Error(), "string parsing warning: string contains data beyond length", "correct error message should be returned") } } func TestStringDataReportsLengthZeroError(t *testing.T) { + assert := assert.New(t) + str_len, err := String([]byte{0x01}).Length() - if str_len != 1 { - t.Fatal("string.Length() reported wrong length with missing data", str_len) - } - if err == nil || err.Error() != "string parsing warning: string data is shorter than specified by length" { - t.Fatal("string.Length() reported wrong error when data was missing", err) + + assert.Equal(str_len, 1, "Length() reported wrong size with missing data") + 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") } } func TestStringDataReportsExtraDataError(t *testing.T) { + assert := assert.New(t) + data, err := String([]byte{0x01, 0x00, 0x01}).Data() data_len := len(data) - if data_len != 1 { - t.Fatal("string.Data() returned wrong size data for length with extra data:", data_len) - } - if err == nil || err.Error() != "string parsing warning: string contains data beyond length" { - t.Fatal("string.Length() reported wrong error with extra data", err) + + assert.Equal(data_len, 1, "Data() reported wrong size on string with extra data") + if assert.NotNil(err) { + assert.Equal(err.Error(), "string parsing warning: string contains data beyond length", "correct error message should be returned") } } func TestStringDataEmptyWhenZeroLength(t *testing.T) { + assert := assert.New(t) + data, err := String(make([]byte, 0)).Data() - data_len := len(data) - if data_len != 0 { - t.Fatal("string.Data() returned data when none was present:", data_len) - } - if err == nil || err.Error() != "error parsing string: zero length" { - t.Fatal("string.Length() reported wrong error with no data", err) + + assert.Equal(len(data), 0, "Data() returned data when none was present:") + if assert.NotNil(err) { + assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned") } } func TestStringDataErrorWhenNonZeroLengthOnly(t *testing.T) { + assert := assert.New(t) + data, err := String([]byte{0x01}).Data() - data_len := len(data) - if data_len != 0 { - t.Fatal("string.Data() returned data when only length was present:", data_len) - } - 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) + + assert.Equal(len(data), 0, "Data() returned data when only length was present") + 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") } } func TestToI2PStringFormatsCorrectly(t *testing.T) { - i2p_string, err := ToI2PString(string([]byte{0x22, 0x33})) - if err != nil { - t.Fatal("ToI2PString() returned error on valid data:", err) - } - if i2p_string[0] != 0x02 { - t.Fatal("ToI2PString() did not prepend the correct length") - } - if i2p_string[1] != 0x22 && i2p_string[2] != 0x33 { - t.Fatal("ToI2PString() did not preserve string") - } + assert := assert.New(t) + + i2p_string, err := ToI2PString(string([]byte{0x08, 0x09})) + + assert.Nil(err, "ToI2PString() returned error on valid data") + 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") + assert.Equal(9, int(i2p_string[2]), "ToI2PString() did not include string") } func TestToI2PStringReportsOverflows(t *testing.T) { + assert := assert.New(t) + i2p_string, err := ToI2PString(string(make([]byte, 256))) - if len(i2p_string) != 0 { - t.Fatal("ToI2PString() returned data when overflowed") - } - if err == nil || err.Error() != "cannot store that much data in I2P string" { - t.Fatal("ToI2pString() did not report overflow") + + assert.Equal(len(i2p_string), 0, "ToI2PString() returned data when overflowed") + if assert.NotNil(err) { + assert.Equal(err.Error(), "cannot store that much data in I2P string", "correct error message should be returned") } + _, 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) { + assert := assert.New(t) + bytes := []byte{0x01, 0x04, 0x06} str, remainder, err := ReadString(bytes) - if err != nil { - t.Fatal("ReadString() returned error reading string with extra data,", err) - } - if len(str) != 2 { - t.Fatal("ReadString() did not return correct string length:", len(str)) - } - if str[0] != 0x01 && str[1] != 0x04 { - 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") - } + + assert.Nil(err, "ReadString() returned error reading string with extra data") + assert.Equal(len(str), 2, "ReadString() did not return correct string length") + assert.Equal(1, int(str[0]), "ReadString() did not return correct string") + assert.Equal(4, int(str[1]), "ReadString() did not return correct string") + assert.Equal(len(remainder), 1, "ReadString() did not return correct remainder length") + assert.Equal(6, int(remainder[0]), "ReadString() did not return correct remainder") } func TestReadStringErrWhenEmptySlice(t *testing.T) { + assert := assert.New(t) + bytes := make([]byte, 0) _, _, 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) { + assert := assert.New(t) + short_str := []byte{0x03, 0x01} 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 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") + + 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") } + 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") }