checks_debug.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // +build debug
  2. package stun
  3. import "github.com/pion/stun/internal/hmac"
  4. // CheckSize returns *AttrLengthError if got is not equal to expected.
  5. func CheckSize(a AttrType, got, expected int) error {
  6. if got == expected {
  7. return nil
  8. }
  9. return &AttrLengthErr{
  10. Got: got,
  11. Expected: expected,
  12. Attr: a,
  13. }
  14. }
  15. func checkHMAC(got, expected []byte) error {
  16. if hmac.Equal(got, expected) {
  17. return nil
  18. }
  19. return &IntegrityErr{
  20. Expected: expected,
  21. Actual: got,
  22. }
  23. }
  24. func checkFingerprint(got, expected uint32) error {
  25. if got == expected {
  26. return nil
  27. }
  28. return &CRCMismatch{
  29. Actual: got,
  30. Expected: expected,
  31. }
  32. }
  33. // IsAttrSizeInvalid returns true if error means that attribute size is invalid.
  34. func IsAttrSizeInvalid(err error) bool {
  35. _, ok := err.(*AttrLengthErr)
  36. return ok
  37. }
  38. // CheckOverflow returns *AttrOverflowErr if got is bigger that max.
  39. func CheckOverflow(t AttrType, got, max int) error {
  40. if got <= max {
  41. return nil
  42. }
  43. return &AttrOverflowErr{
  44. Type: t,
  45. Got: got,
  46. Max: max,
  47. }
  48. }
  49. // IsAttrSizeOverflow returns true if error means that attribute size is too big.
  50. func IsAttrSizeOverflow(err error) bool {
  51. _, ok := err.(*AttrOverflowErr)
  52. return ok
  53. }