diff options
author | Tuan Nguyen <Tnnnguyen@users.noreply.github.com> | 2017-07-31 07:50:48 -0700 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2017-07-31 10:50:48 -0400 |
commit | 2717fdae5eddfdec0171d49cffa6b6ba6523b5b8 (patch) | |
tree | 3436ab7f63e08a87fd7061a0e37ad10c29d350b3 | |
parent | 6c8a2c1aaaa5aab1001a9c9cc0f2b29e790f2022 (diff) | |
download | sdl_android-2717fdae5eddfdec0171d49cffa6b6ba6523b5b8.tar.gz |
Bugfix/issue 463 (#572)
* #463, fix a potential NPE, move packet.length inside null check.
* #463 Do not access bundle if bytes array is null
* #463 Add unit test
* #463 User reflection in unit test to minimize source code change
* #463 Remove source code changes and only use reflection for unit test
* #463 Check Looper’s availability and init if needed
* #463 Fix failing unit tests, get inputStream locally
* Revert "#463 Fix failing unit tests, get inputStream locally"
This reverts commit 75edaa32903e048c265d1c3aa2bf14b98cca0244.
* #463 Remove unrelated test case
* Revert "#463 Remove unrelated test case"
This reverts commit 86ee5fdb517905114598c02807103514bf3b2e54.
3 files changed, 56 insertions, 11 deletions
diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/transport/SdlRouterServiceTests.java b/sdl_android/src/androidTest/java/com/smartdevicelink/transport/SdlRouterServiceTests.java index 36274b99d..7b038882e 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/transport/SdlRouterServiceTests.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/transport/SdlRouterServiceTests.java @@ -13,6 +13,8 @@ import com.smartdevicelink.protocol.SdlPacket; import junit.framework.Assert; import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.lang.reflect.Method; public class SdlRouterServiceTests extends AndroidTestCase { @@ -84,4 +86,47 @@ public class SdlRouterServiceTests extends AndroidTestCase { Assert.fail("Exception in testAlTransportHandlerHandleNullBundle, " + e); } } -}
\ No newline at end of file + + /** + * Test writeBytesToTransport method for handling null byte array in bundle + * + * @see SdlRouterService#writeBytesToTransport(Bundle) + */ + public void testWriteBytesToTransport() { + if (Looper.myLooper() == null) { + Looper.prepare(); + } + Method method; + Field field = null; + Object sdlRouterService = null; + try { + sdlRouterService = Class.forName("com.smartdevicelink.transport.SdlRouterService").newInstance(); + //Send a null bundle + method = SdlRouterService.class.getDeclaredMethod("writeBytesToTransport", Bundle.class); + Bundle bundle = null; + method.invoke(sdlRouterService, bundle); + + //Send a non-null bundle with a null bytes array + //First, set mSerialService to the correct state so we get to test packet being null + MultiplexBluetoothTransport transport = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(null); + transport.setStateManually(MultiplexBluetoothTransport.STATE_CONNECTED); + field = SdlRouterService.class.getDeclaredField("mSerialService"); + field.setAccessible(true); + field.set(sdlRouterService, transport); + bundle = new Bundle(); + bundle.putByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME, null); + method.invoke(sdlRouterService, bundle); + } catch (Exception e) { + Assert.fail("Exception in testWriteBytesToTransport, " + e); + } + + //Return mSerialService to previous state + if (field != null && sdlRouterService != null) { + try { + field.set(sdlRouterService, null); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } +} diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index 4d3dabfa5..897562600 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -98,7 +98,7 @@ public class SdlRouterService extends Service{ */ @Deprecated public static final String START_SERVICE_ACTION = "sdl.router.startservice"; - public static final String REGISTER_WITH_ROUTER_ACTION = "com.sdl.android.register"; + public static final String REGISTER_WITH_ROUTER_ACTION = "com.sdl.android.register"; /** Message types sent from the BluetoothReadService Handler */ public static final int MESSAGE_STATE_CHANGE = 1; @@ -171,7 +171,7 @@ public class SdlRouterService extends Service{ { //Let's grab where to reply to this intent at. We will keep it temp right now because we may have to deny registration String action =intent.getStringExtra(SEND_PACKET_TO_APP_LOCATION_EXTRA_NAME); - sendBroadcast(prepareRegistrationIntent(action)); + sendBroadcast(prepareRegistrationIntent(action)); } }; @@ -220,7 +220,7 @@ public class SdlRouterService extends Service{ BroadcastReceiver registerAnInstanceOfSerialServer = new BroadcastReceiver() { final Object COMPARE_LOCK = new Object(); @Override - public void onReceive(Context context, Intent intent) + public void onReceive(Context context, Intent intent) { LocalRouterService tempService = intent.getParcelableExtra(SdlBroadcastReceiver.LOCAL_ROUTER_SERVICE_EXTRA); synchronized(COMPARE_LOCK){ @@ -307,7 +307,7 @@ public class SdlRouterService extends Service{ *********************************************** Handlers for bound clients ************************************************************** ****************************************************************************************************************************************/ - + /** * Target we publish for clients to send messages to RouterHandler. */ @@ -322,7 +322,7 @@ public class SdlRouterService extends Service{ public RouterHandler(SdlRouterService provider){ this.provider = new WeakReference<SdlRouterService>(provider); } - + @Override public void handleMessage(Message msg) { if(this.provider.get() == null){ @@ -1293,10 +1293,10 @@ public class SdlRouterService extends Service{ return false; } if(mSerialService !=null && mSerialService.getState()==MultiplexBluetoothTransport.STATE_CONNECTED){ - byte[] packet = bundle.getByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME); - int offset = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_OFFSET, 0); //If nothing, start at the begining of the array - int count = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_COUNT, packet.length); //In case there isn't anything just send the whole packet. + byte[] packet = bundle.getByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME); if(packet!=null){ + int offset = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_OFFSET, 0); //If nothing, start at the begining of the array + int count = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_COUNT, packet.length); //In case there isn't anything just send the whole packet. mSerialService.write(packet,offset,count); return true; } diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java b/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java index 29cdea035..6203aa3e7 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/TransportConstants.java @@ -51,9 +51,9 @@ public class TransportConstants { public static final String BIND_REQUEST_TYPE_ALT_TRANSPORT = "BIND_REQUEST_TYPE_ALT_TRANSPORT"; public static final String BIND_REQUEST_TYPE_STATUS = "BIND_REQUEST_TYPE_STATUS"; - + public static final String PING_ROUTER_SERVICE_EXTRA = "ping.router.service"; - + /** * This class houses all important router service versions */ |