-
Notifications
You must be signed in to change notification settings - Fork 79
/
gtp_test.go
102 lines (89 loc) · 2.41 KB
/
gtp_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
// Copyright 2019-2024 go-gtp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package gtp
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/pascaldekloe/goe/verify"
v0msg "github.com/wmnsk/go-gtp/gtpv0/message"
v1msg "github.com/wmnsk/go-gtp/gtpv1/message"
v2ie "github.com/wmnsk/go-gtp/gtpv2/ie"
v2msg "github.com/wmnsk/go-gtp/gtpv2/message"
)
var v0flow = struct {
seq uint16
label uint16
tid uint64
}{1, 0, 0x2143658709214355}
func TestMessage(t *testing.T) {
cases := []struct {
description string
structured Message
serialized []byte
}{
{
"GTPv0 Echo Request",
v0msg.NewEchoRequest(v0flow.seq, v0flow.label, v0flow.tid),
[]byte{
0x1e, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0x21, 0x43, 0x65, 0x87,
0x09, 0x21, 0x43, 0x55,
},
}, {
"GTPv1 Echo Request",
v1msg.NewEchoRequest(0),
[]byte{
0x32, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
},
}, {
"GTPv2 Echo Request",
v2msg.NewEchoRequest(0, v2ie.NewRecovery(0x80)),
[]byte{
0x40, 0x01, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x01, 0x00, 0x80,
},
},
}
for _, c := range cases {
t.Run("Encode/"+c.description, func(t *testing.T) {
got, err := Marshal(c.structured)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(got, c.serialized); diff != "" {
t.Error(diff)
}
})
t.Run("Parse/"+c.description, func(t *testing.T) {
v, err := Parse(c.serialized)
if err != nil {
t.Fatal(err)
}
if got, want := v, c.structured; !verify.Values(t, "", got, want) {
t.Fail()
}
})
t.Run("MarshalLen/"+c.description, func(t *testing.T) {
if got, want := c.structured.MarshalLen(), len(c.serialized); got != want {
t.Fatalf("got %v want %v", got, want)
}
})
t.Run("Interface/"+c.description, func(t *testing.T) {
decoded, err := Parse(c.serialized)
if err != nil {
t.Fatal(err)
}
if got, want := decoded.Version(), c.structured.Version(); got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := decoded.MessageType(), c.structured.MessageType(); got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := decoded.MessageTypeName(), c.structured.MessageTypeName(); got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}