comparison tests/unit/test_plugin_xep_0294.py @ 4063:e12936318177

tests (units): tests for plugin XEP-0294: fix 438
author Goffi <goffi@goffi.org>
date Tue, 30 May 2023 17:58:44 +0200
parents
children 4b842c1fb686
comparison
equal deleted inserted replaced
4062:18719058a914 4063:e12936318177
1 #!/usr/bin/env python3
2
3 # Libervia: an XMPP client
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from twisted.words.xish import domish
21
22 from sat.plugins.plugin_xep_0294 import NS_JINGLE_RTP_HDREXT, XEP_0294
23 from sat.tools.xml_tools import parse
24
25
26 class TestXEP0294:
27 def test_extmap_attribute(self, host):
28 """Session level 'extmap' SDP attribute are set to application_data"""
29 xep_0294 = XEP_0294(host)
30
31 call_data = {}
32 # "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level"
33 parts = ["1", "urn:ietf:params:rtp-hdrext:ssrc-audio-level"]
34
35 # Call the method with application_data as None to make is a session level
36 # attribute
37 xep_0294._parse_sdp_a_trigger("extmap", parts, call_data, {}, "audio", None, {})
38
39 # Call the method again with application_data set
40 application_data = {}
41 xep_0294._parse_sdp_a_trigger(
42 "", [], call_data, {}, "audio", application_data, {}
43 )
44
45 # Assert data has been correctly transferred
46 assert application_data == {
47 "rtp-hdrext": {
48 "1": {
49 "id": "1",
50 "uri": "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
51 "senders": "both",
52 }
53 }
54 }
55
56 def test_extmap_allow_mixed_attribute(self, host):
57 """Session level 'extmap-allow-mixed' SDP attribute are set to application_data"""
58 xep_0294 = XEP_0294(host)
59
60 call_data = {}
61
62 # Call the method with application_data as None to make is a session level
63 # attribute
64 xep_0294._parse_sdp_a_trigger(
65 "extmap-allow-mixed", [], call_data, {}, "audio", None, {}
66 )
67
68 # Call the method again with application_data set
69 application_data = {}
70 xep_0294._parse_sdp_a_trigger(
71 "", [], call_data, {}, "audio", application_data, {}
72 )
73
74 # Assert value has been correctly transferred
75 assert application_data == {"extmap-allow-mixed": True}
76
77 def test_generate_sdp_content(self, host):
78 """SDP for 'extmap' and 'extmap-allow-mixed' attributes is correctly generated"""
79 xep_0294 = XEP_0294(host)
80
81 session = {}
82 local = False
83 idx = 0
84 content_data = {}
85 sdp_lines = []
86 application_data = {}
87 app_data_key = "rtp-hdrext"
88 media_data = {
89 "rtp-hdrext": {
90 "1": {
91 "id": "1",
92 "uri": "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
93 "senders": "both",
94 "parameters": {"param1": "value1", "param2": "value2"},
95 },
96 "2": {
97 "id": "2",
98 "uri": "urn:ietf:params:rtp-hdrext:time-offset",
99 "senders": "initiator",
100 },
101 "3": {
102 "id": "3",
103 "uri": "urn:ietf:params:rtp-hdrext:toffset",
104 "senders": "none",
105 },
106 },
107 "extmap-allow-mixed": True,
108 }
109 media = "audio"
110
111 xep_0294._generate_sdp_content_trigger(
112 session,
113 local,
114 idx,
115 content_data,
116 sdp_lines,
117 application_data,
118 app_data_key,
119 media_data,
120 media,
121 )
122
123 assert sdp_lines == [
124 "a=extmap:1/sendrecv urn:ietf:params:rtp-hdrext:ssrc-audio-level param1=value1 param2=value2",
125 "a=extmap:2/sendonly urn:ietf:params:rtp-hdrext:time-offset",
126 "a=extmap:3/inactive urn:ietf:params:rtp-hdrext:toffset",
127 "a=extmap-allow-mixed",
128 ]
129
130 def test_parse_description(self, host):
131 """'rtp-hdrext' and 'extmap-allow-mixed' elements are correctly parsed"""
132 xep_0294 = XEP_0294(host)
133 media_data = {}
134
135 desc_elt_str = """
136 <description>
137 <rtp-hdrext xmlns='urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'
138 id='1'
139 uri='urn:ietf:params:rtp-hdrext:ssrc-audio-level'
140 senders='both'>
141 <parameter xmlns='urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'
142 name='vad'
143 value='on'/>
144 </rtp-hdrext>
145 <rtp-hdrext xmlns='urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'
146 id='2'
147 uri='urn:ietf:params:rtp-hdrext:toffset'
148 senders='initiator'/>
149 <extmap-allow-mixed xmlns='urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'/>
150 </description>
151 """
152 desc_elt = parse(desc_elt_str)
153
154 xep_0294._parse_description_trigger(desc_elt, media_data)
155
156 assert media_data == {
157 "rtp-hdrext": {
158 "1": {
159 "id": "1",
160 "uri": "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
161 "senders": "both",
162 "parameters": {"vad": "on"},
163 },
164 "2": {
165 "id": "2",
166 "uri": "urn:ietf:params:rtp-hdrext:toffset",
167 "senders": "initiator",
168 },
169 },
170 "extmap-allow-mixed": True,
171 }
172
173 def test_build_description(self, host):
174 """'rtp-hdrext' and 'extmap-allow-mixed' elements are correctly built"""
175 xep_0294 = XEP_0294(host)
176
177 desc_elt = domish.Element((None, "description"))
178 media_data = {
179 "rtp-hdrext": {
180 "1": {
181 "id": "1",
182 "uri": "urn:ietf:params:rtp-hdrext:ssrc-audio-level",
183 "senders": "both",
184 "parameters": {"vad": "on"},
185 },
186 "2": {
187 "id": "2",
188 "uri": "urn:ietf:params:rtp-hdrext:toffset",
189 "senders": "initiator",
190 },
191 },
192 "extmap-allow-mixed": True,
193 }
194
195 xep_0294._build_description_trigger(desc_elt, media_data, {})
196
197 rtp_hdrext_elts = list(desc_elt.elements(NS_JINGLE_RTP_HDREXT, "rtp-hdrext"))
198 assert len(rtp_hdrext_elts) == 2
199
200 rtp_hdrext_elt1, rtp_hdrext_elt2 = rtp_hdrext_elts
201
202 assert rtp_hdrext_elt1["id"] == "1"
203 assert rtp_hdrext_elt1["uri"] == "urn:ietf:params:rtp-hdrext:ssrc-audio-level"
204
205 # both is default and should not be set
206 assert "senders" not in rtp_hdrext_elt1.attributes
207
208 param_elt1 = list(rtp_hdrext_elt1.elements(NS_JINGLE_RTP_HDREXT, "parameter"))[0]
209 assert param_elt1["name"] == "vad"
210 assert param_elt1["value"] == "on"
211
212 assert rtp_hdrext_elt2["id"] == "2"
213 assert rtp_hdrext_elt2["uri"] == "urn:ietf:params:rtp-hdrext:toffset"
214 assert rtp_hdrext_elt2["senders"] == "initiator"
215
216 extmap_allow_mixed_elts = list(
217 desc_elt.elements(NS_JINGLE_RTP_HDREXT, "extmap-allow-mixed")
218 )
219 assert len(extmap_allow_mixed_elts) == 1