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
|
import dataclasses
import secrets
from unittest import TestCase
import pylibsrtp
from pylibsrtp import Error, Policy, Session
RTP = (
b"\x80\x08\x00\x00" # version, packet type, sequence number
b"\x00\x00\x00\x00" # timestamp
b"\x00\x00\x30\x39" # ssrc: 12345
) + (b"\xd4" * 160)
RTCP = (
b"\x80\xc8\x00\x06\xf3\xcb\x20\x01\x83\xab\x03\xa1\xeb\x02\x0b\x3a"
b"\x00\x00\x94\x20\x00\x00\x00\x9e\x00\x00\x9b\x88"
)
@dataclasses.dataclass
class Profile:
key_length: int
protected_rtp_length: int
protected_rtcp_length: int
srtp_profile: int
# AES-GCM may not be supported depending on how libsrtp2 was built.
SRTP_PROFILES = [
Profile(
key_length=30,
protected_rtp_length=182,
protected_rtcp_length=42,
srtp_profile=Policy.SRTP_PROFILE_AES128_CM_SHA1_80,
),
Profile(
key_length=30,
protected_rtp_length=176,
protected_rtcp_length=42,
srtp_profile=Policy.SRTP_PROFILE_AES128_CM_SHA1_32,
),
]
try:
Policy(srtp_profile=Policy.SRTP_PROFILE_AEAD_AES_128_GCM)
except Error:
pass
else:
SRTP_PROFILES += [
Profile(
key_length=28,
protected_rtp_length=188,
protected_rtcp_length=48,
srtp_profile=Policy.SRTP_PROFILE_AEAD_AES_128_GCM,
),
Profile(
key_length=44,
protected_rtp_length=188,
protected_rtcp_length=48,
srtp_profile=Policy.SRTP_PROFILE_AEAD_AES_256_GCM,
),
]
class PolicyTest(TestCase):
def test_allow_repeat_tx(self):
policy = Policy()
self.assertEqual(policy.allow_repeat_tx, False)
policy.allow_repeat_tx = True
self.assertEqual(policy.allow_repeat_tx, True)
policy.allow_repeat_tx = False
self.assertEqual(policy.allow_repeat_tx, False)
policy.allow_repeat_tx = 1
self.assertEqual(policy.allow_repeat_tx, True)
policy.allow_repeat_tx = 0
self.assertEqual(policy.allow_repeat_tx, False)
def test_key(self):
key = secrets.token_bytes(30)
policy = Policy()
self.assertEqual(policy.key, None)
policy.key = key
self.assertEqual(policy.key, key)
policy.key = None
self.assertEqual(policy.key, None)
# Key is not bytes.
with self.assertRaises(TypeError) as cm:
policy.key = 1234
self.assertEqual(policy.key, None)
self.assertEqual(str(cm.exception), "key must be bytes")
# Key is too short.
with self.assertRaises(ValueError) as cm:
policy.key = b"0"
self.assertEqual(policy.key, None)
self.assertEqual(str(cm.exception), "key must contain at least 30 bytes")
def test_srtp_policy(self):
# Default profile.
policy = Policy()
self.assertEqual(policy.srtp_profile, Policy.SRTP_PROFILE_AES128_CM_SHA1_80)
# Valid user-specified profiles.
for profile in SRTP_PROFILES:
with self.subTest(profile=profile):
policy = Policy(srtp_profile=profile.srtp_profile)
self.assertEqual(policy.srtp_profile, profile.srtp_profile)
# Invalid profile.
with self.assertRaises(Error) as cm:
Policy(srtp_profile=0)
self.assertEqual(str(cm.exception), "unsupported parameter")
def test_ssrc_type(self):
policy = Policy()
self.assertEqual(policy.ssrc_type, Policy.SSRC_UNDEFINED)
policy.ssrc_type = Policy.SSRC_ANY_INBOUND
self.assertEqual(policy.ssrc_type, Policy.SSRC_ANY_INBOUND)
def test_ssrc_value(self):
policy = Policy()
self.assertEqual(policy.ssrc_value, 0)
policy.ssrc_value = 12345
self.assertEqual(policy.ssrc_value, 12345)
def test_window_size(self):
policy = Policy()
self.assertEqual(policy.window_size, 0)
policy.window_size = 1024
self.assertEqual(policy.window_size, 1024)
class SessionTest(TestCase):
def test_no_key(self):
policy = Policy(ssrc_type=Policy.SSRC_ANY_OUTBOUND)
with self.assertRaises(Error) as cm:
Session(policy=policy)
self.assertEqual(str(cm.exception), "unsupported parameter")
def test_add_remove_stream(self):
for profile in SRTP_PROFILES:
with self.subTest(profile=profile):
key = secrets.token_bytes(profile.key_length)
# protect RTP
tx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_SPECIFIC,
ssrc_value=12345,
)
)
protected = tx_session.protect(RTP)
self.assertEqual(len(protected), profile.protected_rtp_length)
# add stream and unprotect RTP
rx_session = Session()
rx_session.add_stream(
Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_SPECIFIC,
ssrc_value=12345,
)
)
unprotected = rx_session.unprotect(protected)
self.assertEqual(unprotected, RTP)
# remove stream
rx_session.remove_stream(12345)
# try removing stream again
with self.assertRaises(Error) as cm:
rx_session.remove_stream(12345)
self.assertEqual(str(cm.exception), "no appropriate context found")
def test_rtp_any_ssrc(self):
for profile in SRTP_PROFILES:
with self.subTest(profile=profile):
key = secrets.token_bytes(profile.key_length)
# protect RTP
tx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_ANY_OUTBOUND,
)
)
protected = tx_session.protect(RTP)
self.assertEqual(len(protected), profile.protected_rtp_length)
# bad type
with self.assertRaises(TypeError) as cm:
tx_session.protect(4567)
self.assertEqual(str(cm.exception), "packet must be bytes")
# bad length
with self.assertRaises(ValueError) as cm:
tx_session.protect(b"0" * (1501 - pylibsrtp.SRTP_MAX_TRAILER_LEN))
self.assertEqual(str(cm.exception), "packet is too long")
# unprotect RTP
rx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_ANY_INBOUND,
)
)
unprotected = rx_session.unprotect(protected)
self.assertEqual(unprotected, RTP)
def test_rtcp_any_ssrc(self):
for profile in SRTP_PROFILES:
with self.subTest(profile=profile):
key = secrets.token_bytes(profile.key_length)
# protect RCTP
tx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_ANY_OUTBOUND,
)
)
protected = tx_session.protect_rtcp(RTCP)
self.assertEqual(len(protected), profile.protected_rtcp_length)
# bad type
with self.assertRaises(TypeError) as cm:
tx_session.protect_rtcp(4567)
self.assertEqual(str(cm.exception), "packet must be bytes")
# bad length
with self.assertRaises(ValueError) as cm:
tx_session.protect_rtcp(
b"0" * (1501 - pylibsrtp.SRTP_MAX_SRTCP_TRAILER_LEN)
)
self.assertEqual(str(cm.exception), "packet is too long")
# unprotect RTCP
rx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_ANY_INBOUND,
)
)
unprotected = rx_session.unprotect_rtcp(protected)
self.assertEqual(unprotected, RTCP)
def test_rtp_specific_ssrc(self):
for profile in SRTP_PROFILES:
with self.subTest(profile=profile):
key = secrets.token_bytes(profile.key_length)
# protect RTP
tx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_SPECIFIC,
ssrc_value=12345,
)
)
protected = tx_session.protect(RTP)
self.assertEqual(len(protected), profile.protected_rtp_length)
# unprotect RTP
rx_session = Session(
policy=Policy(
key=key,
srtp_profile=profile.srtp_profile,
ssrc_type=Policy.SSRC_SPECIFIC,
ssrc_value=12345,
)
)
unprotected = rx_session.unprotect(protected)
self.assertEqual(unprotected, RTP)
|