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
|
import unittest
from aioice import Candidate
class CandidateTest(unittest.TestCase):
def test_can_pair_ipv4(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 12345 typ host generation 0"
)
self.assertTrue(candidate_a.can_pair_with(candidate_b))
def test_can_pair_ipv4_case_insensitive(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 1 UDP 659136 1.2.3.4 12345 typ host generation 0"
)
self.assertTrue(candidate_a.can_pair_with(candidate_b))
def test_can_pair_ipv6(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 2a02:0db8:85a3:0000:0000:8a2e:0370:7334 31102"
" typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 1 udp 659136 2a02:0db8:85a3:0000:0000:8a2e:0370:7334 12345"
" typ host generation 0"
)
self.assertTrue(candidate_a.can_pair_with(candidate_b))
def test_cannot_pair_ipv4_ipv6(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 1 udp 659136 2a02:0db8:85a3:0000:0000:8a2e:0370:7334 12345"
" typ host generation 0"
)
self.assertFalse(candidate_a.can_pair_with(candidate_b))
def test_cannot_pair_different_components(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 2 udp 659136 1.2.3.4 12345 typ host generation 0"
)
self.assertFalse(candidate_a.can_pair_with(candidate_b))
def test_cannot_pair_different_transports(self) -> None:
candidate_a = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
candidate_b = Candidate.from_sdp(
"6815297761 1 tcp 659136 1.2.3.4 12345 typ host generation 0 tcptype active"
)
self.assertFalse(candidate_a.can_pair_with(candidate_b))
def test_from_sdp_udp(self) -> None:
candidate = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
self.assertEqual(candidate.foundation, "6815297761")
self.assertEqual(candidate.component, 1)
self.assertEqual(candidate.transport, "udp")
self.assertEqual(candidate.priority, 659136)
self.assertEqual(candidate.host, "1.2.3.4")
self.assertEqual(candidate.port, 31102)
self.assertEqual(candidate.type, "host")
self.assertEqual(candidate.generation, 0)
self.assertEqual(
candidate.to_sdp(),
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0",
)
def test_from_sdp_udp_srflx(self) -> None:
candidate = Candidate.from_sdp(
"1 1 UDP 1686052863 1.2.3.4 42705 typ srflx raddr 192.168.1.101 rport 42705"
)
self.assertEqual(candidate.foundation, "1")
self.assertEqual(candidate.component, 1)
self.assertEqual(candidate.transport, "UDP")
self.assertEqual(candidate.priority, 1686052863)
self.assertEqual(candidate.host, "1.2.3.4")
self.assertEqual(candidate.port, 42705)
self.assertEqual(candidate.type, "srflx")
self.assertEqual(candidate.related_address, "192.168.1.101")
self.assertEqual(candidate.related_port, 42705)
self.assertEqual(candidate.generation, None)
self.assertEqual(
candidate.to_sdp(),
"1 1 UDP 1686052863 1.2.3.4 42705 typ srflx raddr 192.168.1.101 "
"rport 42705",
)
def test_from_sdp_tcp(self) -> None:
candidate = Candidate.from_sdp(
"1936595596 1 tcp 1518214911 1.2.3.4 9 typ host "
"tcptype active generation 0 network-id 1 network-cost 10"
)
self.assertEqual(candidate.foundation, "1936595596")
self.assertEqual(candidate.component, 1)
self.assertEqual(candidate.transport, "tcp")
self.assertEqual(candidate.priority, 1518214911)
self.assertEqual(candidate.host, "1.2.3.4")
self.assertEqual(candidate.port, 9)
self.assertEqual(candidate.type, "host")
self.assertEqual(candidate.tcptype, "active")
self.assertEqual(candidate.generation, 0)
self.assertEqual(
candidate.to_sdp(),
"1936595596 1 tcp 1518214911 1.2.3.4 9 typ host tcptype active "
"generation 0",
)
def test_from_sdp_no_generation(self) -> None:
candidate = Candidate.from_sdp("6815297761 1 udp 659136 1.2.3.4 31102 typ host")
self.assertEqual(candidate.foundation, "6815297761")
self.assertEqual(candidate.component, 1)
self.assertEqual(candidate.transport, "udp")
self.assertEqual(candidate.priority, 659136)
self.assertEqual(candidate.host, "1.2.3.4")
self.assertEqual(candidate.port, 31102)
self.assertEqual(candidate.type, "host")
self.assertEqual(candidate.generation, None)
self.assertEqual(
candidate.to_sdp(), "6815297761 1 udp 659136 1.2.3.4 31102 typ host"
)
def test_from_sdp_truncated(self) -> None:
with self.assertRaises(ValueError):
Candidate.from_sdp("6815297761 1 udp 659136 1.2.3.4 31102 typ")
def test_repr(self) -> None:
candidate = Candidate.from_sdp(
"6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0"
)
self.assertEqual(
repr(candidate),
"Candidate(6815297761 1 udp 659136 1.2.3.4 31102 typ host generation 0)",
)
|