-
Notifications
You must be signed in to change notification settings - Fork 50
/
data_psi_test.go
384 lines (362 loc) · 12.9 KB
/
data_psi_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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package astits
import (
"bytes"
"testing"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)
var psi = &PSIData{
PointerField: 4,
Sections: []*PSISection{
{
CRC32: uint32(0x7ffc6102),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 30,
SectionSyntaxIndicator: true,
TableID: 78,
TableType: PSITableTypeEIT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{EIT: eit},
Header: psiSectionSyntaxHeader,
},
},
{
CRC32: uint32(0xfebaa941),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 25,
SectionSyntaxIndicator: true,
TableID: 64,
TableType: PSITableTypeNIT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{NIT: nit},
Header: psiSectionSyntaxHeader,
},
},
{
CRC32: uint32(0x60739f61),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 17,
SectionSyntaxIndicator: true,
TableID: 0,
TableType: PSITableTypePAT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{PAT: pat},
Header: psiSectionSyntaxHeader,
},
},
{
CRC32: uint32(0xc68442e8),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 24,
SectionSyntaxIndicator: true,
TableID: 2,
TableType: PSITableTypePMT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{PMT: pmt},
Header: psiSectionSyntaxHeader,
},
},
{
CRC32: uint32(0xef3751d6),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 20,
SectionSyntaxIndicator: true,
TableID: 66,
TableType: PSITableTypeSDT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{SDT: sdt},
Header: psiSectionSyntaxHeader,
},
},
{
CRC32: uint32(0x6969b13),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 14,
SectionSyntaxIndicator: true,
TableID: 115,
TableType: PSITableTypeTOT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{TOT: tot},
},
},
{Header: &PSISectionHeader{TableID: 254, TableType: PSITableTypeUnknown}},
},
}
func psiBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(4)) // Pointer field
w.Write([]byte("test")) // Pointer field bytes
w.Write(uint8(78)) // EIT table ID
w.Write("1") // EIT syntax section indicator
w.Write("1") // EIT private bit
w.Write("11") // EIT reserved
w.Write("000000011110") // EIT section length
w.Write(psiSectionSyntaxHeaderBytes()) // EIT syntax section header
w.Write(eitBytes()) // EIT data
w.Write(uint32(0x7ffc6102)) // EIT CRC32
w.Write(uint8(64)) // NIT table ID
w.Write("1") // NIT syntax section indicator
w.Write("1") // NIT private bit
w.Write("11") // NIT reserved
w.Write("000000011001") // NIT section length
w.Write(psiSectionSyntaxHeaderBytes()) // NIT syntax section header
w.Write(nitBytes()) // NIT data
w.Write(uint32(0xfebaa941)) // NIT CRC32
w.Write(uint8(0)) // PAT table ID
w.Write("1") // PAT syntax section indicator
w.Write("1") // PAT private bit
w.Write("11") // PAT reserved
w.Write("000000010001") // PAT section length
w.Write(psiSectionSyntaxHeaderBytes()) // PAT syntax section header
w.Write(patBytes()) // PAT data
w.Write(uint32(0x60739f61)) // PAT CRC32
w.Write(uint8(2)) // PMT table ID
w.Write("1") // PMT syntax section indicator
w.Write("1") // PMT private bit
w.Write("11") // PMT reserved
w.Write("000000011000") // PMT section length
w.Write(psiSectionSyntaxHeaderBytes()) // PMT syntax section header
w.Write(pmtBytes()) // PMT data
w.Write(uint32(0xc68442e8)) // PMT CRC32
w.Write(uint8(66)) // SDT table ID
w.Write("1") // SDT syntax section indicator
w.Write("1") // SDT private bit
w.Write("11") // SDT reserved
w.Write("000000010100") // SDT section length
w.Write(psiSectionSyntaxHeaderBytes()) // SDT syntax section header
w.Write(sdtBytes()) // SDT data
w.Write(uint32(0xef3751d6)) // SDT CRC32
w.Write(uint8(115)) // TOT table ID
w.Write("1") // TOT syntax section indicator
w.Write("1") // TOT private bit
w.Write("11") // TOT reserved
w.Write("000000001110") // TOT section length
w.Write(totBytes()) // TOT data
w.Write(uint32(0x6969b13)) // TOT CRC32
w.Write(uint8(254)) // Unknown table ID
w.Write(uint8(0)) // PAT table ID
return buf.Bytes()
}
func TestParsePSIData(t *testing.T) {
// Invalid CRC32
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(0)) // Pointer field
w.Write(uint8(115)) // TOT table ID
w.Write("1") // TOT syntax section indicator
w.Write("1") // TOT private bit
w.Write("11") // TOT reserved
w.Write("000000001110") // TOT section length
w.Write(totBytes()) // TOT data
w.Write(uint32(32)) // TOT CRC32
_, err := parsePSIData(astikit.NewBytesIterator(buf.Bytes()))
assert.EqualError(t, err, "astits: parsing PSI table failed: astits: Table CRC32 20 != computed CRC32 6969b13")
// Valid
d, err := parsePSIData(astikit.NewBytesIterator(psiBytes()))
assert.NoError(t, err)
assert.Equal(t, d, psi)
}
var psiSectionHeader = &PSISectionHeader{
PrivateBit: true,
SectionLength: 2730,
SectionSyntaxIndicator: true,
TableID: 0,
TableType: PSITableTypePAT,
}
func psiSectionHeaderBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(0)) // Table ID
w.Write("1") // Syntax section indicator
w.Write("1") // Private bit
w.Write("11") // Reserved
w.Write("101010101010") // Section length
return buf.Bytes()
}
func TestParsePSISectionHeader(t *testing.T) {
// Unknown table type
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(254)) // Table ID
w.Write("1") // Syntax section indicator
w.Write("0000000") // Finish the byte
d, _, _, _, _, err := parsePSISectionHeader(astikit.NewBytesIterator(buf.Bytes()))
assert.Equal(t, d, &PSISectionHeader{
TableID: 254,
TableType: PSITableTypeUnknown,
})
assert.NoError(t, err)
// Valid table type
d, offsetStart, offsetSectionsStart, offsetSectionsEnd, offsetEnd, err := parsePSISectionHeader(astikit.NewBytesIterator(psiSectionHeaderBytes()))
assert.Equal(t, d, psiSectionHeader)
assert.Equal(t, 0, offsetStart)
assert.Equal(t, 3, offsetSectionsStart)
assert.Equal(t, 2729, offsetSectionsEnd)
assert.Equal(t, 2733, offsetEnd)
assert.NoError(t, err)
}
func TestPSITableType(t *testing.T) {
for i := PSITableIDEITStart; i <= PSITableIDEITEnd; i++ {
assert.Equal(t, PSITableTypeEIT, i.Type())
}
assert.Equal(t, PSITableTypeDIT, PSITableIDDIT.Type())
assert.Equal(t, PSITableTypeNIT, PSITableIDNITVariant1.Type())
assert.Equal(t, PSITableTypeNIT, PSITableIDNITVariant2.Type())
assert.Equal(t, PSITableTypeSDT, PSITableIDSDTVariant1.Type())
assert.Equal(t, PSITableTypeSDT, PSITableIDSDTVariant2.Type())
assert.Equal(t, PSITableTypeBAT, PSITableIDBAT.Type())
assert.Equal(t, PSITableTypeNull, PSITableIDNull.Type())
assert.Equal(t, PSITableTypePAT, PSITableIDPAT.Type())
assert.Equal(t, PSITableTypePMT, PSITableIDPMT.Type())
assert.Equal(t, PSITableTypeRST, PSITableIDRST.Type())
assert.Equal(t, PSITableTypeSIT, PSITableIDSIT.Type())
assert.Equal(t, PSITableTypeST, PSITableIDST.Type())
assert.Equal(t, PSITableTypeTDT, PSITableIDTDT.Type())
assert.Equal(t, PSITableTypeTOT, PSITableIDTOT.Type())
assert.Equal(t, PSITableTypeUnknown, PSITableID(1).Type())
}
var psiSectionSyntaxHeader = &PSISectionSyntaxHeader{
CurrentNextIndicator: true,
LastSectionNumber: 3,
SectionNumber: 2,
TableIDExtension: 1,
VersionNumber: 21,
}
func psiSectionSyntaxHeaderBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint16(1)) // Table ID extension
w.Write("11") // Reserved bits
w.Write("10101") // Version number
w.Write("1") // Current/next indicator
w.Write(uint8(2)) // Section number
w.Write(uint8(3)) // Last section number
return buf.Bytes()
}
func TestParsePSISectionSyntaxHeader(t *testing.T) {
h, err := parsePSISectionSyntaxHeader(astikit.NewBytesIterator(psiSectionSyntaxHeaderBytes()))
assert.Equal(t, psiSectionSyntaxHeader, h)
assert.NoError(t, err)
}
func TestPSIToData(t *testing.T) {
p := &Packet{}
assert.Equal(t, []*DemuxerData{
{EIT: eit, FirstPacket: p, PID: 2},
{FirstPacket: p, NIT: nit, PID: 2},
{FirstPacket: p, PAT: pat, PID: 2},
{FirstPacket: p, PMT: pmt, PID: 2},
{FirstPacket: p, SDT: sdt, PID: 2},
{FirstPacket: p, TOT: tot, PID: 2},
}, psi.toData(p, uint16(2)))
}
type psiDataTestCase struct {
name string
bytesFunc func(*astikit.BitsWriter)
data *PSIData
}
var psiDataTestCases = []psiDataTestCase{
{
"PAT",
func(w *astikit.BitsWriter) {
w.Write(uint8(4)) // Pointer field
w.Write([]byte{0, 0, 0, 0}) // Pointer field bytes
w.Write(uint8(0)) // PAT table ID
w.Write("1") // PAT syntax section indicator
w.Write("1") // PAT private bit
w.Write("11") // PAT reserved
w.Write("000000010001") // PAT section length
w.Write(psiSectionSyntaxHeaderBytes()) // PAT syntax section header
w.Write(patBytes()) // PAT data
w.Write(uint32(0x60739f61)) // PAT CRC32
},
&PSIData{
PointerField: 4,
Sections: []*PSISection{
{
CRC32: uint32(0x60739f61),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 17,
SectionSyntaxIndicator: true,
TableID: 0,
TableType: PSITableTypePAT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{PAT: pat},
Header: psiSectionSyntaxHeader,
},
},
},
},
},
{
"PMT",
func(w *astikit.BitsWriter) {
w.Write(uint8(4)) // Pointer field
w.Write([]byte{0, 0, 0, 0}) // Pointer field bytes
w.Write(uint8(2)) // PMT table ID
w.Write("1") // PMT syntax section indicator
w.Write("1") // PMT private bit
w.Write("11") // PMT reserved
w.Write("000000011000") // PMT section length
w.Write(psiSectionSyntaxHeaderBytes()) // PMT syntax section header
w.Write(pmtBytes()) // PMT data
w.Write(uint32(0xc68442e8)) // PMT CRC32
},
&PSIData{
PointerField: 4,
Sections: []*PSISection{
{
CRC32: uint32(0xc68442e8),
Header: &PSISectionHeader{
PrivateBit: true,
SectionLength: 24,
SectionSyntaxIndicator: true,
TableID: 2,
TableType: PSITableTypePMT,
},
Syntax: &PSISectionSyntax{
Data: &PSISectionSyntaxData{PMT: pmt},
Header: psiSectionSyntaxHeader,
},
},
},
},
},
}
func TestWritePSIData(t *testing.T) {
for _, tc := range psiDataTestCases {
t.Run(tc.name, func(t *testing.T) {
bufExpected := bytes.Buffer{}
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})
bufActual := bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})
tc.bytesFunc(wExpected)
n, err := writePSIData(wActual, tc.data)
assert.NoError(t, err)
assert.Equal(t, bufExpected.Len(), n)
assert.Equal(t, n, bufActual.Len())
assert.Equal(t, bufExpected.Bytes(), bufActual.Bytes())
})
}
}
func BenchmarkParsePSIData(b *testing.B) {
pb := psiBytes()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
parsePSIData(astikit.NewBytesIterator(pb))
}
}