Skip to content

Commit dfc09c2

Browse files
committed
Tests: Add crypto related fixtures / boilerplate test & class
1 parent 852fb1e commit dfc09c2

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

tests/conftest.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Dict
1+
from typing import Dict, Tuple
22
import os
33
import pytest
44
from binascii import unhexlify
55

6+
from xcloud.protocol import srtp_crypto
7+
68
@pytest.fixture(scope='session')
79
def test_data() -> Dict[str, bytes]:
810
data = {}
@@ -38,4 +40,24 @@ def teredo_packet() -> bytes:
3840
return unhexlify(
3941
'6000000000003b1520010000338c24f41c38f3fdd2f3c93d20010000'
4042
'338c24f4043b30e3d2f3c93d01049eb8960803080000c0a889db0c02'
41-
)
43+
)
44+
45+
@pytest.fixture(scope='session')
46+
def session_id() -> str:
47+
return 'ED309CA5-F87C-439D-A429-63F417B552FA'
48+
49+
@pytest.fixture(scope='session')
50+
def ice_credentials_client() -> Tuple[str, str]:
51+
return ('m99KewV+44E=', 'AneALie0L4P2tpvbh76nremwgQrT12/R3UYTG5VmUJ8=')
52+
53+
@pytest.fixture(scope='session')
54+
def ice_credentials_host() -> Tuple[str, str]:
55+
return ('5yUsZtOzQ+w=', 'bWpvx/cXTk3/IeadJHO4T19W/OZopsbn0MwTAZqZu8w=')
56+
57+
@pytest.fixture(scope='session')
58+
def srtp_key() -> str:
59+
return 'RdHzuLLVGuO1aHILIEVJ1UzR7RWVioepmpy+9SRf'
60+
61+
@pytest.fixture(scope='session')
62+
def crypto_context(srtp_key: str) -> srtp_crypto.MsSrtpCrypto:
63+
return srtp_crypto.MsSrtpCrypto(srtp_key)
1.33 KB
Binary file not shown.

tests/test_crypto.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from xcloud.protocol import srtp_crypto
2+
3+
def test_decrypt(test_data: dict, crypto_context: srtp_crypto.MsSrtpCrypto):
4+
rtp_packet_raw = test_data['rtp_connection_probing.bin']
5+
plaintext = crypto_context.decrypt_raw(rtp_packet_raw)
6+
7+
print(plaintext)
8+
assert plaintext is not None

xcloud/protocol/srtp_crypto.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import base64
2+
from aiortc.rtp import RtpPacket
3+
4+
class MsSrtpCrypto:
5+
def __init__(self, master_key: str):
6+
try:
7+
master_key = base64.b64decode(master_key)
8+
except Exception:
9+
raise ValueError('Master key is not base64-decodable')
10+
11+
self.master_key = master_key
12+
13+
def decrypt(self, rtp_data: RtpPacket) -> RtpPacket:
14+
raise NotImplementedError('Decryption not implemented')
15+
16+
def decrypt_raw(self, data: bytes) -> RtpPacket:
17+
packet = RtpPacket.parse(data)
18+
return self.decrypt(packet)
19+
20+
def encrypt(self, rtp_data: RtpPacket) -> RtpPacket:
21+
raise NotImplementedError('Encryption not implemented')

0 commit comments

Comments
 (0)