-
Notifications
You must be signed in to change notification settings - Fork 14
/
sccp_test.go
213 lines (192 loc) · 5.21 KB
/
sccp_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2019-2024 go-sccp 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 sccp_test
import (
"encoding"
"io"
"log"
"strings"
"testing"
"github.com/pascaldekloe/goe/verify"
"github.com/wmnsk/go-sccp"
"github.com/wmnsk/go-sccp/params"
)
type serializable interface {
encoding.BinaryMarshaler
MarshalLen() int
}
var testcases = []struct {
description string
structured serializable
serialized []byte
decodeFunc func([]byte) (serializable, error)
}{
{
description: "Header",
structured: sccp.NewHeader(0, []byte{0xde, 0xad, 0xbe, 0xef}),
serialized: []byte{0x00, 0xde, 0xad, 0xbe, 0xef},
decodeFunc: func(b []byte) (serializable, error) {
v, err := sccp.ParseHeader(b)
if err != nil {
return nil, err
}
return v, nil
},
}, {
description: "UDT",
structured: sccp.NewUDT(
1, // Protocol Class
true, // Message handling
params.NewPartyAddress( // CalledPartyAddress
0x12, 0, 6, 0x00, // Indicator, SPC, SSN, TT
0x01, 0x01, 0x04, // NP, ES, NAI
[]byte{0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0x65},
),
params.NewPartyAddress( // CallingPartyAddress
0x12, 0, 7, 0x00, // Indicator, SPC, SSN, TT
0x01, 0x02, 0x04, // NP, ES, NAI
[]byte{0x89, 0x67, 0x45, 0x23, 0x01},
),
[]byte{0xde, 0xad, 0xbe, 0xef},
),
serialized: []byte{
0x09, 0x81, 0x03, 0x10, 0x1a, 0x0d, 0x12, 0x06, 0x00, 0x11, 0x04, 0x21, 0x43, 0x65, 0x87, 0x09,
0x21, 0x43, 0x65, 0x0a, 0x12, 0x07, 0x00, 0x12, 0x04, 0x89, 0x67, 0x45, 0x23, 0x01, 0x04, 0xde,
0xad, 0xbe, 0xef,
},
decodeFunc: func(b []byte) (serializable, error) {
v, err := sccp.ParseUDT(b)
if err != nil {
return nil, err
}
return v, nil
},
},
{
description: "UDT-2Bytes-PartyAddress",
structured: sccp.NewUDT(
1, // Protocol Class
true, // Message handling
params.NewPartyAddress( // CalledPartyAddress: 1234567890123456
0x42, 0, 6, 0x00, // Indicator, SPC, SSN, TT
0x00, 0x00, 0x00, // NP, ES, NAI
nil, // GlobalTitleInformation
),
params.NewPartyAddress( // CalledPartyAddress: 1234567890123456
0x42, 0, 7, 0x00, // Indicator, SPC, SSN, TT
0x00, 0x00, 0x00, // NP, ES, NAI
nil, // GlobalTitleInformation
),
nil,
),
serialized: []byte{
0x09, 0x81, 0x03, 0x05, 0x07, 0x02, 0x42, 0x06, 0x02, 0x42, 0x07, 0x00,
},
decodeFunc: func(b []byte) (serializable, error) {
v, err := sccp.ParseUDT(b)
if err != nil {
return nil, err
}
return v, nil
},
},
{
description: "SCMG SSA",
structured: sccp.NewSCMG(sccp.SCMGTypeSSA, 9, 405, 0, 0),
serialized: []byte{0x1, 0x09, 0x95, 0x01, 0x00},
decodeFunc: func(b []byte) (serializable, error) {
v, err := sccp.ParseSCMG(b)
if err != nil {
return nil, err
}
return v, nil
},
},
{
description: "SCMG SSC",
structured: sccp.NewSCMG(sccp.SCMGTypeSSC, 9, 405, 0, 4),
serialized: []byte{0x6, 0x09, 0x95, 0x01, 0x00, 0x04},
decodeFunc: func(b []byte) (serializable, error) {
v, err := sccp.ParseSCMG(b)
if err != nil {
return nil, err
}
return v, nil
},
},
}
func TestMessages(t *testing.T) {
t.Helper()
for _, c := range testcases {
t.Run(c.description, func(t *testing.T) {
t.Run("Decode", func(t *testing.T) {
msg, err := c.decodeFunc(c.serialized)
if err != nil {
t.Fatal(err)
}
if got, want := msg, c.structured; !verify.Values(t, "", got, want) {
t.Fail()
}
})
t.Run("Serialize", func(t *testing.T) {
b, err := c.structured.MarshalBinary()
if err != nil {
log.Println(err)
t.Fatal(err)
}
if got, want := b, c.serialized; !verify.Values(t, "", got, want) {
t.Fail()
}
})
t.Run("Len", 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", func(t *testing.T) {
// Ignore *Header and Generic in this tests.
if _, ok := c.structured.(*sccp.Header); ok {
return
}
if _, ok := c.structured.(*sccp.SCMG); ok {
return
}
decoded, err := sccp.ParseMessage(c.serialized)
if err != nil {
t.Fatal(err)
}
if got, want := decoded.MessageType(), c.structured.(sccp.Message).MessageType(); got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := decoded.MessageTypeName(), c.structured.(sccp.Message).MessageTypeName(); got != want {
t.Fatalf("got %v want %v", got, want)
}
})
})
}
}
func TestPartialStructuredMessages(t *testing.T) {
for _, c := range testcases {
if c.description == "Header" || strings.Contains(c.description, "SCMG") {
// TODO: consider removing Header struct as it's almost useless.
continue
}
for i := range c.serialized {
partial := c.serialized[:i]
_, err := c.decodeFunc(partial)
if err != io.ErrUnexpectedEOF {
t.Errorf("%#x: got error %v, want unexpected EOF", partial, err)
}
}
for i := range c.serialized {
if i == len(c.serialized) {
continue
}
b := make([]byte, i)
if err := c.structured.(*sccp.UDT).MarshalTo(b); err != io.ErrUnexpectedEOF {
t.Errorf("%#x: got error %v, want unexpected EOF", b, err)
}
}
}
}