async def test_socks5_invalid_address_type(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x00\x00\xFF') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'invalid data' in str(ct)
async def test_socks5_cmd_not_granted(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x01\x00') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'General SOCKS server failure' in str(ct)
async def test_socks5_auth_failed(loop):
    async with FakeSocksSrv(loop, b'\x05\x02\x01\x01') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'authentication failed' in str(ct)
async def test_socks5_auth_ver_err(loop):
    async with FakeSocksSrv(loop, b'\x04\x02') as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert 'invalid version' in str(ct)
async def test_socks4_srv_error(loop):
    pld = b'\x00\x5b\x04W\x01\x01\x01\x01'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks4Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks4Auth('usr')
        dst = ('python.org', 80)

        with pytest.raises(aiosocksy.SocksError) as ct:
            await aiosocksy.create_connection(None, addr, auth, dst, loop=loop)
        assert '0x5b' in str(ct)
async def test_socks5_atype_domain(loop):
    pld = b'\x05\x02\x01\x00\x05\x00\x00\x03\x0apython.org\x04W'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        transport, protocol = await aiosocksy.create_connection(None,
                                                                addr,
                                                                auth,
                                                                dst,
                                                                loop=loop)
        assert protocol.proxy_sockname == (b'python.org', 1111)

        transport.close()
async def test_socks5_connect_success_usr_pwd(loop):
    pld = b'\x05\x02\x01\x00\x05\x00\x00\x01\x01\x01\x01\x01\x04Wtest'

    async with FakeSocksSrv(loop, pld) as srv:
        addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port)
        auth = aiosocksy.Socks5Auth('usr', 'pwd')
        dst = ('python.org', 80)

        transport, protocol = await aiosocksy.create_connection(None,
                                                                addr,
                                                                auth,
                                                                dst,
                                                                loop=loop)
        assert protocol.proxy_sockname == ('1.1.1.1', 1111)

        data = await protocol._stream_reader.read(4)
        assert data == b'test'
        transport.close()