Skip to content

Commit 56f4948

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

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

session_srtcp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type SessionSRTCP struct {
2222
func NewSessionSRTCP(conn net.Conn, config *Config) (*SessionSRTCP, error) {
2323
if config == nil {
2424
return nil, errors.New("no config provided")
25+
} else if conn == nil {
26+
return nil, errors.New("no conn provided")
2527
}
2628

2729
loggerFactory := config.LoggerFactory

session_srtp.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type SessionSRTP struct {
2222
func NewSessionSRTP(conn net.Conn, config *Config) (*SessionSRTP, error) {
2323
if config == nil {
2424
return nil, errors.New("no config provided")
25+
} else if conn == nil {
26+
return nil, errors.New("no conn provided")
2527
}
2628

2729
loggerFactory := config.LoggerFactory
@@ -53,12 +55,6 @@ func NewSessionSRTP(conn net.Conn, config *Config) (*SessionSRTP, error) {
5355
return s, nil
5456
}
5557

56-
// Start initializes any crypto context and allows reading/writing to begin
57-
func (s *SessionSRTP) Start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, nextConn net.Conn) error {
58-
s.session.nextConn = nextConn
59-
return s.session.start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt, profile, s)
60-
}
61-
6258
// OpenWriteStream returns the global write stream for the Session
6359
func (s *SessionSRTP) OpenWriteStream() (*WriteStreamSRTP, error) {
6460
return s.writeStream, nil

session_srtp_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package srtp
2+
3+
import (
4+
"bytes"
5+
"net"
6+
"testing"
7+
"time"
8+
9+
"github.com/pion/rtp"
10+
"github.com/pion/transport/test"
11+
)
12+
13+
func TestSessionSRTPNoConfig(t *testing.T) {
14+
if _, err := NewSessionSRTP(nil, nil); err == nil {
15+
t.Fatal("NewSessionSRTP should error if not config was provided")
16+
}
17+
}
18+
19+
func TestSessionSRTP(t *testing.T) {
20+
lim := test.TimeOut(time.Second * 5)
21+
defer lim.Stop()
22+
23+
report := test.CheckRoutines(t)
24+
defer report()
25+
26+
const (
27+
testSSRC = 5000
28+
rtpHeaderSize = 12
29+
)
30+
testPayload := []byte{0x00, 0x01, 0x03, 0x04}
31+
readBuffer := make([]byte, rtpHeaderSize+len(testPayload))
32+
33+
aPipe, bPipe := net.Pipe()
34+
config := &Config{
35+
Profile: ProtectionProfileAes128CmHmacSha1_80,
36+
Keys: SessionKeys{
37+
[]byte{0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0, 0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39},
38+
[]byte{0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB, 0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6},
39+
[]byte{0xE1, 0xF9, 0x7A, 0x0D, 0x3E, 0x01, 0x8B, 0xE0, 0xD6, 0x4F, 0xA3, 0x2C, 0x06, 0xDE, 0x41, 0x39},
40+
[]byte{0x0E, 0xC6, 0x75, 0xAD, 0x49, 0x8A, 0xFE, 0xEB, 0xB6, 0x96, 0x0B, 0x3A, 0xAB, 0xE6},
41+
},
42+
}
43+
44+
aSession, err := NewSessionSRTP(aPipe, config)
45+
if err != nil {
46+
t.Fatal(err)
47+
} else if aSession == nil {
48+
t.Fatal("NewSessionSRTP did not error, but returned nil session")
49+
}
50+
51+
bSession, err := NewSessionSRTP(bPipe, config)
52+
if err != nil {
53+
t.Fatal(err)
54+
} else if bSession == nil {
55+
t.Fatal("NewSessionSRTP did not error, but returned nil session")
56+
}
57+
58+
aWriteStream, err := aSession.OpenWriteStream()
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
if _, err = aWriteStream.WriteRTP(&rtp.Header{SSRC: testSSRC}, append([]byte{}, testPayload...)); err != nil {
63+
t.Fatal(err)
64+
}
65+
66+
bReadStream, ssrc, err := bSession.AcceptStream()
67+
if err != nil {
68+
t.Fatal(err)
69+
} else if ssrc != testSSRC {
70+
t.Fatalf("SSRC mismatch during accept exp(%v) actual%v)", testSSRC, ssrc)
71+
}
72+
73+
if _, err = bReadStream.Read(readBuffer); err != nil {
74+
t.Fatal(err)
75+
}
76+
77+
if !bytes.Equal(testPayload, readBuffer[rtpHeaderSize:]) {
78+
t.Fatalf("Sent buffer does not match the one received exp(%v) actual(%v)", testPayload, readBuffer[rtpHeaderSize:])
79+
}
80+
81+
if err = aSession.Close(); err != nil {
82+
t.Fatal(err)
83+
}
84+
85+
if err = bSession.Close(); err != nil {
86+
t.Fatal(err)
87+
}
88+
}

0 commit comments

Comments
 (0)