Skip to content

Commit

Permalink
[test] Skip source address tests if the address cannot be bound to (y…
Browse files Browse the repository at this point in the history
…t-dlp#8900)

Fixes yt-dlp#8890

Authored by: coletdjnz
  • Loading branch information
coletdjnz authored Jan 19, 2024
1 parent 50e06e2 commit 69d3191
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import yt_dlp.extractor
from yt_dlp import YoutubeDL
from yt_dlp.compat import compat_os_name
from yt_dlp.utils import preferredencoding, try_call, write_string
from yt_dlp.utils import preferredencoding, try_call, write_string, find_available_port

if 'pytest' in sys.modules:
import pytest
Expand Down Expand Up @@ -329,3 +329,8 @@ def http_server_port(httpd):
else:
sock = httpd.socket
return sock.getsockname()[1]


def verify_address_availability(address):
if find_available_port(address) is None:
pytest.skip(f'Unable to bind to source address {address} (address may not exist)')
5 changes: 4 additions & 1 deletion test/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from email.message import Message
from http.cookiejar import CookieJar

from test.helper import FakeYDL, http_server_port
from test.helper import FakeYDL, http_server_port, verify_address_availability
from yt_dlp.cookies import YoutubeDLCookieJar
from yt_dlp.dependencies import brotli, requests, urllib3
from yt_dlp.networking import (
Expand Down Expand Up @@ -538,6 +538,9 @@ def test_timeout(self, handler):
@pytest.mark.parametrize('handler', ['Urllib', 'Requests'], indirect=True)
def test_source_address(self, handler):
source_address = f'127.0.0.{random.randint(5, 255)}'
# on some systems these loopback addresses we need for testing may not be available
# see: https://github.com/yt-dlp/yt-dlp/issues/8890
verify_address_availability(source_address)
with handler(source_address=source_address) as rh:
data = validate_and_send(
rh, Request(f'http://127.0.0.1:{self.http_port}/source_address')).read().decode()
Expand Down
4 changes: 3 additions & 1 deletion test/test_socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ThreadingTCPServer,
)

from test.helper import http_server_port
from test.helper import http_server_port, verify_address_availability
from yt_dlp.networking import Request
from yt_dlp.networking.exceptions import ProxyError, TransportError
from yt_dlp.socks import (
Expand Down Expand Up @@ -326,6 +326,7 @@ def test_socks4a_domain_target(self, handler, ctx):
def test_ipv4_client_source_address(self, handler, ctx):
with ctx.socks_server(Socks4ProxyHandler) as server_address:
source_address = f'127.0.0.{random.randint(5, 255)}'
verify_address_availability(source_address)
with handler(proxies={'all': f'socks4://{server_address}'},
source_address=source_address) as rh:
response = ctx.socks_info_request(rh)
Expand Down Expand Up @@ -441,6 +442,7 @@ def test_ipv6_socks5_proxy(self, handler, ctx):
def test_ipv4_client_source_address(self, handler, ctx):
with ctx.socks_server(Socks5ProxyHandler) as server_address:
source_address = f'127.0.0.{random.randint(5, 255)}'
verify_address_availability(source_address)
with handler(proxies={'all': f'socks5://{server_address}'}, source_address=source_address) as rh:
response = ctx.socks_info_request(rh)
assert response['client_address'][0] == source_address
Expand Down
3 changes: 3 additions & 0 deletions test/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import pytest

from test.helper import verify_address_availability

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import http.client
Expand Down Expand Up @@ -227,6 +229,7 @@ def test_cookies(self, handler):
@pytest.mark.parametrize('handler', ['Websockets'], indirect=True)
def test_source_address(self, handler):
source_address = f'127.0.0.{random.randint(5, 255)}'
verify_address_availability(source_address)
with handler(source_address=source_address) as rh:
ws = validate_and_send(rh, Request(self.ws_base_url))
ws.send('source_address')
Expand Down

0 comments on commit 69d3191

Please sign in to comment.