14 changed files with 430 additions and 228 deletions
@ -1,35 +0,0 @@ |
|||||||
package chunk |
|
||||||
|
|
||||||
import ( |
|
||||||
"bytes" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/require" |
|
||||||
) |
|
||||||
|
|
||||||
var chunk0enc = []byte{ |
|
||||||
0x19, 0xb1, 0xa1, 0x91, 0x0, 0x0, 0x14, 0x14, |
|
||||||
0x3, 0x5d, 0x17, 0x3d, 0x1, 0x2, 0x3, 0x4, |
|
||||||
} |
|
||||||
|
|
||||||
var chunk0dec = Chunk0{ |
|
||||||
ChunkStreamID: 25, |
|
||||||
Timestamp: 11641233, |
|
||||||
Type: 20, |
|
||||||
MessageStreamID: 56432445, |
|
||||||
BodyLen: 20, |
|
||||||
Body: []byte{0x01, 0x02, 0x03, 0x04}, |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk0Read(t *testing.T) { |
|
||||||
var chunk0 Chunk0 |
|
||||||
err := chunk0.Read(bytes.NewReader(chunk0enc), 4) |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk0dec, chunk0) |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk0Marshal(t *testing.T) { |
|
||||||
buf, err := chunk0dec.Marshal() |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk0enc, buf) |
|
||||||
} |
|
||||||
@ -1,34 +0,0 @@ |
|||||||
package chunk |
|
||||||
|
|
||||||
import ( |
|
||||||
"bytes" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/require" |
|
||||||
) |
|
||||||
|
|
||||||
var chunk1enc = []byte{ |
|
||||||
0x59, 0xb1, 0xa1, 0x91, 0x0, 0x0, 0x14, 0x14, |
|
||||||
0x1, 0x2, 0x3, 0x4, |
|
||||||
} |
|
||||||
|
|
||||||
var chunk1dec = Chunk1{ |
|
||||||
ChunkStreamID: 25, |
|
||||||
TimestampDelta: 11641233, |
|
||||||
Type: 20, |
|
||||||
BodyLen: 20, |
|
||||||
Body: []byte{0x01, 0x02, 0x03, 0x04}, |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk1Read(t *testing.T) { |
|
||||||
var chunk1 Chunk1 |
|
||||||
err := chunk1.Read(bytes.NewReader(chunk1enc), 4) |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk1dec, chunk1) |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk1Marshal(t *testing.T) { |
|
||||||
buf, err := chunk1dec.Marshal() |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk1enc, buf) |
|
||||||
} |
|
||||||
@ -1,31 +0,0 @@ |
|||||||
package chunk |
|
||||||
|
|
||||||
import ( |
|
||||||
"bytes" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/require" |
|
||||||
) |
|
||||||
|
|
||||||
var chunk2enc = []byte{ |
|
||||||
0x99, 0xb1, 0xa1, 0x91, 0x1, 0x2, 0x3, 0x4, |
|
||||||
} |
|
||||||
|
|
||||||
var chunk2dec = Chunk2{ |
|
||||||
ChunkStreamID: 25, |
|
||||||
TimestampDelta: 11641233, |
|
||||||
Body: []byte{0x01, 0x02, 0x03, 0x04}, |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk2Read(t *testing.T) { |
|
||||||
var chunk2 Chunk2 |
|
||||||
err := chunk2.Read(bytes.NewReader(chunk2enc), 4) |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk2dec, chunk2) |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk2Marshal(t *testing.T) { |
|
||||||
buf, err := chunk2dec.Marshal() |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk2enc, buf) |
|
||||||
} |
|
||||||
@ -1,30 +0,0 @@ |
|||||||
package chunk |
|
||||||
|
|
||||||
import ( |
|
||||||
"bytes" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/require" |
|
||||||
) |
|
||||||
|
|
||||||
var chunk3enc = []byte{ |
|
||||||
0xd9, 0x1, 0x2, 0x3, 0x4, |
|
||||||
} |
|
||||||
|
|
||||||
var chunk3dec = Chunk3{ |
|
||||||
ChunkStreamID: 25, |
|
||||||
Body: []byte{0x01, 0x02, 0x03, 0x04}, |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk3Read(t *testing.T) { |
|
||||||
var chunk3 Chunk3 |
|
||||||
err := chunk3.Read(bytes.NewReader(chunk3enc), 4) |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk3dec, chunk3) |
|
||||||
} |
|
||||||
|
|
||||||
func TestChunk3Marshal(t *testing.T) { |
|
||||||
buf, err := chunk3dec.Marshal() |
|
||||||
require.NoError(t, err) |
|
||||||
require.Equal(t, chunk3enc, buf) |
|
||||||
} |
|
||||||
@ -0,0 +1,158 @@ |
|||||||
|
package chunk |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"reflect" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
) |
||||||
|
|
||||||
|
var cases = []struct { |
||||||
|
name string |
||||||
|
enc []byte |
||||||
|
bodyLen uint32 |
||||||
|
hasExtendedTimestamp bool |
||||||
|
dec Chunk |
||||||
|
}{ |
||||||
|
{ |
||||||
|
"chunk0 standard", |
||||||
|
[]byte{ |
||||||
|
0x19, 0xb1, 0xa1, 0x91, 0x0, 0x0, 0x14, 0x14, |
||||||
|
0x3, 0x5d, 0x17, 0x3d, 0x1, 0x2, 0x3, 0x4, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk0{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
Timestamp: 11641233, |
||||||
|
Type: 20, |
||||||
|
MessageStreamID: 56432445, |
||||||
|
BodyLen: 20, |
||||||
|
Body: []byte{1, 2, 3, 4}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk0 extended timestamp", |
||||||
|
[]byte{ |
||||||
|
0x19, 0xff, 0xff, 0xff, 0x00, 0x00, 0x14, 0x0f, |
||||||
|
0x00, 0x31, 0x84, 0xb2, 0xff, 0x34, 0x86, 0xa2, |
||||||
|
0x05, 0x06, 0x07, 0x08, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk0{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
Timestamp: 0xFF3486a2, |
||||||
|
Type: 15, |
||||||
|
MessageStreamID: 3245234, |
||||||
|
BodyLen: 20, |
||||||
|
Body: []byte{5, 6, 7, 8}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk1 standard", |
||||||
|
[]byte{ |
||||||
|
0x59, 0xb1, 0xa1, 0x91, 0x0, 0x0, 0x14, 0x14, |
||||||
|
0x1, 0x2, 0x3, 0x4, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk1{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
TimestampDelta: 11641233, |
||||||
|
Type: 20, |
||||||
|
BodyLen: 20, |
||||||
|
Body: []byte{1, 2, 3, 4}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk1 extended timestamp", |
||||||
|
[]byte{ |
||||||
|
0x59, 0xff, 0xff, 0xff, 0x00, 0x00, 0x14, 0x14, |
||||||
|
0xff, 0x88, 0x4b, 0x6c, 0x05, 0x06, 0x07, 0x08, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk1{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
TimestampDelta: 0xFF884B6C, |
||||||
|
Type: 20, |
||||||
|
BodyLen: 20, |
||||||
|
Body: []byte{5, 6, 7, 8}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk2 standard", |
||||||
|
[]byte{ |
||||||
|
0x99, 0xb1, 0xa1, 0x91, 0x1, 0x2, 0x3, 0x4, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk2{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
TimestampDelta: 11641233, |
||||||
|
Body: []byte{1, 2, 3, 4}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk2 extended timestamp", |
||||||
|
[]byte{ |
||||||
|
0x99, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xbb, 0xcc, |
||||||
|
0x05, 0x06, 0x07, 0x08, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk2{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
TimestampDelta: 0xFFAABBCC, |
||||||
|
Body: []byte{5, 6, 7, 8}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk3 standard", |
||||||
|
[]byte{ |
||||||
|
0xd9, 0x1, 0x2, 0x3, 0x4, |
||||||
|
}, |
||||||
|
4, |
||||||
|
false, |
||||||
|
&Chunk3{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
Body: []byte{1, 2, 3, 4}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"chunk3 extended timestamp", |
||||||
|
[]byte{ |
||||||
|
0xd9, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, |
||||||
|
0x08, |
||||||
|
}, |
||||||
|
4, |
||||||
|
true, |
||||||
|
&Chunk3{ |
||||||
|
ChunkStreamID: 25, |
||||||
|
Body: []byte{5, 6, 7, 8}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
func TestChunkRead(t *testing.T) { |
||||||
|
for _, ca := range cases { |
||||||
|
t.Run(ca.name, func(t *testing.T) { |
||||||
|
chunk := reflect.New(reflect.TypeOf(ca.dec).Elem()).Interface().(Chunk) |
||||||
|
err := chunk.Read(bytes.NewReader(ca.enc), ca.bodyLen, ca.hasExtendedTimestamp) |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, ca.dec, chunk) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestChunkMarshal(t *testing.T) { |
||||||
|
for _, ca := range cases { |
||||||
|
t.Run(ca.name, func(t *testing.T) { |
||||||
|
buf, err := ca.dec.Marshal(ca.hasExtendedTimestamp) |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, ca.enc, buf) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue