diff --git a/lib/common/router_info.go b/lib/common/router_info.go index b03cc73..f563537 100644 --- a/lib/common/router_info.go +++ b/lib/common/router_info.go @@ -93,7 +93,10 @@ func (router_info RouterInfo) RouterIdentity() (router_identity RouterIdentity, // Return the Date the RouterInfo was published and any errors encountered parsing the RouterInfo. // func (router_info RouterInfo) Published() (date Date, err error) { - _, remainder, _ := ReadRouterIdentity(router_info) + _, remainder, err := ReadRouterIdentity(router_info) + if err != nil { + return + } remainder_len := len(remainder) if remainder_len < 8 { log.WithFields(log.Fields{ @@ -113,7 +116,10 @@ func (router_info RouterInfo) Published() (date Date, err error) { // Return the Integer representing the number of RouterAddresses that are contained in this RouterInfo. // func (router_info RouterInfo) RouterAddressCount() (count int, err error) { - _, remainder, _ := ReadRouterIdentity(router_info) + _, remainder, err := ReadRouterIdentity(router_info) + if err != nil { + return + } remainder_len := len(remainder) if remainder_len < 9 { log.WithFields(log.Fields{ @@ -134,7 +140,10 @@ func (router_info RouterInfo) RouterAddressCount() (count int, err error) { // a partial list if data is missing. // func (router_info RouterInfo) RouterAddresses() (router_addresses []RouterAddress, err error) { - _, remainder, _ := ReadRouterIdentity(router_info) + _, remainder, err := ReadRouterIdentity(router_info) + if err != nil { + return + } remainder_len := len(remainder) if remainder_len < 9 { log.WithFields(log.Fields{ diff --git a/lib/common/router_info_test.go b/lib/common/router_info_test.go index 020e562..ee62fb2 100644 --- a/lib/common/router_info_test.go +++ b/lib/common/router_info_test.go @@ -36,6 +36,28 @@ func TestPublishedReturnsCorrectDate(t *testing.T) { assert.Equal(int64(86400), date.Time().Unix(), "RouterInfo.Published() did not return correct date") } +func TestPublishedReturnsCorrectErrorWithPartialDate(t *testing.T) { + assert := assert.New(t) + + router_info := buildFullRouterInfo() + router_info = router_info[:387+4] + _, err := router_info.Published() + if assert.NotNil(err) { + assert.Equal("error parsing date: not enough data", err.Error()) + } +} + +func TestPublishedReturnsCorrectErrorWithInvalidData(t *testing.T) { + assert := assert.New(t) + + router_info := buildFullRouterInfo() + router_info = router_info[:56] + _, err := router_info.Published() + if assert.NotNil(err) { + assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error()) + } +} + func TestRouterAddressCountReturnsCorrectCount(t *testing.T) { assert := assert.New(t) @@ -45,11 +67,23 @@ func TestRouterAddressCountReturnsCorrectCount(t *testing.T) { assert.Equal(1, count, "RouterInfo.RouterAddressCount() did not return correct count") } -func TestRouterAdrressesReturnsAddresses(t *testing.T) { +func TestRouterAddressCountReturnsCorrectErrorWithInvalidData(t *testing.T) { + assert := assert.New(t) + + router_info := buildFullRouterInfo() + router_info = router_info[:387+8] + count, err := router_info.RouterAddressCount() + if assert.NotNil(err) { + assert.Equal("error parsing router addresses: not enough data", err.Error()) + } + assert.Equal(0, count) +} + +func TestRouterAddressesReturnsAddresses(t *testing.T) { } -func TestRouterAdrressesReturnsPartialListWithMissing(t *testing.T) { +func TestRouterAddressesReturnsPartialListWithMissing(t *testing.T) { }