-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathiana_test.go
More file actions
103 lines (95 loc) · 2.59 KB
/
iana_test.go
File metadata and controls
103 lines (95 loc) · 2.59 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
package stun
import (
"bytes"
"encoding/csv"
"maps"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func loadCSV(tb testing.TB, name string) [][]string {
tb.Helper()
data := loadData(tb, name)
r := csv.NewReader(bytes.NewReader(data))
r.Comment = '#'
records, err := r.ReadAll()
assert.NoError(tb, err)
return records
}
func TestIANA(t *testing.T) { //nolint:cyclop
t.Run("Methods", func(t *testing.T) {
records := loadCSV(t, "stun-parameters-2.csv")
methods := make(map[string]Method)
for _, r := range records[1:] {
var (
v = r[0]
name = r[1]
)
if strings.Contains(v, "-") {
continue
}
val, parseErr := strconv.ParseInt(v[2:], 16, 64)
assert.NoError(t, parseErr)
t.Logf("value: 0x%x, name: %s", val, name)
methods[name] = Method(val) //nolint:gosec // G115
}
for val, name := range methodName() {
mapped, ok := methods[name]
assert.True(t, ok, "failed to find method %s in IANA", name)
assert.Equal(t, mapped, val, "%s: IANA %d != actual %d", name, mapped, val)
}
})
t.Run("Attributes", func(t *testing.T) {
records := loadCSV(t, "stun-parameters-4.csv")
attrTypes := map[string]AttrType{}
for _, r := range records[1:] {
var (
v = r[0]
name = r[1]
)
if strings.Contains(v, "-") {
continue
}
val, parseErr := strconv.ParseInt(v[2:], 16, 64)
assert.NoError(t, parseErr)
t.Logf("value: 0x%x, name: %s", val, name)
attrTypes[name] = AttrType(val) //nolint:gosec // G115
}
// Not registered in IANA.
maps.Copy(attrTypes, map[string]AttrType{
"ORIGIN": 0x802F,
})
for val, name := range attrNames() {
mapped, ok := attrTypes[name]
assert.True(t, ok, "failed to find attribute %s in IANA", name)
assert.Equal(t, mapped, val, "%s: IANA %d != actual %d", name, mapped, val)
}
})
t.Run("ErrorCodes", func(t *testing.T) {
records := loadCSV(t, "stun-parameters-6.csv")
errorCodes := 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)
assert.NoError(t, parseErr)
t.Logf("value: 0x%x, name: %s", val, name)
errorCodes[name] = ErrorCode(val)
}
for val, nameB := range errorReasons {
name := string(nameB)
mapped, ok := errorCodes[name]
assert.True(t, ok, "failed to find error code %s in IANA", name)
assert.Equal(t, mapped, val, "%s: IANA %d != actual %d", name, mapped, val)
}
})
}