From 77edb104953af9944838a820a87518fa7400aac1 Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 4 Aug 2021 15:40:46 -0400 Subject: [PATCH 01/16] Add SecurityQuery and BinaryQueryHeader --- .../test/protocol/BinaryQueryHeaderTests.java | 118 +++++++++++++++ .../managers/audio/AudioStreamManager.java | 2 +- .../managers/video/VideoStreamManager.java | 2 +- .../lifecycle/BaseLifecycleManager.java | 2 +- .../protocol/BaseSdlPacket.java | 5 + .../protocol/BinaryQueryHeader.java | 139 ++++++++++++++++++ .../protocol/ProtocolMessage.java | 5 + .../protocol/SdlPacketFactory.java | 10 ++ .../protocol/SdlProtocolBase.java | 19 ++- .../protocol/SecurityQuery.java | 102 +++++++++++++ .../protocol/enums/QueryErrorCode.java | 58 ++++++++ .../protocol/enums/QueryID.java | 36 +++++ .../protocol/enums/QueryType.java | 38 +++++ .../session/BaseSdlSession.java | 23 ++- 14 files changed, 546 insertions(+), 13 deletions(-) create mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java new file mode 100644 index 0000000000..33c567c013 --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -0,0 +1,118 @@ +package com.smartdevicelink.test.protocol; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.smartdevicelink.protocol.BinaryQueryHeader; +import com.smartdevicelink.protocol.enums.QueryID; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +@RunWith(AndroidJUnit4.class) +public class BinaryQueryHeaderTests { + + public static final byte QUERY_TYPE_REQUEST = 0x00; + public static final byte QUERY_TYPE_RESPONSE = 0x01; + public static final byte QUERY_TYPE_NOTIFICATION = 0x02; + + public static BinaryQueryHeader createDummyBqh() { + BinaryQueryHeader bqh = new BinaryQueryHeader(); + bqh.setCorrelationID(123); + bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA.getValue()); + bqh.setQueryType(QUERY_TYPE_REQUEST); + bqh.setBulkData(null); + bqh.setJsonSize(0); + return bqh; + } + + public BinaryQueryHeader safeParse(byte[] array) { + try { + return BinaryQueryHeader.parseBinaryQueryHeader(array); + } catch (Exception e) { + return null; + } + } + + @Test + public void testAssemblyAndParse() { + BinaryQueryHeader bqh = createDummyBqh(); + + byte[] bqhBytes = bqh.assembleHeaderBytes(); + assertNotNull(bqhBytes); + + BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); + assertNotNull(parsedBqh); + + assertEquals(bqh.getCorrelationID(), parsedBqh.getCorrelationID()); + assertEquals(bqh.getQueryID(), parsedBqh.getQueryID()); + assertEquals(bqh.getQueryType(), parsedBqh.getQueryType()); + assertEquals(bqh.getBulkData(), parsedBqh.getBulkData()); + assertEquals(bqh.getJsonData(), parsedBqh.getJsonData()); + assertEquals(bqh.getJsonSize(), parsedBqh.getJsonSize()); + } + + @Test + public void testCorruptHeader() { + BinaryQueryHeader bqh = createDummyBqh(); + bqh.setJsonSize(5); + bqh.setJsonData(new byte[5]); + bqh.setJsonSize(Integer.MAX_VALUE); + + assertNotEquals(bqh.getJsonData().length, bqh.getJsonSize()); + + byte[] bqhBytes = bqh.assembleHeaderBytes(); + + assertNull(safeParse(bqhBytes)); + + int size = bqhBytes.length; + for (int i = 0; i < size; i++) { + bqhBytes[i] = (byte) 0x99; + } + + assertNull(safeParse(bqhBytes)); + BinaryQueryHeader head = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); + assertNull(head); + } + + @Test + public void testErrorCodeAssemble() { + BinaryQueryHeader bqh = new BinaryQueryHeader(); + bqh.setCorrelationID(123); + bqh.setQueryID(QueryID.SEND_INTERNAL_ERROR.getValue()); + bqh.setQueryType(QUERY_TYPE_NOTIFICATION); + bqh.setBulkData(null); + bqh.setErrorCode(2); + bqh.setJsonSize(0); + + byte[] bqhBytes = bqh.assembleHeaderBytes(); + assertNotNull(bqhBytes); + + BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); + assertNotNull(parsedBqh); + + assertEquals(bqh.getCorrelationID(), parsedBqh.getCorrelationID()); + assertEquals(bqh.getQueryID(), parsedBqh.getQueryID()); + assertEquals(bqh.getQueryType(), parsedBqh.getQueryType()); + assertEquals(bqh.getBulkData(), parsedBqh.getBulkData()); + assertEquals(bqh.getJsonData(), parsedBqh.getJsonData()); + assertEquals(bqh.getJsonSize(), parsedBqh.getJsonSize()); + assertEquals(bqh.getErrorCode(), parsedBqh.getErrorCode()); + } + + @Test + public void testJsonSetException() { + try { + BinaryQueryHeader bqh = createDummyBqh(); + bqh.setJsonData(null); + fail("Setting JSON data to null should have thrown an exception"); + } catch (Exception e) { + //Pass + } + } +} diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java index 667522372a..c28401b076 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java @@ -530,7 +530,7 @@ protected IAudioStreamListener startAudioStream(final SdlSession session) { IStreamListener streamListener = new IStreamListener() { @Override public void sendStreamPacket(ProtocolMessage pm) { - session.sendMessage(pm); + session.sendMessage(pm, null); } }; diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java index cce131baa2..b3899c539c 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java @@ -1065,7 +1065,7 @@ protected IVideoStreamListener startVideoStream(VideoStreamingParameters params, IStreamListener iStreamListener = new IStreamListener() { @Override public void sendStreamPacket(ProtocolMessage pm) { - session.sendMessage(pm); + session.sendMessage(pm, null); } }; diff --git a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java index 9cf72f7cdf..8a783862b6 100644 --- a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java +++ b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java @@ -866,7 +866,7 @@ private void sendRPCMessagePrivate(RPCMessage message, boolean isInternalMessage pm.setPriorityCoefficient(1); } - session.sendMessage(pm); + session.sendMessage(pm, null); } catch (OutOfMemoryError e) { DebugTool.logError(TAG,"Error attempting to send RPC message.", e); diff --git a/base/src/main/java/com/smartdevicelink/protocol/BaseSdlPacket.java b/base/src/main/java/com/smartdevicelink/protocol/BaseSdlPacket.java index 9694d3f067..fdbba048d7 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/BaseSdlPacket.java +++ b/base/src/main/java/com/smartdevicelink/protocol/BaseSdlPacket.java @@ -264,10 +264,15 @@ public void setPriorityCoefficient(int priority) { this.priorityCoefficient = priority; } + @Deprecated public int getPrioirtyCoefficient() { return this.priorityCoefficient; } + public int getPriorityCoefficient() { + return this.priorityCoefficient; + } + public void setTransportRecord(TransportRecord transportRecord) { this.transportRecord = transportRecord; } diff --git a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java new file mode 100644 index 0000000000..7a118a1426 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java @@ -0,0 +1,139 @@ +package com.smartdevicelink.protocol; + +import androidx.annotation.RestrictTo; + +import com.smartdevicelink.util.BitConverter; +import com.smartdevicelink.util.DebugTool; + +@RestrictTo(RestrictTo.Scope.LIBRARY) +public class BinaryQueryHeader { + private static final String TAG = "BinaryQueryHeader"; + + private byte _queryType; + private int _queryID; + private int _correlationID; + private int _jsonSize; + private int _errorCode; + + private byte[] _jsonData; + private byte[] _bulkData; + + public BinaryQueryHeader() { + } + + public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { + BinaryQueryHeader msg = new BinaryQueryHeader(); + + byte QUERY_Type = (byte) (binHeader[0] >>> 4); + msg.setQueryType(QUERY_Type); + + int _queryID = (BitConverter.intFromByteArray(binHeader, 0) & 0x0FFFFFFF); + msg.setQueryID(_queryID); + + int corrID = BitConverter.intFromByteArray(binHeader, 4); + msg.setCorrelationID(corrID); + + int _jsonSize = BitConverter.intFromByteArray(binHeader, 8); + msg.setJsonSize(_jsonSize); + + if (msg.getQueryType() == 0x20 && msg.getQueryID() == 0x02) { + int _errorCode = BitConverter.intFromByteArray(binHeader, binHeader.length-1); + msg.setErrorCode(_errorCode); + } + + try { + if (_jsonSize > 0) { + byte[] _jsonData = new byte[_jsonSize]; + System.arraycopy(binHeader, 12, _jsonData, 0, _jsonSize); + msg.setJsonData(_jsonData); + } + + if (binHeader.length - _jsonSize - 12 > 0) { + byte[] _bulkData; + if (msg.getQueryType() == 0x20 && msg.getQueryID() == 0x02) { + _bulkData = new byte[binHeader.length - _jsonSize - 12 - 1]; + System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length - 1); + } else { + _bulkData = new byte[binHeader.length - _jsonSize - 12]; + System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length); + } + msg.setBulkData(_bulkData); + } + + } catch (OutOfMemoryError | ArrayIndexOutOfBoundsException e) { + DebugTool.logError(TAG, "Unable to process data to form header"); + return null; + } + + return msg; + } + + public byte[] assembleHeaderBytes() { + int binHeader = _queryID; + binHeader &= 0xFFFFFFFF >>> 4; + binHeader |= (_queryType << 28); + + byte[] ret = new byte[12]; + System.arraycopy(BitConverter.intToByteArray(binHeader), 0, ret, 0, 4); + System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4); + System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4); + return ret; + } + + public byte getQueryType() { + return _queryType; + } + + public void setQueryType(byte _queryType) { + this._queryType = _queryType; + } + + public int getQueryID() { + return _queryID; + } + + public void setQueryID(int _queryID) { + this._queryID = _queryID; + } + + public int getCorrelationID() { + return _correlationID; + } + + public void setCorrelationID(int _correlationID) { + this._correlationID = _correlationID; + } + + public int getJsonSize() { + return _jsonSize; + } + + public void setJsonSize(int _jsonSize) { + this._jsonSize = _jsonSize; + } + + public int getErrorCode() { + return _errorCode; + } + + public void setErrorCode(int _errorCode) { + this._errorCode = _errorCode; + } + + public byte[] getJsonData() { + return _jsonData; + } + + public void setJsonData(byte[] _jsonData) { + this._jsonData = new byte[this._jsonSize]; + System.arraycopy(_jsonData, 0, this._jsonData, 0, _jsonSize); + } + + public byte[] getBulkData() { + return _bulkData; + } + + public void setBulkData(byte[] _bulkData) { + this._bulkData = _bulkData; + } +} diff --git a/base/src/main/java/com/smartdevicelink/protocol/ProtocolMessage.java b/base/src/main/java/com/smartdevicelink/protocol/ProtocolMessage.java index 8a1968f36d..9e2ab38d03 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/ProtocolMessage.java +++ b/base/src/main/java/com/smartdevicelink/protocol/ProtocolMessage.java @@ -182,7 +182,12 @@ public void setPriorityCoefficient(int priority) { this.priorityCoefficient = priority; } + @Deprecated public int getPrioirtyCoefficient() { return this.priorityCoefficient; } + + public int getPriorityCoefficient() { + return this.priorityCoefficient; + } } // end-class \ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java index 364dc4e305..75d122da56 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java @@ -144,4 +144,14 @@ public static BinaryFrameHeader createBinaryFrameHeader(byte rpcType, int functi return msg; } + public static BinaryQueryHeader createBinaryQueryHeader(byte queryType, int queryId, int corrID, int jsonSize) { + BinaryQueryHeader msg = new BinaryQueryHeader(); + msg.setQueryType(queryType); + msg.setQueryID(queryId); + msg.setCorrelationID(corrID); + msg.setJsonSize(jsonSize); + + return msg; + } + } diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java b/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java index 5cac1302cf..b1723dc6a0 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java @@ -562,7 +562,7 @@ public void endSession(byte sessionID) { } } // end-method - public void sendMessage(ProtocolMessage protocolMsg) { + public void sendMessage(ProtocolMessage protocolMsg, SecurityQuery securityQuery) { SessionType sessionType = protocolMsg.getSessionType(); byte sessionID = protocolMsg.getSessionID(); boolean requiresEncryption = protocolMsg.getPayloadProtected(); @@ -571,12 +571,19 @@ public void sendMessage(ProtocolMessage protocolMsg) { if (protocolVersion.getMajor() > 1 && sessionType != SessionType.NAV && sessionType != SessionType.PCM) { if (sessionType.eq(SessionType.CONTROL)) { final byte[] secureData = protocolMsg.getData().clone(); - data = new byte[headerSize + secureData.length]; - final BinaryFrameHeader binFrameHeader = - SdlPacketFactory.createBinaryFrameHeader(protocolMsg.getRPCType(), protocolMsg.getFunctionID(), protocolMsg.getCorrID(), 0); - System.arraycopy(binFrameHeader.assembleHeaderBytes(), 0, data, 0, headerSize); - System.arraycopy(secureData, 0, data, headerSize, secureData.length); + if (securityQuery != null) { + data = new byte[12 + secureData.length]; + final BinaryQueryHeader binQueryHeader = SdlPacketFactory.createBinaryQueryHeader(securityQuery.getQueryType().getValue(), securityQuery.getQueryType().getValue(), securityQuery.getCorrelationId(), securityQuery.getJsonSize()); + System.arraycopy(binQueryHeader.assembleHeaderBytes(), 0, data, 0, 12); + System.arraycopy(secureData, 0, data, 12, secureData.length); + } else { + data = new byte[headerSize + secureData.length]; + final BinaryFrameHeader binFrameHeader = + SdlPacketFactory.createBinaryFrameHeader(protocolMsg.getRPCType(), protocolMsg.getFunctionID(), protocolMsg.getCorrID(), 0); + System.arraycopy(binFrameHeader.assembleHeaderBytes(), 0, data, 0, headerSize); + System.arraycopy(secureData, 0, data, headerSize, secureData.length); + } } else if (protocolMsg.getBulkData() != null) { data = new byte[12 + protocolMsg.getJsonSize() + protocolMsg.getBulkData().length]; sessionType = SessionType.BULK_DATA; diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java new file mode 100644 index 0000000000..e00a211378 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java @@ -0,0 +1,102 @@ +package com.smartdevicelink.protocol; + +import androidx.annotation.RestrictTo; + +import com.smartdevicelink.protocol.enums.QueryErrorCode; +import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.QueryType; + +@RestrictTo(RestrictTo.Scope.LIBRARY) +public class SecurityQuery { + + private QueryType _queryType = QueryType.INVALID_QUERY_TYPE; + private QueryID _queryID = QueryID.INVALID_QUERY_ID; + private int _correlationId; + private int _jsonSize; + private QueryErrorCode _queryErrorCode = QueryErrorCode.ERROR_SUCCESS; + + private byte[] _data = null; + private byte[] _bulkData = null; + + public SecurityQuery() { + } + + public QueryType getQueryType() { + return this._queryType; + } + + public void setQueryType(QueryType queryType) { + this._queryType = queryType; + } + + public QueryID getQueryID() { + return this._queryID; + } + + public void setQueryID(QueryID queryID) { + this._queryID = queryID; + } + + public int getCorrelationId() { + return this._correlationId; + } + + public void setCorrelationId(int msgId) { + this._correlationId = msgId; + } + + public int getJsonSize() { + return this._jsonSize; + } + + public void setJsonSize(int jsonSize) { + this._jsonSize = jsonSize; + } + + public QueryErrorCode getQueryErrorCode() { + return this._queryErrorCode; + } + + public void setQueryErrorCode(QueryErrorCode queryErrorCode) { + this._queryErrorCode = queryErrorCode; + } + + public byte[] getData() { + return _data; + } + + public void setData(byte[] data) { + this._data = data; + this._jsonSize = data.length; + } + + public void setData(byte[] data, int offset, int length) { + if (this._data != null) + this._data = null; + this._data = new byte[length]; + System.arraycopy(data, offset, this._data, 0, length); + this._jsonSize = 0; + } + + public byte[] getBulkData() { + return _bulkData; + } + + public void setBulkDataNoCopy(byte[] bulkData) { + this._bulkData = bulkData; + } + + public void setBulkData(byte[] bulkData) { + if (this._bulkData != null) + this._bulkData = null; + this._bulkData = new byte[bulkData.length]; + System.arraycopy(bulkData, 0, this._bulkData, 0, bulkData.length); + } + + public void setBulkData(byte[] bulkData, int length) { + if (this._bulkData != null) + this._bulkData = null; + this._bulkData = new byte[length]; + System.arraycopy(bulkData, 0, this._bulkData, 0, length); + } +} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java new file mode 100644 index 0000000000..269c5021e1 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java @@ -0,0 +1,58 @@ +package com.smartdevicelink.protocol.enums; + +import com.smartdevicelink.util.ByteEnumer; + +import java.util.Vector; + +public class QueryErrorCode extends ByteEnumer { + + private static final Vector theList = new Vector<>(); + + public static Vector getList() { + return theList; + } + + protected QueryErrorCode(byte value, String name) { + super(value, name); + } + + public final static QueryErrorCode ERROR_SUCCESS = new QueryErrorCode((byte) 0x00, "ERROR_SUCCESS"); + public final static QueryErrorCode ERROR_INVALID_QUERY_SIZE = new QueryErrorCode((byte) 0x01, "ERROR_INVALID_QUERY_SIZE"); + public final static QueryErrorCode ERROR_INVALID_QUERY_ID = new QueryErrorCode((byte) 0x02, "ERROR_INVALID_QUERY_ID"); + public final static QueryErrorCode ERROR_NOT_SUPPORTED = new QueryErrorCode((byte) 0x03, "ERROR_NOT_SUPPORTED"); + public final static QueryErrorCode ERROR_SERVICE_ALREADY_PROTECTED = new QueryErrorCode((byte) 0x04, "ERROR_SERVICE_ALREADY_PROTECTED"); + public final static QueryErrorCode ERROR_SERVICE_NOT_PROTECTED = new QueryErrorCode((byte) 0x05, "ERROR_SERVICE_NOT_PROTECTED"); + public final static QueryErrorCode ERROR_DECRYPTION_FAILED = new QueryErrorCode((byte) 0x06, "ERROR_DECRYPTION_FAILED"); + public final static QueryErrorCode ERROR_ENCRYPTION_FAILED = new QueryErrorCode((byte) 0x07, "ERROR_ENCRYPTION_FAILED"); + public final static QueryErrorCode ERROR_SSL_INVALID_DATA = new QueryErrorCode((byte) 0x08, "ERROR_SSL_INVALID_DATA"); + public final static QueryErrorCode ERROR_HANDSHAKE_FAILED = new QueryErrorCode((byte) 0x09, "ERROR_HANDSHAKE_FAILED"); + public final static QueryErrorCode INVALID_CERT = new QueryErrorCode((byte) 0x0A, "INVALID_CERT"); + public final static QueryErrorCode EXPIRED_CERT = new QueryErrorCode((byte) 0x0B, "EXPIRED_CERT"); + public final static QueryErrorCode ERROR_INTERNAL = new QueryErrorCode((byte) 0xFF, "ERROR_INTERNAL"); + public final static QueryErrorCode ERROR_UNKNOWN_INTERNAL_ERROR = new QueryErrorCode((byte) 0xFE, "ERROR_UNKNOWN_INTERNAL_ERROR"); + + static { + theList.addElement(ERROR_SUCCESS); + theList.addElement(ERROR_INVALID_QUERY_SIZE); + theList.addElement(ERROR_INVALID_QUERY_ID); + theList.addElement(ERROR_NOT_SUPPORTED); + theList.addElement(ERROR_SERVICE_ALREADY_PROTECTED); + theList.addElement(ERROR_SERVICE_NOT_PROTECTED); + theList.addElement(ERROR_DECRYPTION_FAILED); + theList.addElement(ERROR_ENCRYPTION_FAILED); + theList.addElement(ERROR_SSL_INVALID_DATA); + theList.addElement(ERROR_HANDSHAKE_FAILED); + theList.addElement(INVALID_CERT); + theList.addElement(EXPIRED_CERT); + theList.addElement(ERROR_INTERNAL); + theList.addElement(ERROR_UNKNOWN_INTERNAL_ERROR); + } + + public static QueryErrorCode valueOf(byte passedByte) { + return (QueryErrorCode) get(theList, passedByte); + } + + public static QueryErrorCode[] values() { + return theList.toArray(new QueryErrorCode[theList.size()]); + } +} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java new file mode 100644 index 0000000000..3668932d14 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java @@ -0,0 +1,36 @@ +package com.smartdevicelink.protocol.enums; + +import com.smartdevicelink.util.ByteEnumer; + +import java.util.Vector; + +public class QueryID extends ByteEnumer { + + private static final Vector theList = new Vector<>(); + + public static Vector getList() { + return theList; + } + + protected QueryID(byte value, String name) { + super(value, name); + } + + public final static QueryID SEND_HANDSHAKE_DATA = new QueryID((byte) 0x01, "SEND_HANDSHAKE_DATA"); + public final static QueryID SEND_INTERNAL_ERROR = new QueryID((byte) 0x02, "SEND_INTERNAL_ERROR"); + public final static QueryID INVALID_QUERY_ID = new QueryID((byte) 0xFF, "INVALID_QUERY_ID"); + + static { + theList.addElement(SEND_HANDSHAKE_DATA); + theList.addElement(SEND_INTERNAL_ERROR); + theList.addElement(INVALID_QUERY_ID); + } + + public static QueryID valueOf(byte passedByte) { + return (QueryID) get(theList, passedByte); + } + + public static QueryID[] values() { + return theList.toArray(new QueryID[theList.size()]); + } +} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java new file mode 100644 index 0000000000..543fb56c6e --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java @@ -0,0 +1,38 @@ +package com.smartdevicelink.protocol.enums; + +import com.smartdevicelink.util.ByteEnumer; + +import java.util.Vector; + +public class QueryType extends ByteEnumer { + + private static final Vector theList = new Vector<>(); + + public static Vector getList() { + return theList; + } + + protected QueryType(byte value, String name) { + super(value, name); + } + + public final static QueryType REQUEST = new QueryType((byte) 0x00, "REQUEST"); + public final static QueryType RESPONSE = new QueryType((byte) 0x10, "RESPONSE"); + public final static QueryType NOTIFICATION = new QueryType((byte) 0x20, "NOTIFICATION"); + public final static QueryType INVALID_QUERY_TYPE = new QueryType((byte) 0xFF, "INVALID_QUERY_TYPE"); + + static { + theList.addElement(REQUEST); + theList.addElement(RESPONSE); + theList.addElement(NOTIFICATION); + theList.addElement(INVALID_QUERY_TYPE); + } + + public static QueryType valueOf(byte passedByte) { + return (QueryType) get(theList, passedByte); + } + + public static QueryType[] values() { + return theList.toArray(new QueryType[theList.size()]); + } +} \ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 8b35541b42..b154117811 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -41,7 +41,10 @@ import com.smartdevicelink.protocol.ProtocolMessage; import com.smartdevicelink.protocol.SdlPacket; import com.smartdevicelink.protocol.SdlProtocolBase; +import com.smartdevicelink.protocol.SecurityQuery; import com.smartdevicelink.protocol.enums.ControlFrameTags; +import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.RPCMessage; import com.smartdevicelink.proxy.rpc.VehicleType; @@ -147,11 +150,11 @@ public void startSession() throws SdlException { } - public void sendMessage(ProtocolMessage msg) { + public void sendMessage(ProtocolMessage msg, SecurityQuery securityQuery) { if (sdlProtocol == null) { return; } - sdlProtocol.sendMessage(msg); + sdlProtocol.sendMessage(msg, securityQuery); } public TransportType getCurrentTransportType() { @@ -193,13 +196,18 @@ protected void processControlService(ProtocolMessage msg) { byte[] dataToRead = new byte[4096]; - Integer iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + Integer iNumBytes = null; + + if (data[3] == QueryID.SEND_HANDSHAKE_DATA.getValue()) { + iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + } if (iNumBytes == null || iNumBytes <= 0) return; byte[] returnBytes = new byte[iNumBytes]; System.arraycopy(dataToRead, 0, returnBytes, 0, iNumBytes); + ProtocolMessage protocolMessage = new ProtocolMessage(); protocolMessage.setSessionType(SessionType.CONTROL); protocolMessage.setData(returnBytes); @@ -207,9 +215,16 @@ protected void processControlService(ProtocolMessage msg) { protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor()); protocolMessage.setSessionID((byte) this.sessionId); + SecurityQuery securityQuery = new SecurityQuery(); + securityQuery.setQueryID(QueryID.SEND_HANDSHAKE_DATA); + securityQuery.setQueryType(QueryType.REQUEST); + securityQuery.setCorrelationId(msg.getCorrID()); + securityQuery.setJsonSize(msg.getJsonSize()); + securityQuery.setData(returnBytes); + //sdlSecurity.hs(); - sendMessage(protocolMessage); + sendMessage(msg, securityQuery); } /** From 472a81c49f00731974352a9e70f5d43ed1b32ea3 Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 4 Aug 2021 15:51:16 -0400 Subject: [PATCH 02/16] Remove ErrorCode test --- .../test/protocol/BinaryQueryHeaderTests.java | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java index 33c567c013..c4c5d70010 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -80,31 +80,6 @@ public void testCorruptHeader() { assertNull(head); } - @Test - public void testErrorCodeAssemble() { - BinaryQueryHeader bqh = new BinaryQueryHeader(); - bqh.setCorrelationID(123); - bqh.setQueryID(QueryID.SEND_INTERNAL_ERROR.getValue()); - bqh.setQueryType(QUERY_TYPE_NOTIFICATION); - bqh.setBulkData(null); - bqh.setErrorCode(2); - bqh.setJsonSize(0); - - byte[] bqhBytes = bqh.assembleHeaderBytes(); - assertNotNull(bqhBytes); - - BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); - assertNotNull(parsedBqh); - - assertEquals(bqh.getCorrelationID(), parsedBqh.getCorrelationID()); - assertEquals(bqh.getQueryID(), parsedBqh.getQueryID()); - assertEquals(bqh.getQueryType(), parsedBqh.getQueryType()); - assertEquals(bqh.getBulkData(), parsedBqh.getBulkData()); - assertEquals(bqh.getJsonData(), parsedBqh.getJsonData()); - assertEquals(bqh.getJsonSize(), parsedBqh.getJsonSize()); - assertEquals(bqh.getErrorCode(), parsedBqh.getErrorCode()); - } - @Test public void testJsonSetException() { try { From e09ce8f5ff8c00f42f2cfeaeeca3b8543bf32216 Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 11 Aug 2021 09:06:19 -0400 Subject: [PATCH 03/16] Fix bit manipulation --- .../test/protocol/BinaryQueryHeaderTests.java | 39 +++++++++++++++++++ .../protocol/BinaryQueryHeader.java | 11 ++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java index c4c5d70010..660334e32d 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -4,6 +4,7 @@ import com.smartdevicelink.protocol.BinaryQueryHeader; import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.util.BitConverter; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,6 +40,44 @@ public BinaryQueryHeader safeParse(byte[] array) { } } + @Test + public void testCorrectParsing() { + byte[] array = new byte[12]; + array[0] = 1; + array[1] = 0; + array[2] = 0; + array[3] = 2; + array[4] = 0; + array[5] = 0; + array[6] = 0; + array[7] = 3; + array[8] = 0; + array[9] = 0; + array[10] = 0; + array[11] = 0; + + BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(array); + assertEquals(parsedBqh.getQueryType(), 0x1); + assertEquals(parsedBqh.getQueryID(), 2); + assertEquals(parsedBqh.getCorrelationID(), 3); + assertEquals(parsedBqh.getJsonSize(), 0); + } + + @Test + public void testCorrectHeaderAssembly() { + BinaryQueryHeader dummyBqh = new BinaryQueryHeader(); + dummyBqh.setQueryType((byte) 1); + dummyBqh.setQueryID(2); + dummyBqh.setCorrelationID(3); + dummyBqh.setJsonSize(0); + + byte[] assembledHeader = dummyBqh.assembleHeaderBytes(); + assertEquals(dummyBqh.getQueryType(), assembledHeader[0]); + assertEquals(dummyBqh.getQueryID(), BitConverter.intFromByteArray(assembledHeader, 0) & 0x00FFFFFF); + assertEquals(dummyBqh.getCorrelationID(), BitConverter.intFromByteArray(assembledHeader, 4)); + assertEquals(dummyBqh.getJsonSize(), BitConverter.intFromByteArray(assembledHeader, 8)); + } + @Test public void testAssemblyAndParse() { BinaryQueryHeader bqh = createDummyBqh(); diff --git a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java index 7a118a1426..50781f2bf4 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java +++ b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java @@ -24,10 +24,10 @@ public BinaryQueryHeader() { public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { BinaryQueryHeader msg = new BinaryQueryHeader(); - byte QUERY_Type = (byte) (binHeader[0] >>> 4); + byte QUERY_Type = (byte) (binHeader[0]); msg.setQueryType(QUERY_Type); - int _queryID = (BitConverter.intFromByteArray(binHeader, 0) & 0x0FFFFFFF); + int _queryID = (BitConverter.intFromByteArray(binHeader, 0) & 0x00FFFFFF); msg.setQueryID(_queryID); int corrID = BitConverter.intFromByteArray(binHeader, 4); @@ -69,12 +69,9 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { } public byte[] assembleHeaderBytes() { - int binHeader = _queryID; - binHeader &= 0xFFFFFFFF >>> 4; - binHeader |= (_queryType << 28); - byte[] ret = new byte[12]; - System.arraycopy(BitConverter.intToByteArray(binHeader), 0, ret, 0, 4); + ret[0] = _queryType; + System.arraycopy(BitConverter.intToByteArray(_queryID), 1, ret, 1, 3); System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4); System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4); return ret; From 9e0c9c87883206ea2f4f08f7db036aa2614b5782 Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 11 Aug 2021 13:46:56 -0400 Subject: [PATCH 04/16] Update QueryID to byte array enum --- .../test/protocol/BinaryQueryHeaderTests.java | 2 +- .../protocol/SecurityQuery.java | 4 +- .../protocol/enums/QueryID.java | 81 ++++++++++++++++--- .../session/BaseSdlSession.java | 12 ++- 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java index 660334e32d..50631d726c 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -25,7 +25,7 @@ public class BinaryQueryHeaderTests { public static BinaryQueryHeader createDummyBqh() { BinaryQueryHeader bqh = new BinaryQueryHeader(); bqh.setCorrelationID(123); - bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA.getValue()); + bqh.setQueryID(BitConverter.intFromByteArray(QueryID.SEND_HANDSHAKE_DATA.getValue(), 0)); bqh.setQueryType(QUERY_TYPE_REQUEST); bqh.setBulkData(null); bqh.setJsonSize(0); diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java index e00a211378..5ed989bc21 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java @@ -9,8 +9,8 @@ @RestrictTo(RestrictTo.Scope.LIBRARY) public class SecurityQuery { - private QueryType _queryType = QueryType.INVALID_QUERY_TYPE; - private QueryID _queryID = QueryID.INVALID_QUERY_ID; + private QueryType _queryType; + private QueryID _queryID; private int _correlationId; private int _jsonSize; private QueryErrorCode _queryErrorCode = QueryErrorCode.ERROR_SUCCESS; diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java index 3668932d14..f9d90160be 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java @@ -1,10 +1,10 @@ package com.smartdevicelink.protocol.enums; -import com.smartdevicelink.util.ByteEnumer; - +import java.util.Enumeration; +import java.util.Objects; import java.util.Vector; -public class QueryID extends ByteEnumer { +public class QueryID { private static final Vector theList = new Vector<>(); @@ -12,13 +12,12 @@ public static Vector getList() { return theList; } - protected QueryID(byte value, String name) { - super(value, name); - } - - public final static QueryID SEND_HANDSHAKE_DATA = new QueryID((byte) 0x01, "SEND_HANDSHAKE_DATA"); - public final static QueryID SEND_INTERNAL_ERROR = new QueryID((byte) 0x02, "SEND_INTERNAL_ERROR"); - public final static QueryID INVALID_QUERY_ID = new QueryID((byte) 0xFF, "INVALID_QUERY_ID"); + public static final byte[] sendHandshakeDataByteArray= {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; + public static final byte[] sendInternalErrorByteArray= {0x0, 0x0, 0x0, 0x0, 0x0, 0x2}; + public static final byte[] invalidQueryIdByteArray= {0xF, 0xF, 0xF, 0xF, 0xF, 0xF}; + public final static QueryID SEND_HANDSHAKE_DATA = new QueryID(sendHandshakeDataByteArray, "SEND_HANDSHAKE_DATA"); + public final static QueryID SEND_INTERNAL_ERROR = new QueryID(sendInternalErrorByteArray, "SEND_INTERNAL_ERROR"); + public final static QueryID INVALID_QUERY_ID = new QueryID(invalidQueryIdByteArray, "INVALID_QUERY_ID"); static { theList.addElement(SEND_HANDSHAKE_DATA); @@ -26,8 +25,66 @@ protected QueryID(byte value, String name) { theList.addElement(INVALID_QUERY_ID); } - public static QueryID valueOf(byte passedByte) { - return (QueryID) get(theList, passedByte); + protected QueryID(byte[] value, String name) { + this.value = value; + this.name = name; + } + + private final byte[] value; + private final String name; + + public byte[] getValue() { + return value; + } + + public String getName() { + return name; + } + + public boolean equals(QueryID other) { + return Objects.equals(name, other.getName()); + } + + public boolean eq(QueryID other) { + return equals(other); + } + + public byte[] value() { + return value; + } + + public static QueryID get(Vector theList, byte[] value) { + Enumeration enumer = theList.elements(); + while (enumer.hasMoreElements()) { + try { + QueryID current = (QueryID) enumer.nextElement(); + if (current.getValue() == value) { + return current; + } + } catch (ClassCastException e) { + return null; + } + } + return null; + } + + public static QueryID get(Vector theList, String name) { + Enumeration enumer = theList.elements(); + while (enumer.hasMoreElements()) { + try { + QueryID current = (QueryID) enumer.nextElement(); + if (current.getName().equals(name)) { + return current; + } + } catch (ClassCastException e) { + return null; + } + } + return null; + } + + public static QueryID valueOf(byte[] passedByteArray) { + return (QueryID) get(theList, passedByteArray); } public static QueryID[] values() { diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index b154117811..0c5228fa48 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -190,6 +190,10 @@ public void setSdlSecurity(SdlSecurityBase sec) { protected void processControlService(ProtocolMessage msg) { if (sdlSecurity == null) return; + + byte[] securityQueryHeader = new byte[12]; + System.arraycopy(msg.getData(), 0, securityQueryHeader, 0, 12); + int iLen = msg.getData().length - 12; byte[] data = new byte[iLen]; System.arraycopy(msg.getData(), 12, data, 0, iLen); @@ -198,8 +202,12 @@ protected void processControlService(ProtocolMessage msg) { Integer iNumBytes = null; - if (data[3] == QueryID.SEND_HANDSHAKE_DATA.getValue()) { + byte[] queryID = new byte[3]; + System.arraycopy(securityQueryHeader, 1, queryID, 0, 3); + if (queryID == QueryID.SEND_HANDSHAKE_DATA.getValue() && securityQueryHeader[0] == QueryType.REQUEST.value()) { iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + } else if (queryID == QueryID.SEND_INTERNAL_ERROR.value()) { + DebugTool.logError(TAG, "Internal Error processing control service"); } if (iNumBytes == null || iNumBytes <= 0) @@ -217,7 +225,7 @@ protected void processControlService(ProtocolMessage msg) { SecurityQuery securityQuery = new SecurityQuery(); securityQuery.setQueryID(QueryID.SEND_HANDSHAKE_DATA); - securityQuery.setQueryType(QueryType.REQUEST); + securityQuery.setQueryType(QueryType.RESPONSE); securityQuery.setCorrelationId(msg.getCorrID()); securityQuery.setJsonSize(msg.getJsonSize()); securityQuery.setData(returnBytes); From fe5928a4bebccd187cb63f854a66d21b5db73e21 Mon Sep 17 00:00:00 2001 From: Henigan Date: Thu, 12 Aug 2021 13:57:46 -0400 Subject: [PATCH 05/16] Fix Lib response --- .../test/protocol/BinaryQueryHeaderTests.java | 2 +- .../managers/audio/AudioStreamManager.java | 2 +- .../managers/video/VideoStreamManager.java | 2 +- .../lifecycle/BaseLifecycleManager.java | 2 +- .../protocol/SdlProtocolBase.java | 17 ++----- .../protocol/enums/QueryID.java | 14 ++++-- .../session/BaseSdlSession.java | 49 ++++++++++++------- 7 files changed, 49 insertions(+), 39 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java index 50631d726c..a8ab052d8f 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -25,7 +25,7 @@ public class BinaryQueryHeaderTests { public static BinaryQueryHeader createDummyBqh() { BinaryQueryHeader bqh = new BinaryQueryHeader(); bqh.setCorrelationID(123); - bqh.setQueryID(BitConverter.intFromByteArray(QueryID.SEND_HANDSHAKE_DATA.getValue(), 0)); + bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA.getIntValue()); bqh.setQueryType(QUERY_TYPE_REQUEST); bqh.setBulkData(null); bqh.setJsonSize(0); diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java index c28401b076..667522372a 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/audio/AudioStreamManager.java @@ -530,7 +530,7 @@ protected IAudioStreamListener startAudioStream(final SdlSession session) { IStreamListener streamListener = new IStreamListener() { @Override public void sendStreamPacket(ProtocolMessage pm) { - session.sendMessage(pm, null); + session.sendMessage(pm); } }; diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java index b3899c539c..cce131baa2 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/video/VideoStreamManager.java @@ -1065,7 +1065,7 @@ protected IVideoStreamListener startVideoStream(VideoStreamingParameters params, IStreamListener iStreamListener = new IStreamListener() { @Override public void sendStreamPacket(ProtocolMessage pm) { - session.sendMessage(pm, null); + session.sendMessage(pm); } }; diff --git a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java index 8a783862b6..9cf72f7cdf 100644 --- a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java +++ b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java @@ -866,7 +866,7 @@ private void sendRPCMessagePrivate(RPCMessage message, boolean isInternalMessage pm.setPriorityCoefficient(1); } - session.sendMessage(pm, null); + session.sendMessage(pm); } catch (OutOfMemoryError e) { DebugTool.logError(TAG,"Error attempting to send RPC message.", e); diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java b/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java index b1723dc6a0..b49c82da3a 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlProtocolBase.java @@ -562,7 +562,7 @@ public void endSession(byte sessionID) { } } // end-method - public void sendMessage(ProtocolMessage protocolMsg, SecurityQuery securityQuery) { + public void sendMessage(ProtocolMessage protocolMsg) { SessionType sessionType = protocolMsg.getSessionType(); byte sessionID = protocolMsg.getSessionID(); boolean requiresEncryption = protocolMsg.getPayloadProtected(); @@ -571,19 +571,8 @@ public void sendMessage(ProtocolMessage protocolMsg, SecurityQuery securityQuery if (protocolVersion.getMajor() > 1 && sessionType != SessionType.NAV && sessionType != SessionType.PCM) { if (sessionType.eq(SessionType.CONTROL)) { final byte[] secureData = protocolMsg.getData().clone(); - - if (securityQuery != null) { - data = new byte[12 + secureData.length]; - final BinaryQueryHeader binQueryHeader = SdlPacketFactory.createBinaryQueryHeader(securityQuery.getQueryType().getValue(), securityQuery.getQueryType().getValue(), securityQuery.getCorrelationId(), securityQuery.getJsonSize()); - System.arraycopy(binQueryHeader.assembleHeaderBytes(), 0, data, 0, 12); - System.arraycopy(secureData, 0, data, 12, secureData.length); - } else { - data = new byte[headerSize + secureData.length]; - final BinaryFrameHeader binFrameHeader = - SdlPacketFactory.createBinaryFrameHeader(protocolMsg.getRPCType(), protocolMsg.getFunctionID(), protocolMsg.getCorrID(), 0); - System.arraycopy(binFrameHeader.assembleHeaderBytes(), 0, data, 0, headerSize); - System.arraycopy(secureData, 0, data, headerSize, secureData.length); - } + data = new byte[headerSize + secureData.length]; + System.arraycopy(secureData, 0, data, 0, secureData.length); } else if (protocolMsg.getBulkData() != null) { data = new byte[12 + protocolMsg.getJsonSize() + protocolMsg.getBulkData().length]; sessionType = SessionType.BULK_DATA; diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java index f9d90160be..7e6a8c89c2 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java @@ -1,5 +1,7 @@ package com.smartdevicelink.protocol.enums; +import com.smartdevicelink.util.BitConverter; + import java.util.Enumeration; import java.util.Objects; import java.util.Vector; @@ -12,9 +14,9 @@ public static Vector getList() { return theList; } - public static final byte[] sendHandshakeDataByteArray= {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; - public static final byte[] sendInternalErrorByteArray= {0x0, 0x0, 0x0, 0x0, 0x0, 0x2}; - public static final byte[] invalidQueryIdByteArray= {0xF, 0xF, 0xF, 0xF, 0xF, 0xF}; + private static final byte[] sendHandshakeDataByteArray= {(byte) 0x00, (byte) 0x00, (byte) 0x01}; + private static final byte[] sendInternalErrorByteArray= {(byte) 0x00, (byte) 0x00, (byte) 0x02}; + private static final byte[] invalidQueryIdByteArray= {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public final static QueryID SEND_HANDSHAKE_DATA = new QueryID(sendHandshakeDataByteArray, "SEND_HANDSHAKE_DATA"); public final static QueryID SEND_INTERNAL_ERROR = new QueryID(sendInternalErrorByteArray, "SEND_INTERNAL_ERROR"); public final static QueryID INVALID_QUERY_ID = new QueryID(invalidQueryIdByteArray, "INVALID_QUERY_ID"); @@ -37,6 +39,12 @@ public byte[] getValue() { return value; } + public int getIntValue() { + byte[] copy = new byte[4]; + System.arraycopy(value, 0, copy, 1, 3); + return BitConverter.intFromByteArray(copy, 0); + } + public String getName() { return name; } diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 0c5228fa48..9cfaa21a90 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -36,10 +36,12 @@ import com.smartdevicelink.exception.SdlException; import com.smartdevicelink.managers.lifecycle.RpcConverter; +import com.smartdevicelink.protocol.BinaryQueryHeader; import com.smartdevicelink.protocol.ISdlProtocol; import com.smartdevicelink.protocol.ISdlServiceListener; import com.smartdevicelink.protocol.ProtocolMessage; import com.smartdevicelink.protocol.SdlPacket; +import com.smartdevicelink.protocol.SdlPacketFactory; import com.smartdevicelink.protocol.SdlProtocolBase; import com.smartdevicelink.protocol.SecurityQuery; import com.smartdevicelink.protocol.enums.ControlFrameTags; @@ -55,10 +57,12 @@ import com.smartdevicelink.streaming.video.VideoStreamingParameters; import com.smartdevicelink.transport.BaseTransportConfig; import com.smartdevicelink.transport.enums.TransportType; +import com.smartdevicelink.util.BitConverter; import com.smartdevicelink.util.DebugTool; import com.smartdevicelink.util.SystemInfo; import com.smartdevicelink.util.Version; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.ListIterator; @@ -150,11 +154,11 @@ public void startSession() throws SdlException { } - public void sendMessage(ProtocolMessage msg, SecurityQuery securityQuery) { + public void sendMessage(ProtocolMessage msg) { if (sdlProtocol == null) { return; } - sdlProtocol.sendMessage(msg, securityQuery); + sdlProtocol.sendMessage(msg); } public TransportType getCurrentTransportType() { @@ -204,17 +208,33 @@ protected void processControlService(ProtocolMessage msg) { byte[] queryID = new byte[3]; System.arraycopy(securityQueryHeader, 1, queryID, 0, 3); - if (queryID == QueryID.SEND_HANDSHAKE_DATA.getValue() && securityQueryHeader[0] == QueryType.REQUEST.value()) { - iNumBytes = sdlSecurity.runHandshake(data, dataToRead); - } else if (queryID == QueryID.SEND_INTERNAL_ERROR.value()) { - DebugTool.logError(TAG, "Internal Error processing control service"); - } - if (iNumBytes == null || iNumBytes <= 0) + if (!Arrays.equals(queryID, QueryID.SEND_HANDSHAKE_DATA.getValue()) + || (securityQueryHeader[0] != QueryType.NOTIFICATION.value() && securityQueryHeader[0] != QueryType.REQUEST.value())) { return; + } + + iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + SecurityQuery securityQuery = new SecurityQuery(); + + if (iNumBytes == null || iNumBytes <= 0) { + DebugTool.logError(TAG, "Internal Error processing control service"); + + securityQuery.setQueryID(QueryID.SEND_INTERNAL_ERROR); + securityQuery.setQueryType(QueryType.NOTIFICATION); + securityQuery.setCorrelationId(msg.getCorrID()); + securityQuery.setJsonSize(0); + } else { + securityQuery.setQueryID(QueryID.SEND_HANDSHAKE_DATA); + securityQuery.setQueryType(QueryType.RESPONSE); + securityQuery.setCorrelationId(msg.getCorrID()); + securityQuery.setJsonSize(0); + } - byte[] returnBytes = new byte[iNumBytes]; - System.arraycopy(dataToRead, 0, returnBytes, 0, iNumBytes); + byte[] returnBytes = new byte[iNumBytes + 12]; + BinaryQueryHeader binaryQueryHeader = SdlPacketFactory.createBinaryQueryHeader(securityQuery.getQueryType().getValue(), securityQuery.getQueryID().getIntValue(), securityQuery.getCorrelationId(), securityQuery.getJsonSize()); + System.arraycopy(binaryQueryHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12); + System.arraycopy(dataToRead, 0, returnBytes, 12, iNumBytes); ProtocolMessage protocolMessage = new ProtocolMessage(); protocolMessage.setSessionType(SessionType.CONTROL); @@ -223,16 +243,9 @@ protected void processControlService(ProtocolMessage msg) { protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor()); protocolMessage.setSessionID((byte) this.sessionId); - SecurityQuery securityQuery = new SecurityQuery(); - securityQuery.setQueryID(QueryID.SEND_HANDSHAKE_DATA); - securityQuery.setQueryType(QueryType.RESPONSE); - securityQuery.setCorrelationId(msg.getCorrID()); - securityQuery.setJsonSize(msg.getJsonSize()); - securityQuery.setData(returnBytes); - //sdlSecurity.hs(); - sendMessage(msg, securityQuery); + sendMessage(protocolMessage); } /** From a2cee42e4183f452427f2df64ec02d65f016e448 Mon Sep 17 00:00:00 2001 From: Henigan Date: Thu, 12 Aug 2021 16:31:24 -0400 Subject: [PATCH 06/16] remove SecurityQuery in favor of BQH --- .../test/protocol/BinaryQueryHeaderTests.java | 25 +++-- .../protocol/BinaryQueryHeader.java | 42 ++++---- .../protocol/SdlPacketFactory.java | 4 +- .../protocol/SecurityQuery.java | 102 ------------------ .../protocol/enums/QueryID.java | 3 +- .../session/BaseSdlSession.java | 42 ++++---- 6 files changed, 63 insertions(+), 155 deletions(-) delete mode 100644 base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java index a8ab052d8f..f55816f99f 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java @@ -4,6 +4,7 @@ import com.smartdevicelink.protocol.BinaryQueryHeader; import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.util.BitConverter; import org.junit.Test; @@ -18,15 +19,11 @@ @RunWith(AndroidJUnit4.class) public class BinaryQueryHeaderTests { - public static final byte QUERY_TYPE_REQUEST = 0x00; - public static final byte QUERY_TYPE_RESPONSE = 0x01; - public static final byte QUERY_TYPE_NOTIFICATION = 0x02; - public static BinaryQueryHeader createDummyBqh() { BinaryQueryHeader bqh = new BinaryQueryHeader(); bqh.setCorrelationID(123); - bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA.getIntValue()); - bqh.setQueryType(QUERY_TYPE_REQUEST); + bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); + bqh.setQueryType(QueryType.REQUEST); bqh.setBulkData(null); bqh.setJsonSize(0); return bqh; @@ -43,7 +40,7 @@ public BinaryQueryHeader safeParse(byte[] array) { @Test public void testCorrectParsing() { byte[] array = new byte[12]; - array[0] = 1; + array[0] = 0; array[1] = 0; array[2] = 0; array[3] = 2; @@ -57,8 +54,8 @@ public void testCorrectParsing() { array[11] = 0; BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(array); - assertEquals(parsedBqh.getQueryType(), 0x1); - assertEquals(parsedBqh.getQueryID(), 2); + assertEquals(parsedBqh.getQueryType(), QueryType.REQUEST); + assertEquals(parsedBqh.getQueryID(), QueryID.SEND_INTERNAL_ERROR); assertEquals(parsedBqh.getCorrelationID(), 3); assertEquals(parsedBqh.getJsonSize(), 0); } @@ -66,14 +63,16 @@ public void testCorrectParsing() { @Test public void testCorrectHeaderAssembly() { BinaryQueryHeader dummyBqh = new BinaryQueryHeader(); - dummyBqh.setQueryType((byte) 1); - dummyBqh.setQueryID(2); + dummyBqh.setQueryType(QueryType.REQUEST); + dummyBqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); dummyBqh.setCorrelationID(3); dummyBqh.setJsonSize(0); byte[] assembledHeader = dummyBqh.assembleHeaderBytes(); - assertEquals(dummyBqh.getQueryType(), assembledHeader[0]); - assertEquals(dummyBqh.getQueryID(), BitConverter.intFromByteArray(assembledHeader, 0) & 0x00FFFFFF); + assertEquals(dummyBqh.getQueryType(), QueryType.valueOf(assembledHeader[0])); + byte[] queryIDFromHeader = new byte[3]; + System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3); + assertEquals(dummyBqh.getQueryID(), QueryID.valueOf(queryIDFromHeader)); assertEquals(dummyBqh.getCorrelationID(), BitConverter.intFromByteArray(assembledHeader, 4)); assertEquals(dummyBqh.getJsonSize(), BitConverter.intFromByteArray(assembledHeader, 8)); } diff --git a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java index 50781f2bf4..ea474ee628 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java +++ b/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java @@ -2,6 +2,9 @@ import androidx.annotation.RestrictTo; +import com.smartdevicelink.protocol.enums.QueryErrorCode; +import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.util.BitConverter; import com.smartdevicelink.util.DebugTool; @@ -9,14 +12,14 @@ public class BinaryQueryHeader { private static final String TAG = "BinaryQueryHeader"; - private byte _queryType; - private int _queryID; + private QueryType _queryType; + private QueryID _queryID; private int _correlationID; private int _jsonSize; - private int _errorCode; + private QueryErrorCode _errorCode; - private byte[] _jsonData; - private byte[] _bulkData; + private byte[] _jsonData = null; + private byte[] _bulkData = null; public BinaryQueryHeader() { } @@ -25,10 +28,11 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { BinaryQueryHeader msg = new BinaryQueryHeader(); byte QUERY_Type = (byte) (binHeader[0]); - msg.setQueryType(QUERY_Type); + msg.setQueryType(QueryType.valueOf(QUERY_Type)); - int _queryID = (BitConverter.intFromByteArray(binHeader, 0) & 0x00FFFFFF); - msg.setQueryID(_queryID); + byte[] _queryID = new byte[3]; + System.arraycopy(binHeader, 1, _queryID, 0, 3); + msg.setQueryID(QueryID.valueOf(_queryID)); int corrID = BitConverter.intFromByteArray(binHeader, 4); msg.setCorrelationID(corrID); @@ -36,9 +40,9 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { int _jsonSize = BitConverter.intFromByteArray(binHeader, 8); msg.setJsonSize(_jsonSize); - if (msg.getQueryType() == 0x20 && msg.getQueryID() == 0x02) { + if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { int _errorCode = BitConverter.intFromByteArray(binHeader, binHeader.length-1); - msg.setErrorCode(_errorCode); + msg.setErrorCode(QueryErrorCode.valueOf((byte) _errorCode)); } try { @@ -50,7 +54,7 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { if (binHeader.length - _jsonSize - 12 > 0) { byte[] _bulkData; - if (msg.getQueryType() == 0x20 && msg.getQueryID() == 0x02) { + if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { _bulkData = new byte[binHeader.length - _jsonSize - 12 - 1]; System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length - 1); } else { @@ -70,26 +74,26 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { public byte[] assembleHeaderBytes() { byte[] ret = new byte[12]; - ret[0] = _queryType; - System.arraycopy(BitConverter.intToByteArray(_queryID), 1, ret, 1, 3); + ret[0] = _queryType.getValue(); + System.arraycopy(_queryID.getValue(), 0, ret, 1, 3); System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4); System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4); return ret; } - public byte getQueryType() { + public QueryType getQueryType() { return _queryType; } - public void setQueryType(byte _queryType) { + public void setQueryType(QueryType _queryType) { this._queryType = _queryType; } - public int getQueryID() { + public QueryID getQueryID() { return _queryID; } - public void setQueryID(int _queryID) { + public void setQueryID(QueryID _queryID) { this._queryID = _queryID; } @@ -109,11 +113,11 @@ public void setJsonSize(int _jsonSize) { this._jsonSize = _jsonSize; } - public int getErrorCode() { + public QueryErrorCode getErrorCode() { return _errorCode; } - public void setErrorCode(int _errorCode) { + public void setErrorCode(QueryErrorCode _errorCode) { this._errorCode = _errorCode; } diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java index 75d122da56..eaad041ca3 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java @@ -35,6 +35,8 @@ import com.smartdevicelink.protocol.enums.ControlFrameTags; import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; +import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.util.BitConverter; @@ -144,7 +146,7 @@ public static BinaryFrameHeader createBinaryFrameHeader(byte rpcType, int functi return msg; } - public static BinaryQueryHeader createBinaryQueryHeader(byte queryType, int queryId, int corrID, int jsonSize) { + public static BinaryQueryHeader createBinaryQueryHeader(QueryType queryType, QueryID queryId, int corrID, int jsonSize) { BinaryQueryHeader msg = new BinaryQueryHeader(); msg.setQueryType(queryType); msg.setQueryID(queryId); diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java deleted file mode 100644 index 5ed989bc21..0000000000 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQuery.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.smartdevicelink.protocol; - -import androidx.annotation.RestrictTo; - -import com.smartdevicelink.protocol.enums.QueryErrorCode; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; - -@RestrictTo(RestrictTo.Scope.LIBRARY) -public class SecurityQuery { - - private QueryType _queryType; - private QueryID _queryID; - private int _correlationId; - private int _jsonSize; - private QueryErrorCode _queryErrorCode = QueryErrorCode.ERROR_SUCCESS; - - private byte[] _data = null; - private byte[] _bulkData = null; - - public SecurityQuery() { - } - - public QueryType getQueryType() { - return this._queryType; - } - - public void setQueryType(QueryType queryType) { - this._queryType = queryType; - } - - public QueryID getQueryID() { - return this._queryID; - } - - public void setQueryID(QueryID queryID) { - this._queryID = queryID; - } - - public int getCorrelationId() { - return this._correlationId; - } - - public void setCorrelationId(int msgId) { - this._correlationId = msgId; - } - - public int getJsonSize() { - return this._jsonSize; - } - - public void setJsonSize(int jsonSize) { - this._jsonSize = jsonSize; - } - - public QueryErrorCode getQueryErrorCode() { - return this._queryErrorCode; - } - - public void setQueryErrorCode(QueryErrorCode queryErrorCode) { - this._queryErrorCode = queryErrorCode; - } - - public byte[] getData() { - return _data; - } - - public void setData(byte[] data) { - this._data = data; - this._jsonSize = data.length; - } - - public void setData(byte[] data, int offset, int length) { - if (this._data != null) - this._data = null; - this._data = new byte[length]; - System.arraycopy(data, offset, this._data, 0, length); - this._jsonSize = 0; - } - - public byte[] getBulkData() { - return _bulkData; - } - - public void setBulkDataNoCopy(byte[] bulkData) { - this._bulkData = bulkData; - } - - public void setBulkData(byte[] bulkData) { - if (this._bulkData != null) - this._bulkData = null; - this._bulkData = new byte[bulkData.length]; - System.arraycopy(bulkData, 0, this._bulkData, 0, bulkData.length); - } - - public void setBulkData(byte[] bulkData, int length) { - if (this._bulkData != null) - this._bulkData = null; - this._bulkData = new byte[length]; - System.arraycopy(bulkData, 0, this._bulkData, 0, length); - } -} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java index 7e6a8c89c2..e24481340a 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java @@ -2,6 +2,7 @@ import com.smartdevicelink.util.BitConverter; +import java.util.Arrays; import java.util.Enumeration; import java.util.Objects; import java.util.Vector; @@ -66,7 +67,7 @@ public static QueryID get(Vector theList, byte[] value) { while (enumer.hasMoreElements()) { try { QueryID current = (QueryID) enumer.nextElement(); - if (current.getValue() == value) { + if (Arrays.equals(current.getValue(), value)) { return current; } } catch (ClassCastException e) { diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 9cfaa21a90..93c3358a2b 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -41,10 +41,9 @@ import com.smartdevicelink.protocol.ISdlServiceListener; import com.smartdevicelink.protocol.ProtocolMessage; import com.smartdevicelink.protocol.SdlPacket; -import com.smartdevicelink.protocol.SdlPacketFactory; import com.smartdevicelink.protocol.SdlProtocolBase; -import com.smartdevicelink.protocol.SecurityQuery; import com.smartdevicelink.protocol.enums.ControlFrameTags; +import com.smartdevicelink.protocol.enums.QueryErrorCode; import com.smartdevicelink.protocol.enums.QueryID; import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; @@ -195,8 +194,7 @@ protected void processControlService(ProtocolMessage msg) { if (sdlSecurity == null) return; - byte[] securityQueryHeader = new byte[12]; - System.arraycopy(msg.getData(), 0, securityQueryHeader, 0, 12); + BinaryQueryHeader receivedHeader = BinaryQueryHeader.parseBinaryQueryHeader(msg.getData().clone()); int iLen = msg.getData().length - 12; byte[] data = new byte[iLen]; @@ -206,34 +204,40 @@ protected void processControlService(ProtocolMessage msg) { Integer iNumBytes = null; - byte[] queryID = new byte[3]; - System.arraycopy(securityQueryHeader, 1, queryID, 0, 3); + if (receivedHeader.getQueryID() == QueryID.SEND_INTERNAL_ERROR + && receivedHeader.getQueryType() == QueryType.NOTIFICATION) { + if (receivedHeader.getErrorCode() != null) { + DebugTool.logError(TAG, "Client internal error: " + receivedHeader.getErrorCode().getName()); + } else { + DebugTool.logError(TAG, "Client internal error: No information provided"); + } + return; + } - if (!Arrays.equals(queryID, QueryID.SEND_HANDSHAKE_DATA.getValue()) - || (securityQueryHeader[0] != QueryType.NOTIFICATION.value() && securityQueryHeader[0] != QueryType.REQUEST.value())) { + if (receivedHeader.getQueryID() != QueryID.SEND_HANDSHAKE_DATA + && (receivedHeader.getQueryType() != QueryType.NOTIFICATION || receivedHeader.getQueryType() != QueryType.REQUEST)) { return; } iNumBytes = sdlSecurity.runHandshake(data, dataToRead); - SecurityQuery securityQuery = new SecurityQuery(); + BinaryQueryHeader responseHeader = new BinaryQueryHeader(); if (iNumBytes == null || iNumBytes <= 0) { DebugTool.logError(TAG, "Internal Error processing control service"); - securityQuery.setQueryID(QueryID.SEND_INTERNAL_ERROR); - securityQuery.setQueryType(QueryType.NOTIFICATION); - securityQuery.setCorrelationId(msg.getCorrID()); - securityQuery.setJsonSize(0); + responseHeader.setQueryID(QueryID.SEND_INTERNAL_ERROR); + responseHeader.setQueryType(QueryType.NOTIFICATION); + responseHeader.setCorrelationID(msg.getCorrID()); + responseHeader.setJsonSize(0); } else { - securityQuery.setQueryID(QueryID.SEND_HANDSHAKE_DATA); - securityQuery.setQueryType(QueryType.RESPONSE); - securityQuery.setCorrelationId(msg.getCorrID()); - securityQuery.setJsonSize(0); + responseHeader.setQueryID(QueryID.SEND_HANDSHAKE_DATA); + responseHeader.setQueryType(QueryType.RESPONSE); + responseHeader.setCorrelationID(msg.getCorrID()); + responseHeader.setJsonSize(0); } byte[] returnBytes = new byte[iNumBytes + 12]; - BinaryQueryHeader binaryQueryHeader = SdlPacketFactory.createBinaryQueryHeader(securityQuery.getQueryType().getValue(), securityQuery.getQueryID().getIntValue(), securityQuery.getCorrelationId(), securityQuery.getJsonSize()); - System.arraycopy(binaryQueryHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12); + System.arraycopy(responseHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12); System.arraycopy(dataToRead, 0, returnBytes, 12, iNumBytes); ProtocolMessage protocolMessage = new ProtocolMessage(); From 25951389c6a11db8d612c64ad2fe3de7259cb460 Mon Sep 17 00:00:00 2001 From: Henigan Date: Thu, 12 Aug 2021 16:43:17 -0400 Subject: [PATCH 07/16] Refactor to SecurityQueryPayload --- ...ts.java => SecurityQueryPayloadTests.java} | 26 +++++++++---------- .../protocol/SdlPacketFactory.java | 10 ------- ...yHeader.java => SecurityQueryPayload.java} | 11 ++++---- .../session/BaseSdlSession.java | 9 +++---- 4 files changed, 21 insertions(+), 35 deletions(-) rename android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/{BinaryQueryHeaderTests.java => SecurityQueryPayloadTests.java} (80%) rename base/src/main/java/com/smartdevicelink/protocol/{BinaryQueryHeader.java => SecurityQueryPayload.java} (91%) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java similarity index 80% rename from android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java rename to android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java index f55816f99f..cc0cd914a5 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/BinaryQueryHeaderTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java @@ -2,7 +2,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.smartdevicelink.protocol.BinaryQueryHeader; +import com.smartdevicelink.protocol.SecurityQueryPayload; import com.smartdevicelink.protocol.enums.QueryID; import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.util.BitConverter; @@ -17,10 +17,10 @@ import static org.junit.Assert.fail; @RunWith(AndroidJUnit4.class) -public class BinaryQueryHeaderTests { +public class SecurityQueryPayloadTests { - public static BinaryQueryHeader createDummyBqh() { - BinaryQueryHeader bqh = new BinaryQueryHeader(); + public static SecurityQueryPayload createDummyBqh() { + SecurityQueryPayload bqh = new SecurityQueryPayload(); bqh.setCorrelationID(123); bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); bqh.setQueryType(QueryType.REQUEST); @@ -29,9 +29,9 @@ public static BinaryQueryHeader createDummyBqh() { return bqh; } - public BinaryQueryHeader safeParse(byte[] array) { + public SecurityQueryPayload safeParse(byte[] array) { try { - return BinaryQueryHeader.parseBinaryQueryHeader(array); + return SecurityQueryPayload.parseBinaryQueryHeader(array); } catch (Exception e) { return null; } @@ -53,7 +53,7 @@ public void testCorrectParsing() { array[10] = 0; array[11] = 0; - BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(array); + SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(array); assertEquals(parsedBqh.getQueryType(), QueryType.REQUEST); assertEquals(parsedBqh.getQueryID(), QueryID.SEND_INTERNAL_ERROR); assertEquals(parsedBqh.getCorrelationID(), 3); @@ -62,7 +62,7 @@ public void testCorrectParsing() { @Test public void testCorrectHeaderAssembly() { - BinaryQueryHeader dummyBqh = new BinaryQueryHeader(); + SecurityQueryPayload dummyBqh = new SecurityQueryPayload(); dummyBqh.setQueryType(QueryType.REQUEST); dummyBqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); dummyBqh.setCorrelationID(3); @@ -79,12 +79,12 @@ public void testCorrectHeaderAssembly() { @Test public void testAssemblyAndParse() { - BinaryQueryHeader bqh = createDummyBqh(); + SecurityQueryPayload bqh = createDummyBqh(); byte[] bqhBytes = bqh.assembleHeaderBytes(); assertNotNull(bqhBytes); - BinaryQueryHeader parsedBqh = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); + SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); assertNotNull(parsedBqh); assertEquals(bqh.getCorrelationID(), parsedBqh.getCorrelationID()); @@ -97,7 +97,7 @@ public void testAssemblyAndParse() { @Test public void testCorruptHeader() { - BinaryQueryHeader bqh = createDummyBqh(); + SecurityQueryPayload bqh = createDummyBqh(); bqh.setJsonSize(5); bqh.setJsonData(new byte[5]); bqh.setJsonSize(Integer.MAX_VALUE); @@ -114,14 +114,14 @@ public void testCorruptHeader() { } assertNull(safeParse(bqhBytes)); - BinaryQueryHeader head = BinaryQueryHeader.parseBinaryQueryHeader(bqhBytes); + SecurityQueryPayload head = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); assertNull(head); } @Test public void testJsonSetException() { try { - BinaryQueryHeader bqh = createDummyBqh(); + SecurityQueryPayload bqh = createDummyBqh(); bqh.setJsonData(null); fail("Setting JSON data to null should have thrown an exception"); } catch (Exception e) { diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java index eaad041ca3..98b1e35da2 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java @@ -146,14 +146,4 @@ public static BinaryFrameHeader createBinaryFrameHeader(byte rpcType, int functi return msg; } - public static BinaryQueryHeader createBinaryQueryHeader(QueryType queryType, QueryID queryId, int corrID, int jsonSize) { - BinaryQueryHeader msg = new BinaryQueryHeader(); - msg.setQueryType(queryType); - msg.setQueryID(queryId); - msg.setCorrelationID(corrID); - msg.setJsonSize(jsonSize); - - return msg; - } - } diff --git a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java similarity index 91% rename from base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java rename to base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index ea474ee628..67f45171e3 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/BinaryQueryHeader.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -9,7 +9,7 @@ import com.smartdevicelink.util.DebugTool; @RestrictTo(RestrictTo.Scope.LIBRARY) -public class BinaryQueryHeader { +public class SecurityQueryPayload { private static final String TAG = "BinaryQueryHeader"; private QueryType _queryType; @@ -21,11 +21,11 @@ public class BinaryQueryHeader { private byte[] _jsonData = null; private byte[] _bulkData = null; - public BinaryQueryHeader() { + public SecurityQueryPayload() { } - public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { - BinaryQueryHeader msg = new BinaryQueryHeader(); + public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { + SecurityQueryPayload msg = new SecurityQueryPayload(); byte QUERY_Type = (byte) (binHeader[0]); msg.setQueryType(QueryType.valueOf(QUERY_Type)); @@ -41,8 +41,7 @@ public static BinaryQueryHeader parseBinaryQueryHeader(byte[] binHeader) { msg.setJsonSize(_jsonSize); if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { - int _errorCode = BitConverter.intFromByteArray(binHeader, binHeader.length-1); - msg.setErrorCode(QueryErrorCode.valueOf((byte) _errorCode)); + msg.setErrorCode(QueryErrorCode.valueOf(binHeader[binHeader.length - 1])); } try { diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 93c3358a2b..6be68e5069 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -36,14 +36,13 @@ import com.smartdevicelink.exception.SdlException; import com.smartdevicelink.managers.lifecycle.RpcConverter; -import com.smartdevicelink.protocol.BinaryQueryHeader; +import com.smartdevicelink.protocol.SecurityQueryPayload; import com.smartdevicelink.protocol.ISdlProtocol; import com.smartdevicelink.protocol.ISdlServiceListener; import com.smartdevicelink.protocol.ProtocolMessage; import com.smartdevicelink.protocol.SdlPacket; import com.smartdevicelink.protocol.SdlProtocolBase; import com.smartdevicelink.protocol.enums.ControlFrameTags; -import com.smartdevicelink.protocol.enums.QueryErrorCode; import com.smartdevicelink.protocol.enums.QueryID; import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; @@ -56,12 +55,10 @@ import com.smartdevicelink.streaming.video.VideoStreamingParameters; import com.smartdevicelink.transport.BaseTransportConfig; import com.smartdevicelink.transport.enums.TransportType; -import com.smartdevicelink.util.BitConverter; import com.smartdevicelink.util.DebugTool; import com.smartdevicelink.util.SystemInfo; import com.smartdevicelink.util.Version; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.ListIterator; @@ -194,7 +191,7 @@ protected void processControlService(ProtocolMessage msg) { if (sdlSecurity == null) return; - BinaryQueryHeader receivedHeader = BinaryQueryHeader.parseBinaryQueryHeader(msg.getData().clone()); + SecurityQueryPayload receivedHeader = SecurityQueryPayload.parseBinaryQueryHeader(msg.getData().clone()); int iLen = msg.getData().length - 12; byte[] data = new byte[iLen]; @@ -220,7 +217,7 @@ protected void processControlService(ProtocolMessage msg) { } iNumBytes = sdlSecurity.runHandshake(data, dataToRead); - BinaryQueryHeader responseHeader = new BinaryQueryHeader(); + SecurityQueryPayload responseHeader = new SecurityQueryPayload(); if (iNumBytes == null || iNumBytes <= 0) { DebugTool.logError(TAG, "Internal Error processing control service"); From 55508208890888b7c13fa7455e0ea23441f4650a Mon Sep 17 00:00:00 2001 From: Henigan Date: Thu, 12 Aug 2021 17:19:47 -0400 Subject: [PATCH 08/16] Add enum unit tests --- .../com/smartdevicelink/test/Validator.java | 24 +++++ .../test/protocol/enums/QueryTypeTests.java | 97 +++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java index e8d89c818d..7eadcbe559 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java @@ -3,6 +3,7 @@ import com.smartdevicelink.managers.file.filetypes.SdlFile; import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; import com.smartdevicelink.protocol.enums.FrameType; +import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.rpc.*; import com.smartdevicelink.proxy.rpc.enums.AppServiceType; @@ -126,6 +127,29 @@ public static boolean validateSessionTypeArray(SessionType[] array1, SessionType return true; } + public static boolean validateQueryTypeArray(QueryType[] array1, QueryType[] array2) { + + if (array1 == null) { + return (array2 == null); + } + + if (array2 == null) { + return (array1 == null); + } + + if (array1.length != array2.length) { + return false; + } + + for (int i = 0; i < array1.length; i++) { + if (array1[i] != array2[i]) { + return false; + } + } + + return true; + } + public static boolean validateFrameDataControlFrameTypeArray(FrameDataControlFrameType[] array1, FrameDataControlFrameType[] array2) { if (array1 == null) { diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java new file mode 100644 index 0000000000..13d606cffd --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java @@ -0,0 +1,97 @@ +package com.smartdevicelink.test.protocol.enums; + +import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.test.Validator; + +import junit.framework.TestCase; + +import java.util.Vector; + +public class QueryTypeTests extends TestCase { + + private Vector list = QueryType.getList(); + + public void testValidEnums() { + final byte REQUEST_BYTE = (byte) 0x00; + final String REQUEST_STRING = "REQUEST"; + + final byte RESPONSE_BYTE = (byte) 0x10; + final String RESPONSE_STRING = "RESPONSE"; + + final byte NOTIFICATION_BYTE = (byte) 0x20; + final String NOTIFICATION_STRING = "NOTIFICATION"; + + final byte INVALID_QUERY_TYPE_BYTE = (byte) 0xFF; + final String INVALID_QUERY_TYPE_STRING = "INVALID_QUERY_TYPE"; + + try { + assertNotNull("QueryType list returned null", list); + + QueryType enumRequest = (QueryType) QueryType.get(list, REQUEST_BYTE); + QueryType enumResponse = (QueryType) QueryType.get(list, RESPONSE_BYTE); + QueryType enumNotification = (QueryType) QueryType.get(list, NOTIFICATION_BYTE); + QueryType enumInvalidQueryType = (QueryType) QueryType.get(list, INVALID_QUERY_TYPE_BYTE); + + assertNotNull("Request byte match returned null", enumRequest); + assertNotNull("Response byte match returned null", enumResponse); + assertNotNull("Notification byte match returned null", enumNotification); + assertNotNull("Invalid Query Type byte match returned null", enumInvalidQueryType); + + enumRequest = (QueryType) QueryType.get(list, REQUEST_STRING); + enumResponse = (QueryType) QueryType.get(list, RESPONSE_STRING); + enumNotification = (QueryType) QueryType.get(list, NOTIFICATION_STRING); + enumInvalidQueryType = (QueryType) QueryType.get(list, INVALID_QUERY_TYPE_STRING); + + assertNotNull("Request string match returned null", enumRequest); + assertNotNull("Response string match returned null", enumResponse); + assertNotNull("Notification string match returned null", enumNotification); + assertNotNull("Invalid Query string byte match returned null", enumInvalidQueryType); + + + }catch (NullPointerException exception) { + fail("Null enum list throws NullPointerException."); + } + } + + public void testInvalidEnum() { + + final byte INVALID_BYTE = (byte) 0xAB; + final String INVALID_STRING = "Invalid"; + + try { + QueryType enumInvalid = (QueryType) QueryType.get(list, INVALID_BYTE); + assertNull("Invalid byte match didn't return null", enumInvalid); + + enumInvalid = (QueryType) QueryType.get(list, INVALID_STRING); + assertNull("Invalid string match didn't return null", enumInvalid); + } catch (IllegalArgumentException exception) { + fail("Invalid enum throws IllegalArgumentException."); + } + } + + public void testNullEnum() { + try { + QueryType enumNull = (QueryType) QueryType.get(list, null); + assertNull("Null lookup returns a value", enumNull); + } catch (NullPointerException exception) { + fail("Null string throws NullPointerException."); + } + } + + public void testListEnum() { + Vector enumTestList = new Vector<>(); + enumTestList.add(QueryType.REQUEST); + enumTestList.add(QueryType.RESPONSE); + enumTestList.add(QueryType.NOTIFICATION); + enumTestList.add(QueryType.INVALID_QUERY_TYPE); + + assertTrue("List does not match enum test list.", + list.containsAll(enumTestList) && + enumTestList.containsAll(list)); + + QueryType[] enumValueArray = QueryType.values(); + QueryType[] enumTestArray = {QueryType.REQUEST, QueryType.RESPONSE, QueryType.NOTIFICATION, QueryType.INVALID_QUERY_TYPE}; + assertTrue("Array does not match enum values array.", + Validator.validateQueryTypeArray(enumValueArray, enumTestArray)); + } +} From 380383147d57a6cc2af726c78cb481ed4dee4a74 Mon Sep 17 00:00:00 2001 From: Henigan Date: Fri, 13 Aug 2021 08:06:32 -0400 Subject: [PATCH 09/16] Add QueryErrorCodeTests --- .../com/smartdevicelink/test/Validator.java | 24 +++ .../protocol/enums/QueryErrorCodeTests.java | 189 ++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java index 7eadcbe559..a6ca6385cc 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java @@ -3,6 +3,7 @@ import com.smartdevicelink.managers.file.filetypes.SdlFile; import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; import com.smartdevicelink.protocol.enums.FrameType; +import com.smartdevicelink.protocol.enums.QueryErrorCode; import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.rpc.*; @@ -150,6 +151,29 @@ public static boolean validateQueryTypeArray(QueryType[] array1, QueryType[] arr return true; } + public static boolean validateQueryErrorCodeArray(QueryErrorCode[] array1, QueryErrorCode[] array2) { + + if (array1 == null) { + return (array2 == null); + } + + if (array2 == null) { + return (array1 == null); + } + + if (array1.length != array2.length) { + return false; + } + + for (int i = 0; i < array1.length; i++) { + if (array1[i] != array2[i]) { + return false; + } + } + + return true; + } + public static boolean validateFrameDataControlFrameTypeArray(FrameDataControlFrameType[] array1, FrameDataControlFrameType[] array2) { if (array1 == null) { diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java new file mode 100644 index 0000000000..a3daf76af1 --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java @@ -0,0 +1,189 @@ +package com.smartdevicelink.test.protocol.enums; + +import com.smartdevicelink.protocol.enums.QueryErrorCode; +import com.smartdevicelink.test.Validator; + +import junit.framework.TestCase; + +import java.util.Vector; + +public class QueryErrorCodeTests extends TestCase { + + private Vector list = QueryErrorCode.getList(); + + public void testValidEnums() { + final byte ERROR_SUCCESS_BYTE = (byte) 0x00; + final String ERROR_SUCCESS_STRING = "ERROR_SUCCESS"; + + final byte ERROR_INVALID_QUERY_SIZE_BYTE = (byte) 0x01; + final String ERROR_INVALID_QUERY_SIZE_STRING = "ERROR_INVALID_QUERY_SIZE"; + + final byte ERROR_INVALID_QUERY_ID_BYTE = (byte) 0x02; + final String ERROR_INVALID_QUERY_ID_STRING = "ERROR_INVALID_QUERY_ID"; + + final byte ERROR_NOT_SUPPORTED_BYTE = (byte) 0x03; + final String ERROR_NOT_SUPPORTED_STRING = "ERROR_NOT_SUPPORTED"; + + final byte ERROR_SERVICE_ALREADY_PROTECTED_BYTE = (byte) 0x04; + final String ERROR_SERVICE_ALREADY_PROTECTED_STRING = "ERROR_SERVICE_ALREADY_PROTECTED"; + + final byte ERROR_SERVICE_NOT_PROTECTED_BYTE = (byte) 0x05; + final String ERROR_SERVICE_NOT_PROTECTED_STRING = "ERROR_SERVICE_NOT_PROTECTED"; + + final byte ERROR_DECRYPTION_FAILED_BYTE = (byte) 0x06; + final String ERROR_DECRYPTION_FAILED_STRING = "ERROR_DECRYPTION_FAILED"; + + final byte ERROR_ENCRYPTION_FAILED_BYTE = (byte) 0x07; + final String ERROR_ENCRYPTION_FAILED_STRING = "ERROR_ENCRYPTION_FAILED"; + + final byte ERROR_SSL_INVALID_DATA_BYTE = (byte) 0x08; + final String ERROR_SSL_INVALID_DATA_STRING = "ERROR_SSL_INVALID_DATA"; + + final byte ERROR_HANDSHAKE_FAILED_BYTE = (byte) 0x09; + final String ERROR_HANDSHAKE_FAILED_STRING = "ERROR_HANDSHAKE_FAILED"; + + final byte INVALID_CERT_BYTE = (byte) 0x0A; + final String INVALID_CERT_STRING = "INVALID_CERT"; + + final byte EXPIRED_CERT_BYTE = (byte) 0x0B; + final String EXPIRED_CERT_STRING = "EXPIRED_CERT"; + + final byte ERROR_INTERNAL_BYTE = (byte) 0xFF; + final String ERROR_INTERNAL_STRING = "ERROR_INTERNAL"; + + final byte ERROR_UNKNOWN_INTERNAL_ERROR_BYTE = (byte) 0xFE; + final String ERROR_UNKNOWN_INTERNAL_ERROR_STRING = "ERROR_UNKNOWN_INTERNAL_ERROR"; + + try { + assertNotNull("QueryErrorCode list returned null", list); + + QueryErrorCode enumSuccess = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SUCCESS_BYTE); + QueryErrorCode enumInvalidQuerySize = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_BYTE); + QueryErrorCode enumInvalidQueryID = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_BYTE); + QueryErrorCode enumNotSupported = (QueryErrorCode) QueryErrorCode.get(list, ERROR_NOT_SUPPORTED_BYTE); + QueryErrorCode enumServiceAlreadyProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_BYTE); + QueryErrorCode enumServiceNotProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_BYTE); + QueryErrorCode enumDecryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_BYTE); + QueryErrorCode enumEncryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_BYTE); + QueryErrorCode enumSSLInvalidData = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_BYTE); + QueryErrorCode enumHandshakeFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_BYTE); + QueryErrorCode enumInvalidCert = (QueryErrorCode) QueryErrorCode.get(list, INVALID_CERT_BYTE); + QueryErrorCode enumExpiredCert = (QueryErrorCode) QueryErrorCode.get(list, EXPIRED_CERT_BYTE); + QueryErrorCode enumInternal = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INTERNAL_BYTE); + QueryErrorCode enumUnknownInternalError = (QueryErrorCode) QueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_BYTE); + + assertNotNull("Success byte match returned null", enumSuccess); + assertNotNull("Invalid Query Size byte match returned null", enumInvalidQuerySize); + assertNotNull("Invalid Query ID byte match returned null", enumInvalidQueryID); + assertNotNull("Not Supported byte match returned null", enumNotSupported); + assertNotNull("Service Already Protected byte match returned null", enumServiceAlreadyProtected); + assertNotNull("Service Not Protected byte match returned null", enumServiceNotProtected); + assertNotNull("Decryption Failed byte match returned null", enumDecryptionFailed); + assertNotNull("Encryption Failed byte match returned null", enumEncryptionFailed); + assertNotNull("SSL Invalid Data byte match returned null", enumSSLInvalidData); + assertNotNull("Handshake Failed byte match returned null", enumHandshakeFailed); + assertNotNull("Invalid Cert byte match returned null", enumInvalidCert); + assertNotNull("Expired Cert byte match returned null", enumExpiredCert); + assertNotNull("Internal byte match returned null", enumInternal); + assertNotNull("Unknown Internal byte match returned null", enumUnknownInternalError); + + enumSuccess = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SUCCESS_STRING); + enumInvalidQuerySize = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_STRING); + enumInvalidQueryID = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_STRING); + enumNotSupported = (QueryErrorCode) QueryErrorCode.get(list, ERROR_NOT_SUPPORTED_STRING); + enumServiceAlreadyProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_STRING); + enumServiceNotProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_STRING); + enumDecryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_STRING); + enumEncryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_STRING); + enumSSLInvalidData = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_STRING); + enumHandshakeFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_STRING); + enumInvalidCert = (QueryErrorCode) QueryErrorCode.get(list, INVALID_CERT_STRING); + enumExpiredCert = (QueryErrorCode) QueryErrorCode.get(list, EXPIRED_CERT_STRING); + enumInternal = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INTERNAL_STRING); + enumUnknownInternalError = (QueryErrorCode) QueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_STRING); + + assertNotNull("Success string match returned null", enumSuccess); + assertNotNull("Invalid Query Size string match returned null", enumInvalidQuerySize); + assertNotNull("Invalid Query ID string match returned null", enumInvalidQueryID); + assertNotNull("Not Supported string match returned null", enumNotSupported); + assertNotNull("Service Already Protected string match returned null", enumServiceAlreadyProtected); + assertNotNull("Service Not Protected string match returned null", enumServiceNotProtected); + assertNotNull("Decryption Failed string match returned null", enumDecryptionFailed); + assertNotNull("Encryption Failed string match returned null", enumEncryptionFailed); + assertNotNull("SSL Invalid Data string match returned null", enumSSLInvalidData); + assertNotNull("Handshake Failed string match returned null", enumHandshakeFailed); + assertNotNull("Invalid Cert string match returned null", enumInvalidCert); + assertNotNull("Expired Cert string match returned null", enumExpiredCert); + assertNotNull("Internal string match returned null", enumInternal); + assertNotNull("Unknown Internal string match returned null", enumUnknownInternalError); + } catch (NullPointerException exception) { + fail("Null enum list throws NullPointerException."); + } + } + + public void testInvalidEnum() { + final byte INVALID_BYTE = (byte) 0xAB; + final String INVALID_STRING = "Invalid"; + + try { + QueryErrorCode enumInvalid = (QueryErrorCode) QueryErrorCode.get(list, INVALID_BYTE); + assertNull("Invalid byte match didn't return null", enumInvalid); + + enumInvalid = (QueryErrorCode) QueryErrorCode.get(list, INVALID_STRING); + assertNull("Invalid byte match didn't return null", enumInvalid); + } catch (IllegalArgumentException exception) { + fail("Invalid enum throws IllegalArgumentException."); + } + } + + public void testNullEnum() { + try { + QueryErrorCode enumNull = (QueryErrorCode) QueryErrorCode.get(list, null); + assertNull("Null lookup returns a value", enumNull); + } catch (NullPointerException exception) { + fail("Null string throws NullPointerException."); + } + } + + public void testListEnum() { + Vector enumTestList = new Vector<>(); + enumTestList.add(QueryErrorCode.ERROR_SUCCESS); + enumTestList.add(QueryErrorCode.ERROR_INVALID_QUERY_SIZE); + enumTestList.add(QueryErrorCode.ERROR_INVALID_QUERY_ID); + enumTestList.add(QueryErrorCode.ERROR_NOT_SUPPORTED); + enumTestList.add(QueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED); + enumTestList.add(QueryErrorCode.ERROR_SERVICE_NOT_PROTECTED); + enumTestList.add(QueryErrorCode.ERROR_DECRYPTION_FAILED); + enumTestList.add(QueryErrorCode.ERROR_ENCRYPTION_FAILED); + enumTestList.add(QueryErrorCode.ERROR_SSL_INVALID_DATA); + enumTestList.add(QueryErrorCode.ERROR_HANDSHAKE_FAILED); + enumTestList.add(QueryErrorCode.INVALID_CERT); + enumTestList.add(QueryErrorCode.EXPIRED_CERT); + enumTestList.add(QueryErrorCode.ERROR_INTERNAL); + enumTestList.add(QueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR); + + assertTrue("List does not match enum test list.", + list.containsAll(enumTestList) && + enumTestList.containsAll(list)); + + QueryErrorCode[] enumValueArray = QueryErrorCode.values(); + QueryErrorCode[] enumTestArray = { + QueryErrorCode.ERROR_SUCCESS, + QueryErrorCode.ERROR_INVALID_QUERY_SIZE, + QueryErrorCode.ERROR_INVALID_QUERY_ID, + QueryErrorCode.ERROR_NOT_SUPPORTED, + QueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED, + QueryErrorCode.ERROR_SERVICE_NOT_PROTECTED, + QueryErrorCode.ERROR_DECRYPTION_FAILED, + QueryErrorCode.ERROR_ENCRYPTION_FAILED, + QueryErrorCode.ERROR_SSL_INVALID_DATA, + QueryErrorCode.ERROR_HANDSHAKE_FAILED, + QueryErrorCode.INVALID_CERT, + QueryErrorCode.EXPIRED_CERT, + QueryErrorCode.ERROR_INTERNAL, + QueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR + }; + assertTrue("Array does not match enum values array.", + Validator.validateQueryErrorCodeArray(enumValueArray, enumTestArray)); + } +} From 197937e93d40b1447d250d3a6e45cc51179288c7 Mon Sep 17 00:00:00 2001 From: Henigan Date: Fri, 13 Aug 2021 08:25:48 -0400 Subject: [PATCH 10/16] Add QueryID Tests --- .../com/smartdevicelink/test/Validator.java | 24 +++++ .../test/protocol/enums/QueryIDTests.java | 91 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java index a6ca6385cc..fd2e6fc30f 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java @@ -4,6 +4,7 @@ import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; import com.smartdevicelink.protocol.enums.FrameType; import com.smartdevicelink.protocol.enums.QueryErrorCode; +import com.smartdevicelink.protocol.enums.QueryID; import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.rpc.*; @@ -151,6 +152,29 @@ public static boolean validateQueryTypeArray(QueryType[] array1, QueryType[] arr return true; } + public static boolean validateQueryIDArray(QueryID[] array1, QueryID[] array2) { + + if (array1 == null) { + return (array2 == null); + } + + if (array2 == null) { + return (array1 == null); + } + + if (array1.length != array2.length) { + return false; + } + + for (int i = 0; i < array1.length; i++) { + if (array1[i] != array2[i]) { + return false; + } + } + + return true; + } + public static boolean validateQueryErrorCodeArray(QueryErrorCode[] array1, QueryErrorCode[] array2) { if (array1 == null) { diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java new file mode 100644 index 0000000000..90118eb178 --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java @@ -0,0 +1,91 @@ +package com.smartdevicelink.test.protocol.enums; + +import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.test.Validator; + +import junit.framework.TestCase; + +import java.util.Vector; + +public class QueryIDTests extends TestCase { + + private Vector list = QueryID.getList(); + + public void testValidEnums() { + final byte[] SEND_HANDSHAKE_DATA_BYTES = {(byte) 0x00, (byte) 0x00, (byte) 0x01}; + final String SEND_HANDSHAKE_DATA_STRING = "SEND_HANDSHAKE_DATA"; + + final byte[] SEND_INTERNAL_ERROR_BYTES = {(byte) 0x00, (byte) 0x00, (byte) 0x02}; + final String SEND_INTERNAL_ERROR_STRING = "SEND_INTERNAL_ERROR"; + + final byte[] INVALID_QUERY_ID_BYTES = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + final String INVALID_QUERY_ID_STRING = "INVALID_QUERY_ID"; + + try { + assertNotNull("QueryID list returned null", list); + + QueryID enumHandshakeData = (QueryID) QueryID.get(list, SEND_HANDSHAKE_DATA_BYTES); + QueryID enumInternalError = (QueryID) QueryID.get(list, SEND_INTERNAL_ERROR_BYTES); + QueryID enumInvalidQueryId = (QueryID) QueryID.get(list, INVALID_QUERY_ID_BYTES); + + assertNotNull("Send Handshake Data byte match returned null", enumHandshakeData); + assertNotNull("Send Internal Error byte match returned null", enumInternalError); + assertNotNull("Send Invalid QueryID byte match returned null", enumInvalidQueryId); + + enumHandshakeData = (QueryID) QueryID.get(list, SEND_HANDSHAKE_DATA_STRING); + enumInternalError = (QueryID) QueryID.get(list, SEND_INTERNAL_ERROR_STRING); + enumInvalidQueryId = (QueryID) QueryID.get(list, INVALID_QUERY_ID_STRING); + + assertNotNull("Send Handshake Data string match returned null", enumHandshakeData); + assertNotNull("Send Internal Error string match returned null", enumInternalError); + assertNotNull("Send Invalid QueryID string match returned null", enumInvalidQueryId); + } catch(NullPointerException exception) { + fail("Null enum list throws NullPointerException."); + } + } + + public void testInvalidEnum() { + + final byte[] INVALID_BYTE_ARRAY = {(byte) 0xAB, (byte) 0xAB, (byte) 0xAB}; + final String INVALID_STRING = "Invalid"; + + try { + QueryID enumInvalid = (QueryID) QueryID.get(list, INVALID_BYTE_ARRAY); + assertNull("Invalid byte[] match didn't return null", enumInvalid); + + enumInvalid = (QueryID) QueryID.get(list, INVALID_STRING); + assertNull("Invalid string match didn't return null", enumInvalid); + } catch (IllegalArgumentException exception) { + fail("Invalid enum throws IllegalArgumentException."); + } + } + + public void testNullEnum() { + try { + QueryID enumNull = (QueryID) QueryID.get(list, (String) null); + assertNull("Null lookup returns a null string value", enumNull); + + enumNull = (QueryID) QueryID.get(list, (byte[]) null); + assertNull("Null lookup returns a null byte[] value", enumNull); + }catch (NullPointerException exception) { + fail("Null string throws NullPointerException."); + } + } + + public void testListEnum() { + Vector enumTestList = new Vector<>(); + enumTestList.add(QueryID.SEND_HANDSHAKE_DATA); + enumTestList.add(QueryID.SEND_INTERNAL_ERROR); + enumTestList.add(QueryID.INVALID_QUERY_ID); + + assertTrue("List does not match enum test list.", + list.containsAll(enumTestList) && + enumTestList.containsAll(list)); + + QueryID[] enumValueArray = QueryID.values(); + QueryID[] enumTestArray = {QueryID.SEND_HANDSHAKE_DATA, QueryID.SEND_INTERNAL_ERROR, QueryID.INVALID_QUERY_ID}; + assertTrue("Array does not match enum values array.", + Validator.validateQueryIDArray(enumValueArray, enumTestArray)); + } + +} From a9c8fd82b8bca1b9c623dc19f84f33eff11bce61 Mon Sep 17 00:00:00 2001 From: Henigan Date: Fri, 13 Aug 2021 09:11:16 -0400 Subject: [PATCH 11/16] Fix bulkData and add null check --- .../com/smartdevicelink/protocol/SecurityQueryPayload.java | 3 +-- .../main/java/com/smartdevicelink/session/BaseSdlSession.java | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index 67f45171e3..c2553482b6 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -55,11 +55,10 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { byte[] _bulkData; if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { _bulkData = new byte[binHeader.length - _jsonSize - 12 - 1]; - System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length - 1); } else { _bulkData = new byte[binHeader.length - _jsonSize - 12]; - System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length); } + System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length); msg.setBulkData(_bulkData); } diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 6be68e5069..276e29ae22 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -192,6 +192,10 @@ protected void processControlService(ProtocolMessage msg) { return; SecurityQueryPayload receivedHeader = SecurityQueryPayload.parseBinaryQueryHeader(msg.getData().clone()); + if (receivedHeader == null) { + DebugTool.logError(TAG, "Malformed Security Query Header"); + return; + } int iLen = msg.getData().length - 12; byte[] data = new byte[iLen]; From 1f4cfcc589dc892bed0e8d000a3b3b722508ff7b Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 18 Aug 2021 11:52:38 -0400 Subject: [PATCH 12/16] Code Review Fixes --- .../com/smartdevicelink/protocol/SdlPacketFactory.java | 2 -- .../smartdevicelink/protocol/enums/QueryErrorCode.java | 3 +++ .../java/com/smartdevicelink/protocol/enums/QueryID.java | 9 ++++++--- .../com/smartdevicelink/protocol/enums/QueryType.java | 3 +++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java index 98b1e35da2..364dc4e305 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SdlPacketFactory.java @@ -35,8 +35,6 @@ import com.smartdevicelink.protocol.enums.ControlFrameTags; import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.util.BitConverter; diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java index 269c5021e1..83f7d85529 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java @@ -1,9 +1,12 @@ package com.smartdevicelink.protocol.enums; +import androidx.annotation.RestrictTo; + import com.smartdevicelink.util.ByteEnumer; import java.util.Vector; +@RestrictTo(RestrictTo.Scope.LIBRARY) public class QueryErrorCode extends ByteEnumer { private static final Vector theList = new Vector<>(); diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java index e24481340a..da1e3fab36 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java @@ -1,5 +1,7 @@ package com.smartdevicelink.protocol.enums; +import androidx.annotation.RestrictTo; + import com.smartdevicelink.util.BitConverter; import java.util.Arrays; @@ -7,6 +9,7 @@ import java.util.Objects; import java.util.Vector; +@RestrictTo(RestrictTo.Scope.LIBRARY) public class QueryID { private static final Vector theList = new Vector<>(); @@ -15,9 +18,9 @@ public static Vector getList() { return theList; } - private static final byte[] sendHandshakeDataByteArray= {(byte) 0x00, (byte) 0x00, (byte) 0x01}; - private static final byte[] sendInternalErrorByteArray= {(byte) 0x00, (byte) 0x00, (byte) 0x02}; - private static final byte[] invalidQueryIdByteArray= {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; + private static final byte[] sendHandshakeDataByteArray = {(byte) 0x00, (byte) 0x00, (byte) 0x01}; + private static final byte[] sendInternalErrorByteArray = {(byte) 0x00, (byte) 0x00, (byte) 0x02}; + private static final byte[] invalidQueryIdByteArray = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; public final static QueryID SEND_HANDSHAKE_DATA = new QueryID(sendHandshakeDataByteArray, "SEND_HANDSHAKE_DATA"); public final static QueryID SEND_INTERNAL_ERROR = new QueryID(sendInternalErrorByteArray, "SEND_INTERNAL_ERROR"); public final static QueryID INVALID_QUERY_ID = new QueryID(invalidQueryIdByteArray, "INVALID_QUERY_ID"); diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java index 543fb56c6e..ce26e005bc 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java @@ -1,9 +1,12 @@ package com.smartdevicelink.protocol.enums; +import androidx.annotation.RestrictTo; + import com.smartdevicelink.util.ByteEnumer; import java.util.Vector; +@RestrictTo(RestrictTo.Scope.LIBRARY) public class QueryType extends ByteEnumer { private static final Vector theList = new Vector<>(); From 7a9911edb713a0e96cc4fc30d593800fb5975ae9 Mon Sep 17 00:00:00 2001 From: Henigan Date: Wed, 1 Sep 2021 15:20:28 -0400 Subject: [PATCH 13/16] Align to latest iOS PR --- .../com/smartdevicelink/test/Validator.java | 13 +- .../protocol/SecurityQueryPayloadTests.java | 27 +-- .../protocol/enums/QueryErrorCodeTests.java | 189 ------------------ .../enums/SecurityQueryErrorCodeTests.java | 189 ++++++++++++++++++ ...IDTests.java => SecurityQueryIDTests.java} | 42 ++-- ...Tests.java => SecurityQueryTypeTests.java} | 46 ++--- .../protocol/SecurityQueryPayload.java | 79 +++++--- .../protocol/enums/QueryErrorCode.java | 61 ------ .../protocol/enums/QueryType.java | 41 ---- .../enums/SecurityQueryErrorCode.java | 61 ++++++ .../{QueryID.java => SecurityQueryID.java} | 34 ++-- .../protocol/enums/SecurityQueryType.java | 41 ++++ .../session/BaseSdlSession.java | 38 ++-- 13 files changed, 439 insertions(+), 422 deletions(-) delete mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java create mode 100644 android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryErrorCodeTests.java rename android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/{QueryIDTests.java => SecurityQueryIDTests.java} (58%) rename android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/{QueryTypeTests.java => SecurityQueryTypeTests.java} (55%) delete mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java delete mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java create mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryErrorCode.java rename base/src/main/java/com/smartdevicelink/protocol/enums/{QueryID.java => SecurityQueryID.java} (62%) create mode 100644 base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryType.java diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java index fd2e6fc30f..094b297949 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/Validator.java @@ -3,9 +3,9 @@ import com.smartdevicelink.managers.file.filetypes.SdlFile; import com.smartdevicelink.protocol.enums.FrameDataControlFrameType; import com.smartdevicelink.protocol.enums.FrameType; -import com.smartdevicelink.protocol.enums.QueryErrorCode; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode; +import com.smartdevicelink.protocol.enums.SecurityQueryID; +import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.rpc.*; import com.smartdevicelink.proxy.rpc.enums.AppServiceType; @@ -13,7 +13,6 @@ import com.smartdevicelink.proxy.rpc.enums.FileType; import com.smartdevicelink.proxy.rpc.enums.HMILevel; import com.smartdevicelink.proxy.rpc.enums.HmiZoneCapabilities; -import com.smartdevicelink.proxy.rpc.enums.KeyboardLayout; import com.smartdevicelink.proxy.rpc.enums.PRNDL; import com.smartdevicelink.proxy.rpc.enums.PrerecordedSpeech; import com.smartdevicelink.proxy.rpc.enums.SpeechCapabilities; @@ -129,7 +128,7 @@ public static boolean validateSessionTypeArray(SessionType[] array1, SessionType return true; } - public static boolean validateQueryTypeArray(QueryType[] array1, QueryType[] array2) { + public static boolean validateQueryTypeArray(SecurityQueryType[] array1, SecurityQueryType[] array2) { if (array1 == null) { return (array2 == null); @@ -152,7 +151,7 @@ public static boolean validateQueryTypeArray(QueryType[] array1, QueryType[] arr return true; } - public static boolean validateQueryIDArray(QueryID[] array1, QueryID[] array2) { + public static boolean validateQueryIDArray(SecurityQueryID[] array1, SecurityQueryID[] array2) { if (array1 == null) { return (array2 == null); @@ -175,7 +174,7 @@ public static boolean validateQueryIDArray(QueryID[] array1, QueryID[] array2) { return true; } - public static boolean validateQueryErrorCodeArray(QueryErrorCode[] array1, QueryErrorCode[] array2) { + public static boolean validateQueryErrorCodeArray(SecurityQueryErrorCode[] array1, SecurityQueryErrorCode[] array2) { if (array1 == null) { return (array2 == null); diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java index cc0cd914a5..8c0c360417 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java @@ -3,8 +3,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import com.smartdevicelink.protocol.SecurityQueryPayload; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.protocol.enums.SecurityQueryID; +import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.util.BitConverter; import org.junit.Test; @@ -22,8 +22,8 @@ public class SecurityQueryPayloadTests { public static SecurityQueryPayload createDummyBqh() { SecurityQueryPayload bqh = new SecurityQueryPayload(); bqh.setCorrelationID(123); - bqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); - bqh.setQueryType(QueryType.REQUEST); + bqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); + bqh.setQueryType(SecurityQueryType.REQUEST); bqh.setBulkData(null); bqh.setJsonSize(0); return bqh; @@ -54,8 +54,8 @@ public void testCorrectParsing() { array[11] = 0; SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(array); - assertEquals(parsedBqh.getQueryType(), QueryType.REQUEST); - assertEquals(parsedBqh.getQueryID(), QueryID.SEND_INTERNAL_ERROR); + assertEquals(parsedBqh.getQueryType(), SecurityQueryType.REQUEST); + assertEquals(parsedBqh.getQueryID(), SecurityQueryID.SEND_INTERNAL_ERROR); assertEquals(parsedBqh.getCorrelationID(), 3); assertEquals(parsedBqh.getJsonSize(), 0); } @@ -63,16 +63,16 @@ public void testCorrectParsing() { @Test public void testCorrectHeaderAssembly() { SecurityQueryPayload dummyBqh = new SecurityQueryPayload(); - dummyBqh.setQueryType(QueryType.REQUEST); - dummyBqh.setQueryID(QueryID.SEND_HANDSHAKE_DATA); + dummyBqh.setQueryType(SecurityQueryType.REQUEST); + dummyBqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); dummyBqh.setCorrelationID(3); dummyBqh.setJsonSize(0); byte[] assembledHeader = dummyBqh.assembleHeaderBytes(); - assertEquals(dummyBqh.getQueryType(), QueryType.valueOf(assembledHeader[0])); + assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0])); byte[] queryIDFromHeader = new byte[3]; System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3); - assertEquals(dummyBqh.getQueryID(), QueryID.valueOf(queryIDFromHeader)); + assertEquals(dummyBqh.getQueryID(), SecurityQueryID.valueOf(queryIDFromHeader)); assertEquals(dummyBqh.getCorrelationID(), BitConverter.intFromByteArray(assembledHeader, 4)); assertEquals(dummyBqh.getJsonSize(), BitConverter.intFromByteArray(assembledHeader, 8)); } @@ -98,15 +98,10 @@ public void testAssemblyAndParse() { @Test public void testCorruptHeader() { SecurityQueryPayload bqh = createDummyBqh(); - bqh.setJsonSize(5); - bqh.setJsonData(new byte[5]); - bqh.setJsonSize(Integer.MAX_VALUE); - - assertNotEquals(bqh.getJsonData().length, bqh.getJsonSize()); byte[] bqhBytes = bqh.assembleHeaderBytes(); - assertNull(safeParse(bqhBytes)); + assertNotNull(safeParse(bqhBytes)); int size = bqhBytes.length; for (int i = 0; i < size; i++) { diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java deleted file mode 100644 index a3daf76af1..0000000000 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryErrorCodeTests.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.smartdevicelink.test.protocol.enums; - -import com.smartdevicelink.protocol.enums.QueryErrorCode; -import com.smartdevicelink.test.Validator; - -import junit.framework.TestCase; - -import java.util.Vector; - -public class QueryErrorCodeTests extends TestCase { - - private Vector list = QueryErrorCode.getList(); - - public void testValidEnums() { - final byte ERROR_SUCCESS_BYTE = (byte) 0x00; - final String ERROR_SUCCESS_STRING = "ERROR_SUCCESS"; - - final byte ERROR_INVALID_QUERY_SIZE_BYTE = (byte) 0x01; - final String ERROR_INVALID_QUERY_SIZE_STRING = "ERROR_INVALID_QUERY_SIZE"; - - final byte ERROR_INVALID_QUERY_ID_BYTE = (byte) 0x02; - final String ERROR_INVALID_QUERY_ID_STRING = "ERROR_INVALID_QUERY_ID"; - - final byte ERROR_NOT_SUPPORTED_BYTE = (byte) 0x03; - final String ERROR_NOT_SUPPORTED_STRING = "ERROR_NOT_SUPPORTED"; - - final byte ERROR_SERVICE_ALREADY_PROTECTED_BYTE = (byte) 0x04; - final String ERROR_SERVICE_ALREADY_PROTECTED_STRING = "ERROR_SERVICE_ALREADY_PROTECTED"; - - final byte ERROR_SERVICE_NOT_PROTECTED_BYTE = (byte) 0x05; - final String ERROR_SERVICE_NOT_PROTECTED_STRING = "ERROR_SERVICE_NOT_PROTECTED"; - - final byte ERROR_DECRYPTION_FAILED_BYTE = (byte) 0x06; - final String ERROR_DECRYPTION_FAILED_STRING = "ERROR_DECRYPTION_FAILED"; - - final byte ERROR_ENCRYPTION_FAILED_BYTE = (byte) 0x07; - final String ERROR_ENCRYPTION_FAILED_STRING = "ERROR_ENCRYPTION_FAILED"; - - final byte ERROR_SSL_INVALID_DATA_BYTE = (byte) 0x08; - final String ERROR_SSL_INVALID_DATA_STRING = "ERROR_SSL_INVALID_DATA"; - - final byte ERROR_HANDSHAKE_FAILED_BYTE = (byte) 0x09; - final String ERROR_HANDSHAKE_FAILED_STRING = "ERROR_HANDSHAKE_FAILED"; - - final byte INVALID_CERT_BYTE = (byte) 0x0A; - final String INVALID_CERT_STRING = "INVALID_CERT"; - - final byte EXPIRED_CERT_BYTE = (byte) 0x0B; - final String EXPIRED_CERT_STRING = "EXPIRED_CERT"; - - final byte ERROR_INTERNAL_BYTE = (byte) 0xFF; - final String ERROR_INTERNAL_STRING = "ERROR_INTERNAL"; - - final byte ERROR_UNKNOWN_INTERNAL_ERROR_BYTE = (byte) 0xFE; - final String ERROR_UNKNOWN_INTERNAL_ERROR_STRING = "ERROR_UNKNOWN_INTERNAL_ERROR"; - - try { - assertNotNull("QueryErrorCode list returned null", list); - - QueryErrorCode enumSuccess = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SUCCESS_BYTE); - QueryErrorCode enumInvalidQuerySize = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_BYTE); - QueryErrorCode enumInvalidQueryID = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_BYTE); - QueryErrorCode enumNotSupported = (QueryErrorCode) QueryErrorCode.get(list, ERROR_NOT_SUPPORTED_BYTE); - QueryErrorCode enumServiceAlreadyProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_BYTE); - QueryErrorCode enumServiceNotProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_BYTE); - QueryErrorCode enumDecryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_BYTE); - QueryErrorCode enumEncryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_BYTE); - QueryErrorCode enumSSLInvalidData = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_BYTE); - QueryErrorCode enumHandshakeFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_BYTE); - QueryErrorCode enumInvalidCert = (QueryErrorCode) QueryErrorCode.get(list, INVALID_CERT_BYTE); - QueryErrorCode enumExpiredCert = (QueryErrorCode) QueryErrorCode.get(list, EXPIRED_CERT_BYTE); - QueryErrorCode enumInternal = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INTERNAL_BYTE); - QueryErrorCode enumUnknownInternalError = (QueryErrorCode) QueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_BYTE); - - assertNotNull("Success byte match returned null", enumSuccess); - assertNotNull("Invalid Query Size byte match returned null", enumInvalidQuerySize); - assertNotNull("Invalid Query ID byte match returned null", enumInvalidQueryID); - assertNotNull("Not Supported byte match returned null", enumNotSupported); - assertNotNull("Service Already Protected byte match returned null", enumServiceAlreadyProtected); - assertNotNull("Service Not Protected byte match returned null", enumServiceNotProtected); - assertNotNull("Decryption Failed byte match returned null", enumDecryptionFailed); - assertNotNull("Encryption Failed byte match returned null", enumEncryptionFailed); - assertNotNull("SSL Invalid Data byte match returned null", enumSSLInvalidData); - assertNotNull("Handshake Failed byte match returned null", enumHandshakeFailed); - assertNotNull("Invalid Cert byte match returned null", enumInvalidCert); - assertNotNull("Expired Cert byte match returned null", enumExpiredCert); - assertNotNull("Internal byte match returned null", enumInternal); - assertNotNull("Unknown Internal byte match returned null", enumUnknownInternalError); - - enumSuccess = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SUCCESS_STRING); - enumInvalidQuerySize = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_STRING); - enumInvalidQueryID = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_STRING); - enumNotSupported = (QueryErrorCode) QueryErrorCode.get(list, ERROR_NOT_SUPPORTED_STRING); - enumServiceAlreadyProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_STRING); - enumServiceNotProtected = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_STRING); - enumDecryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_STRING); - enumEncryptionFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_STRING); - enumSSLInvalidData = (QueryErrorCode) QueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_STRING); - enumHandshakeFailed = (QueryErrorCode) QueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_STRING); - enumInvalidCert = (QueryErrorCode) QueryErrorCode.get(list, INVALID_CERT_STRING); - enumExpiredCert = (QueryErrorCode) QueryErrorCode.get(list, EXPIRED_CERT_STRING); - enumInternal = (QueryErrorCode) QueryErrorCode.get(list, ERROR_INTERNAL_STRING); - enumUnknownInternalError = (QueryErrorCode) QueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_STRING); - - assertNotNull("Success string match returned null", enumSuccess); - assertNotNull("Invalid Query Size string match returned null", enumInvalidQuerySize); - assertNotNull("Invalid Query ID string match returned null", enumInvalidQueryID); - assertNotNull("Not Supported string match returned null", enumNotSupported); - assertNotNull("Service Already Protected string match returned null", enumServiceAlreadyProtected); - assertNotNull("Service Not Protected string match returned null", enumServiceNotProtected); - assertNotNull("Decryption Failed string match returned null", enumDecryptionFailed); - assertNotNull("Encryption Failed string match returned null", enumEncryptionFailed); - assertNotNull("SSL Invalid Data string match returned null", enumSSLInvalidData); - assertNotNull("Handshake Failed string match returned null", enumHandshakeFailed); - assertNotNull("Invalid Cert string match returned null", enumInvalidCert); - assertNotNull("Expired Cert string match returned null", enumExpiredCert); - assertNotNull("Internal string match returned null", enumInternal); - assertNotNull("Unknown Internal string match returned null", enumUnknownInternalError); - } catch (NullPointerException exception) { - fail("Null enum list throws NullPointerException."); - } - } - - public void testInvalidEnum() { - final byte INVALID_BYTE = (byte) 0xAB; - final String INVALID_STRING = "Invalid"; - - try { - QueryErrorCode enumInvalid = (QueryErrorCode) QueryErrorCode.get(list, INVALID_BYTE); - assertNull("Invalid byte match didn't return null", enumInvalid); - - enumInvalid = (QueryErrorCode) QueryErrorCode.get(list, INVALID_STRING); - assertNull("Invalid byte match didn't return null", enumInvalid); - } catch (IllegalArgumentException exception) { - fail("Invalid enum throws IllegalArgumentException."); - } - } - - public void testNullEnum() { - try { - QueryErrorCode enumNull = (QueryErrorCode) QueryErrorCode.get(list, null); - assertNull("Null lookup returns a value", enumNull); - } catch (NullPointerException exception) { - fail("Null string throws NullPointerException."); - } - } - - public void testListEnum() { - Vector enumTestList = new Vector<>(); - enumTestList.add(QueryErrorCode.ERROR_SUCCESS); - enumTestList.add(QueryErrorCode.ERROR_INVALID_QUERY_SIZE); - enumTestList.add(QueryErrorCode.ERROR_INVALID_QUERY_ID); - enumTestList.add(QueryErrorCode.ERROR_NOT_SUPPORTED); - enumTestList.add(QueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED); - enumTestList.add(QueryErrorCode.ERROR_SERVICE_NOT_PROTECTED); - enumTestList.add(QueryErrorCode.ERROR_DECRYPTION_FAILED); - enumTestList.add(QueryErrorCode.ERROR_ENCRYPTION_FAILED); - enumTestList.add(QueryErrorCode.ERROR_SSL_INVALID_DATA); - enumTestList.add(QueryErrorCode.ERROR_HANDSHAKE_FAILED); - enumTestList.add(QueryErrorCode.INVALID_CERT); - enumTestList.add(QueryErrorCode.EXPIRED_CERT); - enumTestList.add(QueryErrorCode.ERROR_INTERNAL); - enumTestList.add(QueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR); - - assertTrue("List does not match enum test list.", - list.containsAll(enumTestList) && - enumTestList.containsAll(list)); - - QueryErrorCode[] enumValueArray = QueryErrorCode.values(); - QueryErrorCode[] enumTestArray = { - QueryErrorCode.ERROR_SUCCESS, - QueryErrorCode.ERROR_INVALID_QUERY_SIZE, - QueryErrorCode.ERROR_INVALID_QUERY_ID, - QueryErrorCode.ERROR_NOT_SUPPORTED, - QueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED, - QueryErrorCode.ERROR_SERVICE_NOT_PROTECTED, - QueryErrorCode.ERROR_DECRYPTION_FAILED, - QueryErrorCode.ERROR_ENCRYPTION_FAILED, - QueryErrorCode.ERROR_SSL_INVALID_DATA, - QueryErrorCode.ERROR_HANDSHAKE_FAILED, - QueryErrorCode.INVALID_CERT, - QueryErrorCode.EXPIRED_CERT, - QueryErrorCode.ERROR_INTERNAL, - QueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR - }; - assertTrue("Array does not match enum values array.", - Validator.validateQueryErrorCodeArray(enumValueArray, enumTestArray)); - } -} diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryErrorCodeTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryErrorCodeTests.java new file mode 100644 index 0000000000..0b6cd3f616 --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryErrorCodeTests.java @@ -0,0 +1,189 @@ +package com.smartdevicelink.test.protocol.enums; + +import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode; +import com.smartdevicelink.test.Validator; + +import junit.framework.TestCase; + +import java.util.Vector; + +public class SecurityQueryErrorCodeTests extends TestCase { + + private Vector list = SecurityQueryErrorCode.getList(); + + public void testValidEnums() { + final byte ERROR_SUCCESS_BYTE = (byte) 0x00; + final String ERROR_SUCCESS_STRING = "ERROR_SUCCESS"; + + final byte ERROR_INVALID_QUERY_SIZE_BYTE = (byte) 0x01; + final String ERROR_INVALID_QUERY_SIZE_STRING = "ERROR_INVALID_QUERY_SIZE"; + + final byte ERROR_INVALID_QUERY_ID_BYTE = (byte) 0x02; + final String ERROR_INVALID_QUERY_ID_STRING = "ERROR_INVALID_QUERY_ID"; + + final byte ERROR_NOT_SUPPORTED_BYTE = (byte) 0x03; + final String ERROR_NOT_SUPPORTED_STRING = "ERROR_NOT_SUPPORTED"; + + final byte ERROR_SERVICE_ALREADY_PROTECTED_BYTE = (byte) 0x04; + final String ERROR_SERVICE_ALREADY_PROTECTED_STRING = "ERROR_SERVICE_ALREADY_PROTECTED"; + + final byte ERROR_SERVICE_NOT_PROTECTED_BYTE = (byte) 0x05; + final String ERROR_SERVICE_NOT_PROTECTED_STRING = "ERROR_SERVICE_NOT_PROTECTED"; + + final byte ERROR_DECRYPTION_FAILED_BYTE = (byte) 0x06; + final String ERROR_DECRYPTION_FAILED_STRING = "ERROR_DECRYPTION_FAILED"; + + final byte ERROR_ENCRYPTION_FAILED_BYTE = (byte) 0x07; + final String ERROR_ENCRYPTION_FAILED_STRING = "ERROR_ENCRYPTION_FAILED"; + + final byte ERROR_SSL_INVALID_DATA_BYTE = (byte) 0x08; + final String ERROR_SSL_INVALID_DATA_STRING = "ERROR_SSL_INVALID_DATA"; + + final byte ERROR_HANDSHAKE_FAILED_BYTE = (byte) 0x09; + final String ERROR_HANDSHAKE_FAILED_STRING = "ERROR_HANDSHAKE_FAILED"; + + final byte INVALID_CERT_BYTE = (byte) 0x0A; + final String INVALID_CERT_STRING = "INVALID_CERT"; + + final byte EXPIRED_CERT_BYTE = (byte) 0x0B; + final String EXPIRED_CERT_STRING = "EXPIRED_CERT"; + + final byte ERROR_INTERNAL_BYTE = (byte) 0xFF; + final String ERROR_INTERNAL_STRING = "ERROR_INTERNAL"; + + final byte ERROR_UNKNOWN_INTERNAL_ERROR_BYTE = (byte) 0xFE; + final String ERROR_UNKNOWN_INTERNAL_ERROR_STRING = "ERROR_UNKNOWN_INTERNAL_ERROR"; + + try { + assertNotNull("QueryErrorCode list returned null", list); + + SecurityQueryErrorCode enumSuccess = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SUCCESS_BYTE); + SecurityQueryErrorCode enumInvalidQuerySize = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_BYTE); + SecurityQueryErrorCode enumInvalidQueryID = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_BYTE); + SecurityQueryErrorCode enumNotSupported = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_NOT_SUPPORTED_BYTE); + SecurityQueryErrorCode enumServiceAlreadyProtected = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_BYTE); + SecurityQueryErrorCode enumServiceNotProtected = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_BYTE); + SecurityQueryErrorCode enumDecryptionFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_BYTE); + SecurityQueryErrorCode enumEncryptionFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_BYTE); + SecurityQueryErrorCode enumSSLInvalidData = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_BYTE); + SecurityQueryErrorCode enumHandshakeFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_BYTE); + SecurityQueryErrorCode enumInvalidCert = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, INVALID_CERT_BYTE); + SecurityQueryErrorCode enumExpiredCert = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, EXPIRED_CERT_BYTE); + SecurityQueryErrorCode enumInternal = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INTERNAL_BYTE); + SecurityQueryErrorCode enumUnknownInternalError = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_BYTE); + + assertNotNull("Success byte match returned null", enumSuccess); + assertNotNull("Invalid Query Size byte match returned null", enumInvalidQuerySize); + assertNotNull("Invalid Query ID byte match returned null", enumInvalidQueryID); + assertNotNull("Not Supported byte match returned null", enumNotSupported); + assertNotNull("Service Already Protected byte match returned null", enumServiceAlreadyProtected); + assertNotNull("Service Not Protected byte match returned null", enumServiceNotProtected); + assertNotNull("Decryption Failed byte match returned null", enumDecryptionFailed); + assertNotNull("Encryption Failed byte match returned null", enumEncryptionFailed); + assertNotNull("SSL Invalid Data byte match returned null", enumSSLInvalidData); + assertNotNull("Handshake Failed byte match returned null", enumHandshakeFailed); + assertNotNull("Invalid Cert byte match returned null", enumInvalidCert); + assertNotNull("Expired Cert byte match returned null", enumExpiredCert); + assertNotNull("Internal byte match returned null", enumInternal); + assertNotNull("Unknown Internal byte match returned null", enumUnknownInternalError); + + enumSuccess = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SUCCESS_STRING); + enumInvalidQuerySize = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INVALID_QUERY_SIZE_STRING); + enumInvalidQueryID = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INVALID_QUERY_ID_STRING); + enumNotSupported = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_NOT_SUPPORTED_STRING); + enumServiceAlreadyProtected = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SERVICE_ALREADY_PROTECTED_STRING); + enumServiceNotProtected = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SERVICE_NOT_PROTECTED_STRING); + enumDecryptionFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_DECRYPTION_FAILED_STRING); + enumEncryptionFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_ENCRYPTION_FAILED_STRING); + enumSSLInvalidData = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_SSL_INVALID_DATA_STRING); + enumHandshakeFailed = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_HANDSHAKE_FAILED_STRING); + enumInvalidCert = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, INVALID_CERT_STRING); + enumExpiredCert = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, EXPIRED_CERT_STRING); + enumInternal = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_INTERNAL_STRING); + enumUnknownInternalError = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, ERROR_UNKNOWN_INTERNAL_ERROR_STRING); + + assertNotNull("Success string match returned null", enumSuccess); + assertNotNull("Invalid Query Size string match returned null", enumInvalidQuerySize); + assertNotNull("Invalid Query ID string match returned null", enumInvalidQueryID); + assertNotNull("Not Supported string match returned null", enumNotSupported); + assertNotNull("Service Already Protected string match returned null", enumServiceAlreadyProtected); + assertNotNull("Service Not Protected string match returned null", enumServiceNotProtected); + assertNotNull("Decryption Failed string match returned null", enumDecryptionFailed); + assertNotNull("Encryption Failed string match returned null", enumEncryptionFailed); + assertNotNull("SSL Invalid Data string match returned null", enumSSLInvalidData); + assertNotNull("Handshake Failed string match returned null", enumHandshakeFailed); + assertNotNull("Invalid Cert string match returned null", enumInvalidCert); + assertNotNull("Expired Cert string match returned null", enumExpiredCert); + assertNotNull("Internal string match returned null", enumInternal); + assertNotNull("Unknown Internal string match returned null", enumUnknownInternalError); + } catch (NullPointerException exception) { + fail("Null enum list throws NullPointerException."); + } + } + + public void testInvalidEnum() { + final byte INVALID_BYTE = (byte) 0xAB; + final String INVALID_STRING = "Invalid"; + + try { + SecurityQueryErrorCode enumInvalid = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, INVALID_BYTE); + assertNull("Invalid byte match didn't return null", enumInvalid); + + enumInvalid = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, INVALID_STRING); + assertNull("Invalid byte match didn't return null", enumInvalid); + } catch (IllegalArgumentException exception) { + fail("Invalid enum throws IllegalArgumentException."); + } + } + + public void testNullEnum() { + try { + SecurityQueryErrorCode enumNull = (SecurityQueryErrorCode) SecurityQueryErrorCode.get(list, null); + assertNull("Null lookup returns a value", enumNull); + } catch (NullPointerException exception) { + fail("Null string throws NullPointerException."); + } + } + + public void testListEnum() { + Vector enumTestList = new Vector<>(); + enumTestList.add(SecurityQueryErrorCode.ERROR_SUCCESS); + enumTestList.add(SecurityQueryErrorCode.ERROR_INVALID_QUERY_SIZE); + enumTestList.add(SecurityQueryErrorCode.ERROR_INVALID_QUERY_ID); + enumTestList.add(SecurityQueryErrorCode.ERROR_NOT_SUPPORTED); + enumTestList.add(SecurityQueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED); + enumTestList.add(SecurityQueryErrorCode.ERROR_SERVICE_NOT_PROTECTED); + enumTestList.add(SecurityQueryErrorCode.ERROR_DECRYPTION_FAILED); + enumTestList.add(SecurityQueryErrorCode.ERROR_ENCRYPTION_FAILED); + enumTestList.add(SecurityQueryErrorCode.ERROR_SSL_INVALID_DATA); + enumTestList.add(SecurityQueryErrorCode.ERROR_HANDSHAKE_FAILED); + enumTestList.add(SecurityQueryErrorCode.INVALID_CERT); + enumTestList.add(SecurityQueryErrorCode.EXPIRED_CERT); + enumTestList.add(SecurityQueryErrorCode.ERROR_INTERNAL); + enumTestList.add(SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR); + + assertTrue("List does not match enum test list.", + list.containsAll(enumTestList) && + enumTestList.containsAll(list)); + + SecurityQueryErrorCode[] enumValueArray = SecurityQueryErrorCode.values(); + SecurityQueryErrorCode[] enumTestArray = { + SecurityQueryErrorCode.ERROR_SUCCESS, + SecurityQueryErrorCode.ERROR_INVALID_QUERY_SIZE, + SecurityQueryErrorCode.ERROR_INVALID_QUERY_ID, + SecurityQueryErrorCode.ERROR_NOT_SUPPORTED, + SecurityQueryErrorCode.ERROR_SERVICE_ALREADY_PROTECTED, + SecurityQueryErrorCode.ERROR_SERVICE_NOT_PROTECTED, + SecurityQueryErrorCode.ERROR_DECRYPTION_FAILED, + SecurityQueryErrorCode.ERROR_ENCRYPTION_FAILED, + SecurityQueryErrorCode.ERROR_SSL_INVALID_DATA, + SecurityQueryErrorCode.ERROR_HANDSHAKE_FAILED, + SecurityQueryErrorCode.INVALID_CERT, + SecurityQueryErrorCode.EXPIRED_CERT, + SecurityQueryErrorCode.ERROR_INTERNAL, + SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR + }; + assertTrue("Array does not match enum values array.", + Validator.validateQueryErrorCodeArray(enumValueArray, enumTestArray)); + } +} diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryIDTests.java similarity index 58% rename from android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java rename to android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryIDTests.java index 90118eb178..4f73f81392 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryIDTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryIDTests.java @@ -1,15 +1,15 @@ package com.smartdevicelink.test.protocol.enums; -import com.smartdevicelink.protocol.enums.QueryID; +import com.smartdevicelink.protocol.enums.SecurityQueryID; import com.smartdevicelink.test.Validator; import junit.framework.TestCase; import java.util.Vector; -public class QueryIDTests extends TestCase { +public class SecurityQueryIDTests extends TestCase { - private Vector list = QueryID.getList(); + private Vector list = SecurityQueryID.getList(); public void testValidEnums() { final byte[] SEND_HANDSHAKE_DATA_BYTES = {(byte) 0x00, (byte) 0x00, (byte) 0x01}; @@ -24,21 +24,21 @@ public void testValidEnums() { try { assertNotNull("QueryID list returned null", list); - QueryID enumHandshakeData = (QueryID) QueryID.get(list, SEND_HANDSHAKE_DATA_BYTES); - QueryID enumInternalError = (QueryID) QueryID.get(list, SEND_INTERNAL_ERROR_BYTES); - QueryID enumInvalidQueryId = (QueryID) QueryID.get(list, INVALID_QUERY_ID_BYTES); + SecurityQueryID enumHandshakeData = (SecurityQueryID) SecurityQueryID.get(list, SEND_HANDSHAKE_DATA_BYTES); + SecurityQueryID enumInternalError = (SecurityQueryID) SecurityQueryID.get(list, SEND_INTERNAL_ERROR_BYTES); + SecurityQueryID enumInvalidSecurityQueryId = (SecurityQueryID) SecurityQueryID.get(list, INVALID_QUERY_ID_BYTES); assertNotNull("Send Handshake Data byte match returned null", enumHandshakeData); assertNotNull("Send Internal Error byte match returned null", enumInternalError); - assertNotNull("Send Invalid QueryID byte match returned null", enumInvalidQueryId); + assertNotNull("Send Invalid QueryID byte match returned null", enumInvalidSecurityQueryId); - enumHandshakeData = (QueryID) QueryID.get(list, SEND_HANDSHAKE_DATA_STRING); - enumInternalError = (QueryID) QueryID.get(list, SEND_INTERNAL_ERROR_STRING); - enumInvalidQueryId = (QueryID) QueryID.get(list, INVALID_QUERY_ID_STRING); + enumHandshakeData = (SecurityQueryID) SecurityQueryID.get(list, SEND_HANDSHAKE_DATA_STRING); + enumInternalError = (SecurityQueryID) SecurityQueryID.get(list, SEND_INTERNAL_ERROR_STRING); + enumInvalidSecurityQueryId = (SecurityQueryID) SecurityQueryID.get(list, INVALID_QUERY_ID_STRING); assertNotNull("Send Handshake Data string match returned null", enumHandshakeData); assertNotNull("Send Internal Error string match returned null", enumInternalError); - assertNotNull("Send Invalid QueryID string match returned null", enumInvalidQueryId); + assertNotNull("Send Invalid QueryID string match returned null", enumInvalidSecurityQueryId); } catch(NullPointerException exception) { fail("Null enum list throws NullPointerException."); } @@ -50,10 +50,10 @@ public void testInvalidEnum() { final String INVALID_STRING = "Invalid"; try { - QueryID enumInvalid = (QueryID) QueryID.get(list, INVALID_BYTE_ARRAY); + SecurityQueryID enumInvalid = (SecurityQueryID) SecurityQueryID.get(list, INVALID_BYTE_ARRAY); assertNull("Invalid byte[] match didn't return null", enumInvalid); - enumInvalid = (QueryID) QueryID.get(list, INVALID_STRING); + enumInvalid = (SecurityQueryID) SecurityQueryID.get(list, INVALID_STRING); assertNull("Invalid string match didn't return null", enumInvalid); } catch (IllegalArgumentException exception) { fail("Invalid enum throws IllegalArgumentException."); @@ -62,10 +62,10 @@ public void testInvalidEnum() { public void testNullEnum() { try { - QueryID enumNull = (QueryID) QueryID.get(list, (String) null); + SecurityQueryID enumNull = (SecurityQueryID) SecurityQueryID.get(list, (String) null); assertNull("Null lookup returns a null string value", enumNull); - enumNull = (QueryID) QueryID.get(list, (byte[]) null); + enumNull = (SecurityQueryID) SecurityQueryID.get(list, (byte[]) null); assertNull("Null lookup returns a null byte[] value", enumNull); }catch (NullPointerException exception) { fail("Null string throws NullPointerException."); @@ -73,17 +73,17 @@ public void testNullEnum() { } public void testListEnum() { - Vector enumTestList = new Vector<>(); - enumTestList.add(QueryID.SEND_HANDSHAKE_DATA); - enumTestList.add(QueryID.SEND_INTERNAL_ERROR); - enumTestList.add(QueryID.INVALID_QUERY_ID); + Vector enumTestList = new Vector<>(); + enumTestList.add(SecurityQueryID.SEND_HANDSHAKE_DATA); + enumTestList.add(SecurityQueryID.SEND_INTERNAL_ERROR); + enumTestList.add(SecurityQueryID.INVALID_QUERY_ID); assertTrue("List does not match enum test list.", list.containsAll(enumTestList) && enumTestList.containsAll(list)); - QueryID[] enumValueArray = QueryID.values(); - QueryID[] enumTestArray = {QueryID.SEND_HANDSHAKE_DATA, QueryID.SEND_INTERNAL_ERROR, QueryID.INVALID_QUERY_ID}; + SecurityQueryID[] enumValueArray = SecurityQueryID.values(); + SecurityQueryID[] enumTestArray = {SecurityQueryID.SEND_HANDSHAKE_DATA, SecurityQueryID.SEND_INTERNAL_ERROR, SecurityQueryID.INVALID_QUERY_ID}; assertTrue("Array does not match enum values array.", Validator.validateQueryIDArray(enumValueArray, enumTestArray)); } diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryTypeTests.java similarity index 55% rename from android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java rename to android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryTypeTests.java index 13d606cffd..6835da9468 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/QueryTypeTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryTypeTests.java @@ -1,15 +1,15 @@ package com.smartdevicelink.test.protocol.enums; -import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.test.Validator; import junit.framework.TestCase; import java.util.Vector; -public class QueryTypeTests extends TestCase { +public class SecurityQueryTypeTests extends TestCase { - private Vector list = QueryType.getList(); + private Vector list = SecurityQueryType.getList(); public void testValidEnums() { final byte REQUEST_BYTE = (byte) 0x00; @@ -27,25 +27,25 @@ public void testValidEnums() { try { assertNotNull("QueryType list returned null", list); - QueryType enumRequest = (QueryType) QueryType.get(list, REQUEST_BYTE); - QueryType enumResponse = (QueryType) QueryType.get(list, RESPONSE_BYTE); - QueryType enumNotification = (QueryType) QueryType.get(list, NOTIFICATION_BYTE); - QueryType enumInvalidQueryType = (QueryType) QueryType.get(list, INVALID_QUERY_TYPE_BYTE); + SecurityQueryType enumRequest = (SecurityQueryType) SecurityQueryType.get(list, REQUEST_BYTE); + SecurityQueryType enumResponse = (SecurityQueryType) SecurityQueryType.get(list, RESPONSE_BYTE); + SecurityQueryType enumNotification = (SecurityQueryType) SecurityQueryType.get(list, NOTIFICATION_BYTE); + SecurityQueryType enumInvalidSecurityQueryType = (SecurityQueryType) SecurityQueryType.get(list, INVALID_QUERY_TYPE_BYTE); assertNotNull("Request byte match returned null", enumRequest); assertNotNull("Response byte match returned null", enumResponse); assertNotNull("Notification byte match returned null", enumNotification); - assertNotNull("Invalid Query Type byte match returned null", enumInvalidQueryType); + assertNotNull("Invalid Query Type byte match returned null", enumInvalidSecurityQueryType); - enumRequest = (QueryType) QueryType.get(list, REQUEST_STRING); - enumResponse = (QueryType) QueryType.get(list, RESPONSE_STRING); - enumNotification = (QueryType) QueryType.get(list, NOTIFICATION_STRING); - enumInvalidQueryType = (QueryType) QueryType.get(list, INVALID_QUERY_TYPE_STRING); + enumRequest = (SecurityQueryType) SecurityQueryType.get(list, REQUEST_STRING); + enumResponse = (SecurityQueryType) SecurityQueryType.get(list, RESPONSE_STRING); + enumNotification = (SecurityQueryType) SecurityQueryType.get(list, NOTIFICATION_STRING); + enumInvalidSecurityQueryType = (SecurityQueryType) SecurityQueryType.get(list, INVALID_QUERY_TYPE_STRING); assertNotNull("Request string match returned null", enumRequest); assertNotNull("Response string match returned null", enumResponse); assertNotNull("Notification string match returned null", enumNotification); - assertNotNull("Invalid Query string byte match returned null", enumInvalidQueryType); + assertNotNull("Invalid Query string byte match returned null", enumInvalidSecurityQueryType); }catch (NullPointerException exception) { @@ -59,10 +59,10 @@ public void testInvalidEnum() { final String INVALID_STRING = "Invalid"; try { - QueryType enumInvalid = (QueryType) QueryType.get(list, INVALID_BYTE); + SecurityQueryType enumInvalid = (SecurityQueryType) SecurityQueryType.get(list, INVALID_BYTE); assertNull("Invalid byte match didn't return null", enumInvalid); - enumInvalid = (QueryType) QueryType.get(list, INVALID_STRING); + enumInvalid = (SecurityQueryType) SecurityQueryType.get(list, INVALID_STRING); assertNull("Invalid string match didn't return null", enumInvalid); } catch (IllegalArgumentException exception) { fail("Invalid enum throws IllegalArgumentException."); @@ -71,7 +71,7 @@ public void testInvalidEnum() { public void testNullEnum() { try { - QueryType enumNull = (QueryType) QueryType.get(list, null); + SecurityQueryType enumNull = (SecurityQueryType) SecurityQueryType.get(list, null); assertNull("Null lookup returns a value", enumNull); } catch (NullPointerException exception) { fail("Null string throws NullPointerException."); @@ -79,18 +79,18 @@ public void testNullEnum() { } public void testListEnum() { - Vector enumTestList = new Vector<>(); - enumTestList.add(QueryType.REQUEST); - enumTestList.add(QueryType.RESPONSE); - enumTestList.add(QueryType.NOTIFICATION); - enumTestList.add(QueryType.INVALID_QUERY_TYPE); + Vector enumTestList = new Vector<>(); + enumTestList.add(SecurityQueryType.REQUEST); + enumTestList.add(SecurityQueryType.RESPONSE); + enumTestList.add(SecurityQueryType.NOTIFICATION); + enumTestList.add(SecurityQueryType.INVALID_QUERY_TYPE); assertTrue("List does not match enum test list.", list.containsAll(enumTestList) && enumTestList.containsAll(list)); - QueryType[] enumValueArray = QueryType.values(); - QueryType[] enumTestArray = {QueryType.REQUEST, QueryType.RESPONSE, QueryType.NOTIFICATION, QueryType.INVALID_QUERY_TYPE}; + SecurityQueryType[] enumValueArray = SecurityQueryType.values(); + SecurityQueryType[] enumTestArray = {SecurityQueryType.REQUEST, SecurityQueryType.RESPONSE, SecurityQueryType.NOTIFICATION, SecurityQueryType.INVALID_QUERY_TYPE}; assertTrue("Array does not match enum values array.", Validator.validateQueryTypeArray(enumValueArray, enumTestArray)); } diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index c2553482b6..c7bc1326d9 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -2,9 +2,9 @@ import androidx.annotation.RestrictTo; -import com.smartdevicelink.protocol.enums.QueryErrorCode; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode; +import com.smartdevicelink.protocol.enums.SecurityQueryID; +import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.util.BitConverter; import com.smartdevicelink.util.DebugTool; @@ -12,53 +12,67 @@ public class SecurityQueryPayload { private static final String TAG = "BinaryQueryHeader"; - private QueryType _queryType; - private QueryID _queryID; + private SecurityQueryType _securityQueryType; + private SecurityQueryID _securityQueryID; private int _correlationID; private int _jsonSize; - private QueryErrorCode _errorCode; + private SecurityQueryErrorCode _errorCode; private byte[] _jsonData = null; private byte[] _bulkData = null; + private static final int SECURITY_QUERY_HEADER_SIZE = 12; + public SecurityQueryPayload() { } public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { + if (binHeader == null || binHeader.length < SECURITY_QUERY_HEADER_SIZE) { + DebugTool.logError(TAG, "Security Payload error: not enough data to form a Security Query Header. Data length: " + (binHeader != null ? binHeader.length : "null")); + return null; + } + SecurityQueryPayload msg = new SecurityQueryPayload(); + //Set QueryType from the first 8 bits byte QUERY_Type = (byte) (binHeader[0]); - msg.setQueryType(QueryType.valueOf(QUERY_Type)); + msg.setQueryType(SecurityQueryType.valueOf(QUERY_Type)); + //Set queryID from the last 24 bits of the first 32 bits byte[] _queryID = new byte[3]; System.arraycopy(binHeader, 1, _queryID, 0, 3); - msg.setQueryID(QueryID.valueOf(_queryID)); + msg.setQueryID(SecurityQueryID.valueOf(_queryID)); + //set correlationID from the 32 bits after the first 32 bits int corrID = BitConverter.intFromByteArray(binHeader, 4); msg.setCorrelationID(corrID); + //set jsonSize from the last 32 bits after the first 64 bits int _jsonSize = BitConverter.intFromByteArray(binHeader, 8); msg.setJsonSize(_jsonSize); - if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { - msg.setErrorCode(QueryErrorCode.valueOf(binHeader[binHeader.length - 1])); + //If we get an error message we want the error code from the last 8 bits + if (msg.getQueryType() == SecurityQueryType.NOTIFICATION && msg.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR) { + msg.setErrorCode(SecurityQueryErrorCode.valueOf(binHeader[binHeader.length - 1])); } try { - if (_jsonSize > 0) { + //Get the JsonData after the header (after 96 bits) based on the jsonData size + if (_jsonSize > 0 && _jsonSize <= (binHeader.length - SECURITY_QUERY_HEADER_SIZE)) { byte[] _jsonData = new byte[_jsonSize]; - System.arraycopy(binHeader, 12, _jsonData, 0, _jsonSize); + System.arraycopy(binHeader, SECURITY_QUERY_HEADER_SIZE, _jsonData, 0, _jsonSize); msg.setJsonData(_jsonData); } - if (binHeader.length - _jsonSize - 12 > 0) { + //Get the binaryData after the header (after 96 bits) and the jsonData size + if (binHeader.length - _jsonSize - SECURITY_QUERY_HEADER_SIZE > 0) { byte[] _bulkData; - if (msg.getQueryType() == QueryType.NOTIFICATION && msg.getQueryID() == QueryID.SEND_INTERNAL_ERROR) { - _bulkData = new byte[binHeader.length - _jsonSize - 12 - 1]; + if (msg.getQueryType() == SecurityQueryType.NOTIFICATION && msg.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR) { + _bulkData = new byte[binHeader.length - _jsonSize - SECURITY_QUERY_HEADER_SIZE - 1]; } else { - _bulkData = new byte[binHeader.length - _jsonSize - 12]; + _bulkData = new byte[binHeader.length - _jsonSize - SECURITY_QUERY_HEADER_SIZE]; } - System.arraycopy(binHeader, 12 + _jsonSize, _bulkData, 0, _bulkData.length); + System.arraycopy(binHeader, SECURITY_QUERY_HEADER_SIZE + _jsonSize, _bulkData, 0, _bulkData.length); msg.setBulkData(_bulkData); } @@ -71,28 +85,33 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { } public byte[] assembleHeaderBytes() { - byte[] ret = new byte[12]; - ret[0] = _queryType.getValue(); - System.arraycopy(_queryID.getValue(), 0, ret, 1, 3); + // From the properties, create a data buffer + // Query Type - first 8 bits + // Query ID - next 24 bits + // Sequence Number - next 32 bits + // JSON size - next 32 bits + byte[] ret = new byte[SECURITY_QUERY_HEADER_SIZE]; + ret[0] = _securityQueryType.getValue(); + System.arraycopy(_securityQueryID.getValue(), 0, ret, 1, 3); System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4); System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4); return ret; } - public QueryType getQueryType() { - return _queryType; + public SecurityQueryType getQueryType() { + return _securityQueryType; } - public void setQueryType(QueryType _queryType) { - this._queryType = _queryType; + public void setQueryType(SecurityQueryType _securityQueryType) { + this._securityQueryType = _securityQueryType; } - public QueryID getQueryID() { - return _queryID; + public SecurityQueryID getQueryID() { + return _securityQueryID; } - public void setQueryID(QueryID _queryID) { - this._queryID = _queryID; + public void setQueryID(SecurityQueryID _securityQueryID) { + this._securityQueryID = _securityQueryID; } public int getCorrelationID() { @@ -111,11 +130,11 @@ public void setJsonSize(int _jsonSize) { this._jsonSize = _jsonSize; } - public QueryErrorCode getErrorCode() { + public SecurityQueryErrorCode getErrorCode() { return _errorCode; } - public void setErrorCode(QueryErrorCode _errorCode) { + public void setErrorCode(SecurityQueryErrorCode _errorCode) { this._errorCode = _errorCode; } diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java deleted file mode 100644 index 83f7d85529..0000000000 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryErrorCode.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.smartdevicelink.protocol.enums; - -import androidx.annotation.RestrictTo; - -import com.smartdevicelink.util.ByteEnumer; - -import java.util.Vector; - -@RestrictTo(RestrictTo.Scope.LIBRARY) -public class QueryErrorCode extends ByteEnumer { - - private static final Vector theList = new Vector<>(); - - public static Vector getList() { - return theList; - } - - protected QueryErrorCode(byte value, String name) { - super(value, name); - } - - public final static QueryErrorCode ERROR_SUCCESS = new QueryErrorCode((byte) 0x00, "ERROR_SUCCESS"); - public final static QueryErrorCode ERROR_INVALID_QUERY_SIZE = new QueryErrorCode((byte) 0x01, "ERROR_INVALID_QUERY_SIZE"); - public final static QueryErrorCode ERROR_INVALID_QUERY_ID = new QueryErrorCode((byte) 0x02, "ERROR_INVALID_QUERY_ID"); - public final static QueryErrorCode ERROR_NOT_SUPPORTED = new QueryErrorCode((byte) 0x03, "ERROR_NOT_SUPPORTED"); - public final static QueryErrorCode ERROR_SERVICE_ALREADY_PROTECTED = new QueryErrorCode((byte) 0x04, "ERROR_SERVICE_ALREADY_PROTECTED"); - public final static QueryErrorCode ERROR_SERVICE_NOT_PROTECTED = new QueryErrorCode((byte) 0x05, "ERROR_SERVICE_NOT_PROTECTED"); - public final static QueryErrorCode ERROR_DECRYPTION_FAILED = new QueryErrorCode((byte) 0x06, "ERROR_DECRYPTION_FAILED"); - public final static QueryErrorCode ERROR_ENCRYPTION_FAILED = new QueryErrorCode((byte) 0x07, "ERROR_ENCRYPTION_FAILED"); - public final static QueryErrorCode ERROR_SSL_INVALID_DATA = new QueryErrorCode((byte) 0x08, "ERROR_SSL_INVALID_DATA"); - public final static QueryErrorCode ERROR_HANDSHAKE_FAILED = new QueryErrorCode((byte) 0x09, "ERROR_HANDSHAKE_FAILED"); - public final static QueryErrorCode INVALID_CERT = new QueryErrorCode((byte) 0x0A, "INVALID_CERT"); - public final static QueryErrorCode EXPIRED_CERT = new QueryErrorCode((byte) 0x0B, "EXPIRED_CERT"); - public final static QueryErrorCode ERROR_INTERNAL = new QueryErrorCode((byte) 0xFF, "ERROR_INTERNAL"); - public final static QueryErrorCode ERROR_UNKNOWN_INTERNAL_ERROR = new QueryErrorCode((byte) 0xFE, "ERROR_UNKNOWN_INTERNAL_ERROR"); - - static { - theList.addElement(ERROR_SUCCESS); - theList.addElement(ERROR_INVALID_QUERY_SIZE); - theList.addElement(ERROR_INVALID_QUERY_ID); - theList.addElement(ERROR_NOT_SUPPORTED); - theList.addElement(ERROR_SERVICE_ALREADY_PROTECTED); - theList.addElement(ERROR_SERVICE_NOT_PROTECTED); - theList.addElement(ERROR_DECRYPTION_FAILED); - theList.addElement(ERROR_ENCRYPTION_FAILED); - theList.addElement(ERROR_SSL_INVALID_DATA); - theList.addElement(ERROR_HANDSHAKE_FAILED); - theList.addElement(INVALID_CERT); - theList.addElement(EXPIRED_CERT); - theList.addElement(ERROR_INTERNAL); - theList.addElement(ERROR_UNKNOWN_INTERNAL_ERROR); - } - - public static QueryErrorCode valueOf(byte passedByte) { - return (QueryErrorCode) get(theList, passedByte); - } - - public static QueryErrorCode[] values() { - return theList.toArray(new QueryErrorCode[theList.size()]); - } -} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java b/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java deleted file mode 100644 index ce26e005bc..0000000000 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryType.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.smartdevicelink.protocol.enums; - -import androidx.annotation.RestrictTo; - -import com.smartdevicelink.util.ByteEnumer; - -import java.util.Vector; - -@RestrictTo(RestrictTo.Scope.LIBRARY) -public class QueryType extends ByteEnumer { - - private static final Vector theList = new Vector<>(); - - public static Vector getList() { - return theList; - } - - protected QueryType(byte value, String name) { - super(value, name); - } - - public final static QueryType REQUEST = new QueryType((byte) 0x00, "REQUEST"); - public final static QueryType RESPONSE = new QueryType((byte) 0x10, "RESPONSE"); - public final static QueryType NOTIFICATION = new QueryType((byte) 0x20, "NOTIFICATION"); - public final static QueryType INVALID_QUERY_TYPE = new QueryType((byte) 0xFF, "INVALID_QUERY_TYPE"); - - static { - theList.addElement(REQUEST); - theList.addElement(RESPONSE); - theList.addElement(NOTIFICATION); - theList.addElement(INVALID_QUERY_TYPE); - } - - public static QueryType valueOf(byte passedByte) { - return (QueryType) get(theList, passedByte); - } - - public static QueryType[] values() { - return theList.toArray(new QueryType[theList.size()]); - } -} \ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryErrorCode.java b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryErrorCode.java new file mode 100644 index 0000000000..5601271d3d --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryErrorCode.java @@ -0,0 +1,61 @@ +package com.smartdevicelink.protocol.enums; + +import androidx.annotation.RestrictTo; + +import com.smartdevicelink.util.ByteEnumer; + +import java.util.Vector; + +@RestrictTo(RestrictTo.Scope.LIBRARY) +public class SecurityQueryErrorCode extends ByteEnumer { + + private static final Vector theList = new Vector<>(); + + public static Vector getList() { + return theList; + } + + protected SecurityQueryErrorCode(byte value, String name) { + super(value, name); + } + + public final static SecurityQueryErrorCode ERROR_SUCCESS = new SecurityQueryErrorCode((byte) 0x00, "ERROR_SUCCESS"); + public final static SecurityQueryErrorCode ERROR_INVALID_QUERY_SIZE = new SecurityQueryErrorCode((byte) 0x01, "ERROR_INVALID_QUERY_SIZE"); + public final static SecurityQueryErrorCode ERROR_INVALID_QUERY_ID = new SecurityQueryErrorCode((byte) 0x02, "ERROR_INVALID_QUERY_ID"); + public final static SecurityQueryErrorCode ERROR_NOT_SUPPORTED = new SecurityQueryErrorCode((byte) 0x03, "ERROR_NOT_SUPPORTED"); + public final static SecurityQueryErrorCode ERROR_SERVICE_ALREADY_PROTECTED = new SecurityQueryErrorCode((byte) 0x04, "ERROR_SERVICE_ALREADY_PROTECTED"); + public final static SecurityQueryErrorCode ERROR_SERVICE_NOT_PROTECTED = new SecurityQueryErrorCode((byte) 0x05, "ERROR_SERVICE_NOT_PROTECTED"); + public final static SecurityQueryErrorCode ERROR_DECRYPTION_FAILED = new SecurityQueryErrorCode((byte) 0x06, "ERROR_DECRYPTION_FAILED"); + public final static SecurityQueryErrorCode ERROR_ENCRYPTION_FAILED = new SecurityQueryErrorCode((byte) 0x07, "ERROR_ENCRYPTION_FAILED"); + public final static SecurityQueryErrorCode ERROR_SSL_INVALID_DATA = new SecurityQueryErrorCode((byte) 0x08, "ERROR_SSL_INVALID_DATA"); + public final static SecurityQueryErrorCode ERROR_HANDSHAKE_FAILED = new SecurityQueryErrorCode((byte) 0x09, "ERROR_HANDSHAKE_FAILED"); + public final static SecurityQueryErrorCode INVALID_CERT = new SecurityQueryErrorCode((byte) 0x0A, "INVALID_CERT"); + public final static SecurityQueryErrorCode EXPIRED_CERT = new SecurityQueryErrorCode((byte) 0x0B, "EXPIRED_CERT"); + public final static SecurityQueryErrorCode ERROR_INTERNAL = new SecurityQueryErrorCode((byte) 0xFF, "ERROR_INTERNAL"); + public final static SecurityQueryErrorCode ERROR_UNKNOWN_INTERNAL_ERROR = new SecurityQueryErrorCode((byte) 0xFE, "ERROR_UNKNOWN_INTERNAL_ERROR"); + + static { + theList.addElement(ERROR_SUCCESS); + theList.addElement(ERROR_INVALID_QUERY_SIZE); + theList.addElement(ERROR_INVALID_QUERY_ID); + theList.addElement(ERROR_NOT_SUPPORTED); + theList.addElement(ERROR_SERVICE_ALREADY_PROTECTED); + theList.addElement(ERROR_SERVICE_NOT_PROTECTED); + theList.addElement(ERROR_DECRYPTION_FAILED); + theList.addElement(ERROR_ENCRYPTION_FAILED); + theList.addElement(ERROR_SSL_INVALID_DATA); + theList.addElement(ERROR_HANDSHAKE_FAILED); + theList.addElement(INVALID_CERT); + theList.addElement(EXPIRED_CERT); + theList.addElement(ERROR_INTERNAL); + theList.addElement(ERROR_UNKNOWN_INTERNAL_ERROR); + } + + public static SecurityQueryErrorCode valueOf(byte passedByte) { + return (SecurityQueryErrorCode) get(theList, passedByte); + } + + public static SecurityQueryErrorCode[] values() { + return theList.toArray(new SecurityQueryErrorCode[theList.size()]); + } +} diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryID.java similarity index 62% rename from base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java rename to base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryID.java index da1e3fab36..c1b799ec94 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/enums/QueryID.java +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryID.java @@ -10,20 +10,20 @@ import java.util.Vector; @RestrictTo(RestrictTo.Scope.LIBRARY) -public class QueryID { +public class SecurityQueryID { - private static final Vector theList = new Vector<>(); + private static final Vector theList = new Vector<>(); - public static Vector getList() { + public static Vector getList() { return theList; } private static final byte[] sendHandshakeDataByteArray = {(byte) 0x00, (byte) 0x00, (byte) 0x01}; private static final byte[] sendInternalErrorByteArray = {(byte) 0x00, (byte) 0x00, (byte) 0x02}; private static final byte[] invalidQueryIdByteArray = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; - public final static QueryID SEND_HANDSHAKE_DATA = new QueryID(sendHandshakeDataByteArray, "SEND_HANDSHAKE_DATA"); - public final static QueryID SEND_INTERNAL_ERROR = new QueryID(sendInternalErrorByteArray, "SEND_INTERNAL_ERROR"); - public final static QueryID INVALID_QUERY_ID = new QueryID(invalidQueryIdByteArray, "INVALID_QUERY_ID"); + public final static SecurityQueryID SEND_HANDSHAKE_DATA = new SecurityQueryID(sendHandshakeDataByteArray, "SEND_HANDSHAKE_DATA"); + public final static SecurityQueryID SEND_INTERNAL_ERROR = new SecurityQueryID(sendInternalErrorByteArray, "SEND_INTERNAL_ERROR"); + public final static SecurityQueryID INVALID_QUERY_ID = new SecurityQueryID(invalidQueryIdByteArray, "INVALID_QUERY_ID"); static { theList.addElement(SEND_HANDSHAKE_DATA); @@ -31,7 +31,7 @@ public static Vector getList() { theList.addElement(INVALID_QUERY_ID); } - protected QueryID(byte[] value, String name) { + protected SecurityQueryID(byte[] value, String name) { this.value = value; this.name = name; } @@ -53,11 +53,11 @@ public String getName() { return name; } - public boolean equals(QueryID other) { + public boolean equals(SecurityQueryID other) { return Objects.equals(name, other.getName()); } - public boolean eq(QueryID other) { + public boolean eq(SecurityQueryID other) { return equals(other); } @@ -65,11 +65,11 @@ public byte[] value() { return value; } - public static QueryID get(Vector theList, byte[] value) { + public static SecurityQueryID get(Vector theList, byte[] value) { Enumeration enumer = theList.elements(); while (enumer.hasMoreElements()) { try { - QueryID current = (QueryID) enumer.nextElement(); + SecurityQueryID current = (SecurityQueryID) enumer.nextElement(); if (Arrays.equals(current.getValue(), value)) { return current; } @@ -80,11 +80,11 @@ public static QueryID get(Vector theList, byte[] value) { return null; } - public static QueryID get(Vector theList, String name) { + public static SecurityQueryID get(Vector theList, String name) { Enumeration enumer = theList.elements(); while (enumer.hasMoreElements()) { try { - QueryID current = (QueryID) enumer.nextElement(); + SecurityQueryID current = (SecurityQueryID) enumer.nextElement(); if (current.getName().equals(name)) { return current; } @@ -95,11 +95,11 @@ public static QueryID get(Vector theList, String name) { return null; } - public static QueryID valueOf(byte[] passedByteArray) { - return (QueryID) get(theList, passedByteArray); + public static SecurityQueryID valueOf(byte[] passedByteArray) { + return (SecurityQueryID) get(theList, passedByteArray); } - public static QueryID[] values() { - return theList.toArray(new QueryID[theList.size()]); + public static SecurityQueryID[] values() { + return theList.toArray(new SecurityQueryID[theList.size()]); } } diff --git a/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryType.java b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryType.java new file mode 100644 index 0000000000..bb021e79c1 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/protocol/enums/SecurityQueryType.java @@ -0,0 +1,41 @@ +package com.smartdevicelink.protocol.enums; + +import androidx.annotation.RestrictTo; + +import com.smartdevicelink.util.ByteEnumer; + +import java.util.Vector; + +@RestrictTo(RestrictTo.Scope.LIBRARY) +public class SecurityQueryType extends ByteEnumer { + + private static final Vector theList = new Vector<>(); + + public static Vector getList() { + return theList; + } + + protected SecurityQueryType(byte value, String name) { + super(value, name); + } + + public final static SecurityQueryType REQUEST = new SecurityQueryType((byte) 0x00, "REQUEST"); + public final static SecurityQueryType RESPONSE = new SecurityQueryType((byte) 0x10, "RESPONSE"); + public final static SecurityQueryType NOTIFICATION = new SecurityQueryType((byte) 0x20, "NOTIFICATION"); + public final static SecurityQueryType INVALID_QUERY_TYPE = new SecurityQueryType((byte) 0xFF, "INVALID_QUERY_TYPE"); + + static { + theList.addElement(REQUEST); + theList.addElement(RESPONSE); + theList.addElement(NOTIFICATION); + theList.addElement(INVALID_QUERY_TYPE); + } + + public static SecurityQueryType valueOf(byte passedByte) { + return (SecurityQueryType) get(theList, passedByte); + } + + public static SecurityQueryType[] values() { + return theList.toArray(new SecurityQueryType[theList.size()]); + } +} \ No newline at end of file diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 276e29ae22..50e45fe8a6 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -43,8 +43,8 @@ import com.smartdevicelink.protocol.SdlPacket; import com.smartdevicelink.protocol.SdlProtocolBase; import com.smartdevicelink.protocol.enums.ControlFrameTags; -import com.smartdevicelink.protocol.enums.QueryID; -import com.smartdevicelink.protocol.enums.QueryType; +import com.smartdevicelink.protocol.enums.SecurityQueryID; +import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.protocol.enums.SessionType; import com.smartdevicelink.proxy.RPCMessage; import com.smartdevicelink.proxy.rpc.VehicleType; @@ -188,12 +188,18 @@ public void setSdlSecurity(SdlSecurityBase sec) { protected void processControlService(ProtocolMessage msg) { - if (sdlSecurity == null) + if (sdlSecurity == null || msg.getData() == null) return; + + if (msg.getData().length < 12) { + DebugTool.logError(TAG, "Security message is malformed, less than 12 bytes. It does not have a security payload header."); + } + // Check the client's message header for any internal errors + // NOTE: Before Core v8.0.0, all these messages will be notifications. In Core v8.0.0 and later, received messages will have the proper query type. Therefore, we cannot do things based only on the query type being request or response. SecurityQueryPayload receivedHeader = SecurityQueryPayload.parseBinaryQueryHeader(msg.getData().clone()); if (receivedHeader == null) { - DebugTool.logError(TAG, "Malformed Security Query Header"); + DebugTool.logError(TAG, "Module Security Query could not convert to object."); return; } @@ -205,34 +211,32 @@ protected void processControlService(ProtocolMessage msg) { Integer iNumBytes = null; - if (receivedHeader.getQueryID() == QueryID.SEND_INTERNAL_ERROR - && receivedHeader.getQueryType() == QueryType.NOTIFICATION) { + // If the query is of type `Notification` and the id represents a client internal error, we abort the response message and the encryptionManager will not be in state ready. + if (receivedHeader.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR + && receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION) { if (receivedHeader.getErrorCode() != null) { - DebugTool.logError(TAG, "Client internal error: " + receivedHeader.getErrorCode().getName()); + DebugTool.logError(TAG, "Security Query module internal error:" + receivedHeader.getErrorCode().getName()); } else { - DebugTool.logError(TAG, "Client internal error: No information provided"); + DebugTool.logError(TAG, "Security Query module error: No information provided"); } return; } - if (receivedHeader.getQueryID() != QueryID.SEND_HANDSHAKE_DATA - && (receivedHeader.getQueryType() != QueryType.NOTIFICATION || receivedHeader.getQueryType() != QueryType.REQUEST)) { - return; - } - iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + + // Assemble a security query payload header for our response SecurityQueryPayload responseHeader = new SecurityQueryPayload(); if (iNumBytes == null || iNumBytes <= 0) { DebugTool.logError(TAG, "Internal Error processing control service"); - responseHeader.setQueryID(QueryID.SEND_INTERNAL_ERROR); - responseHeader.setQueryType(QueryType.NOTIFICATION); + responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR); + responseHeader.setQueryType(SecurityQueryType.NOTIFICATION); responseHeader.setCorrelationID(msg.getCorrID()); responseHeader.setJsonSize(0); } else { - responseHeader.setQueryID(QueryID.SEND_HANDSHAKE_DATA); - responseHeader.setQueryType(QueryType.RESPONSE); + responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); + responseHeader.setQueryType(SecurityQueryType.RESPONSE); responseHeader.setCorrelationID(msg.getCorrID()); responseHeader.setJsonSize(0); } From 014a5e4de92e32f0b8da146dec3ba9e30c4e5040 Mon Sep 17 00:00:00 2001 From: Henigan Date: Thu, 2 Sep 2021 09:28:15 -0400 Subject: [PATCH 14/16] Add check for handshake data back in --- .../java/com/smartdevicelink/session/BaseSdlSession.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 50e45fe8a6..ee1f8133ab 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -222,6 +222,12 @@ protected void processControlService(ProtocolMessage msg) { return; } + if (receivedHeader.getQueryID() != SecurityQueryID.SEND_HANDSHAKE_DATA + || !(receivedHeader.getQueryType() == SecurityQueryType.REQUEST || receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION)) { + DebugTool.logError(TAG, "Security Query module error: Message is not a SEND_HANDSHAKE_DATA REQUEST"); + return; + } + iNumBytes = sdlSecurity.runHandshake(data, dataToRead); // Assemble a security query payload header for our response From cd00bb13000aadb673b192758fcd6015b1bee615 Mon Sep 17 00:00:00 2001 From: Robert Henigan Date: Fri, 3 Sep 2021 09:36:35 -0400 Subject: [PATCH 15/16] Update base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java Co-authored-by: Julian Kast --- .../main/java/com/smartdevicelink/session/BaseSdlSession.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index ee1f8133ab..42459dfd59 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -215,7 +215,7 @@ protected void processControlService(ProtocolMessage msg) { if (receivedHeader.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR && receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION) { if (receivedHeader.getErrorCode() != null) { - DebugTool.logError(TAG, "Security Query module internal error:" + receivedHeader.getErrorCode().getName()); + DebugTool.logError(TAG, "Security Query module internal error: " + receivedHeader.getErrorCode().getName()); } else { DebugTool.logError(TAG, "Security Query module error: No information provided"); } From 0c711c849ec7e67bd235391afc1a63e325e9f985 Mon Sep 17 00:00:00 2001 From: Henigan Date: Tue, 7 Sep 2021 10:13:14 -0400 Subject: [PATCH 16/16] Cleanup error messaging around queryid and type --- .../java/com/smartdevicelink/session/BaseSdlSession.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 42459dfd59..55df1f6830 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -222,12 +222,16 @@ protected void processControlService(ProtocolMessage msg) { return; } - if (receivedHeader.getQueryID() != SecurityQueryID.SEND_HANDSHAKE_DATA - || !(receivedHeader.getQueryType() == SecurityQueryType.REQUEST || receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION)) { + if (receivedHeader.getQueryID() != SecurityQueryID.SEND_HANDSHAKE_DATA) { DebugTool.logError(TAG, "Security Query module error: Message is not a SEND_HANDSHAKE_DATA REQUEST"); return; } + if (receivedHeader.getQueryType() == SecurityQueryType.RESPONSE) { + DebugTool.logError(TAG, "Security Query module error: Message is a response, which is not supported"); + return; + } + iNumBytes = sdlSecurity.runHandshake(data, dataToRead); // Assemble a security query payload header for our response