From c60d62d85832e137b6df07c63a76ff349e296eb4 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 23 Oct 2020 09:32:13 -0400 Subject: [PATCH] Direct test for the new implementation --- src/allmydata/test/test_iputil.py | 40 +++++++++++++++++++++++++++++++ src/allmydata/util/iputil.py | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/allmydata/test/test_iputil.py b/src/allmydata/test/test_iputil.py index 6f6100410..54f414bfe 100644 --- a/src/allmydata/test/test_iputil.py +++ b/src/allmydata/test/test_iputil.py @@ -16,6 +16,13 @@ if PY2: import re, os, socket import gc +from testtools.matchers import ( + MatchesAll, + IsInstance, + AllMatch, + MatchesPredicate, +) + from twisted.trial import unittest from tenacity import retry, stop_after_attempt @@ -24,6 +31,13 @@ from foolscap.api import Tub from allmydata.util import iputil, gcutil +from ..util.iputil import ( + get_local_addresses_sync, +) + +from .common import ( + SyncTestCase, +) class ListenOnUsed(unittest.TestCase): """Tests for listenOnUnused.""" @@ -96,3 +110,29 @@ class GcUtil(unittest.TestCase): self.assertEqual(len(collections), 0) tracker.allocate() self.assertEqual(len(collections), 1) + + +class GetLocalAddressesSyncTests(SyncTestCase): + """ + Tests for ``get_local_addresses_sync``. + """ + def test_some_ipv4_addresses(self): + """ + ``get_local_addresses_sync`` returns a list of IPv4 addresses as native + strings. + """ + self.assertThat( + get_local_addresses_sync(), + MatchesAll( + IsInstance(list), + AllMatch( + MatchesAll( + IsInstance(native_str), + MatchesPredicate( + lambda addr: socket.inet_pton(socket.AF_INET, addr), + "%r is not an IPv4 address.", + ), + ), + ), + ), + ) diff --git a/src/allmydata/util/iputil.py b/src/allmydata/util/iputil.py index cbb4922e4..fd3e88c7f 100644 --- a/src/allmydata/util/iputil.py +++ b/src/allmydata/util/iputil.py @@ -114,7 +114,7 @@ def get_local_addresses_sync(): for iface_name in interfaces() for address - in ifaddresses(iface_name).get(socket.AF_INET) + in ifaddresses(iface_name).get(socket.AF_INET, []) )