forked from wmnsk/go-gtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp_test.go
102 lines (89 loc) · 2.43 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-2020 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/v0/messages"
v1msg "github.com/wmnsk/go-gtp/v1/messages"
v2ie "github.com/wmnsk/go-gtp/v2/ies"
v2msg "github.com/wmnsk/go-gtp/v2/messages"
)
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.(Message).Version(); got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := decoded.MessageType(), c.structured.(Message).MessageType(); got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := decoded.MessageTypeName(), c.structured.(Message).MessageTypeName(); got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}