|
| 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