Skip to content
This repository was archived by the owner on Jun 5, 2021. It is now read-only.

Commit 6ebe032

Browse files
committed
iana: add test for error codes and fix Code{RoleConflict,Unauthorized}
1 parent 7539bdf commit 6ebe032

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

api/except.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ pkg github.com/gortc/stun, type ClientOptions struct, Connection Connection
2222
pkg github.com/gortc/stun, type ClientOptions struct, Handler Handler
2323
pkg github.com/gortc/stun, type ClientOptions struct, RTO time.Duration
2424
pkg github.com/gortc/stun, type ClientOptions struct, TimeoutRate time.Duration
25+
pkg github.com/gortc/stun, const CodeRoleConflict = 478

api/stun1.18.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pkg github.com/gortc/stun, const CodeRoleConflict = 487
2+
pkg github.com/gortc/stun, const CodeUnauthorized = 401
3+
pkg github.com/gortc/stun, const CodeUnauthorized ErrorCode

e2e/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func test(network string) {
5252
if codeErr := errCode.GetFrom(response); codeErr != nil {
5353
log.Fatalln("failed to get error code:", codeErr)
5454
}
55-
if errCode.Code != stun.CodeUnauthorised {
55+
if errCode.Code != stun.CodeUnauthorized {
5656
log.Fatalln("unexpected error code:", errCode)
5757
}
5858
if parseErr := response.Parse(&nonce, &realm); parseErr != nil {

errorcode.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,19 @@ func (c ErrorCode) AddTo(m *Message) error {
9090
const (
9191
CodeTryAlternate ErrorCode = 300
9292
CodeBadRequest ErrorCode = 400
93-
CodeUnauthorised ErrorCode = 401
93+
CodeUnauthorized ErrorCode = 401
9494
CodeUnknownAttribute ErrorCode = 420
9595
CodeStaleNonce ErrorCode = 438
96-
CodeRoleConflict ErrorCode = 478
96+
CodeRoleConflict ErrorCode = 487
9797
CodeServerError ErrorCode = 500
9898
)
9999

100+
// DEPRECATED constants.
101+
const (
102+
// DEPRECATED, use CodeUnauthorized.
103+
CodeUnauthorised = CodeUnauthorized
104+
)
105+
100106
// Error codes from RFC 5766.
101107
//
102108
// RFC 5766 Section 15
@@ -128,7 +134,7 @@ const (
128134
var errorReasons = map[ErrorCode][]byte{
129135
CodeTryAlternate: []byte("Try Alternate"),
130136
CodeBadRequest: []byte("Bad Request"),
131-
CodeUnauthorised: []byte("Unauthorised"),
137+
CodeUnauthorized: []byte("Unauthorized"),
132138
CodeUnknownAttribute: []byte("Unknown Attribute"),
133139
CodeStaleNonce: []byte("Stale Nonce"),
134140
CodeServerError: []byte("Server Error"),

iana_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,36 @@ func TestIANA(t *testing.T) {
7575
for val, name := range attrNames {
7676
mapped, ok := m[name]
7777
if !ok {
78-
t.Errorf("failed to find method %s in IANA", name)
78+
t.Errorf("failed to find attribute %s in IANA", name)
79+
}
80+
if mapped != val {
81+
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)
82+
}
83+
}
84+
})
85+
t.Run("ErrorCodes", func(t *testing.T) {
86+
records := loadCSV(t, "stun-parameters-6.csv")
87+
m := map[string]ErrorCode{}
88+
for _, r := range records[1:] {
89+
var (
90+
v = r[0]
91+
name = r[1]
92+
)
93+
if strings.Contains(v, "-") {
94+
continue
95+
}
96+
val, parseErr := strconv.ParseInt(v, 10, 64)
97+
if parseErr != nil {
98+
t.Fatal(parseErr)
99+
}
100+
t.Logf("value: 0x%x, name: %s", val, name)
101+
m[name] = ErrorCode(val)
102+
}
103+
for val, nameB := range errorReasons {
104+
name := string(nameB)
105+
mapped, ok := m[name]
106+
if !ok {
107+
t.Errorf("failed to find error code %s in IANA", name)
79108
}
80109
if mapped != val {
81110
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)

0 commit comments

Comments
 (0)