Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix FreeBsd Interface parsing in cases that the interface has no spee…
…d or mac

Signed-off-by: Kai Dederichs <[email protected]>
  • Loading branch information
KDederichs committed May 5, 2022
commit 7b7cb97604457ca184ac9094477e082c2890277a
9 changes: 7 additions & 2 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ public function getNetworkInterfaces(): array {
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];
Expand Down
9 changes: 9 additions & 0 deletions tests/data/freebsd_interface_epair0b
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
epair0b: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=8<VLAN_MTU>
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 <full-duplex>)
status: active
nd6 options=1<PERFORMNUD>
7 changes: 7 additions & 0 deletions tests/data/freebsd_interface_lo0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 127.0.0.1 netmask 0xff000000
groups: lo
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
2 changes: 2 additions & 0 deletions tests/data/freebsd_interface_pflog0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pflog0: flags=0<> metric 0 mtu 33160
groups: pflog
18 changes: 18 additions & 0 deletions tests/data/freebsd_interfaces
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 127.0.0.1 netmask 0xff000000
groups: lo
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
pflog0: flags=0<> metric 0 mtu 33160
groups: pflog
epair0b: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=8<VLAN_MTU>
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 <full-duplex>)
status: active
nd6 options=1<PERFORMNUD>
48 changes: 48 additions & 0 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,54 @@ public function testGetMemoryNoData(): void {
$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());
}
Expand Down