mirror of
https://github.com/gortc/stun.git
synced 2025-09-26 20:41:36 +08:00
iana: add test for error codes and fix Code{RoleConflict,Unauthorized}
This commit is contained in:
@@ -22,3 +22,4 @@ pkg github.com/gortc/stun, type ClientOptions struct, Connection Connection
|
|||||||
pkg github.com/gortc/stun, type ClientOptions struct, Handler Handler
|
pkg github.com/gortc/stun, type ClientOptions struct, Handler Handler
|
||||||
pkg github.com/gortc/stun, type ClientOptions struct, RTO time.Duration
|
pkg github.com/gortc/stun, type ClientOptions struct, RTO time.Duration
|
||||||
pkg github.com/gortc/stun, type ClientOptions struct, TimeoutRate time.Duration
|
pkg github.com/gortc/stun, type ClientOptions struct, TimeoutRate time.Duration
|
||||||
|
pkg github.com/gortc/stun, const CodeRoleConflict = 478
|
3
api/stun1.18.1.txt
Normal file
3
api/stun1.18.1.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pkg github.com/gortc/stun, const CodeRoleConflict = 487
|
||||||
|
pkg github.com/gortc/stun, const CodeUnauthorized = 401
|
||||||
|
pkg github.com/gortc/stun, const CodeUnauthorized ErrorCode
|
@@ -52,7 +52,7 @@ func test(network string) {
|
|||||||
if codeErr := errCode.GetFrom(response); codeErr != nil {
|
if codeErr := errCode.GetFrom(response); codeErr != nil {
|
||||||
log.Fatalln("failed to get error code:", codeErr)
|
log.Fatalln("failed to get error code:", codeErr)
|
||||||
}
|
}
|
||||||
if errCode.Code != stun.CodeUnauthorised {
|
if errCode.Code != stun.CodeUnauthorized {
|
||||||
log.Fatalln("unexpected error code:", errCode)
|
log.Fatalln("unexpected error code:", errCode)
|
||||||
}
|
}
|
||||||
if parseErr := response.Parse(&nonce, &realm); parseErr != nil {
|
if parseErr := response.Parse(&nonce, &realm); parseErr != nil {
|
||||||
|
12
errorcode.go
12
errorcode.go
@@ -90,13 +90,19 @@ func (c ErrorCode) AddTo(m *Message) error {
|
|||||||
const (
|
const (
|
||||||
CodeTryAlternate ErrorCode = 300
|
CodeTryAlternate ErrorCode = 300
|
||||||
CodeBadRequest ErrorCode = 400
|
CodeBadRequest ErrorCode = 400
|
||||||
CodeUnauthorised ErrorCode = 401
|
CodeUnauthorized ErrorCode = 401
|
||||||
CodeUnknownAttribute ErrorCode = 420
|
CodeUnknownAttribute ErrorCode = 420
|
||||||
CodeStaleNonce ErrorCode = 438
|
CodeStaleNonce ErrorCode = 438
|
||||||
CodeRoleConflict ErrorCode = 478
|
CodeRoleConflict ErrorCode = 487
|
||||||
CodeServerError ErrorCode = 500
|
CodeServerError ErrorCode = 500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DEPRECATED constants.
|
||||||
|
const (
|
||||||
|
// DEPRECATED, use CodeUnauthorized.
|
||||||
|
CodeUnauthorised = CodeUnauthorized
|
||||||
|
)
|
||||||
|
|
||||||
// Error codes from RFC 5766.
|
// Error codes from RFC 5766.
|
||||||
//
|
//
|
||||||
// RFC 5766 Section 15
|
// RFC 5766 Section 15
|
||||||
@@ -128,7 +134,7 @@ const (
|
|||||||
var errorReasons = map[ErrorCode][]byte{
|
var errorReasons = map[ErrorCode][]byte{
|
||||||
CodeTryAlternate: []byte("Try Alternate"),
|
CodeTryAlternate: []byte("Try Alternate"),
|
||||||
CodeBadRequest: []byte("Bad Request"),
|
CodeBadRequest: []byte("Bad Request"),
|
||||||
CodeUnauthorised: []byte("Unauthorised"),
|
CodeUnauthorized: []byte("Unauthorized"),
|
||||||
CodeUnknownAttribute: []byte("Unknown Attribute"),
|
CodeUnknownAttribute: []byte("Unknown Attribute"),
|
||||||
CodeStaleNonce: []byte("Stale Nonce"),
|
CodeStaleNonce: []byte("Stale Nonce"),
|
||||||
CodeServerError: []byte("Server Error"),
|
CodeServerError: []byte("Server Error"),
|
||||||
|
31
iana_test.go
31
iana_test.go
@@ -75,7 +75,36 @@ func TestIANA(t *testing.T) {
|
|||||||
for val, name := range attrNames {
|
for val, name := range attrNames {
|
||||||
mapped, ok := m[name]
|
mapped, ok := m[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("failed to find method %s in IANA", name)
|
t.Errorf("failed to find attribute %s in IANA", name)
|
||||||
|
}
|
||||||
|
if mapped != val {
|
||||||
|
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("ErrorCodes", func(t *testing.T) {
|
||||||
|
records := loadCSV(t, "stun-parameters-6.csv")
|
||||||
|
m := map[string]ErrorCode{}
|
||||||
|
for _, r := range records[1:] {
|
||||||
|
var (
|
||||||
|
v = r[0]
|
||||||
|
name = r[1]
|
||||||
|
)
|
||||||
|
if strings.Contains(v, "-") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val, parseErr := strconv.ParseInt(v, 10, 64)
|
||||||
|
if parseErr != nil {
|
||||||
|
t.Fatal(parseErr)
|
||||||
|
}
|
||||||
|
t.Logf("value: 0x%x, name: %s", val, name)
|
||||||
|
m[name] = ErrorCode(val)
|
||||||
|
}
|
||||||
|
for val, nameB := range errorReasons {
|
||||||
|
name := string(nameB)
|
||||||
|
mapped, ok := m[name]
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("failed to find error code %s in IANA", name)
|
||||||
}
|
}
|
||||||
if mapped != val {
|
if mapped != val {
|
||||||
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)
|
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)
|
||||||
|
Reference in New Issue
Block a user