1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
"""Test voip_utils SIP functionality."""
from voip_utils.sip import CallInfo, SdpInfo, SipDatagramProtocol, SipEndpoint, SipMessage, get_sip_endpoint, parse_via_header
from unittest.mock import Mock
_CRLF = "\r\n"
def test_parse_header_for_uri():
endpoint = SipEndpoint('"Test Name" <sip:12345@example.com>')
assert endpoint.description == "Test Name"
assert endpoint.uri == "sip:12345@example.com"
assert endpoint.username == "12345"
assert endpoint.host == "example.com"
assert endpoint.port == 5060
def test_parse_header_for_uri_no_name():
endpoint = SipEndpoint("sip:12345@example.com")
assert endpoint.description is None
assert endpoint.uri == "sip:12345@example.com"
def test_parse_header_for_uri_sips():
endpoint = SipEndpoint('"Test Name" <sips:12345@example.com>')
assert endpoint.description == "Test Name"
assert endpoint.uri == "sips:12345@example.com"
def test_parse_header_for_uri_no_space_name():
endpoint = SipEndpoint("Test <sip:12345@example.com>")
assert endpoint.description == "Test"
assert endpoint.uri == "sip:12345@example.com"
def test_parse_header_for_uri_no_space_between_name():
endpoint = SipEndpoint("Test<sip:12345@example.com>")
assert endpoint.description == "Test"
assert endpoint.uri == "sip:12345@example.com"
def test_parse_header_for_uri_no_space_between_quoted_name():
endpoint = SipEndpoint('"Test Endpoint"<sip:12345@example.com>')
assert endpoint.description == "Test Endpoint"
assert endpoint.uri == "sip:12345@example.com"
def test_parse_header_for_uri_no_username():
endpoint = SipEndpoint("Test <sip:example.com>")
assert endpoint.description == "Test"
assert endpoint.username is None
assert endpoint.uri == "sip:example.com"
def test_parse_header_for_parameters():
endpoint = SipEndpoint("Test <sip:example.com;transport=tcp;lr;method=INVITE>")
assert endpoint.description == "Test"
assert endpoint.uri == "sip:example.com;transport=tcp;lr;method=INVITE"
assert endpoint.host == "example.com"
assert endpoint.uri_parameters
assert "transport" in endpoint.uri_parameters
assert "lr" in endpoint.uri_parameters
assert "method" in endpoint.uri_parameters
assert endpoint.uri_parameters["transport"] == "tcp"
assert not endpoint.uri_parameters["lr"]
assert endpoint.uri_parameters["method"] == "INVITE"
def test_parse_header_for_uri_headers():
endpoint = SipEndpoint("Test <sip:example.com?priority=urgent&subject=Hello>")
assert endpoint.description == "Test"
assert endpoint.uri == "sip:example.com?priority=urgent&subject=Hello"
assert endpoint.host == "example.com"
assert endpoint.uri_headers
assert "priority" in endpoint.uri_headers
assert "subject" in endpoint.uri_headers
assert endpoint.uri_headers["priority"] == "urgent"
assert endpoint.uri_headers["subject"] == "Hello"
def test_parse_header_for_parameters_and_headers():
endpoint = SipEndpoint("Test <sip:example.com;transport=tcp;lr;method=INVITE?priority=urgent&subject=Hello>")
assert endpoint.description == "Test"
assert endpoint.uri == "sip:example.com;transport=tcp;lr;method=INVITE?priority=urgent&subject=Hello"
assert endpoint.host == "example.com"
assert endpoint.uri_parameters
assert "transport" in endpoint.uri_parameters
assert "lr" in endpoint.uri_parameters
assert "method" in endpoint.uri_parameters
assert endpoint.uri_parameters["transport"] == "tcp"
assert not endpoint.uri_parameters["lr"]
assert endpoint.uri_parameters["method"] == "INVITE"
assert endpoint.uri_headers
assert "priority" in endpoint.uri_headers
assert "subject" in endpoint.uri_headers
assert endpoint.uri_headers["priority"] == "urgent"
assert endpoint.uri_headers["subject"] == "Hello"
assert endpoint.base_uri == "sip:example.com"
def test_get_sip_endpoint():
endpoint = get_sip_endpoint("example.com")
assert endpoint.host == "example.com"
assert endpoint.port == 5060
assert endpoint.description is None
assert endpoint.username is None
assert endpoint.uri == "sip:example.com"
def test_get_sip_endpoint_with_username():
endpoint = get_sip_endpoint("example.com", username="test")
assert endpoint.host == "example.com"
assert endpoint.port == 5060
assert endpoint.description is None
assert endpoint.username == "test"
assert endpoint.uri == "sip:test@example.com"
def test_get_sip_endpoint_with_description():
endpoint = get_sip_endpoint("example.com", description="Test Endpoint")
assert endpoint.host == "example.com"
assert endpoint.port == 5060
assert endpoint.description == "Test Endpoint"
assert endpoint.username is None
assert endpoint.uri == "sip:example.com"
assert endpoint.sip_header == '"Test Endpoint" <sip:example.com>'
def test_get_sip_endpoint_with_scheme():
endpoint = get_sip_endpoint("example.com", scheme="sips")
assert endpoint.host == "example.com"
assert endpoint.port == 5060
assert endpoint.description is None
assert endpoint.username is None
assert endpoint.uri == "sips:example.com"
def test_get_sip_endpoint_with_description_and_parameters():
endpoint = get_sip_endpoint("example.com", description="Test Endpoint", header_parameters={"tag": "decafc0ffee"})
assert endpoint.host == "example.com"
assert endpoint.port == 5060
assert endpoint.description == "Test Endpoint"
assert endpoint.username is None
assert endpoint.uri == "sip:example.com"
assert endpoint.sip_header == '"Test Endpoint" <sip:example.com>;tag=decafc0ffee'
def test_parse_via_header():
via_header_value = "SIP/2.0/UDP testhost:5061"
result = parse_via_header(via_header_value)
assert result
host, port = result
assert host == "testhost"
assert port == 5061
def test_parse_via_header_default_port():
via_header_value = "SIP/2.0/UDP testhost"
result = parse_via_header(via_header_value)
assert result
host, port = result
assert host == "testhost"
assert port == 5060
def test_parse_via_header_with_parameters():
via_header_value = "SIP/2.0/UDP testhost;branch=brnch12345"
result = parse_via_header(via_header_value)
assert result
host, port = result
assert host == "testhost"
assert port == 5060
def test_parse_via_header_error():
via_header_value = "some garbage text"
result = parse_via_header(via_header_value)
assert result is None
def test_parse_freepbx_options():
options_lines = [
"",
"OPTIONS sip:10.5.1.2:5060 SIP/2.0"
"Via: SIP/2.0/UDP 10.5.1.3:5060;rport;branch=z9hG4bKPj67dd8ad6-5b27-4860-b9fb-8bae195d6443"
"From: <sip:HomeAssistant@10.5.1.3>;tag=bab3c78d-7659-466f-8326-61d6da0c5267"
"To: <sip:10.5.1.2>"
"Contact: <sip:999@10.5.1.3:5060>"
"Call-ID: 9aa75329-b33d-4e27-b2e3-73ab30677942"
"CSeq: 14010 OPTIONS"
"Max-Forwards: 70"
"User-Agent: FPBX-17.0.19.27(21.8.0)"
"Content-Length: 0"
"",
]
options_text = _CRLF.join(options_lines) + _CRLF
options_msg = SipMessage.parse_sip(options_text, False)
assert options_msg is not None
def test_parse_with_body():
invite_lines = [
"",
"INVITE sip:6002@192.168.0.18 SIP/2.0",
"Via: SIP/2.0/UDP 192.168.0.18:5062",
"From: sip:5000@192.168.0.18:5062",
"Contact: sip:5000@192.168.0.18:5062",
"To: sip:6002@192.168.0.18",
"Call-ID: 5443482144267586",
"CSeq: 50 INVITE",
"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 391",
"",
"v=0",
"o=5000 5443482144267586 5443482144267586 IN IP4 192.168.0.18",
"s=Talk",
"c=IN IP4 192.168.0.18",
"t=0 0",
"m=audio 59756 RTP/AVP 123 96 101 103 104",
"a=sendrecv",
"a=rtpmap:96 opus/48000/2",
"a=fmtp:96 useinbandfec=0",
"a=rtpmap:123 opus/48000/2",
"a=fmtp:123 maxplaybackrate=16000",
"a=rtpmap:101 telephone-event/48000",
"a=rtpmap:103 telephone-event/16000",
"a=rtpmap:104 telephone-event/8000",
"a=ptime:20"
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, False)
assert invite_msg is not None
assert invite_msg.body is not None
assert invite_msg.body.startswith("v=0")
class MockSipDatagramProtocol(SipDatagramProtocol):
def on_call(self, call_info: CallInfo):
pass
def test_cancel():
protocol = MockSipDatagramProtocol(SdpInfo("username", 5, "session", "version"))
source = get_sip_endpoint("testsource")
destination = get_sip_endpoint("destination")
invite_lines = [
f"INVITE {destination.uri} SIP/2.0",
f"Via: SIP/2.0/UDP {source.host}:{source.port}",
f"From: {source.sip_header}",
f"Contact: {source.sip_header}",
f"To: {destination.sip_header}",
f"Call-ID: 100",
"CSeq: 50 INVITE",
f"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 0",
"",
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, False)
call_info = CallInfo(
caller_endpoint=destination,
local_endpoint=source,
caller_rtp_port=12345,
server_ip=source.host,
headers=invite_msg.headers,
)
transport = Mock()
protocol.connection_made(transport)
protocol.cancel_call(call_info)
transport.sendto.assert_called_once_with(b'CANCEL sip:destination SIP/2.0\r\nVia: SIP/2.0/UDP testsource:5060\r\nFrom: sip:testsource\r\nTo: sip:destination\r\nCall-ID: 100\r\nCSeq: 50 CANCEL\r\nUser-Agent: voip-utils 1.0\r\nContent-Length: 0\r\n\r\n', ('destination', 5060))
def test_answer_to_add_tag():
protocol = MockSipDatagramProtocol(SdpInfo("username", 5, "session", "version"))
transport = Mock()
source = get_sip_endpoint("testsource")
destination = get_sip_endpoint("destination")
invite_lines = [
f"INVITE {destination.uri} SIP/2.0",
f"Via: SIP/2.0/UDP {source.host}:{source.port}",
f"From: {source.sip_header}",
f"Contact: {source.sip_header}",
f"To: {destination.sip_header};tag=deadbeef",
f"Call-ID: 100",
"CSeq: 50 INVITE",
f"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 0",
"",
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, True)
call_info = CallInfo(
caller_endpoint=destination,
local_endpoint=source,
caller_rtp_port=12345,
server_ip=source.host,
headers=invite_msg.headers,
)
protocol.connection_made(transport)
protocol.answer(call_info, 12345)
transport.sendto.assert_called_once_with(b'SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP testsource:5060\r\nFrom: sip:testsource\r\nTo: sip:destination;tag=deadbeef\r\nCall-ID: 100\r\nContent-Type: application/sdp\r\nContent-Length: 174\r\nCSeq: 50 INVITE\r\nContact: sip:testsource\r\nUser-Agent: username 5 version\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS\r\n\r\nv=0\r\no=username 5 1 IN IP4 testsource\r\ns=session\r\nc=IN IP4 testsource\r\nt=0 0\r\nm=audio 12345 RTP/AVP 123\r\na=rtpmap:123 opus/48000/2\r\na=ptime:20\r\na=maxptime:150\r\na=sendrecv\r\n\r\n', ('destination', 5060))
class TagBytesMatcher:
def __init__(self, prefix: bytes, suffix: bytes, expected_length: int):
self.prefix = prefix
self.suffix = suffix
self.expected_length = expected_length
def __eq__(self, other):
if not isinstance(other, bytes):
return False
if not other.startswith(self.prefix) or not other.endswith(self.suffix):
return False
middle = other[len(self.prefix):-len(self.suffix) or None]
return len(middle) == self.expected_length
def __repr__(self):
return f"<TagBytesMatcher(prefix={self.prefix}, expected_length={self.expected_length}, suffix={self.suffix}"
def test_answer_to_generated_tag():
protocol = MockSipDatagramProtocol(SdpInfo("username", 5, "session", "version"))
transport = Mock()
source = get_sip_endpoint("testsource")
destination = get_sip_endpoint("destination")
invite_lines = [
f"INVITE {destination.uri} SIP/2.0",
f"Via: SIP/2.0/UDP {source.host}:{source.port}",
f"From: {source.sip_header}",
f"Contact: {source.sip_header}",
f"To: {destination.sip_header}",
f"Call-ID: 100",
"CSeq: 50 INVITE",
f"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 0",
"",
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, True)
call_info = CallInfo(
caller_endpoint=destination,
local_endpoint=source,
caller_rtp_port=12345,
server_ip=source.host,
headers=invite_msg.headers,
)
protocol.connection_made(transport)
protocol.answer(call_info, 12345)
transport.sendto.assert_called_once_with(TagBytesMatcher(b'SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP testsource:5060\r\nFrom: sip:testsource\r\nTo: sip:destination;tag=', b'\r\nCall-ID: 100\r\nContent-Type: application/sdp\r\nContent-Length: 174\r\nCSeq: 50 INVITE\r\nContact: sip:testsource\r\nUser-Agent: username 5 version\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS\r\n\r\nv=0\r\no=username 5 1 IN IP4 testsource\r\ns=session\r\nc=IN IP4 testsource\r\nt=0 0\r\nm=audio 12345 RTP/AVP 123\r\na=rtpmap:123 opus/48000/2\r\na=ptime:20\r\na=maxptime:150\r\na=sendrecv\r\n\r\n', 16), ('destination', 5060))
def test_answer_to_generated_tag_with_desc():
protocol = MockSipDatagramProtocol(SdpInfo("username", 5, "session", "version"))
transport = Mock()
source = get_sip_endpoint("testsource")
destination = get_sip_endpoint(host="destination", description="Test Endpoint")
invite_lines = [
f"INVITE {destination.uri} SIP/2.0",
f"Via: SIP/2.0/UDP {source.host}:{source.port}",
f"From: {source.sip_header}",
f"Contact: {source.sip_header}",
f"To: {destination.sip_header}",
f"Call-ID: 100",
"CSeq: 50 INVITE",
f"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 0",
"",
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, True)
call_info = CallInfo(
caller_endpoint=destination,
local_endpoint=source,
caller_rtp_port=12345,
server_ip=source.host,
headers=invite_msg.headers,
)
protocol.connection_made(transport)
protocol.answer(call_info, 12345)
transport.sendto.assert_called_once_with(TagBytesMatcher(b'SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP testsource:5060\r\nFrom: sip:testsource\r\nTo: "Test Endpoint" <sip:destination>;tag=', b'\r\nCall-ID: 100\r\nContent-Type: application/sdp\r\nContent-Length: 174\r\nCSeq: 50 INVITE\r\nContact: sip:testsource\r\nUser-Agent: username 5 version\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS\r\n\r\nv=0\r\no=username 5 1 IN IP4 testsource\r\ns=session\r\nc=IN IP4 testsource\r\nt=0 0\r\nm=audio 12345 RTP/AVP 123\r\na=rtpmap:123 opus/48000/2\r\na=ptime:20\r\na=maxptime:150\r\na=sendrecv\r\n\r\n', 16), ('destination', 5060))
def test_cancel_via():
protocol = MockSipDatagramProtocol(SdpInfo("username", 5, "session", "version"))
source = get_sip_endpoint("testsource")
destination = get_sip_endpoint("destination")
invite_lines = [
f"INVITE {destination.uri} SIP/2.0",
f"Via: SIP/2.0/UDP {source.host}:{source.port}",
f"From: {source.sip_header}",
f"Contact: {source.sip_header}",
f"To: {destination.sip_header}",
f"Call-ID: 100",
"CSeq: 50 INVITE",
f"User-Agent: test-agent 1.0",
"Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE",
"Accept: application/sdp, application/dtmf-relay",
"Content-Type: application/sdp",
"Content-Length: 0",
"",
]
invite_text = _CRLF.join(invite_lines) + _CRLF
invite_msg = SipMessage.parse_sip(invite_text, False)
call_info = CallInfo(
caller_endpoint=destination,
local_endpoint=source,
caller_rtp_port=12345,
server_ip=source.host,
headers=invite_msg.headers,
via_host="viahost",
via_port=5061
)
transport = Mock()
protocol.connection_made(transport)
protocol.cancel_call(call_info)
transport.sendto.assert_called_once_with(b'CANCEL sip:destination SIP/2.0\r\nVia: SIP/2.0/UDP testsource:5060\r\nFrom: sip:testsource\r\nTo: sip:destination\r\nCall-ID: 100\r\nCSeq: 50 CANCEL\r\nUser-Agent: voip-utils 1.0\r\nContent-Length: 0\r\n\r\n', ('viahost', 5061))
|