mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 05:07:40 +00:00
(un)serialize will now throw instead of failing silently
This commit is contained in:
parent
65c3dc4570
commit
f9aeca8ef9
@ -23,6 +23,7 @@ package org.kde.kdeconnect.Helpers.SecurityHelpers;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
@ -107,7 +108,7 @@ public class RsaHelper {
|
||||
|
||||
}
|
||||
|
||||
public static NetworkPackage encrypt(NetworkPackage np, PublicKey publicKey) throws GeneralSecurityException {
|
||||
public static NetworkPackage encrypt(NetworkPackage np, PublicKey publicKey) throws GeneralSecurityException, JSONException {
|
||||
|
||||
String serialized = np.serialize();
|
||||
|
||||
@ -152,7 +153,7 @@ public class RsaHelper {
|
||||
decryptedJson += decryptedChunk;
|
||||
}
|
||||
|
||||
NetworkPackage decrypted = np.unserialize(decryptedJson);
|
||||
NetworkPackage decrypted = NetworkPackage.unserialize(decryptedJson);
|
||||
decrypted.setPayload(np.getPayload(), np.getPayloadSize());
|
||||
return decrypted;
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
||||
|
||||
@ -134,48 +135,34 @@ public class NetworkPackage {
|
||||
|
||||
public boolean isEncrypted() { return mType.equals(PACKAGE_TYPE_ENCRYPTED); }
|
||||
|
||||
public String serialize() {
|
||||
public String serialize() throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
try {
|
||||
jo.put("id", mId);
|
||||
jo.put("type", mType);
|
||||
jo.put("body", mBody);
|
||||
if (hasPayload()) {
|
||||
jo.put("payloadSize", mPayloadSize);
|
||||
jo.put("payloadTransferInfo", mPayloadTransferInfo);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("NetworkPackage", "Serialization exception");
|
||||
jo.put("id", mId);
|
||||
jo.put("type", mType);
|
||||
jo.put("body", mBody);
|
||||
if (hasPayload()) {
|
||||
jo.put("payloadSize", mPayloadSize);
|
||||
jo.put("payloadTransferInfo", mPayloadTransferInfo);
|
||||
}
|
||||
|
||||
//QJSon does not escape slashes, but Java JSONObject does. Converting to QJson format.
|
||||
String json = jo.toString().replace("\\/","/")+"\n";
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
static public NetworkPackage unserialize(String s) {
|
||||
static public NetworkPackage unserialize(String s) throws JSONException {
|
||||
|
||||
NetworkPackage np = new NetworkPackage();
|
||||
try {
|
||||
JSONObject jo = new JSONObject(s);
|
||||
np.mId = jo.getLong("id");
|
||||
np.mType = jo.getString("type");
|
||||
np.mBody = jo.getJSONObject("body");
|
||||
if (jo.has("payloadSize")) {
|
||||
np.mPayloadTransferInfo = jo.getJSONObject("payloadTransferInfo");
|
||||
np.mPayloadSize = jo.getLong("payloadSize");
|
||||
} else {
|
||||
np.mPayloadTransferInfo = new JSONObject();
|
||||
np.mPayloadSize = 0;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("NetworkPackage", "Unserialization exception unserializing "+s);
|
||||
return null;
|
||||
JSONObject jo = new JSONObject(s);
|
||||
np.mId = jo.getLong("id");
|
||||
np.mType = jo.getString("type");
|
||||
np.mBody = jo.getJSONObject("body");
|
||||
if (jo.has("payloadSize")) {
|
||||
np.mPayloadTransferInfo = jo.getJSONObject("payloadTransferInfo");
|
||||
np.mPayloadSize = jo.getLong("payloadSize");
|
||||
} else {
|
||||
np.mPayloadTransferInfo = new JSONObject();
|
||||
np.mPayloadSize = 0;
|
||||
}
|
||||
|
||||
return np;
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ package org.kde.kdeconnect;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
|
||||
|
||||
import java.security.KeyPair;
|
||||
@ -44,7 +45,7 @@ public class NetworkPackageTest extends AndroidTestCase{
|
||||
// Called after each test
|
||||
}
|
||||
|
||||
public void testNetworkPackage(){
|
||||
public void testNetworkPackage() throws JSONException {
|
||||
NetworkPackage np = new NetworkPackage("com.test");
|
||||
|
||||
np.set("hello", "hola");
|
||||
@ -80,7 +81,7 @@ public class NetworkPackageTest extends AndroidTestCase{
|
||||
|
||||
}
|
||||
|
||||
public void testEncryption(){
|
||||
public void testEncryption() throws JSONException {
|
||||
NetworkPackage original = new NetworkPackage("com.test");
|
||||
original.set("hello", "hola");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user