From 6ebe032e0d8eb7633342ac9b5ef9b1f0fee62c49 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Fri, 16 Nov 2018 21:51:35 +0300 Subject: [PATCH] iana: add test for error codes and fix Code{RoleConflict,Unauthorized} --- api/except.txt | 1 + api/stun1.18.1.txt | 3 +++ e2e/main.go | 2 +- errorcode.go | 12 +++++++++--- iana_test.go | 31 ++++++++++++++++++++++++++++++- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 api/stun1.18.1.txt diff --git a/api/except.txt b/api/except.txt index 7b87fec..b12877a 100644 --- a/api/except.txt +++ b/api/except.txt @@ -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, RTO time.Duration pkg github.com/gortc/stun, type ClientOptions struct, TimeoutRate time.Duration +pkg github.com/gortc/stun, const CodeRoleConflict = 478 \ No newline at end of file diff --git a/api/stun1.18.1.txt b/api/stun1.18.1.txt new file mode 100644 index 0000000..e6c75d8 --- /dev/null +++ b/api/stun1.18.1.txt @@ -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 diff --git a/e2e/main.go b/e2e/main.go index d25a7c5..7151f0f 100644 --- a/e2e/main.go +++ b/e2e/main.go @@ -52,7 +52,7 @@ func test(network string) { if codeErr := errCode.GetFrom(response); codeErr != nil { log.Fatalln("failed to get error code:", codeErr) } - if errCode.Code != stun.CodeUnauthorised { + if errCode.Code != stun.CodeUnauthorized { log.Fatalln("unexpected error code:", errCode) } if parseErr := response.Parse(&nonce, &realm); parseErr != nil { diff --git a/errorcode.go b/errorcode.go index af3a01e..8095048 100644 --- a/errorcode.go +++ b/errorcode.go @@ -90,13 +90,19 @@ func (c ErrorCode) AddTo(m *Message) error { const ( CodeTryAlternate ErrorCode = 300 CodeBadRequest ErrorCode = 400 - CodeUnauthorised ErrorCode = 401 + CodeUnauthorized ErrorCode = 401 CodeUnknownAttribute ErrorCode = 420 CodeStaleNonce ErrorCode = 438 - CodeRoleConflict ErrorCode = 478 + CodeRoleConflict ErrorCode = 487 CodeServerError ErrorCode = 500 ) +// DEPRECATED constants. +const ( + // DEPRECATED, use CodeUnauthorized. + CodeUnauthorised = CodeUnauthorized +) + // Error codes from RFC 5766. // // RFC 5766 Section 15 @@ -128,7 +134,7 @@ const ( var errorReasons = map[ErrorCode][]byte{ CodeTryAlternate: []byte("Try Alternate"), CodeBadRequest: []byte("Bad Request"), - CodeUnauthorised: []byte("Unauthorised"), + CodeUnauthorized: []byte("Unauthorized"), CodeUnknownAttribute: []byte("Unknown Attribute"), CodeStaleNonce: []byte("Stale Nonce"), CodeServerError: []byte("Server Error"), diff --git a/iana_test.go b/iana_test.go index 56cc053..df6f22c 100644 --- a/iana_test.go +++ b/iana_test.go @@ -75,7 +75,36 @@ func TestIANA(t *testing.T) { for val, name := range attrNames { mapped, ok := m[name] 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 { t.Errorf("%s: IANA %d != actual %d", name, mapped, val)