Skip to content

Commit 08def32

Browse files
committed
Update SecurityQueryPayload to include json data
1 parent 13a7d2f commit 08def32

3 files changed

Lines changed: 54 additions & 21 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static SecurityQueryPayload createDummyBqh() {
2525
bqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
2626
bqh.setQueryType(SecurityQueryType.REQUEST);
2727
bqh.setBulkData(null);
28-
bqh.setJsonSize(0);
28+
bqh.setJsonData(null);
2929
return bqh;
3030
}
3131

@@ -66,9 +66,9 @@ public void testCorrectHeaderAssembly() {
6666
dummyBqh.setQueryType(SecurityQueryType.REQUEST);
6767
dummyBqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
6868
dummyBqh.setCorrelationID(3);
69-
dummyBqh.setJsonSize(0);
69+
dummyBqh.setJsonData(new byte[0]);
7070

71-
byte[] assembledHeader = dummyBqh.assembleHeaderBytes();
71+
byte[] assembledHeader = dummyBqh.assembleSecurityQueryPayload(0);
7272
assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0]));
7373
byte[] queryIDFromHeader = new byte[3];
7474
System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3);
@@ -81,7 +81,7 @@ public void testCorrectHeaderAssembly() {
8181
public void testAssemblyAndParse() {
8282
SecurityQueryPayload bqh = createDummyBqh();
8383

84-
byte[] bqhBytes = bqh.assembleHeaderBytes();
84+
byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0);
8585
assertNotNull(bqhBytes);
8686

8787
SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes);
@@ -99,7 +99,7 @@ public void testAssemblyAndParse() {
9999
public void testCorruptHeader() {
100100
SecurityQueryPayload bqh = createDummyBqh();
101101

102-
byte[] bqhBytes = bqh.assembleHeaderBytes();
102+
byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0);
103103

104104
assertNotNull(safeParse(bqhBytes));
105105

@@ -114,13 +114,10 @@ public void testCorruptHeader() {
114114
}
115115

116116
@Test
117-
public void testJsonSetException() {
118-
try {
119-
SecurityQueryPayload bqh = createDummyBqh();
120-
bqh.setJsonData(null);
121-
fail("Setting JSON data to null should have thrown an exception");
122-
} catch (Exception e) {
123-
//Pass
124-
}
117+
public void testNullJsonData() {
118+
SecurityQueryPayload bqh = createDummyBqh();
119+
bqh.setJsonData(null);
120+
assertEquals(0, bqh.getJsonSize());
121+
assertEquals(null, bqh.getJsonData());
125122
}
126123
}

base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,20 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) {
8484
return msg;
8585
}
8686

87-
public byte[] assembleHeaderBytes() {
87+
public byte[] assembleSecurityQueryPayload(int payloadSize) {
88+
byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize];
89+
System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE);
90+
91+
if (_securityQueryID == SecurityQueryID.SEND_INTERNAL_ERROR && _securityQueryType == SecurityQueryType.NOTIFICATION) {
92+
System.arraycopy(_jsonData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, _jsonSize);
93+
} else if (_securityQueryID == SecurityQueryID.SEND_HANDSHAKE_DATA && _securityQueryType == SecurityQueryType.RESPONSE) {
94+
System.arraycopy(_bulkData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, payloadSize);
95+
}
96+
97+
return payLoad;
98+
}
99+
100+
private byte[] assembleHeaderBytes() {
88101
// From the properties, create a data buffer
89102
// Query Type - first 8 bits
90103
// Query ID - next 24 bits
@@ -126,7 +139,7 @@ public int getJsonSize() {
126139
return _jsonSize;
127140
}
128141

129-
public void setJsonSize(int _jsonSize) {
142+
private void setJsonSize(int _jsonSize) {
130143
this._jsonSize = _jsonSize;
131144
}
132145

@@ -143,6 +156,12 @@ public byte[] getJsonData() {
143156
}
144157

145158
public void setJsonData(byte[] _jsonData) {
159+
if (_jsonData == null) {
160+
this._jsonSize = 0;
161+
this._jsonData = null;
162+
return;
163+
}
164+
this._jsonSize = _jsonData.length;
146165
this._jsonData = new byte[this._jsonSize];
147166
System.arraycopy(_jsonData, 0, this._jsonData, 0, _jsonSize);
148167
}

base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.smartdevicelink.protocol.SdlPacket;
4545
import com.smartdevicelink.protocol.SdlProtocolBase;
4646
import com.smartdevicelink.protocol.enums.ControlFrameTags;
47+
import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode;
4748
import com.smartdevicelink.protocol.enums.SecurityQueryID;
4849
import com.smartdevicelink.protocol.enums.SecurityQueryType;
4950
import com.smartdevicelink.protocol.enums.SessionType;
@@ -61,6 +62,9 @@
6162
import com.smartdevicelink.util.SystemInfo;
6263
import com.smartdevicelink.util.Version;
6364

65+
import org.json.JSONException;
66+
import org.json.JSONObject;
67+
6468
import java.util.ArrayList;
6569
import java.util.HashMap;
6670
import java.util.List;
@@ -240,24 +244,37 @@ protected void processControlService(ProtocolMessage msg) {
240244
// Assemble a security query payload header for our response
241245
SecurityQueryPayload responseHeader = new SecurityQueryPayload();
242246

247+
byte[] returnBytes;
243248
if (iNumBytes == null || iNumBytes <= 0) {
244249
DebugTool.logError(TAG, "Internal Error processing control service");
245250

246251
responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR);
247252
responseHeader.setQueryType(SecurityQueryType.NOTIFICATION);
248253
responseHeader.setCorrelationID(msg.getCorrID());
249-
responseHeader.setJsonSize(0);
254+
byte[] jsonData;
255+
JSONObject jsonObject = new JSONObject();
256+
try {
257+
jsonObject.put("id", 254);
258+
jsonObject.put("text", "Error value for testing");
259+
jsonData = jsonObject.toString().getBytes();
260+
} catch (JSONException e) {
261+
DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification");
262+
e.printStackTrace();
263+
jsonData = new byte[0];
264+
}
265+
responseHeader.setJsonData(jsonData);
266+
responseHeader.setBulkData(null);
267+
responseHeader.setErrorCode(SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR);
268+
returnBytes = responseHeader.assembleSecurityQueryPayload(responseHeader.getJsonSize());
250269
} else {
251270
responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
252271
responseHeader.setQueryType(SecurityQueryType.RESPONSE);
253272
responseHeader.setCorrelationID(msg.getCorrID());
254-
responseHeader.setJsonSize(0);
273+
responseHeader.setBulkData(dataToRead);
274+
responseHeader.setJsonData(null);
275+
returnBytes = responseHeader.assembleSecurityQueryPayload(iNumBytes);
255276
}
256277

257-
byte[] returnBytes = new byte[iNumBytes + 12];
258-
System.arraycopy(responseHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12);
259-
System.arraycopy(dataToRead, 0, returnBytes, 12, iNumBytes);
260-
261278
ProtocolMessage protocolMessage = new ProtocolMessage();
262279
protocolMessage.setSessionType(SessionType.CONTROL);
263280
protocolMessage.setData(returnBytes);

0 commit comments

Comments
 (0)