-
Notifications
You must be signed in to change notification settings - Fork 26
/
dataheader_test.go
225 lines (180 loc) · 5.25 KB
/
dataheader_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
214
215
216
217
218
219
220
221
222
223
224
225
package dmr
import (
"encoding/hex"
"testing"
)
func testDataHeader(want *DataHeader, t *testing.T) *DataHeader {
want.SrcID = 2042214
want.DstID = 2043044
t.Logf("encode:\n%s", want.String())
data, err := want.Bytes()
if err != nil {
t.Fatalf("encode failed: %v", err)
}
t.Logf("encoded:\n%s", hex.Dump(data))
test, err := ParseDataHeader(data, false)
if err != nil {
t.Fatalf("decode failed: %v", err)
}
if test.SrcID != want.SrcID || test.DstID != want.DstID {
t.Fatal("decode failed, ID wrong")
}
t.Logf("decoded:\n%s", test.String())
return test
}
func TestDataHeaderUDT(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatUDT,
Data: &UDTData{
Format: UDTFormatIPAddress,
PadNibble: 2,
AppendedBlocks: 3,
Opcode: 4,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*UDTData)
switch {
case !ok:
t.Fatalf("decode failed: expected UDTData, got %T", test.Data)
case d.Format != UDTFormatIPAddress:
t.Fatalf("decode failed: format wrong")
case d.PadNibble != 2:
t.Fatalf("decode failed: pad nibble wrong")
case d.AppendedBlocks != 3:
t.Fatalf("decode failed: appended blocks wrong")
case d.Opcode != 4:
t.Fatalf("decode failed: opcode wrong")
}
}
func TestDataHeaderResponse(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatResponse,
Data: &ResponseData{
BlocksToFollow: 0x10,
ClassType: ResponseTypeSelectiveACK,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*ResponseData)
switch {
case !ok:
t.Fatalf("decode failed: expected ResponseData, got %T", test.Data)
case d.BlocksToFollow != 0x10:
t.Fatalf("decode failed: wrong blocks %d, expected 16", d.BlocksToFollow)
case d.ClassType != ResponseTypeSelectiveACK:
t.Fatalf("decode failed: wrong type %s (%d), expected selective ACK", ResponseTypeName[d.ClassType], d.ClassType)
}
}
func TestDataHeaderUnconfirmedData(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatUnconfirmedData,
Data: &UnconfirmedData{
PadOctetCount: 2,
FullMessage: true,
BlocksToFollow: 5,
FragmentSequenceNumber: 3,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*UnconfirmedData)
switch {
case !ok:
t.Fatalf("decode failed: expected UnconfirmedData, got %T", test.Data)
case d.PadOctetCount != 2:
t.Fatalf("decode failed: pad octet count wrong")
case !d.FullMessage:
t.Fatalf("decode failed: full message bit wrong")
case d.BlocksToFollow != 5:
t.Fatalf("decode failed: blocks to follow wrong")
case d.FragmentSequenceNumber != 3:
t.Fatalf("decode failed: fragment sequence number wrong")
}
}
func TestDataHeaderConfirmedData(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatConfirmedData,
Data: &ConfirmedData{
PadOctetCount: 2,
FullMessage: true,
BlocksToFollow: 5,
SendSequenceNumber: 4,
FragmentSequenceNumber: 3,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*ConfirmedData)
switch {
case !ok:
t.Fatalf("decode failed: expected ConfirmedData, got %T", test.Data)
case d.PadOctetCount != 2:
t.Fatalf("decode failed: pad octet count wrong")
case !d.FullMessage:
t.Fatalf("decode failed: full message bit wrong")
case d.Resync:
t.Fatalf("decode failed: resync bit wrong")
case d.BlocksToFollow != 5:
t.Fatalf("decode failed: blocks to follow wrong")
case d.SendSequenceNumber != 4:
t.Fatalf("decode failed: fragment sequence number wrong")
case d.FragmentSequenceNumber != 3:
t.Fatalf("decode failed: fragment sequence number wrong")
}
}
func TestDataHeaderShortDataRaw(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatShortDataRaw,
Data: &ShortDataRawData{
AppendedBlocks: 3,
SrcPort: 4,
DstPort: 5,
FullMessage: true,
BitPadding: 2,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*ShortDataRawData)
switch {
case !ok:
t.Fatalf("decode failed: expected ShortDataRawData, got %T", test.Data)
case d.AppendedBlocks != 3:
t.Fatalf("decode failed: appended blocks wrong")
case d.SrcPort != 4:
t.Fatalf("decode failed: src port wrong")
case d.DstPort != 5:
t.Fatalf("decode failed: dst port wrong")
case d.Resync:
t.Fatalf("decode failed: rsync bit wrong")
case !d.FullMessage:
t.Fatalf("decode failed: full message bit wrong")
case d.BitPadding != 2:
t.Fatalf("decode failed: bit padding wrong")
}
}
func TestDataHeaderShortDataDefined(t *testing.T) {
want := &DataHeader{
PacketFormat: PacketFormatShortDataDefined,
Data: &ShortDataDefinedData{
AppendedBlocks: 3,
DDFormat: DDFormatUTF16,
FullMessage: true,
BitPadding: 2,
},
}
test := testDataHeader(want, t)
d, ok := test.Data.(*ShortDataDefinedData)
switch {
case !ok:
t.Fatalf("decode failed: expected ShortDataDefinedData, got %T", test.Data)
case d.AppendedBlocks != 3:
t.Fatalf("decode failed: appended blocks wrong")
case d.DDFormat != DDFormatUTF16:
t.Fatalf("decode failed: dd format wrong, expected UTF-16, got %s", DDFormatName[d.DDFormat])
case d.Resync:
t.Fatalf("decode failed: rsync bit wrong")
case !d.FullMessage:
t.Fatalf("decode failed: full message bit wrong")
case d.BitPadding != 2:
t.Fatalf("decode failed: bit padding wrong")
}
}