From fbcbd8edd030f83767a9b83ee25cd7a142d6171e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 24 Sep 2020 18:21:11 +0200 Subject: add IPv6 address test --- README.rst | 9 +++++---- psutil/tests/test_linux.py | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 5c1ef288..31119c31 100644 --- a/README.rst +++ b/README.rst @@ -131,11 +131,12 @@ Security To report a security vulnerability, please use the `Tidelift security contact`_. Tidelift will coordinate the fix and disclosure. -Donate -====== +Sponsorship +=========== A lot of time and effort went into making psutil as it is today. If you whish -to help its future development consider making me a `donation `__. +to help its future development consider +`sponsoring `__ it. Projects using psutil ===================== @@ -146,7 +147,7 @@ psutil has roughly the following monthly downloads: :target: https://pepy.tech/project/psutil :alt: Downloads -...and has over 45,000 projects on GitHub depending from it. +...and has over 55,000 projects on GitHub depending from it. Here's some I find particularly interesting: - https://github.com/google/grr diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 09461ec5..65e55449 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -75,6 +75,23 @@ def get_ipv4_address(ifname): struct.pack('256s', ifname))[20:24]) +def get_ipv6_address(ifname): + with open("/proc/net/if_inet6", 'rt') as f: + for line in f.readlines(): + fields = line.split() + if fields[-1] == ifname: + break + else: + raise ValueError("could not find interface %r" % ifname) + unformatted = fields[0] + groups = [] + for i in range(0, len(unformatted), 4): + groups.append(unformatted[i:i + 4]) + formatted = ":".join(groups) + packed = socket.inet_pton(socket.AF_INET6, formatted) + return socket.inet_ntop(socket.AF_INET6, packed) + + def get_mac_address(ifname): import fcntl ifname = ifname[:15] @@ -890,7 +907,15 @@ class TestSystemNetIfAddrs(PsutilTestCase): self.assertEqual(addr.address, get_mac_address(name)) elif addr.family == socket.AF_INET: self.assertEqual(addr.address, get_ipv4_address(name)) - # TODO: test for AF_INET6 family + elif addr.family == socket.AF_INET6: + # IPv6 addresses can have a percent symbol at the end. + # E.g. these 2 are equivalent: + # "fe80::1ff:fe23:4567:890a" + # "fe80::1ff:fe23:4567:890a%eth0" + # That is the "zone id" portion, which usually is the name + # of the network interface. + address = addr.address.split('%')[0] + self.assertEqual(address, get_ipv6_address(name)) # XXX - not reliable when having virtual NICs installed by Docker. # @unittest.skipIf(not which('ip'), "'ip' utility not available") -- cgit v1.2.1