-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_test.go
More file actions
301 lines (277 loc) · 7.46 KB
/
header_test.go
File metadata and controls
301 lines (277 loc) · 7.46 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package epp_test
import (
"testing"
"github.com/ravisuhag/astro/pkg/epp"
)
func TestHeaderFormat(t *testing.T) {
tests := []struct {
name string
protocolID uint8
lengthOfLength uint8
wantFormat int
wantSize int
}{
{"idle", epp.ProtocolIDIdle, 0, 1, 1},
{"short IPE", epp.ProtocolIDIPE, 0, 2, 2},
{"short user-defined", epp.ProtocolIDUserDef, 0, 2, 2},
{"medium IPE", epp.ProtocolIDIPE, 1, 3, 4},
{"medium user-defined", epp.ProtocolIDUserDef, 1, 3, 4},
{"extended medium", epp.ProtocolIDExtended, 0, 4, 4},
{"extended long", epp.ProtocolIDExtended, 1, 5, 8},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: tt.protocolID,
LengthOfLength: tt.lengthOfLength,
}
if got := h.Format(); got != tt.wantFormat {
t.Errorf("Format() = %d, want %d", got, tt.wantFormat)
}
if got := h.Size(); got != tt.wantSize {
t.Errorf("Size() = %d, want %d", got, tt.wantSize)
}
})
}
}
func TestHeaderEncodeDecodeFormat1(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDIdle,
LengthOfLength: 0,
PacketLength: 1,
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if len(encoded) != 1 {
t.Fatalf("Expected 1 byte, got %d", len(encoded))
}
// First nibble should be 0111 = 0x70, PID=000, LoL=0
if encoded[0] != 0x70 {
t.Errorf("Expected 0x70, got 0x%02X", encoded[0])
}
var decoded epp.Header
if err := decoded.Decode(encoded); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.PVN != epp.PVN {
t.Errorf("PVN = %d, want %d", decoded.PVN, epp.PVN)
}
if decoded.ProtocolID != epp.ProtocolIDIdle {
t.Errorf("ProtocolID = %d, want %d", decoded.ProtocolID, epp.ProtocolIDIdle)
}
}
func TestHeaderEncodeDecodeFormat2(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDIPE,
LengthOfLength: 0,
PacketLength: 200,
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if len(encoded) != 2 {
t.Fatalf("Expected 2 bytes, got %d", len(encoded))
}
var decoded epp.Header
if err := decoded.Decode(encoded); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.ProtocolID != epp.ProtocolIDIPE {
t.Errorf("ProtocolID = %d, want %d", decoded.ProtocolID, epp.ProtocolIDIPE)
}
if decoded.PacketLength != 200 {
t.Errorf("PacketLength = %d, want 200", decoded.PacketLength)
}
}
func TestHeaderEncodeDecodeFormat3(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDIPE,
LengthOfLength: 1,
UserDefined: 0xAB,
PacketLength: 5000,
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if len(encoded) != 4 {
t.Fatalf("Expected 4 bytes, got %d", len(encoded))
}
var decoded epp.Header
if err := decoded.Decode(encoded); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.UserDefined != 0xAB {
t.Errorf("UserDefined = 0x%02X, want 0xAB", decoded.UserDefined)
}
if decoded.PacketLength != 5000 {
t.Errorf("PacketLength = %d, want 5000", decoded.PacketLength)
}
}
func TestHeaderEncodeDecodeFormat4(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDExtended,
LengthOfLength: 0,
ExtendedProtocolID: 42,
PacketLength: 1024,
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if len(encoded) != 4 {
t.Fatalf("Expected 4 bytes, got %d", len(encoded))
}
var decoded epp.Header
if err := decoded.Decode(encoded); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.ExtendedProtocolID != 42 {
t.Errorf("ExtendedProtocolID = %d, want 42", decoded.ExtendedProtocolID)
}
if decoded.PacketLength != 1024 {
t.Errorf("PacketLength = %d, want 1024", decoded.PacketLength)
}
}
func TestHeaderEncodeDecodeFormat5(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDExtended,
LengthOfLength: 1,
ExtendedProtocolID: 99,
CCSDSDefined: 0x1234,
PacketLength: 100000,
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if len(encoded) != 8 {
t.Fatalf("Expected 8 bytes, got %d", len(encoded))
}
var decoded epp.Header
if err := decoded.Decode(encoded); err != nil {
t.Fatalf("Decode failed: %v", err)
}
if decoded.ExtendedProtocolID != 99 {
t.Errorf("ExtendedProtocolID = %d, want 99", decoded.ExtendedProtocolID)
}
if decoded.CCSDSDefined != 0x1234 {
t.Errorf("CCSDSDefined = 0x%04X, want 0x1234", decoded.CCSDSDefined)
}
if decoded.PacketLength != 100000 {
t.Errorf("PacketLength = %d, want 100000", decoded.PacketLength)
}
}
func TestHeaderDecodeInvalidPVN(t *testing.T) {
// PVN=0 (SPP-like), not 7
data := []byte{0x00, 0x00}
var h epp.Header
err := h.Decode(data)
if err != epp.ErrInvalidPVN {
t.Errorf("Expected ErrInvalidPVN, got %v", err)
}
}
func TestHeaderDecodeTooShort(t *testing.T) {
var h epp.Header
err := h.Decode(nil)
if err != epp.ErrDataTooShort {
t.Errorf("Expected ErrDataTooShort, got %v", err)
}
err = h.Decode([]byte{})
if err != epp.ErrDataTooShort {
t.Errorf("Expected ErrDataTooShort, got %v", err)
}
}
func TestHeaderDecodeTooShortForFormat(t *testing.T) {
// Format 2 header (2 bytes) with only 1 byte of data
data := []byte{0x74} // PVN=7, PID=2, LoL=0 → Format 2 needs 2 bytes
var h epp.Header
err := h.Decode(data)
if err != epp.ErrDataTooShort {
t.Errorf("Expected ErrDataTooShort for format 2 with 1 byte, got %v", err)
}
}
func TestHeaderValidateInvalidPVN(t *testing.T) {
h := epp.Header{PVN: 0, ProtocolID: 0}
if err := h.Validate(); err != epp.ErrInvalidPVN {
t.Errorf("Expected ErrInvalidPVN, got %v", err)
}
}
func TestHeaderSize(t *testing.T) {
tests := []struct {
name string
data []byte
want int
}{
{"idle", []byte{0x70}, 1},
{"short", []byte{0x74}, 2},
{"medium", []byte{0x75}, 4},
{"ext medium", []byte{0x7E}, 4},
{"ext long", []byte{0x7F}, 8},
{"empty", []byte{}, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := epp.HeaderSize(tt.data); got != tt.want {
t.Errorf("HeaderSize() = %d, want %d", got, tt.want)
}
})
}
}
func TestHeaderHumanize(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: epp.ProtocolIDIPE,
LengthOfLength: 0,
PacketLength: 100,
}
s := h.Humanize()
if s == "" {
t.Error("Humanize returned empty string")
}
}
func TestHeaderOctet0BitLayout(t *testing.T) {
// Verify the bit layout: PVN(4) | PID(3) | LoL(1)
tests := []struct {
name string
protocolID uint8
lol uint8
wantByte0 byte
}{
{"idle", 0, 0, 0x70}, // 0111_000_0
{"IPE short", 2, 0, 0x74}, // 0111_010_0
{"IPE medium", 2, 1, 0x75}, // 0111_010_1
{"user short", 6, 0, 0x7C}, // 0111_110_0
{"ext medium", 7, 0, 0x7E}, // 0111_111_0
{"ext long", 7, 1, 0x7F}, // 0111_111_1
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := epp.Header{
PVN: epp.PVN,
ProtocolID: tt.protocolID,
LengthOfLength: tt.lol,
PacketLength: 10, // needs non-zero for non-idle
}
if tt.protocolID == 0 && tt.lol == 0 {
h.PacketLength = 1
}
encoded, err := h.Encode()
if err != nil {
t.Fatalf("Encode failed: %v", err)
}
if encoded[0] != tt.wantByte0 {
t.Errorf("Byte 0 = 0x%02X, want 0x%02X", encoded[0], tt.wantByte0)
}
})
}
}