Skip to content

Commit

Permalink
[ADD] Added pytest and tests for SIP authentication header parsing.
Browse files Browse the repository at this point in the history
[ADD] Added black checks for tests.
[CHANGE] Changed auth_match compile to use r-string to fix deprecation.
[CHANGE] Removed version pinning from requirements-test.
[CHANGE] Ran black over setup.py
  • Loading branch information
tayler6000 committed Sep 27, 2022
1 parent 234e6b0 commit 87e8d50
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -e . -r requirements-test.txt
- name: Black
- name: Black pyVoIP
run: black --check pyVoIP
- name: Black tests
run: black --check tests
34 changes: 34 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will install Python dependencies, run tests and lint
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Check pytest

on:
push:
branches:
- "master"
- "development"
pull_request:

jobs:
check-pytest:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- "3.7"

steps:
- uses: actions/checkout@v3.0.2
with:
fetch-depth: 2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4.0.0
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e . -r requirements-test.txt
- name: pytest
run: pytest
2 changes: 1 addition & 1 deletion pyVoIP/SIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def __init__(self, data: bytes):
self.body: Dict[str, Any] = {}
self.authentication: Dict[str, str] = {}
self.raw = data
self.auth_match = re.compile('(\w+)=("[^",]+"|[^ \t,]+)')
self.auth_match = re.compile(r'(\w+)=("[^",]+"|[^ \t,]+)')
self.parse(data)

def summary(self) -> str:
Expand Down
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
black==22.8.0
black
pytest
24 changes: 12 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
from setuptools import setup


with open("README.md", "r", encoding='utf-8') as f:
with open("README.md", "r", encoding="utf-8") as f:
long_description = f.read()

setup(
name='pyVoIP',
version='1.6.2',
description='PyVoIP is a pure python VoIP/SIP/RTP library.',
name="pyVoIP",
version="1.6.2",
description="PyVoIP is a pure python VoIP/SIP/RTP library.",
long_description=long_description,
long_description_content_type="text/markdown",
author='Tayler Porter',
author_email='taylerporter@gmail.com',
url='https://github.com/tayler6000/pyVoIP',
author="Tayler Porter",
author_email="taylerporter@gmail.com",
url="https://github.com/tayler6000/pyVoIP",
project_urls={
"Bug Tracker": "https://github.com/tayler6000/pyVoIP/issues",
"Documentaiton": "https://pyvoip.readthedocs.io/"
"Documentaiton": "https://pyvoip.readthedocs.io/",
},
classifiers=[
"Programming Language :: Python :: 3",
Expand All @@ -28,9 +28,9 @@
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Natural Language :: English",
"Topic :: Communications :: Internet Phone",
"Topic :: Communications :: Telephony"
"Topic :: Communications :: Telephony",
],
packages=find_packages(),
package_data={'pyVoIP': ['py.typed']},
python_requires=">=3.6"
packages=find_packages(exclude=("tests",)),
package_data={"pyVoIP": ["py.typed"]},
python_requires=">=3.6",
)
Empty file added tests/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions tests/test_sip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pyVoIP.SIP import SIPMessage
import pytest


@pytest.mark.parametrize(
"packet,expected",
[
(
b"""SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK03150189fc65493a9d4e3a582;rport=5060;received=192.168.178.110\r\nFrom: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=9338abd3\r\nTo: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=950C00889AC0DB3B\r\nCall-ID: 6b86b273ff34fce19d6b804eff5a3f57@0.0.0.0:5060\r\nCSeq: 1 REGISTER\r\nWWW-Authenticate: Digest realm="fritz.box", nonce="78B29326485EAE52"\r\nUser-Agent: FRITZ!OS\r\nContent-Length: 0\r\n\r\n""",
{"realm": "fritz.box", "nonce": "78B29326485EAE52"},
),
(
b"""SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK03150189fc65493a9d4e3a582;rport=5060;received=192.168.178.110\r\nFrom: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=9338abd3\r\nTo: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=950C00889AC0DB3B\r\nCall-ID: 6b86b273ff34fce19d6b804eff5a3f57@0.0.0.0:5060\r\nCSeq: 1 REGISTER\r\nWWW-Authenticate: Digest algorithm=MD5,realm="local",nonce="111111:222222aaaaaa333333bbbbbb444444"\r\nUser-Agent: FRITZ!OS\r\nContent-Length: 0\r\n\r\n""",
{
"algorithm": "MD5",
"realm": "local",
"nonce": "111111:222222aaaaaa333333bbbbbb444444",
},
),
(
b"""SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK03150189fc65493a9d4e3a582;rport=5060;received=192.168.178.110\r\nFrom: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=9338abd3\r\nTo: "tarantulla" <sip:tarantulla@192.168.178.1>;tag=950C00889AC0DB3B\r\nCall-ID: 6b86b273ff34fce19d6b804eff5a3f57@0.0.0.0:5060\r\nCSeq: 1 REGISTER\r\nWWW-Authenticate: Digest algorithm=MD5, realm="asterisk",nonce="45f77cee"\r\nUser-Agent: FRITZ!OS\r\nContent-Length: 0\r\n\r\n""",
{
"algorithm": "MD5",
"realm": "asterisk",
"nonce": "45f77cee",
},
),
(
b"""SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 192.168.0.76:5060;rport=5060;received=192.168.0.76;branch=z9hG4bK92b19bf363d84d2ea95d18cd3\r\nCall-ID: 6b86b273ff34fce19d6b804eff5a3f57@192.168.0.76:5060\r\nFrom: "5555" <sip:5555@192.168.0.100>;tag=fb11549a\r\nTo: "5555" <sip:5555@192.168.0.100>;tag=z9hG4bK92b19bf363d84d2ea95d18cd3\r\nCSeq: 1 REGISTER\r\nWWW-Authenticate: Digest realm="asterisk",nonce="1664256201/30ff48bd45c78b935077262030d584bd",opaque="5f0937be1ccec4cf",algorithm=md5,qop="auth"\r\nServer: Asterisk PBX 18.2.0\r\nContent-Length: 0\r\n\r\n""",
{
"algorithm": "md5",
"realm": "asterisk",
"nonce": "1664256201/30ff48bd45c78b935077262030d584bd",
"opaque": "5f0937be1ccec4cf",
"qop": "auth",
},
),
],
)
def test_sip_authentication(packet, expected):
message = SIPMessage(packet)
assert message.authentication == expected

0 comments on commit 87e8d50

Please sign in to comment.