-
Notifications
You must be signed in to change notification settings - Fork 96
/
iana_test.go
121 lines (116 loc) · 2.55 KB
/
iana_test.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package stun
import (
"bytes"
"encoding/csv"
"strconv"
"strings"
"testing"
)
func loadCSV(t testing.TB, name string) [][]string {
t.Helper()
data := loadData(t, name)
r := csv.NewReader(bytes.NewReader(data))
r.Comment = '#'
records, err := r.ReadAll()
if err != nil {
t.Fatal(err)
}
return records
}
func TestIANA(t *testing.T) {
t.Run("Methods", func(t *testing.T) {
records := loadCSV(t, "stun-parameters-2.csv")
m := 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)
if parseErr != nil {
t.Fatal(parseErr)
}
t.Logf("value: 0x%x, name: %s", val, name)
m[name] = Method(val)
}
for val, name := range methodName() {
mapped, ok := m[name]
if !ok {
t.Errorf("failed to find method %s in IANA", name)
}
if mapped != val {
t.Errorf("%s: IANA %d != actual %d", name, mapped, val)
}
}
})
t.Run("Attributes", func(t *testing.T) {
records := loadCSV(t, "stun-parameters-4.csv")
m := 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)
if parseErr != nil {
t.Fatal(parseErr)
}
t.Logf("value: 0x%x, name: %s", val, name)
m[name] = AttrType(val)
}
// Not registered in IANA.
for k, v := range map[string]AttrType{
"ORIGIN": 0x802F,
} {
m[k] = v
}
for val, name := range attrNames() {
mapped, ok := m[name]
if !ok {
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)
}
}
})
}