BluetoothUtils { public static PeerProperties validateReceivedHandshakeMessage( byte[] handshakeMessage, int handshakeMessageLength, BluetoothSocket bluetoothSocketOfSender) { String handshakeMessageAsString = new String(handshakeMessage, StandardCharsets.UTF_8); PeerProperties peerProperties = null; boolean receivedHandshakeMessageValidated = false; if (!handshakeMessageAsString.isEmpty()) { if (handshakeMessageLength == SIMPLE_HANDSHAKE_MESSAGE_AS_BYTE_ARRAY.length) { try { handshakeMessageAsString = handshakeMessageAsString.substring(0, SIMPLE_HANDSHAKE_MESSAGE_AS_STRING.length()); } catch (IndexOutOfBoundsException e) { Log.e(TAG, "validateReceivedHandshakeMessage: " + e.getMessage(), e); } if (handshakeMessageAsString.equals(SIMPLE_HANDSHAKE_MESSAGE_AS_STRING)) { String bluetoothMacAddress = getBluetoothMacAddressFromSocket(bluetoothSocketOfSender); if (isValidBluetoothMacAddress(bluetoothMacAddress)) { receivedHandshakeMessageValidated = true; peerProperties = new PeerProperties(bluetoothMacAddress); } } } else { try { peerProperties = AbstractBluetoothConnectivityAgent.getPropertiesFromIdentityString( handshakeMessageAsString); receivedHandshakeMessageValidated = peerProperties != null && peerProperties.isValid(); } catch (JSONException e) { Log.e(TAG, "validateReceivedHandshakeMessage: Failed to resolve peer properties: " + e.getMessage(), e); } if (receivedHandshakeMessageValidated) { String bluetoothMacAddress = BluetoothUtils.getBluetoothMacAddressFromSocket(bluetoothSocketOfSender); if (bluetoothMacAddress == null || !bluetoothMacAddress.equals(peerProperties.getBluetoothMacAddress())) { Log.e(TAG, "validateReceivedHandshakeMessage: Bluetooth MAC address mismatch: Got \"" + peerProperties.getBluetoothMacAddress() + "\", but was expecting \"" + bluetoothMacAddress + "\""); receivedHandshakeMessageValidated = false; } } } } return receivedHandshakeMessageValidated ? peerProperties : null; }  static boolean isBluetoothMacAddressUnknown(String bluetoothMacAddress); static boolean isValidBluetoothMacAddress(String bluetoothMacAddress); static String getBluetoothMacAddressFromSocket(final BluetoothSocket bluetoothSocket); static PeerProperties validateReceivedHandshakeMessage(
            byte[] handshakeMessage, int handshakeMessageLength, BluetoothSocket bluetoothSocketOfSender); static int getPreviouslyUsedAlternativeChannelOrPort(); static void setNextAlternativeChannelOrPort(int channelOrPort); static BluetoothSocket createBluetoothSocketToServiceRecord(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketToServiceRecordWithNextPort(
            BluetoothDevice bluetoothDevice, UUID serviceRecordUuid, boolean secure); static BluetoothSocket createBluetoothSocket(
            BluetoothSocket originalBluetoothSocket, int channelOrPort, boolean secure); static BluetoothSocket createBluetoothSocketWithNextChannel(
            BluetoothSocket originalBluetoothSocket, boolean secure); static final String BLUETOOTH_ADDRESS_SEPARATOR; static final int BLUETOOTH_ADDRESS_BYTE_COUNT; static final int BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MIN; static final int BLUETOOTH_MAC_ADDRESS_STRING_LENGTH_MAX; static final String SIMPLE_HANDSHAKE_MESSAGE_AS_STRING; static final byte[] SIMPLE_HANDSHAKE_MESSAGE_AS_BYTE_ARRAY; }
@Test public void testValidateReceivedHandshakeMessage_SimpleHandshake() throws Exception { String macAddress = "0A:1B:2C:3D:4E:5F"; when(mMockBluetoothDevice.getAddress()).thenReturn(macAddress); assertThat("The received object is null if handshake message is empty", BluetoothUtils.validateReceivedHandshakeMessage( "".getBytes(), 10, mMockBluetoothSocket), is(nullValue())); assertThat("The received object is null if handshakeMessageLength is wrong", BluetoothUtils.validateReceivedHandshakeMessage( "WrongMessage".getBytes(), 10, mMockBluetoothSocket), is(nullValue())); assertThat("The received object is null if the message is wrong", BluetoothUtils.validateReceivedHandshakeMessage( "WrongMessage".getBytes(), 15, mMockBluetoothSocket), is(nullValue())); assertThat("The proper object is returned if the message is correct", BluetoothUtils.validateReceivedHandshakeMessage( BluetoothUtils.SIMPLE_HANDSHAKE_MESSAGE_AS_BYTE_ARRAY, 15, mMockBluetoothSocket).getBluetoothMacAddress(), is(macAddress)); }