From 22b9f0dff4a453f6a528eeeae7973f1ea9d263b0 Mon Sep 17 00:00:00 2001 From: Kai Dederichs Date: Thu, 5 May 2022 14:45:26 +0200 Subject: Fix FreeBsd Interface parsing in cases that the interface has no speed or mac Signed-off-by: Kai Dederichs --- lib/OperatingSystems/FreeBSD.php | 9 +++++-- tests/data/freebsd_interface_epair0b | 9 +++++++ tests/data/freebsd_interface_lo0 | 7 ++++++ tests/data/freebsd_interface_pflog0 | 2 ++ tests/data/freebsd_interfaces | 18 ++++++++++++++ tests/lib/FreeBSDTest.php | 48 ++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/data/freebsd_interface_epair0b create mode 100644 tests/data/freebsd_interface_lo0 create mode 100644 tests/data/freebsd_interface_pflog0 create mode 100644 tests/data/freebsd_interfaces diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index c2f7c60..a9845fe 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -160,8 +160,13 @@ class FreeBSD implements IOperatingSystem { preg_match("/\b[0-9].*?(?=base)/m", $intface, $speed); preg_match("/(?<=\<).*(?=-)/m", $intface, $duplex); - $iface['mac'] = implode(' ', $mac[0]); - $iface['speed'] = $speed[0]; + if (isset($mac[0])) { + $iface['mac'] = implode(' ', $mac[0]); + } + + if (isset($speed[0])) { + $iface['speed'] = $speed[0]; + } if (isset($status[0])) { $iface['status'] = $status[0]; diff --git a/tests/data/freebsd_interface_epair0b b/tests/data/freebsd_interface_epair0b new file mode 100644 index 0000000..f445314 --- /dev/null +++ b/tests/data/freebsd_interface_epair0b @@ -0,0 +1,9 @@ +epair0b: flags=8843 metric 0 mtu 1500 + options=8 + ether 1a:c0:4d:ba:b5:82 + hwaddr 02:60:e8:04:f6:0b + inet 192.168.178.150 netmask 0xffffff00 broadcast 192.168.178.255 + groups: epair + media: Ethernet 10Gbase-T (10Gbase-T ) + status: active + nd6 options=1 \ No newline at end of file diff --git a/tests/data/freebsd_interface_lo0 b/tests/data/freebsd_interface_lo0 new file mode 100644 index 0000000..b377291 --- /dev/null +++ b/tests/data/freebsd_interface_lo0 @@ -0,0 +1,7 @@ +lo0: flags=8049 metric 0 mtu 16384 + options=680003 + inet6 ::1 prefixlen 128 + inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 + inet 127.0.0.1 netmask 0xff000000 + groups: lo + nd6 options=21 diff --git a/tests/data/freebsd_interface_pflog0 b/tests/data/freebsd_interface_pflog0 new file mode 100644 index 0000000..509a049 --- /dev/null +++ b/tests/data/freebsd_interface_pflog0 @@ -0,0 +1,2 @@ +pflog0: flags=0<> metric 0 mtu 33160 + groups: pflog diff --git a/tests/data/freebsd_interfaces b/tests/data/freebsd_interfaces new file mode 100644 index 0000000..f42787b --- /dev/null +++ b/tests/data/freebsd_interfaces @@ -0,0 +1,18 @@ +lo0: flags=8049 metric 0 mtu 16384 + options=680003 + inet6 ::1 prefixlen 128 + inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 + inet 127.0.0.1 netmask 0xff000000 + groups: lo + nd6 options=21 +pflog0: flags=0<> metric 0 mtu 33160 + groups: pflog +epair0b: flags=8843 metric 0 mtu 1500 + options=8 + ether 1a:c0:4d:ba:b5:82 + hwaddr 02:60:e8:04:f6:0b + inet 192.168.178.150 netmask 0xffffff00 broadcast 192.168.178.255 + groups: epair + media: Ethernet 10Gbase-T (10Gbase-T ) + status: active + nd6 options=1 diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index d986972..e2b43f1 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -96,6 +96,54 @@ class FreeBSDTest extends TestCase { $this->assertEquals(new Memory(), $this->os->getMemory()); } + public function testGetNetworkInterfacesNoDuplex(): void { + $this->os->method('executeCommand') + ->willReturnCallback(static function ($command) { + if ($command === '/sbin/ifconfig -a') { + return file_get_contents(__DIR__ . '/../data/freebsd_interfaces'); + } + if ($command === '/sbin/ifconfig lo0') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_lo0'); + } + if ($command === '/sbin/ifconfig pflog0') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_pflog0'); + } + if ($command === '/sbin/ifconfig epair0b') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_epair0b'); + } + }); + + $interfaces = $this->os->getNetworkInterfaces(); + $this->assertEquals([ + [ + "interface" => "lo0", + "ipv4" => "127.0.0.1", + "ipv6" => "::1 fe80::1", + "status" => "active", + "speed" => "unknown", + "duplex" => "", + ], + [ + "interface" => "pflog0", + "ipv4" => "", + "ipv6" => "", + "mac" => "", + "status" => "active", + "speed" => "unknown", + "duplex" => "", + ], + [ + "interface" => "epair0b", + "ipv4" => "192.168.178.150", + "ipv6" => "", + "mac" => "1a:c0:4d:ba:b5:82", + "speed" => "10 Gbps", + "status" => "active", + "duplex" => "Duplex: full", + ] + ], $interfaces); + } + public function testSupported(): void { $this->assertFalse($this->os->supported()); } -- cgit v1.2.3 From ffbadc6c0a9bc1e27c0a70580a684a41d63b3646 Mon Sep 17 00:00:00 2001 From: Kai Dederichs Date: Thu, 5 May 2022 18:13:19 +0200 Subject: Make test throw when requesting a non-valid interface Signed-off-by: Kai Dederichs --- lib/OperatingSystems/FreeBSD.php | 12 ++--- tests/lib/FreeBSDTest.php | 97 +++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index a9845fe..b51f0f2 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -160,13 +160,13 @@ class FreeBSD implements IOperatingSystem { preg_match("/\b[0-9].*?(?=base)/m", $intface, $speed); preg_match("/(?<=\<).*(?=-)/m", $intface, $duplex); - if (isset($mac[0])) { - $iface['mac'] = implode(' ', $mac[0]); - } + if (isset($mac[0])) { + $iface['mac'] = implode(' ', $mac[0]); + } - if (isset($speed[0])) { - $iface['speed'] = $speed[0]; - } + if (isset($speed[0])) { + $iface['speed'] = $speed[0]; + } if (isset($status[0])) { $iface['status'] = $status[0]; diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index e2b43f1..2f5c6e0 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -96,53 +96,56 @@ class FreeBSDTest extends TestCase { $this->assertEquals(new Memory(), $this->os->getMemory()); } - public function testGetNetworkInterfacesNoDuplex(): void { - $this->os->method('executeCommand') - ->willReturnCallback(static function ($command) { - if ($command === '/sbin/ifconfig -a') { - return file_get_contents(__DIR__ . '/../data/freebsd_interfaces'); - } - if ($command === '/sbin/ifconfig lo0') { - return file_get_contents(__DIR__ . '/../data/freebsd_interface_lo0'); - } - if ($command === '/sbin/ifconfig pflog0') { - return file_get_contents(__DIR__ . '/../data/freebsd_interface_pflog0'); - } - if ($command === '/sbin/ifconfig epair0b') { - return file_get_contents(__DIR__ . '/../data/freebsd_interface_epair0b'); - } - }); - - $interfaces = $this->os->getNetworkInterfaces(); - $this->assertEquals([ - [ - "interface" => "lo0", - "ipv4" => "127.0.0.1", - "ipv6" => "::1 fe80::1", - "status" => "active", - "speed" => "unknown", - "duplex" => "", - ], - [ - "interface" => "pflog0", - "ipv4" => "", - "ipv6" => "", - "mac" => "", - "status" => "active", - "speed" => "unknown", - "duplex" => "", - ], - [ - "interface" => "epair0b", - "ipv4" => "192.168.178.150", - "ipv6" => "", - "mac" => "1a:c0:4d:ba:b5:82", - "speed" => "10 Gbps", - "status" => "active", - "duplex" => "Duplex: full", - ] - ], $interfaces); - } + public function testGetNetworkInterfaces(): void { + $this->os->method('executeCommand') + ->willReturnCallback(static function ($command) { + if ($command === '/sbin/ifconfig -a') { + return file_get_contents(__DIR__ . '/../data/freebsd_interfaces'); + } + if ($command === '/sbin/ifconfig lo0') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_lo0'); + } + if ($command === '/sbin/ifconfig pflog0') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_pflog0'); + } + if ($command === '/sbin/ifconfig epair0b') { + return file_get_contents(__DIR__ . '/../data/freebsd_interface_epair0b'); + } + + // Regex matches way more than the interface names, so if it doesn't match any of the defined ones, throw. + throw new \RuntimeException(); + }); + + $interfaces = $this->os->getNetworkInterfaces(); + $this->assertEquals([ + [ + "interface" => "lo0", + "ipv4" => "127.0.0.1", + "ipv6" => "::1 fe80::1", + "status" => "active", + "speed" => "unknown", + "duplex" => "", + ], + [ + "interface" => "pflog0", + "ipv4" => "", + "ipv6" => "", + "mac" => "", + "status" => "active", + "speed" => "unknown", + "duplex" => "", + ], + [ + "interface" => "epair0b", + "ipv4" => "192.168.178.150", + "ipv6" => "", + "mac" => "1a:c0:4d:ba:b5:82", + "speed" => "10 Gbps", + "status" => "active", + "duplex" => "Duplex: full", + ] + ], $interfaces); + } public function testSupported(): void { $this->assertFalse($this->os->supported()); -- cgit v1.2.3