Various changes

-we're dropping errors instead of trying to recover
-revamp TestReadI2PStringErrWhenDataTooShort to reflect drops instead of recoveries
This commit is contained in:
Haris Khan
2024-11-17 11:52:47 -05:00
parent 11b9018630
commit f0702ffba9
2 changed files with 11 additions and 8 deletions

View File

@ -2,8 +2,6 @@ package data
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
)
@ -162,7 +160,8 @@ func ReadI2PString(data []byte) (str I2PString, remainder []byte, err error) {
}
data_len := length.Int() + 1
if data_len > len(data) {
err = fmt.Errorf("I2PString length %d exceeds available data %d", data_len-1, len(data)-1)
log.Errorf("I2PString length %d exceeds available data %d", data_len-1, len(data)-1)
err = ErrDataTooShort
log.WithError(err).Error("Failed to read I2PString")
return
}

View File

@ -140,10 +140,14 @@ func TestReadI2PStringErrWhenDataTooShort(t *testing.T) {
str, remainder, err := ReadI2PString(short_str)
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(err.Error(), ErrDataTooShort.Error(), "correct error message should be returned")
}
assert.Equal(len(str), 2, "ReadI2PString() did not return the slice as string when too long")
assert.Equal(3, int(str[0]), "ReadI2PString() did not return the correct partial string")
assert.Equal(1, int(str[1]), "ReadI2PString() did not return the correct partial string")
assert.Equal(len(remainder), 0, "ReadI2PString() returned a remainder when the string data was too short")
/*
assert.Equal(len(str), 2, "ReadI2PString() did not return the slice as string when too long")
assert.Equal(3, int(str[0]), "ReadI2PString() did not return the correct partial string")
assert.Equal(1, int(str[1]), "ReadI2PString() did not return the correct partial string")
assert.Equal(len(remainder), 0, "ReadI2PString() returned a remainder when the string data was too short")
*/
assert.Equal(len(str), 0, "ReadI2PString() should not return any data when data is too short")
assert.Equal(len(remainder), 0, "ReadI2PString() should not return any remainder when data is too short")
}