| 1 | import socket |
| 2 | import rfc6555 |
| 3 | |
| 4 | try: |
| 5 | from unittest import mock |
| 6 | except (ImportError, AttributeError): |
| 7 | import mock |
| 8 | |
| 9 | |
| 10 | def test_ipv6_available(): |
| 11 | assert rfc6555._detect_ipv6() |
| 12 | |
| 13 | |
| 14 | def test_ipv6_not_available_socket_has_ipv6_false(): |
| 15 | old_has_ipv6 = socket.has_ipv6 |
| 16 | socket.has_ipv6 = False |
| 17 | assert not rfc6555._detect_ipv6() |
| 18 | socket.has_ipv6 = old_has_ipv6 |
| 19 | |
| 20 | |
| 21 | def test_ipv6_not_available_socket_exception_on_init(): |
| 22 | with mock.patch('socket.socket') as fake_socket: |
| 23 | fake_socket.side_effect = OSError |
| 24 | |
| 25 | assert not rfc6555._detect_ipv6() |
| 26 | |
| 27 | |
| 28 | def test_ipv6_not_available_socket_exception_on_bind(): |
| 29 | sock = mock.Mock() |
| 30 | with mock.patch('socket.socket') as fake_socket: |
| 31 | fake_socket.return_value = sock |
| 32 | sock.bind.side_effect = OSError |
| 33 | |
| 34 | assert not rfc6555._detect_ipv6() |
| 35 | |
| 36 | |
| 37 | def test_ipv6_not_available_socket_AF_INET6_not_defined(): |
| 38 | old_AF_INET6 = socket.AF_INET6 |
| 39 | try: |
| 40 | delattr(socket, 'AF_INET6') |
| 41 | assert not rfc6555._detect_ipv6() |
| 42 | finally: |
| 43 | socket.AF_INET6 = old_AF_INET6 |