Skip to content

Commit

Permalink
fix: handle getNetInterfaces error
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Jun 24, 2024
1 parent c827392 commit ce95c08
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ public function getNetworkInfo(): array {
public function getNetworkInterfaces(): array {
$data = [];

foreach ($this->getNetInterfaces() as $interfaceName => $interface) {
try {
$interfaces = $this->getNetInterfaces();
} catch (RuntimeException) {
return $data;
}

foreach ($interfaces as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;

Expand Down
8 changes: 7 additions & 1 deletion lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ public function getNetworkInfo(): array {
public function getNetworkInterfaces(): array {
$data = [];

foreach ($this->getNetInterfaces() as $interfaceName => $interface) {
try {
$interfaces = $this->getNetInterfaces();
} catch (RuntimeException) {
return $data;
}

foreach ($interfaces as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;

Expand Down
10 changes: 10 additions & 0 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public function testGetNetworkInterfaces(): void {
$this->assertEquals($expected, $actual);
}

public function testGetNetworkInterfacesError(): void {
$this->os->method('getNetInterfaces')
->willThrowException(new RuntimeException('Unable to get network interfaces'));

$expected = [];
$actual = $this->os->getNetworkInterfaces();

$this->assertEquals($expected, $actual);
}

public function testSupported(): void {
$this->assertFalse($this->os->supported());
}
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/LinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,14 @@ public function testGetNetworkInterfaces(): void {

$this->assertEquals($expected, $actual);
}

public function testGetNetworkInterfacesError(): void {
$this->os->method('getNetInterfaces')
->willThrowException(new RuntimeException('Unable to get network interfaces'));

$expected = [];
$actual = $this->os->getNetworkInterfaces();

$this->assertEquals($expected, $actual);
}
}

0 comments on commit ce95c08

Please sign in to comment.