From 84889c9993996f3cc29e4e66af822b346bc590d4 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 24 Jun 2024 18:05:45 +0200 Subject: [PATCH] fix: handle getNetInterfaces error Signed-off-by: Daniel Kesselberg --- lib/OperatingSystems/FreeBSD.php | 8 +++++++- lib/OperatingSystems/Linux.php | 8 +++++++- tests/lib/FreeBSDTest.php | 10 ++++++++++ tests/lib/LinuxTest.php | 10 ++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 8eae4531..23428ff9 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -138,7 +138,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; diff --git a/lib/OperatingSystems/Linux.php b/lib/OperatingSystems/Linux.php index 96d59cc5..740ad9ae 100644 --- a/lib/OperatingSystems/Linux.php +++ b/lib/OperatingSystems/Linux.php @@ -160,7 +160,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; diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index f64be5ea..a8ab6ef5 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -151,6 +151,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()); } diff --git a/tests/lib/LinuxTest.php b/tests/lib/LinuxTest.php index 7d979c0a..27a72c6c 100644 --- a/tests/lib/LinuxTest.php +++ b/tests/lib/LinuxTest.php @@ -262,4 +262,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); + } }