Skip to content

Commit 2eddcf8

Browse files
committed
Cover SessionSRTCP with basic tests
Add basic read/write/close testing Relates to #28
1 parent 56f4948 commit 2eddcf8

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

session_srtcp_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package srtp
2+
3+
import (
4+
"bytes"
5+
"net"
6+
"testing"
7+
"time"
8+
9+
"github.com/pion/rtcp"
10+
"github.com/pion/transport/test"
11+
)
12+
13+
func TestSessionSRTCPBadInit(t *testing.T) {
14+
if _, err := NewSessionSRTCP(nil, nil); err == nil {
15+
t.Fatal("NewSessionSRTCP should error if no config was provided")
16+
} else if _, err := NewSessionSRTCP(nil, &Config{}); err == nil {
17+
t.Fatal("NewSessionSRTCP should error if no net was provided")
18+
}
19+
}
20+
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+
34+
aPipe, bPipe := net.Pipe()
35+
config := &Config{
36+
Profile: ProtectionProfileAes128CmHmacSha1_80,
37+
Keys: SessionKeys{
38+
[]byte{0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0, 0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39},
39+
[]byte{0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB, 0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6},
40+
[]byte{0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0, 0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39},
41+
[]byte{0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB, 0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6},
42+
},
43+
}
44+
45+
aSession, err := NewSessionSRTCP(aPipe, config)
46+
if err != nil {
47+
t.Fatal(err)
48+
} else if aSession == nil {
49+
t.Fatal("NewSessionSRTCP did not error, but returned nil session")
50+
}
51+
52+
bSession, err := NewSessionSRTCP(bPipe, config)
53+
if err != nil {
54+
t.Fatal(err)
55+
} else if bSession == nil {
56+
t.Fatal("NewSessionSRTCP did not error, but returned nil session")
57+
}
58+
59+
aWriteStream, err := aSession.OpenWriteStream()
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
if _, err = aWriteStream.Write(testPayload); err != nil {
64+
t.Fatal(err)
65+
}
66+
67+
bReadStream, _, err := bSession.AcceptStream()
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
72+
if _, err = bReadStream.Read(readBuffer); err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
if !bytes.Equal(testPayload, readBuffer) {
77+
t.Fatalf("Sent buffer does not match the one received exp(%v) actual(%v)", testPayload, readBuffer)
78+
}
79+
80+
if err = aSession.Close(); err != nil {
81+
t.Fatal(err)
82+
}
83+
84+
if err = bSession.Close(); err != nil {
85+
t.Fatal(err)
86+
}
87+
}

session_srtp_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"github.com/pion/transport/test"
1111
)
1212

13-
func TestSessionSRTPNoConfig(t *testing.T) {
13+
func TestSessionSRTPBadInit(t *testing.T) {
1414
if _, err := NewSessionSRTP(nil, nil); err == nil {
15-
t.Fatal("NewSessionSRTP should error if not config was provided")
15+
t.Fatal("NewSessionSRTP should error if no config was provided")
16+
} else if _, err := NewSessionSRTP(nil, &Config{}); err == nil {
17+
t.Fatal("NewSessionSRTP should error if no net was provided")
1618
}
1719
}
1820

0 commit comments

Comments
 (0)