diff options
| author | Giampaolo Rodola <g.rodola@gmail.com> | 2014-09-25 18:50:10 +0200 |
|---|---|---|
| committer | Giampaolo Rodola <g.rodola@gmail.com> | 2014-09-25 18:50:10 +0200 |
| commit | 31e4cb5dba8f1d49b06057df9a367e17bd6ce479 (patch) | |
| tree | 34ff1e6ab9b4397b1cf4437f22eaec5ad9e5092f /test/_linux.py | |
| parent | 36690d62cd21c31a6a6492cc32f25388a073c2b2 (diff) | |
| download | psutil-31e4cb5dba8f1d49b06057df9a367e17bd6ce479.tar.gz | |
add further tests for linux
Diffstat (limited to 'test/_linux.py')
| -rw-r--r-- | test/_linux.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/_linux.py b/test/_linux.py index 77f6da27..752ae7f6 100644 --- a/test/_linux.py +++ b/test/_linux.py @@ -7,8 +7,11 @@ """Linux specific tests. These are implicitly run by test_psutil.py.""" from __future__ import division +import fcntl import os import re +import socket +import struct import sys import time @@ -19,6 +22,29 @@ from test_psutil import (skip_on_not_implemented, sh, get_test_subprocess, import psutil +def get_ipv4_address(ifname): + SIOCGIFADDR = 0x8915 + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + return socket.inet_ntoa( + fcntl.ioctl(s.fileno(), + SIOCGIFADDR, + struct.pack('256s', ifname[:15]))[20:24]) + finally: + s.close() + + +def get_mac_address(ifname): + SIOCGIFHWADDR = 0x8927 + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + info = fcntl.ioctl( + s.fileno(), SIOCGIFHWADDR, struct.pack('256s', ifname[:15])) + return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1] + finally: + s.close() + + class LinuxSpecificTestCase(unittest.TestCase): @unittest.skipIf( @@ -139,6 +165,15 @@ class LinuxSpecificTestCase(unittest.TestCase): else: self.assertNotIn('guest_nice', fields) + def test_net_if_addrs(self): + for name, addrs in psutil.net_if_addrs().items(): + for addr in addrs: + if addr.family == socket.AF_PACKET: + 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 + # --- tests for specific kernel versions @unittest.skipUnless( |
