Skip to content

Commit 7ef5222

Browse files
committed
Add more Session coverage
Add tests for OpenReadStream for SRTP and SRTCP Resolves #28
1 parent 2eddcf8 commit 7ef5222

File tree

2 files changed

+128
-27
lines changed

2 files changed

+128
-27
lines changed

session_srtcp_test.go

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@ func TestSessionSRTCPBadInit(t *testing.T) {
1818
}
1919
}
2020

21-
func TestSessionSRTCP(t *testing.T) {
22-
lim := test.TimeOut(time.Second * 10)
23-
defer lim.Stop()
24-
25-
report := test.CheckRoutines(t)
26-
defer report()
27-
28-
testPayload, err := rtcp.Marshal([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: 5000}})
29-
if err != nil {
30-
t.Fatal(err)
31-
}
32-
readBuffer := make([]byte, len(testPayload))
33-
21+
func buildSessionSRTCPPair(t *testing.T) (*SessionSRTCP, *SessionSRTCP) {
3422
aPipe, bPipe := net.Pipe()
3523
config := &Config{
3624
Profile: ProtectionProfileAes128CmHmacSha1_80,
@@ -56,10 +44,28 @@ func TestSessionSRTCP(t *testing.T) {
5644
t.Fatal("NewSessionSRTCP did not error, but returned nil session")
5745
}
5846

47+
return aSession, bSession
48+
}
49+
50+
func TestSessionSRTCP(t *testing.T) {
51+
lim := test.TimeOut(time.Second * 10)
52+
defer lim.Stop()
53+
54+
report := test.CheckRoutines(t)
55+
defer report()
56+
57+
testPayload, err := rtcp.Marshal([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: 5000}})
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
readBuffer := make([]byte, len(testPayload))
62+
aSession, bSession := buildSessionSRTCPPair(t)
63+
5964
aWriteStream, err := aSession.OpenWriteStream()
6065
if err != nil {
6166
t.Fatal(err)
6267
}
68+
6369
if _, err = aWriteStream.Write(testPayload); err != nil {
6470
t.Fatal(err)
6571
}
@@ -85,3 +91,48 @@ func TestSessionSRTCP(t *testing.T) {
8591
t.Fatal(err)
8692
}
8793
}
94+
95+
func TestSessionSRTCPOpenReadStream(t *testing.T) {
96+
lim := test.TimeOut(time.Second * 10)
97+
defer lim.Stop()
98+
99+
report := test.CheckRoutines(t)
100+
defer report()
101+
102+
testPayload, err := rtcp.Marshal([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: 5000}})
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
readBuffer := make([]byte, len(testPayload))
107+
aSession, bSession := buildSessionSRTCPPair(t)
108+
109+
bReadStream, err := bSession.OpenReadStream(5000)
110+
if err != nil {
111+
t.Fatal(err)
112+
}
113+
114+
aWriteStream, err := aSession.OpenWriteStream()
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
119+
if _, err = aWriteStream.Write(testPayload); err != nil {
120+
t.Fatal(err)
121+
}
122+
123+
if _, err = bReadStream.Read(readBuffer); err != nil {
124+
t.Fatal(err)
125+
}
126+
127+
if !bytes.Equal(testPayload, readBuffer) {
128+
t.Fatalf("Sent buffer does not match the one received exp(%v) actual(%v)", testPayload, readBuffer)
129+
}
130+
131+
if err = aSession.Close(); err != nil {
132+
t.Fatal(err)
133+
}
134+
135+
if err = bSession.Close(); err != nil {
136+
t.Fatal(err)
137+
}
138+
}

session_srtp_test.go

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,7 @@ func TestSessionSRTPBadInit(t *testing.T) {
1818
}
1919
}
2020

21-
func TestSessionSRTP(t *testing.T) {
22-
lim := test.TimeOut(time.Second * 5)
23-
defer lim.Stop()
24-
25-
report := test.CheckRoutines(t)
26-
defer report()
27-
28-
const (
29-
testSSRC = 5000
30-
rtpHeaderSize = 12
31-
)
32-
testPayload := []byte{0x00, 0x01, 0x03, 0x04}
33-
readBuffer := make([]byte, rtpHeaderSize+len(testPayload))
34-
21+
func buildSessionSRTPPair(t *testing.T) (*SessionSRTP, *SessionSRTP) {
3522
aPipe, bPipe := net.Pipe()
3623
config := &Config{
3724
Profile: ProtectionProfileAes128CmHmacSha1_80,
@@ -57,6 +44,24 @@ func TestSessionSRTP(t *testing.T) {
5744
t.Fatal("NewSessionSRTP did not error, but returned nil session")
5845
}
5946

47+
return aSession, bSession
48+
}
49+
50+
func TestSessionSRTP(t *testing.T) {
51+
lim := test.TimeOut(time.Second * 5)
52+
defer lim.Stop()
53+
54+
report := test.CheckRoutines(t)
55+
defer report()
56+
57+
const (
58+
testSSRC = 5000
59+
rtpHeaderSize = 12
60+
)
61+
testPayload := []byte{0x00, 0x01, 0x03, 0x04}
62+
readBuffer := make([]byte, rtpHeaderSize+len(testPayload))
63+
aSession, bSession := buildSessionSRTPPair(t)
64+
6065
aWriteStream, err := aSession.OpenWriteStream()
6166
if err != nil {
6267
t.Fatal(err)
@@ -88,3 +93,48 @@ func TestSessionSRTP(t *testing.T) {
8893
t.Fatal(err)
8994
}
9095
}
96+
97+
func TestSessionSRTPOpenReadStream(t *testing.T) {
98+
lim := test.TimeOut(time.Second * 5)
99+
defer lim.Stop()
100+
101+
report := test.CheckRoutines(t)
102+
defer report()
103+
104+
const (
105+
testSSRC = 5000
106+
rtpHeaderSize = 12
107+
)
108+
testPayload := []byte{0x00, 0x01, 0x03, 0x04}
109+
readBuffer := make([]byte, rtpHeaderSize+len(testPayload))
110+
aSession, bSession := buildSessionSRTPPair(t)
111+
112+
bReadStream, err := bSession.OpenReadStream(5000)
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
117+
aWriteStream, err := aSession.OpenWriteStream()
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
if _, err = aWriteStream.WriteRTP(&rtp.Header{SSRC: testSSRC}, append([]byte{}, testPayload...)); err != nil {
122+
t.Fatal(err)
123+
}
124+
125+
if _, err = bReadStream.Read(readBuffer); err != nil {
126+
t.Fatal(err)
127+
}
128+
129+
if !bytes.Equal(testPayload, readBuffer[rtpHeaderSize:]) {
130+
t.Fatalf("Sent buffer does not match the one received exp(%v) actual(%v)", testPayload, readBuffer[rtpHeaderSize:])
131+
}
132+
133+
if err = aSession.Close(); err != nil {
134+
t.Fatal(err)
135+
}
136+
137+
if err = bSession.Close(); err != nil {
138+
t.Fatal(err)
139+
}
140+
}

0 commit comments

Comments
 (0)