errors: refactor

This commit is contained in:
Aleksandr Razumov 2017-02-27 23:41:04 +03:00
parent e5a4e0b8a4
commit 63b375a10f
7 changed files with 41 additions and 17 deletions

View File

@ -162,19 +162,34 @@ func (m *Message) Get(t AttrType) ([]byte, error) {
return v.Value, nil
}
// AttrLengthError occurs when len(v) > Max.
type AttrLengthError struct {
// AttrOverflowErr occurs when len(v) > Max.
type AttrOverflowErr struct {
Type AttrType
Max int
Got int
}
func (e AttrLengthError) Error() string {
func (e AttrOverflowErr) Error() string {
return fmt.Sprintf("Length of %s attribute %d exceeds maximum %d",
e.Type, e.Got, e.Max,
)
}
// AttrLengthErr means that length for attribute is invalid.
type AttrLengthErr struct {
Attr AttrType
Got int
Expected int
}
func (e AttrLengthErr) Error() string {
return fmt.Sprintf("incorrect length for %s: got %d, expected %d",
e.Attr,
e.Got,
e.Expected,
)
}
// STUN aligns attributes on 32-bit boundaries, attributes whose content
// is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
// padding so that its value contains a multiple of 4 bytes. The

View File

@ -61,7 +61,7 @@ func TestPadding(t *testing.T) {
}
func TestAttrLengthError_Error(t *testing.T) {
err := AttrLengthError{
err := AttrOverflowErr{
Got: 100,
Max: 50,
Type: AttrLifetime,

View File

@ -34,7 +34,11 @@ var ErrReasonLengthTooBig = errors.New("reason for ERROR-CODE is too big")
func (c ErrorCodeAttribute) AddTo(m *Message) error {
value := make([]byte, 0, errorCodeReasonMaxB)
if len(c.Reason) > errorCodeReasonMaxB {
return ErrReasonLengthTooBig
return &AttrOverflowErr{
Got: len(c.Reason) + errorCodeReasonStart,
Max: errorCodeReasonMaxB + errorCodeReasonStart,
Type: AttrErrorCode,
}
}
value = value[:errorCodeReasonStart+len(c.Reason)]
number := byte(c.Code % errorCodeModulo) // error code modulo 100

View File

@ -68,10 +68,11 @@ func (FingerprintAttr) Check(m *Message) error {
return err
}
if len(b) != fingerprintSize {
return newDecodeErr("message",
"fingerprint",
"bad length",
)
return &AttrLengthErr{
Expected: fingerprintSize,
Got: len(b),
Attr: AttrFingerprint,
}
}
val := bin.Uint32(b)
attrStart := len(m.Raw) - (fingerprintSize + attributeHeaderSize)

View File

@ -118,6 +118,10 @@ func FuzzSetters(data []byte) int {
// We allow decoding some text attributes
// when their length is too big, but
// not encoding.
_, ok := err.(*AttrOverflowErr)
if !ok {
panic(err)
}
return 1
}
m3.WriteHeader()

View File

@ -113,7 +113,7 @@ type TextAttribute []byte
// is less than 0, no check is performed.
func (v TextAttribute) AddToAs(m *Message, t AttrType, maxLen int) error {
if maxLen > 0 && len(v) > maxLen {
return &AttrLengthError{
return &AttrOverflowErr{
Max: maxLen,
Got: len(v),
Type: t,

View File

@ -38,8 +38,8 @@ func TestSoftware_GetFrom(t *testing.T) {
func TestSoftware_AddTo_Invalid(t *testing.T) {
m := New()
s := make(Software, 1024)
if err, ok := s.AddTo(m).(*AttrLengthError); !ok {
t.Errorf("AddTo should return *AttrLengthError, got: %v", err)
if err, ok := s.AddTo(m).(*AttrOverflowErr); !ok {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := s.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
@ -87,7 +87,7 @@ func TestUsername(t *testing.T) {
m.WriteHeader()
t.Run("Bad length", func(t *testing.T) {
badU := make(Username, 600)
if err, ok := badU.AddTo(m).(*AttrLengthError); !ok {
if err, ok := badU.AddTo(m).(*AttrOverflowErr); !ok {
t.Errorf("expected length error, got %v", err)
}
})
@ -163,8 +163,8 @@ func TestRealm_GetFrom(t *testing.T) {
func TestRealm_AddTo_Invalid(t *testing.T) {
m := New()
r := make(Realm, 1024)
if err, ok := r.AddTo(m).(*AttrLengthError); !ok || err.Type != AttrRealm {
t.Errorf("AddTo should return *AttrLengthError, got: %v", err)
if err, ok := r.AddTo(m).(*AttrOverflowErr); !ok || err.Type != AttrRealm {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := r.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)
@ -204,8 +204,8 @@ func TestNonce_GetFrom(t *testing.T) {
func TestNonce_AddTo_Invalid(t *testing.T) {
m := New()
n := make(Nonce, 1024)
if err, ok := n.AddTo(m).(*AttrLengthError); !ok || err.Type != AttrNonce {
t.Errorf("AddTo should return *AttrLengthError, got: %v", err)
if err, ok := n.AddTo(m).(*AttrOverflowErr); !ok || err.Type != AttrNonce {
t.Errorf("AddTo should return *AttrOverflowErr, got: %v", err)
}
if err := n.GetFrom(m); err != ErrAttributeNotFound {
t.Errorf("GetFrom should return %q, got: %v", ErrAttributeNotFound, err)