checks_debug.go 1.3 KB

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