示例#1
0
    def test_readv(self):
        """Test readv."""
        global do_sleeps

        def testit(family, address, block_sends, block_receives,
                   expected_results):
            s = coro.make_socket(family, socket.SOCK_STREAM)
            server.block_sends = block_sends
            coro.with_timeout(5, s.connect, (address, server.port))
            blocks = coro.with_timeout(5, s.readv, block_receives)
            self.assertEqual(len(blocks), len(expected_results))
            for block, expected_block in zip(blocks, expected_results):
                self.assertEqual(block, expected_block)

        to_test = [(socket.AF_INET, '127.0.0.1')]
        if coro.has_ipv6():
            to_test.append((socket.AF_INET6, '::1'))
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')
        for family, address in to_test:
            server = TestServer()
            server_thread = coro.spawn(server.serve, family, address)
            # Give the server a chance to start.
            coro.yield_slice()

            # Different levels of greediness.
            for greediness in (1024, 1):
                coro.current().set_max_selfish_acts(greediness)
                # Do it once without sleeps, once with sleeps.
                for sleep in (False, True):
                    do_sleeps = sleep
                    testit(family, address, (5, 19, 3, 8), (5, 19, 3, 8),
                           ('01234', '0123456789012345678', '012', '01234567'))
                    testit(family, address, (5, 19, 3, 8), (24, 3, 8),
                           ('012340123456789012345678', '012', '01234567'))
                    testit(family, address, (5, 19, 3, 8), (2, 3, 19, 3, 8),
                           ('01', '234', '0123456789012345678', '012',
                            '01234567'))
                    testit(family, address, (5, 5),
                           (1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
                           ('0', '1', '2', '3', '4', '0', '1', '2', '3', '4'))
                    testit(family, address, (1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
                           (5, 5), ('00000', '00000'))
                    testit(family, address, (10, ), (5, ), ('01234', ))
                    testit(family, address, (5, ), (10, ), ('01234', ))
                    testit(family, address, (), (), ())
                    testit(family, address, (), (5, 2, 8), ())
                    testit(family, address, (5, 9), (5, 10),
                           ('01234', '012345678'))
                    testit(family, address, (5, 9), (5, 5, 3, 7),
                           ('01234', '01234', '567', '8'))
                    testit(family, address, (5, 5), (5, 5, 10),
                           ('01234', '01234'))
                    testit(family, address, (5, ), (6, ), ('01234', ))
                    testit(family, address, (512 * 1024, ), (512 * 1024, ),
                           (big_block[:512 * 1024], ))

            server_thread.raise_exception(coro.Shutdown)
示例#2
0
    def test_writev(self):
        """Test writev."""
        global send_buffer_size, recv_buffer_size

        big_block = '0123456789' * 1024 * 100

        def testit(family, address, block_sends, expected_buffer_result,
                   expected_return):
            global finished
            finished = coro.condition_variable()
            s = coro.make_socket(family, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_buffer_size)
            coro.with_timeout(5, s.connect, (address, server.port))
            blocks = [big_block[:size] for size in block_sends]
            rc = coro.with_timeout(5, s.writev, blocks)
            s.close()
            if finished is not None:
                coro.with_timeout(5, finished.wait)
            self.assertEqual(expected_buffer_result, current_buffer)
            self.assertEqual(expected_return, rc)

        # Setting the send/recv buffer size to 1 causes writev to indicate it
        # was only able to send 1 byte before blocking.  This allows us to test
        # the "partial" buffer sent code path.
        to_test = [(socket.AF_INET, '127.0.0.1')]
        if coro.has_ipv6():
            to_test.append((socket.AF_INET6, '::1'))
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')
        for family, address in to_test:
            for bufsize in (32768, 1):
                send_buffer_size = bufsize
                recv_buffer_size = bufsize

                server = TestServer()
                server_thread = coro.spawn(server.serve, family, address)
                # Give the server a chance to start.
                coro.yield_slice()

                for greediness in (1024, 1):
                    coro.current().set_max_selfish_acts(greediness)
                    testit(family, address, (), '', 0)
                    testit(family, address, (5, 3, 7, 8),
                           '01234012012345601234567', 23)
                    # bufsize==1 is too slow and not necessary
                    if bufsize != 1:
                        testit(family, address, (512 * 1024, ),
                               big_block[:512 * 1024], 512 * 1024)

                server_thread.raise_exception(coro.Shutdown)
示例#3
0
    def test_make_socket_for_ip(self):
        if coro.has_ipv6():
            sock = aplib.make_socket_for_ip('2001::1', socket.SOCK_STREAM)
            self.assertEquals(sock.domain, socket.AF_INET6)
            sock = aplib.make_socket_for_ip('::', socket.SOCK_STREAM)
            self.assertEquals(sock.domain, socket.AF_INET6)
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')

        sock = aplib.make_socket_for_ip('1.2.3.4', socket.SOCK_STREAM)
        self.assertEquals(sock.domain, socket.AF_INET)
        sock = aplib.make_socket_for_ip('0.0.0.0', socket.SOCK_STREAM)
        self.assertEquals(sock.domain, socket.AF_INET)

        self.assertRaises(ValueError, aplib.make_socket_for_ip, '123', 0)
示例#4
0
    def test_make_socket_for_ip(self):
        if coro.has_ipv6():
            sock = coro.make_socket_for_ip('2001::1', socket.SOCK_STREAM)
            self.assertEquals(sock.domain, socket.AF_INET6)
            sock = coro.make_socket_for_ip('::', socket.SOCK_STREAM)
            self.assertEquals(sock.domain, socket.AF_INET6)
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')

        sock = coro.make_socket_for_ip('1.2.3.4', socket.SOCK_STREAM)
        self.assertEquals(sock.domain, socket.AF_INET)
        sock = coro.make_socket_for_ip('0.0.0.0', socket.SOCK_STREAM)
        self.assertEquals(sock.domain, socket.AF_INET)

        self.assertRaises(ValueError, coro.make_socket_for_ip, '123', 0)
示例#5
0
    def test_writev(self):
        """Test writev."""
        global send_buffer_size, recv_buffer_size

        big_block = '0123456789' * 1024 * 100

        def testit(family, address, block_sends, expected_buffer_result, expected_return):
            global finished
            finished = coro.condition_variable()
            s = coro.make_socket(family, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_buffer_size)
            coro.with_timeout(5, s.connect, (address, server.port))
            blocks = [ big_block[:size] for size in block_sends ]
            rc = coro.with_timeout(5, s.writev, blocks)
            s.close()
            if finished is not None:
                coro.with_timeout(5, finished.wait)
            self.assertEqual(expected_buffer_result, current_buffer)
            self.assertEqual(expected_return, rc)

        # Setting the send/recv buffer size to 1 causes writev to indicate it
        # was only able to send 1 byte before blocking.  This allows us to test
        # the "partial" buffer sent code path.
        to_test = [(socket.AF_INET, '127.0.0.1')]
        if coro.has_ipv6():
            to_test.append((socket.AF_INET6, '::1'))
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')
        for family, address in to_test:
            for bufsize in (32768, 1):
                send_buffer_size = bufsize
                recv_buffer_size = bufsize

                server = TestServer()
                server_thread = coro.spawn(server.serve, family, address)
                # Give the server a chance to start.
                coro.yield_slice()

                for greediness in (1024, 1):
                    coro.current().set_max_selfish_acts(greediness)
                    testit(family, address, (), '', 0)
                    testit(family, address, (5, 3, 7, 8), '01234012012345601234567', 23)
                    # bufsize==1 is too slow and not necessary
                    if bufsize != 1:
                        testit(family, address, (512 * 1024,), big_block[:512*1024], 512*1024)

                server_thread.raise_exception(coro.Shutdown)
示例#6
0
    def test_readv(self):
        """Test readv."""
        global do_sleeps

        def testit(family, address, block_sends, block_receives, expected_results):
            s = coro.make_socket(family, socket.SOCK_STREAM)
            server.block_sends = block_sends
            coro.with_timeout(5, s.connect, (address, server.port))
            blocks = coro.with_timeout(5, s.readv, block_receives)
            self.assertEqual(len(blocks), len(expected_results))
            for block, expected_block in zip(blocks, expected_results):
                self.assertEqual(block, expected_block)

        to_test = [(socket.AF_INET, '127.0.0.1')]
        if coro.has_ipv6():
            to_test.append((socket.AF_INET6, '::1'))
        else:
            sys.stderr.write('Warning: No IPv6 support; skipping tests\n')
        for family, address in to_test:
            server = TestServer()
            server_thread = coro.spawn(server.serve, family, address)
            # Give the server a chance to start.
            coro.yield_slice()

            # Different levels of greediness.
            for greediness in (1024, 1):
                coro.current().set_max_selfish_acts(greediness)
                # Do it once without sleeps, once with sleeps.
                for sleep in (False, True):
                    do_sleeps = sleep
                    testit(family, address, (5, 19, 3, 8), (5, 19, 3, 8), ('01234', '0123456789012345678', '012', '01234567'))
                    testit(family, address, (5, 19, 3, 8), (24, 3, 8), ('012340123456789012345678', '012', '01234567'))
                    testit(family, address, (5, 19, 3, 8), (2, 3, 19, 3, 8), ('01', '234', '0123456789012345678', '012', '01234567'))
                    testit(family, address, (5, 5), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ('0', '1', '2', '3', '4', '0', '1', '2', '3', '4'))
                    testit(family, address, (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (5, 5), ('00000', '00000'))
                    testit(family, address, (10,), (5,), ('01234',))
                    testit(family, address, (5,), (10,), ('01234',))
                    testit(family, address, (), (), ())
                    testit(family, address, (), (5, 2, 8), ())
                    testit(family, address, (5, 9), (5, 10), ('01234', '012345678'))
                    testit(family, address, (5, 9), (5, 5, 3, 7), ('01234', '01234', '567', '8'))
                    testit(family, address, (5, 5), (5, 5, 10), ('01234', '01234'))
                    testit(family, address, (5,), (6,), ('01234',))
                    testit(family, address, (512*1024,), (512*1024,), (big_block[:512*1024],))

            server_thread.raise_exception(coro.Shutdown)
示例#7
0
文件: cache.py 项目: cniclsh/shrapnel
import coro
import coro.dns
import coro.dns.packet as packet
from coro.dns.surf import dns_random, set_seed
import random
from coro import tb
import coro
import socket
from coro.dns.exceptions import *
from coro.lru import lru_with_pin
from coro.dns.reply import unpack_reply        

import os
set_seed (os.urandom (128))

if not coro.has_ipv6():
    root_hints = [ x for x in root_hints if not ':' in x ]

class resolver:
    def __init__ (self):
        self.cache = dns_cache()

    def gethostbyname (self, name, qtype):
        for ttl, addr in permute (self.cache.query (name, qtype)):
            return addr

    def resolve_ipv4 (self, name):
        return self.gethostbyname (name, 'A')

    def resolve_ipv6 (self, name):
        return self.gethostbyname (name, 'AAAA')
示例#8
0
 def test_v6(self):
     if coro.has_ipv6():
         self._test('::1', socket.AF_INET6)
     else:
         sys.stderr.write('Warning: No IPv6 support; skipping tests\n')
示例#9
0
import coro
import coro.dns
import coro.dns.packet as packet
from coro.dns.surf import dns_random, set_seed
import random
from coro import tb
import coro
import socket
from coro.dns.exceptions import *
from coro.lru import lru_with_pin
from coro.dns.reply import unpack_reply

import os
set_seed(os.urandom(128))

if not coro.has_ipv6():
    root_hints = [x for x in root_hints if ':' not in x]


class resolver:
    def __init__(self):
        self.cache = dns_cache()

    def gethostbyname(self, name, qtype):
        for ttl, addr in permute(self.cache.query(name, qtype)):
            return addr

    def resolve_ipv4(self, name):
        return self.gethostbyname(name, 'A')

    def resolve_ipv6(self, name):
示例#10
0
 def test_v6(self):
     if coro.has_ipv6():
         self._test('::1')
     else:
         sys.stderr.write('Warning: No IPv6 support; skipping tests\n')