|
| 1 | +// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package sdp |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewJSEPSessionDescription(t *testing.T) { |
| 13 | + t.Run("Without Identity", func(t *testing.T) { |
| 14 | + sd, err := NewJSEPSessionDescription(false) |
| 15 | + assert.NoError(t, err) |
| 16 | + assert.NotNil(t, sd) |
| 17 | + assert.Zero(t, sd.Version) |
| 18 | + assert.Equal(t, "-", sd.Origin.Username) |
| 19 | + assert.Equal(t, "IN", sd.Origin.NetworkType) |
| 20 | + assert.Equal(t, "IP4", sd.Origin.AddressType) |
| 21 | + assert.Equal(t, "0.0.0.0", sd.Origin.UnicastAddress) |
| 22 | + assert.Equal(t, SessionName("-"), sd.SessionName) |
| 23 | + assert.Len(t, sd.TimeDescriptions, 1) |
| 24 | + assert.Zero(t, sd.TimeDescriptions[0].Timing.StartTime) |
| 25 | + assert.Zero(t, sd.TimeDescriptions[0].Timing.StopTime) |
| 26 | + assert.Empty(t, sd.Attributes) |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("With Identity", func(t *testing.T) { |
| 30 | + sd, err := NewJSEPSessionDescription(true) |
| 31 | + assert.NoError(t, err) |
| 32 | + assert.NotNil(t, sd) |
| 33 | + assert.Len(t, sd.Attributes, 1) |
| 34 | + assert.Equal(t, AttrKeyIdentity, sd.Attributes[0].Key) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func TestSessionDescriptionAttributes(t *testing.T) { |
| 39 | + sd, err := NewJSEPSessionDescription(false) |
| 40 | + assert.NoError(t, err) |
| 41 | + |
| 42 | + t.Run("WithPropertyAttribute", func(t *testing.T) { |
| 43 | + sd = sd.WithPropertyAttribute(AttrKeyRTCPMux) |
| 44 | + assert.Len(t, sd.Attributes, 1) |
| 45 | + assert.Equal(t, AttrKeyRTCPMux, sd.Attributes[0].Key) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("WithValueAttribute", func(t *testing.T) { |
| 49 | + sd = sd.WithValueAttribute(AttrKeyMID, "video") |
| 50 | + assert.Len(t, sd.Attributes, 2) |
| 51 | + assert.Equal(t, AttrKeyMID, sd.Attributes[1].Key) |
| 52 | + assert.Equal(t, "video", sd.Attributes[1].Value) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("WithICETrickleAdvertised", func(t *testing.T) { |
| 56 | + sd = sd.WithICETrickleAdvertised() |
| 57 | + assert.Len(t, sd.Attributes, 3) |
| 58 | + assert.Equal(t, AttrKeyICEOptions, sd.Attributes[2].Key) |
| 59 | + assert.Equal(t, "trickle ice2", sd.Attributes[2].Value) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("WithFingerprint", func(t *testing.T) { |
| 63 | + sd = sd.WithFingerprint("sha-256", "test-fingerprint") |
| 64 | + assert.Len(t, sd.Attributes, 4) |
| 65 | + assert.Equal(t, "fingerprint", sd.Attributes[3].Key) |
| 66 | + assert.Equal(t, "sha-256 test-fingerprint", sd.Attributes[3].Value) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestNewJSEPMediaDescription(t *testing.T) { |
| 71 | + md := NewJSEPMediaDescription("video", []string{"96", "97"}) |
| 72 | + assert.NotNil(t, md) |
| 73 | + assert.Equal(t, "video", md.MediaName.Media) |
| 74 | + assert.Equal(t, int(9), md.MediaName.Port.Value) |
| 75 | + assert.Equal(t, []string{"UDP", "TLS", "RTP", "SAVPF"}, md.MediaName.Protos) |
| 76 | + assert.Equal(t, "IN", md.ConnectionInformation.NetworkType) |
| 77 | + assert.Equal(t, "IP4", md.ConnectionInformation.AddressType) |
| 78 | + assert.Equal(t, "0.0.0.0", md.ConnectionInformation.Address.Address) |
| 79 | +} |
| 80 | + |
| 81 | +func TestMediaDescriptionAttributes(t *testing.T) { |
| 82 | + md := NewJSEPMediaDescription("audio", nil) |
| 83 | + |
| 84 | + t.Run("WithPropertyAttribute", func(t *testing.T) { |
| 85 | + md = md.WithPropertyAttribute(AttrKeyRTCPMux) |
| 86 | + assert.Len(t, md.Attributes, 1) |
| 87 | + assert.Equal(t, AttrKeyRTCPMux, md.Attributes[0].Key) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("WithValueAttribute", func(t *testing.T) { |
| 91 | + md = md.WithValueAttribute(AttrKeyMID, "audio") |
| 92 | + assert.Len(t, md.Attributes, 2) |
| 93 | + assert.Equal(t, AttrKeyMID, md.Attributes[1].Key) |
| 94 | + assert.Equal(t, "audio", md.Attributes[1].Value) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("WithFingerprint", func(t *testing.T) { |
| 98 | + md = md.WithFingerprint("sha-256", "test-fingerprint") |
| 99 | + assert.Len(t, md.Attributes, 3) |
| 100 | + assert.Equal(t, "fingerprint", md.Attributes[2].Key) |
| 101 | + assert.Equal(t, "sha-256 test-fingerprint", md.Attributes[2].Value) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("WithICECredentials", func(t *testing.T) { |
| 105 | + md = md.WithICECredentials("test-ufrag", "test-pwd") |
| 106 | + assert.Len(t, md.Attributes, 5) |
| 107 | + assert.Equal(t, "ice-ufrag", md.Attributes[3].Key) |
| 108 | + assert.Equal(t, "test-ufrag", md.Attributes[3].Value) |
| 109 | + assert.Equal(t, "ice-pwd", md.Attributes[4].Key) |
| 110 | + assert.Equal(t, "test-pwd", md.Attributes[4].Value) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +func TestMediaDescriptionCodec(t *testing.T) { |
| 115 | + md := NewJSEPMediaDescription("audio", nil) |
| 116 | + |
| 117 | + t.Run("WithCodec", func(t *testing.T) { |
| 118 | + md = md.WithCodec(111, "opus", 48000, 2, "minptime=10;useinbandfec=1") |
| 119 | + assert.Len(t, md.MediaName.Formats, 1) |
| 120 | + assert.Equal(t, "111", md.MediaName.Formats[0]) |
| 121 | + assert.Len(t, md.Attributes, 2) |
| 122 | + assert.Equal(t, "rtpmap", md.Attributes[0].Key) |
| 123 | + assert.Equal(t, "111 opus/48000/2", md.Attributes[0].Value) |
| 124 | + assert.Equal(t, "fmtp", md.Attributes[1].Key) |
| 125 | + assert.Equal(t, "111 minptime=10;useinbandfec=1", md.Attributes[1].Value) |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("WithMediaSource", func(t *testing.T) { |
| 129 | + md = md.WithMediaSource(1234567890, "test-cname", "test-stream", "test-label") |
| 130 | + assert.Len(t, md.Attributes, 6) |
| 131 | + assert.Equal(t, "ssrc", md.Attributes[2].Key) |
| 132 | + assert.Equal(t, "1234567890 cname:test-cname", md.Attributes[2].Value) |
| 133 | + assert.Equal(t, "ssrc", md.Attributes[3].Key) |
| 134 | + assert.Equal(t, "1234567890 msid:test-stream test-label", md.Attributes[3].Value) |
| 135 | + assert.Equal(t, "ssrc", md.Attributes[4].Key) |
| 136 | + assert.Equal(t, "1234567890 mslabel:test-stream", md.Attributes[4].Value) |
| 137 | + assert.Equal(t, "ssrc", md.Attributes[5].Key) |
| 138 | + assert.Equal(t, "1234567890 label:test-label", md.Attributes[5].Value) |
| 139 | + }) |
| 140 | +} |
0 commit comments