mediamtx/internal/rtmp/handshake/c1s1_test.go

139 lines
3.6 KiB
Go

package handshake
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
var c1enc = append(
[]byte{
0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a,
0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4,
0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad,
0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4,
0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04,
},
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)...,
)
var s1enc = append(
[]byte{
0x19, 0xf1, 0x27, 0xa3, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x95, 0xc1,
0xb6, 0x2c, 0x99, 0xbe, 0xa0, 0x0c, 0x07, 0x98,
0xb0, 0xf1, 0xbe, 0x54, 0x50, 0x63, 0xa1, 0x25,
0x1c, 0x9a, 0xcd, 0x12, 0x10, 0x98, 0x74, 0x8b,
0x18, 0x66, 0x8d, 0xef, 0xcf, 0x22, 0x03, 0x04,
},
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)...,
)
func TestC1S1Read(t *testing.T) {
for _, ca := range []struct {
isC1 bool
enc []byte
dec C1S1
}{
{
true,
c1enc,
C1S1{
Time: 435234723,
Random: append(
[]byte{
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x2d, 0x0a,
0x37, 0x6f, 0x63, 0x2e, 0xa0, 0x21, 0xa0, 0xa4,
0x81, 0xb1, 0x50, 0x21, 0x5a, 0x6d, 0x81, 0xad,
0xf8, 0x44, 0x69, 0x13, 0xcc, 0x02, 0x8c, 0xd4,
0x64, 0x43, 0xc9, 0x9f, 0xcf, 0xc6, 0x03, 0x04,
},
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)...,
),
Digest: []byte{
0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3,
0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a,
0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d,
0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99,
},
},
},
{
false,
s1enc,
C1S1{
Time: 435234723,
Random: append(
[]byte{
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04,
0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x95, 0xc1,
0xb6, 0x2c, 0x99, 0xbe, 0xa0, 0x0c, 0x07, 0x98,
0xb0, 0xf1, 0xbe, 0x54, 0x50, 0x63, 0xa1, 0x25,
0x1c, 0x9a, 0xcd, 0x12, 0x10, 0x98, 0x74, 0x8b,
0x18, 0x66, 0x8d, 0xef, 0xcf, 0x22, 0x03, 0x04,
},
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 370)...,
),
Digest: []byte{
0x0e, 0x8f, 0x96, 0x19, 0x19, 0xe6, 0xb7, 0xf2,
0xac, 0x9a, 0xc8, 0x7e, 0x6e, 0xe9, 0xd4, 0x72,
0xed, 0x82, 0x87, 0xf1, 0xfa, 0xbd, 0x93, 0xb8,
0x7c, 0x48, 0x85, 0x03, 0x01, 0x7b, 0x54, 0xbe,
},
},
},
} {
var c1s1 C1S1
err := c1s1.Read((bytes.NewReader(ca.enc)), ca.isC1, true)
require.NoError(t, err)
require.Equal(t, ca.dec, c1s1)
}
}
func TestC1S1Write(t *testing.T) {
for _, ca := range []struct {
isC1 bool
enc []byte
dec C1S1
}{
{
true,
c1enc,
C1S1{
Time: 435234723,
Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382),
Digest: []byte{
0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3,
0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a,
0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d,
0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99,
},
},
},
{
false,
s1enc,
C1S1{
Time: 435234723,
Random: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 382),
Digest: []byte{
0x3f, 0xd0, 0xb1, 0xdf, 0xed, 0x6c, 0x9b, 0xc3,
0x73, 0x68, 0xe2, 0x47, 0x92, 0x59, 0x32, 0x9a,
0x3a, 0xc9, 0x1e, 0xeb, 0xfc, 0xad, 0x8e, 0x9d,
0x4e, 0xf4, 0x30, 0xb1, 0x9a, 0xc9, 0x15, 0x99,
},
},
},
} {
var buf bytes.Buffer
err := ca.dec.Write(&buf, ca.isC1)
require.NoError(t, err)
require.Equal(t, ca.enc, buf.Bytes())
}
}