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
Prev Previous commit
Make test throw when requesting a non-valid interface
Signed-off-by: Kai Dederichs <[email protected]>
  • Loading branch information
KDederichs committed May 5, 2022
commit f43e5b5620f26ab8090c36f19976c44cc718e326
12 changes: 6 additions & 6 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ public function getNetworkInterfaces(): array {
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];
Expand Down
97 changes: 50 additions & 47 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,53 +96,56 @@ 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 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());
Expand Down