diff options
author | Joey Grover <joeygrover@gmail.com> | 2018-06-28 10:36:52 -0400 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2018-06-28 10:36:52 -0400 |
commit | 97dea4c670feef32262fdf9011ef2fbbcedd37ef (patch) | |
tree | 69110e0fa6900a554a2dec80fe5b935d5bd98c70 | |
parent | b5403991f0af478c3d28a5aa7bbb3a7fea9605bb (diff) | |
parent | 8b604d31cc4ae3dd843bca5a4b925e8ed5575c1e (diff) | |
download | sdl_android-97dea4c670feef32262fdf9011ef2fbbcedd37ef.tar.gz |
Merge branch 'feature/issue_452_putfile_checksum' of https://github.com/smartdevicelink/sdl_android into develop
# Conflicts:
# sdl_android/src/androidTest/java/com/smartdevicelink/test/Test.java
8 files changed, 104 insertions, 10 deletions
diff --git a/sdl_android/src/androidTest/assets/json/PutFile.json b/sdl_android/src/androidTest/assets/json/PutFile.json index 70e80a09a..e989a62aa 100644 --- a/sdl_android/src/androidTest/assets/json/PutFile.json +++ b/sdl_android/src/androidTest/assets/json/PutFile.json @@ -9,6 +9,7 @@ "syncFileName":"filename.txt", "offset":2, "length":456, + "crc":2020, "bulkData":[ 0, 1, diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/test/Test.java b/sdl_android/src/androidTest/java/com/smartdevicelink/test/Test.java index 76bda6a66..0f7cbe609 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/test/Test.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/test/Test.java @@ -160,6 +160,7 @@ public class Test { public static final String GENERAL_STRING = "test"; public static final Double GENERAL_DOUBLE = 10.01; public static final boolean GENERAL_BOOLEAN = true; + public static final byte[] GENERAL_BYTE_ARRAY = new byte[0]; public static final TPMS GENERAL_TPMS = TPMS.UNKNOWN; public static final TBTState GENERAL_TBTSTATE = TBTState.NEXT_TURN_REQUEST; public static final FileType GENERAL_FILETYPE = FileType.BINARY; diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/enums/ResultTests.java b/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/enums/ResultTests.java index 5156c4f44..4cea3acbe 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/enums/ResultTests.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/enums/ResultTests.java @@ -10,14 +10,14 @@ import com.smartdevicelink.proxy.rpc.enums.Result; /** * This is a unit test class for the SmartDeviceLink library project class : - * {@link com.smartdevicelink.rpc.enums.Result} + * {@link com.smartdevicelink.proxy.rpc.enums.Result} */ public class ResultTests extends TestCase { /** * Verifies that the enum values are not null upon valid assignment. */ - public void testValidEnums () { + public void testValidEnums () { String example = "SUCCESS"; Result enumSuccess = Result.valueForString(example); example = "INVALID_DATA"; @@ -86,6 +86,8 @@ public class ResultTests extends TestCase { Result enumDataNotAvailable = Result.valueForString(example); example = "READ_ONLY"; Result enumReadOnly = Result.valueForString(example); + example = "CORRUPTED_DATA"; + Result enumCorruptData = Result.valueForString(example); assertNotNull("SUCCESS returned null", enumSuccess); assertNotNull("INVALID_DATA returned null", enumInvalidData); @@ -121,6 +123,7 @@ public class ResultTests extends TestCase { assertNotNull("RESUME_FAILED returned null", enumResumeFailed); assertNotNull("DATA_NOT_AVAILABLE returned null", enumDataNotAvailable); assertNotNull("READ_ONLY returned null", enumReadOnly); + assertNotNull("CORRUPTED_DATA", enumCorruptData); } /** @@ -192,6 +195,7 @@ public class ResultTests extends TestCase { enumTestList.add(Result.RESUME_FAILED); enumTestList.add(Result.DATA_NOT_AVAILABLE); enumTestList.add(Result.READ_ONLY); + enumTestList.add(Result.CORRUPTED_DATA); assertTrue("Enum value list does not match enum class list", enumValueList.containsAll(enumTestList) && enumTestList.containsAll(enumValueList)); diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/PutFileTests.java b/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/PutFileTests.java index becca8eee..7b6ce75e5 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/PutFileTests.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/test/rpc/requests/PutFileTests.java @@ -1,6 +1,7 @@ package com.smartdevicelink.test.rpc.requests; import java.util.Hashtable; +import java.util.zip.CRC32; import org.json.JSONException; import org.json.JSONObject; @@ -17,7 +18,7 @@ import com.smartdevicelink.test.json.rpc.JsonFileReader; /** * This is a unit test class for the SmartDeviceLink library project class : - * {@link com.smartdevicelink.rpc.PutFile} + * {@link com.smartdevicelink.proxy.rpc.PutFile} */ public class PutFileTests extends BaseRpcTests { @@ -30,6 +31,8 @@ public class PutFileTests extends BaseRpcTests { msg.setSystemFile(Test.GENERAL_BOOLEAN); msg.setOffset(Test.GENERAL_LONG); msg.setLength(Test.GENERAL_LONG); + msg.setCRC(Test.GENERAL_BYTE_ARRAY); + msg.setCRC(Test.GENERAL_LONG); return msg; } @@ -53,7 +56,8 @@ public class PutFileTests extends BaseRpcTests { result.put(PutFile.KEY_PERSISTENT_FILE, Test.GENERAL_BOOLEAN); result.put(PutFile.KEY_SYSTEM_FILE, Test.GENERAL_BOOLEAN); result.put(PutFile.KEY_OFFSET, Test.GENERAL_LONG); - result.put(PutFile.KEY_LENGTH, Test.GENERAL_LONG); + result.put(PutFile.KEY_LENGTH, Test.GENERAL_LONG); + result.put(PutFile.KEY_CRC, Test.GENERAL_LONG); } catch (JSONException e) { fail(Test.JSON_FAIL); } @@ -71,14 +75,16 @@ public class PutFileTests extends BaseRpcTests { boolean testSystemFile = ( (PutFile) msg ).getSystemFile(); Long testOffset = ( (PutFile) msg ).getOffset(); Long testLength = ( (PutFile) msg ).getLength(); - + Long testCRC = ( (PutFile) msg ).getCRC(); + // Valid Tests assertEquals(Test.MATCH, Test.GENERAL_FILETYPE, testFileType); assertEquals(Test.MATCH, Test.GENERAL_BOOLEAN, testPersistentFile); assertEquals(Test.MATCH, Test.GENERAL_BOOLEAN, testSystemFile); assertEquals(Test.MATCH, Test.GENERAL_LONG, testOffset); assertEquals(Test.MATCH, Test.GENERAL_LONG, testLength); - + assertEquals(Test.MATCH, Test.GENERAL_LONG, testCRC); + // Invalid/Null Tests PutFile msg = new PutFile(); assertNotNull("Null object creation failed.", msg); @@ -89,8 +95,27 @@ public class PutFileTests extends BaseRpcTests { assertNull(Test.NULL, msg.getSystemFile()); assertNull(Test.NULL, msg.getOffset()); assertNull(Test.NULL, msg.getLength()); + assertNull(Test.NULL, msg.getCRC()); } + /** + * Tests the expected values of the CRC checksum. + */ + public void testByteArrayCheckSum () { + // Test Values + PutFile msgCRC = new PutFile(); + msgCRC.setCRC(Test.GENERAL_BYTE_ARRAY); + Long testCRCByteArray = msgCRC.getCRC(); + + CRC32 crc = new CRC32(); + crc.update(Test.GENERAL_BYTE_ARRAY); + Long crcValue = crc.getValue(); + + assertEquals(Test.MATCH, crcValue, testCRCByteArray); + } + + + /** * Tests a valid JSON construction of this RPC message. */ @@ -115,7 +140,8 @@ public class PutFileTests extends BaseRpcTests { assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, PutFile.KEY_FILE_TYPE), cmd.getFileType().toString()); assertEquals(Test.MATCH, JsonUtils.readStringFromJsonObject(parameters, PutFile.KEY_SDL_FILE_NAME), cmd.getSdlFileName()); assertEquals(Test.MATCH, (Long) JsonUtils.readIntegerFromJsonObject(parameters, PutFile.KEY_OFFSET).longValue(), cmd.getOffset()); - assertEquals(Test.MATCH, (Long) JsonUtils.readIntegerFromJsonObject(parameters, PutFile.KEY_LENGTH).longValue(), cmd.getLength()); + assertEquals(Test.MATCH, (Long) JsonUtils.readIntegerFromJsonObject(parameters, PutFile.KEY_LENGTH).longValue(), cmd.getLength()); + assertEquals(Test.MATCH, (Long) JsonUtils.readIntegerFromJsonObject(parameters, PutFile.KEY_CRC).longValue(), cmd.getCRC()); } catch (JSONException e) { fail(Test.JSON_FAIL); } diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/RPCRequestFactory.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/RPCRequestFactory.java index 983c496a7..7f9986822 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/RPCRequestFactory.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/RPCRequestFactory.java @@ -502,6 +502,7 @@ public class RPCRequestFactory { putFile.setFileType(fileType);
putFile.setPersistentFile(persistentFile);
putFile.setBulkData(fileData);
+ putFile.setCRC(fileData);
return putFile;
}
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java index f6ad166cc..f50c1dd7c 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java @@ -20,6 +20,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.zip.CRC32;
import org.json.JSONArray;
import org.json.JSONException;
@@ -1143,6 +1144,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> putFile.setCorrelationID(POLICIES_CORRELATION_ID);
putFile.setSdlFileName("response_data");
putFile.setFileData(response.toString().getBytes("UTF-8"));
+ putFile.setCRC(response.toString().getBytes());
updateBroadcastIntent(sendIntent, "DATA", "Data from cloud response: " + response.toString());
sendRPCRequestPrivate(putFile);
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/PutFile.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/PutFile.java index 0b13f0a1d..2d9010600 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/PutFile.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/PutFile.java @@ -9,6 +9,7 @@ import com.smartdevicelink.proxy.rpc.listeners.OnPutFileUpdateListener; import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
import java.util.Hashtable;
+import java.util.zip.CRC32;
/**
* Used to push a binary data onto the SDL module from a mobile device, such as
@@ -73,6 +74,14 @@ import java.util.Hashtable; * <td>Minvalue=0; Maxvalue=100000000000</td>
* <td>SmartDeviceLink 2.3.2</td>
* </tr>
+ * <tr>
+ * <td>crc</td>
+ * <td>Long</td>
+ * <td>Additional CRC32 checksum to protect data integrity up to 512 Mbits .</td>
+ * <td>N</td>
+ * <td>minvalue="0" maxvalue="4294967295"</td>
+ * <td>SmartDeviceLink 2.3.2</td>
+ * </tr>
* </table>
* <p> <b>Note: </b></p>
* When using PutFiles you may want to check for memory
@@ -119,7 +128,7 @@ public class PutFile extends RPCRequest { public static final String KEY_SDL_FILE_NAME = "syncFileName";
public static final String KEY_OFFSET = "offset";
public static final String KEY_LENGTH = "length";
-
+ public static final String KEY_CRC = "crc";
/**
* Constructs a new PutFile object
@@ -297,16 +306,61 @@ public class PutFile extends RPCRequest { return null;
}
+ /**
+ * This takes the file data as an array of bytes and calculates the
+ * CRC32 for it.
+ * @param fileData - the file as a byte array
+ */
+ public void setCRC(byte[] fileData) {
+ if (fileData != null) {
+ CRC32 crc = new CRC32();
+ crc.update(fileData);
+ parameters.put(KEY_CRC, crc.getValue());
+ } else {
+ parameters.remove(KEY_CRC);
+ }
+ }
+
+ /**
+ * This assumes you have created your own CRC32 and are setting it with the file
+ * <STRONG>Please avoid using your own calculations for this, and use the method
+ * included in java.util</STRONG>
+ * @param crc - the CRC32 of the file being set
+ */
+ public void setCRC(Long crc) {
+ if (crc != null) {
+ parameters.put(KEY_CRC, crc);
+ } else {
+ parameters.remove(KEY_CRC);
+ }
+ }
+
+ /**
+ * This returns the CRC, if it has been set, for the file object
+ * @return - a CRC32 Long
+ */
+ public Long getCRC() {
+ final Object o = parameters.get(KEY_CRC);
+ if (o == null){
+ return null;
+ }
+ if (o instanceof Integer) {
+ return ((Integer) o).longValue();
+ }else if(o instanceof Long){
+ return (Long) o;
+ }
+ return null;
+ }
@Override
public final void setOnRPCResponseListener(OnRPCResponseListener listener) {
super.setOnRPCResponseListener(listener);
}
-
+
public void setOnPutFileUpdateListener(OnPutFileUpdateListener listener) {
super.setOnRPCResponseListener(listener); //We can use the same method because it get stored as a parent class
}
-
+
public OnPutFileUpdateListener getOnPutFileUpdateListener() {
return (OnPutFileUpdateListener)getOnRPCResponseListener();
}
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/enums/Result.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/enums/Result.java index c0a15f235..25b9a16f5 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/enums/Result.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/rpc/enums/Result.java @@ -177,6 +177,11 @@ public enum Result { * The requested data is read only thus cannot be change via remote control
*/
READ_ONLY,
+ /**
+ * The data sent failed to pass CRC check in receiver end
+ */
+ CORRUPTED_DATA,
+
;
/**
* Convert String to Result
|