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