forked from free5gc/nas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nas_test.go
114 lines (101 loc) · 2.59 KB
/
nas_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
package nas
import (
"bytes"
"encoding/hex"
"io"
"math"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var hexString = "7e00560102000021e440b883d63a9f9c56b3703217152eba2010068f241c77748000b2180e54a9760068"
func TestNasGmmMessage(t *testing.T) {
data, _ := hex.DecodeString(hexString)
m := NewMessage()
err := m.GmmMessageDecode(&data)
assert.Nil(t, err)
buff := new(bytes.Buffer)
err = m.GmmMessageEncode(buff)
assert.Nil(t, err)
}
func TestNasGsmMessage(t *testing.T) {
data, _ := hex.DecodeString(hexString)
m := NewMessage()
err := m.GsmMessageDecode(&data)
assert.NotNil(t, err)
buff := new(bytes.Buffer)
err = m.GsmMessageEncode(buff)
assert.NotNil(t, err)
}
func TestPlainNas(t *testing.T) {
data, _ := hex.DecodeString(hexString)
m := NewMessage()
err := m.PlainNasDecode(&data)
assert.Nil(t, err)
buff, err1 := m.PlainNasEncode()
assert.Nil(t, err1)
if !reflect.DeepEqual(data, buff) {
t.Errorf("Expect : 0x%0x\nOutput: 0x%0x", data, buff)
}
}
func TestGmmMessage(t *testing.T) {
for _, tt := range testsGmmMessage {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
fData, err := os.Open("testdata/GmmMessage/" + tt.name)
require.NoError(t, err)
data, err := io.ReadAll(fData)
require.NoError(t, err)
err = fData.Close()
require.NoError(t, err)
var msg Message
err = msg.GmmMessageDecode(&data)
if assert.NoError(t, err) {
assert.Equal(t, tt.want, msg)
}
buf := new(bytes.Buffer)
err = tt.want.GmmMessageEncode(buf)
if assert.NoError(t, err) {
assert.Equal(t, data, buf.Bytes())
}
})
}
}
func TestGsmMessage(t *testing.T) {
for _, tt := range testsGsmMessage {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
fData, err := os.Open("testdata/GsmMessage/" + tt.name)
require.NoError(t, err)
data, err := io.ReadAll(fData)
require.NoError(t, err)
err = fData.Close()
require.NoError(t, err)
var msg Message
err = msg.GsmMessageDecode(&data)
if assert.NoError(t, err) {
assert.Equal(t, tt.want, msg)
}
buf := new(bytes.Buffer)
err = tt.want.GsmMessageEncode(buf)
if assert.NoError(t, err) {
assert.Equal(t, data, buf.Bytes())
}
})
}
}
var bufGenerateBufferSlice []uint8
func generateBufferSlice(l int) []uint8 {
bufGenerateBufferSlice = make([]byte, 256)
for i := 0; i < 256; i++ {
bufGenerateBufferSlice[i] = byte(i)
}
for len(bufGenerateBufferSlice) < math.MaxUint16 {
bufGenerateBufferSlice = append(bufGenerateBufferSlice, bufGenerateBufferSlice...)
}
return bufGenerateBufferSlice[:l]
}