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
|
import asyncio
from functools import partial
from ipaddress import IPv4Address, IPv6Address
from socket import AF_INET, AF_INET6
import pytest
from aiorpcx.util import SignatureInfo, signature_info, is_async_call
from aiorpcx.util import *
async def coro(x, y):
pass
def test_is_async_call():
z = coro(2, 3)
assert not is_async_call(z)
assert is_async_call(coro)
assert is_async_call(partial(coro, 3, 4))
assert is_async_call(partial(partial(coro, 3), 4))
assert not is_async_call(test_is_async_call)
assert not is_async_call(partial(is_async_call))
# Lose a warning
asyncio.get_event_loop().run_until_complete(z)
@pytest.mark.parametrize("hostname,answer", (
('', False),
('a', True),
('_', True),
# Hyphens
('-b', False),
('a.-b', False),
('a-b', True),
('b-', False),
('b-.c', False),
# Dots
('a.', True),
('a..', False),
('foo1.Foo', True),
('foo1..Foo', False),
('12Foo.Bar.Bax_', True),
('12Foo.Bar.Baz_12', True),
# Numeric TLD
('foo1.123', False),
('foo1.d123', True),
('foo1.123d', True),
# IP Addresses
('1.2.3.4', False),
('12::23', False),
# 63 octets in part
('a.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.bar', True),
# Over 63 octets in part
('a.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_1.bar', False),
# Length
(('a' * 62 + '.') * 4 + 'a', True), # 253
(('a' * 62 + '.') * 4 + 'ab', False), # 254
))
def test_is_valid_hostname(hostname,answer):
assert is_valid_hostname(hostname) == answer
@pytest.mark.parametrize("hostname", (2, b'1.2.3.4'))
def test_is_valid_hostname_bad(hostname):
with pytest.raises(TypeError):
is_valid_hostname(hostname)
@pytest.mark.parametrize("host,answer", (
('1.2.3.4', IPv4Address('1.2.3.4')),
('12:32::', IPv6Address('12:32::')),
(IPv4Address('8.8.8.8'), IPv4Address('8.8.8.8')),
(IPv6Address('::1'), IPv6Address('::1')),
('foo.bar.baz.', 'foo.bar.baz.'),
))
def test_classify_host(host, answer):
assert classify_host(host) == answer
@pytest.mark.parametrize("host", (2, b'1.2.3.4'))
def test_classify_host_bad_type(host):
with pytest.raises(TypeError):
classify_host(host)
@pytest.mark.parametrize("host", ('', 'a..', 'b-', 'a' * 64))
def test_classify_host_bad(host):
with pytest.raises(ValueError):
classify_host(host)
class TestNetAddress:
@pytest.mark.parametrize("host,port,answer,host_type",(
('foo.bar', '23', 'foo.bar:23', str),
('foo.bar', 23, 'foo.bar:23', str),
('foo.bar', 23.0, TypeError, None),
('::1', 15, '[::1]:15', IPv6Address),
('5.6.7.8', '23', '5.6.7.8:23', IPv4Address),
('5.6.7.8.9', '23', ValueError, None),
('[::1]', '23', ValueError, None),
('[::1]', 0, ValueError, None),
('[::1]', 65536, ValueError, None),
))
def test_constructor(self, host,port,answer,host_type):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
NetAddress(host, port)
else:
address = NetAddress(host, port)
assert str(address) == answer
assert isinstance(address.host, host_type)
def test_eq(self):
assert NetAddress('1.2.3.4', 23) == NetAddress('1.2.3.4', 23)
assert NetAddress('1.2.3.4', 23) == NetAddress('1.2.3.4', '23')
assert NetAddress('1.2.3.4', 23) != NetAddress('1.2.3.4', 24)
assert NetAddress('1.2.3.4', 24) != NetAddress('1.2.3.5', 24)
assert NetAddress('foo.bar', 24) != NetAddress('foo.baz', 24)
def test_hashable(self):
assert len({NetAddress('1.2.3.4', 23), NetAddress('1.2.3.4', '23')}) == 1
@pytest.mark.parametrize("host,port,answer",(
('foo.bar', '23', "NetAddress('foo.bar', 23)"),
('foo.bar', 23, "NetAddress('foo.bar', 23)"),
('::1', 15, "NetAddress(IPv6Address('::1'), 15)"),
('5.6.7.8', '23', "NetAddress(IPv4Address('5.6.7.8'), 23)"),
))
def test_repr(self, host, port, answer):
assert repr(NetAddress(host, port)) == answer
@pytest.mark.parametrize("string,default_func,answer",(
('foo.bar:23', None, NetAddress('foo.bar', 23)),
(':23', NetAddress.default_host('localhost'), NetAddress('localhost', 23)),
(':23', None, ValueError),
(':23', NetAddress.default_port(23), ValueError),
('foo.bar', NetAddress.default_port(500), NetAddress('foo.bar', 500)),
('foo.bar:', NetAddress.default_port(500), NetAddress('foo.bar', 500)),
('foo.bar', NetAddress.default_port(500), NetAddress('foo.bar', 500)),
(':', NetAddress.default_host_and_port('localhost', 80), NetAddress('localhost', 80)),
('::1:', None, ValueError),
('::1', None, ValueError),
('[::1:22', None, ValueError),
('[::1]:22', NetAddress.default_port(500), NetAddress('::1', 22)),
('[::1]:', NetAddress.default_port(500), NetAddress('::1', 500)),
('[::1]', NetAddress.default_port(500), NetAddress('::1', 500)),
('1.2.3.4:22', None, NetAddress('1.2.3.4', 22)),
('1.2.3.4:', NetAddress.default_port(500), NetAddress('1.2.3.4', 500)),
('1.2.3.4', NetAddress.default_port(500), NetAddress('1.2.3.4', 500)),
('localhost', NetAddress.default_port(500), NetAddress('localhost', 500)),
('1.2.3.4', NetAddress.default_host('localhost'), ValueError),
(2, None, TypeError),
(b'', None, TypeError),
))
def test_from_string(self, string, default_func, answer):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
NetAddress.from_string(string,default_func=default_func)
else:
assert NetAddress.from_string(string,default_func=default_func) == answer
@pytest.mark.parametrize("address,answer",(
(NetAddress('foo.bar', 23), 'foo.bar:23'),
(NetAddress('abcd::dbca', 40), '[abcd::dbca]:40'),
(NetAddress('1.2.3.5', 50000), '1.2.3.5:50000'),
))
def test_str(self, address, answer):
assert str(address) == answer
@pytest.mark.parametrize("attr", ('host', 'port'))
def test_immutable(self, attr):
address = NetAddress('foo.bar', 23)
with pytest.raises(AttributeError):
setattr(address, attr, 'foo')
setattr(address, 'foo', '')
class TestService:
@pytest.mark.parametrize("protocol,address,answer", (
('tcp', 'domain.tld:8000', Service('tcp', NetAddress('domain.tld', 8000))),
('SSL', NetAddress('domain.tld', '23'), Service('ssl', NetAddress('domain.tld', 23))),
('SSL', '[::1]:80', Service('SSL', NetAddress('::1', 80))),
('ws', '1.2.3.4:80', Service('ws', NetAddress('1.2.3.4', 80))),
(4, '1.2.3.4:80', TypeError),
('wss', '1.2.3.4:', ValueError),
))
def test_constructor(self, protocol, address, answer):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
Service(protocol, address)
else:
assert Service(protocol, address) == answer
def test_eq(self):
assert Service('http', '1.2.3.4:23') == Service(
'HTTP', NetAddress(IPv4Address('1.2.3.4'), 23))
assert Service('https', '1.2.3.4:23') != Service('http', '1.2.3.4:23')
assert Service('https', '1.2.3.4:23') != Service('https', '1.2.3.4:22')
def test_hashable(self):
assert 1 == len({Service('http', '1.2.3.4:23'),
Service('HTTP', NetAddress(IPv4Address('1.2.3.4'), 23))})
@pytest.mark.parametrize("protocol,address,answer", (
('TCP', 'foo.bar:23', 'tcp://foo.bar:23'),
('httpS', NetAddress('::1', 80), 'https://[::1]:80'),
('ws', NetAddress('1.2.3.4', '50000'), 'ws://1.2.3.4:50000'),
))
def test_str(self, protocol, address, answer):
assert str(Service(protocol, address)) == answer
@pytest.mark.parametrize("protocol, address, answer", (
('TCP', 'foo.bar:23', "Service('tcp', 'foo.bar:23')"),
('httpS', NetAddress('::1', 80), "Service('https', '[::1]:80')"),
('ws', NetAddress('1.2.3.4', '50000'), "Service('ws', '1.2.3.4:50000')"),
))
def test_repr(self, protocol, address, answer):
assert repr(Service(protocol, address)) == answer
def test_attributes(self):
service = Service('HttpS', '[::1]:80')
assert service.protocol == 'https'
assert service.address == NetAddress('::1', 80)
assert service.host == IPv6Address('::1')
assert service.port == 80
def default_func(protocol, kind):
if kind == ServicePart.PROTOCOL:
return 'SSL'
if kind == ServicePart.HOST:
return {'ssl': 'ssl_host.tld', 'tcp': 'tcp_host.tld'}.get(protocol)
return {'ssl': 443, 'tcp': '80', 'ws': 50001}.get(protocol)
@pytest.mark.parametrize("service,default_func,answer", (
('HTTP://foo.BAR:80', None, Service('http', NetAddress('foo.BAR', 80))),
('ssl://[::1]:80', None, Service('ssl', '[::1]:80')),
('ssl://5.6.7.8:50001', None, Service('ssl', NetAddress('5.6.7.8', 50001))),
('ssl://foo.bar', None, ValueError),
('ssl://:80', None, ValueError),
('foo.bar:80', None, ValueError),
('foo.bar', None, ValueError),
(2, None, TypeError),
# With default funcs
('localhost:80', default_func, Service('ssl', 'localhost:80')),
('localhost', default_func, Service('ssl', 'localhost:443')),
('WS://domain.tld', default_func, Service('ws', 'domain.tld:50001')),
# TCP has a default host and port
('tcp://localhost', default_func, Service('tcp', 'localhost:80')),
('tcp://:', default_func, Service('tcp', 'tcp_host.tld:80')),
('tcp://', default_func, Service('tcp', 'tcp_host.tld:80')),
# As TCP has a default host and port it is interpreted as a protocol not a host
('tcp', default_func, Service('tcp', 'tcp_host.tld:80')),
# WS has no default host
('ws://', default_func, ValueError),
('ws://:45', default_func, ValueError),
('ws://localhost', default_func, Service('ws', 'localhost:50001')),
# WS alone is interpreted as a host name as WS protocol has no default host
('ws', default_func, Service('ssl', 'ws:443')),
# Default everything
('', default_func, Service('ssl', 'ssl_host.tld:443')),
))
def test_from_string(self, service, default_func, answer):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
Service.from_string(service, default_func=default_func)
else:
assert Service.from_string(service, default_func=default_func) == answer
@pytest.mark.parametrize("attr", ('host', 'port', 'address', 'protocol'))
def test_immutable(self, attr):
service = Service.from_string('https://foo.bar:8000')
with pytest.raises(AttributeError):
setattr(service, attr, '')
setattr(service, 'foo', '')
@pytest.mark.parametrize("port,answer", (
('2', 2),
(65535, 65535),
(0, ValueError),
(-1, ValueError),
(65536, ValueError),
(b'', TypeError),
(2.0, TypeError),
('2a', ValueError),
))
def test_validate_port(port, answer):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
validate_port(port)
else:
assert validate_port(port) == answer
@pytest.mark.parametrize("protocol,answer", (
('TCP', 'tcp'),
('http', 'http'),
('Ftp.-xbar+', 'ftp.-xbar+'),
(b'', TypeError),
(2, TypeError),
('', ValueError),
('a@b', ValueError),
('a:b', ValueError),
('[23]', ValueError),
))
def test_validate_protocol(protocol, answer):
if isinstance(answer, type) and issubclass(answer, Exception):
with pytest.raises(answer):
validate_protocol(protocol)
else:
assert validate_protocol(protocol) == answer
|