From 0d46a585031b40d3ea285719a09a4b09538ce1af Mon Sep 17 00:00:00 2001 From: Chris Jackson Date: Wed, 11 Sep 2024 08:01:16 +1200 Subject: [PATCH] Avoid NPE following ASH error (#1435) Signed-off-by: Chris Jackson --- .../ember/internal/ash/AshFrameHandler.java | 21 +++++++++---------- .../internal/transaction/EzspTransaction.java | 2 +- .../internal/ash/AshFrameHandlerTest.java | 19 +++++++++++++++-- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandler.java b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandler.java index ce65d49365..e846bafae9 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandler.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandler.java @@ -869,17 +869,16 @@ public EzspTransaction sendEzspTransaction(EzspTransaction ezspTransaction, long Future futureResponse = sendEzspRequestAsync(ezspTransaction); if (futureResponse == null) { logger.debug("ASH: Error sending EZSP transaction: Future is null"); - return null; - } - - try { - futureResponse.get(timeout, TimeUnit.SECONDS); - } catch (InterruptedException | ExecutionException e) { - futureResponse.cancel(true); - logger.debug("ASH interrupted in sendRequest while sending {}", ezspTransaction.getRequest()); - } catch (TimeoutException e) { - futureResponse.cancel(true); - logger.debug("Sending EZSP transaction timed out after {} seconds", timeout); + } else { + try { + futureResponse.get(timeout, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException e) { + futureResponse.cancel(true); + logger.debug("ASH interrupted in sendRequest while sending {}", ezspTransaction.getRequest()); + } catch (TimeoutException e) { + futureResponse.cancel(true); + logger.debug("Sending EZSP transaction timed out after {} seconds", timeout); + } } return ezspTransaction; diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/transaction/EzspTransaction.java b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/transaction/EzspTransaction.java index 1ec1e81396..122f74e25a 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/transaction/EzspTransaction.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/transaction/EzspTransaction.java @@ -59,7 +59,7 @@ public interface EzspTransaction { * response indicating the final state of the transaction. * * @return {@link EmberStatus} indicating the transaction completion state or - * {@link EmberStatus#EMBED_UNKNOWN_STATUS} on error. + * {@link EmberStatus#UNKNOWN} on error. */ EmberStatus getStatus(); } diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandlerTest.java b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandlerTest.java index f8f76fef01..9b027fb3d3 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandlerTest.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/internal/ash/AshFrameHandlerTest.java @@ -21,7 +21,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; @@ -33,6 +32,7 @@ import com.zsmartsystems.zigbee.dongle.ember.ezsp.EzspFrameRequest; import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspVersionRequest; import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspVersionResponse; +import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EmberStatus; import com.zsmartsystems.zigbee.dongle.ember.internal.EzspFrameHandler; import com.zsmartsystems.zigbee.dongle.ember.internal.transaction.EzspSingleResponseTransaction; import com.zsmartsystems.zigbee.dongle.ember.internal.transaction.EzspTransaction; @@ -170,7 +170,7 @@ public void write(int value) { @Override public void write(int[] bytes) { - for(int val : bytes) { + for (int val : bytes) { outputData.add(val); } } @@ -318,4 +318,19 @@ public void handleReset() throws Exception { Mockito.verify(ezspHandler, Mockito.timeout(TIMEOUT)).handleLinkStateChange(true); } + + @Test + public void sendEzspTransactionNullCheck() { + EzspFrameHandler ezspHandler = Mockito.mock(EzspFrameHandler.class); + AshFrameHandler frameHandler = new AshFrameHandler(ezspHandler); + + frameHandler.setClosing(); + EzspVersionRequest request = new EzspVersionRequest(); + EzspTransaction transaction = frameHandler + .sendEzspTransaction(new EzspSingleResponseTransaction(request, EzspVersionResponse.class)); + + assertNotNull(transaction); + assertEquals(EmberStatus.UNKNOWN, transaction.getStatus()); + assertNull(transaction.getResponse()); + } }