identityKeys and oneTimeKeys return Map instead of JSON.

This commit is contained in:
ylecollen 2016-12-21 12:58:00 +01:00
parent c553d18a9a
commit d741c012f3
6 changed files with 152 additions and 90 deletions

View file

@ -38,6 +38,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
@ -130,16 +131,16 @@ public class OlmAccountTest {
*/
@Test
public void test05IdentityKeys() {
JSONObject identityKeysJson = mOlmAccount.identityKeys();
assertNotNull(identityKeysJson);
Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson);
Map<String, String> identityKeys = mOlmAccount.identityKeys();
assertNotNull(identityKeys);
Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys);
// is JSON_KEY_FINGER_PRINT_KEY present?
String fingerPrintKey = TestHelper.getFingerprintKey(identityKeysJson);
String fingerPrintKey = TestHelper.getFingerprintKey(identityKeys);
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
// is JSON_KEY_IDENTITY_KEY present?
String identityKey = TestHelper.getIdentityKey(identityKeysJson);
String identityKey = TestHelper.getIdentityKey(identityKeys);
assertTrue("identity key missing",!TextUtils.isEmpty(identityKey));
}
@ -169,20 +170,19 @@ public class OlmAccountTest {
@Test
public void test08OneTimeKeysJsonFormat() {
int oneTimeKeysCount = 0;
JSONObject generatedKeysJsonObj;
JSONObject oneTimeKeysJson = mOlmAccount.oneTimeKeys();
Map<String, Map<String, String>> oneTimeKeysJson = mOlmAccount.oneTimeKeys();
assertNotNull(oneTimeKeysJson);
try {
generatedKeysJsonObj = oneTimeKeysJson.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=generatedKeysJsonObj);
Map<String, String> map = oneTimeKeysJson.get(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=map);
// test the count of the generated one time keys:
oneTimeKeysCount = generatedKeysJsonObj.length();
oneTimeKeysCount = map.size();
assertTrue("Expected count="+GENERATION_ONE_TIME_KEYS_NUMBER+" found="+oneTimeKeysCount,GENERATION_ONE_TIME_KEYS_NUMBER==oneTimeKeysCount);
} catch (JSONException e) {
} catch (Exception e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
}
@ -244,8 +244,8 @@ public class OlmAccountTest {
assertTrue(0==retValue);
// get keys references
JSONObject identityKeysRef = accountRef.identityKeys();
JSONObject oneTimeKeysRef = accountRef.oneTimeKeys();
Map<String, String> identityKeysRef = accountRef.identityKeys();
Map<String, Map<String, String>> oneTimeKeysRef = accountRef.oneTimeKeys();
assertNotNull(identityKeysRef);
assertNotNull(oneTimeKeysRef);
@ -268,8 +268,8 @@ public class OlmAccountTest {
assertNotNull(accountDeserial);
// get de-serialized keys
JSONObject identityKeysDeserial = accountDeserial.identityKeys();
JSONObject oneTimeKeysDeserial = accountDeserial.oneTimeKeys();
Map<String, String> identityKeysDeserial = accountDeserial.identityKeys();
Map<String, Map<String, String>> oneTimeKeysDeserial = accountDeserial.oneTimeKeys();
assertNotNull(identityKeysDeserial);
assertNotNull(oneTimeKeysDeserial);
@ -363,43 +363,43 @@ public class OlmAccountTest {
OlmAccount account9 = new OlmAccount();
OlmAccount account10 = new OlmAccount();
JSONObject identityKeysJson1 = account1.identityKeys();
JSONObject identityKeysJson2 = account2.identityKeys();
JSONObject identityKeysJson3 = account3.identityKeys();
JSONObject identityKeysJson4 = account4.identityKeys();
JSONObject identityKeysJson5 = account5.identityKeys();
JSONObject identityKeysJson6 = account6.identityKeys();
JSONObject identityKeysJson7 = account7.identityKeys();
JSONObject identityKeysJson8 = account8.identityKeys();
JSONObject identityKeysJson9 = account9.identityKeys();
JSONObject identityKeysJson10 = account10.identityKeys();
Map<String, String> identityKeys1 = account1.identityKeys();
Map<String, String> identityKeys2 = account2.identityKeys();
Map<String, String> identityKeys3 = account3.identityKeys();
Map<String, String> identityKeys4 = account4.identityKeys();
Map<String, String> identityKeys5 = account5.identityKeys();
Map<String, String> identityKeys6 = account6.identityKeys();
Map<String, String> identityKeys7 = account7.identityKeys();
Map<String, String> identityKeys8 = account8.identityKeys();
Map<String, String> identityKeys9 = account9.identityKeys();
Map<String, String> identityKeys10 = account10.identityKeys();
String identityKey1 = TestHelper.getIdentityKey(identityKeysJson1);
String identityKey2 = TestHelper.getIdentityKey(identityKeysJson2);
String identityKey1 = TestHelper.getIdentityKey(identityKeys1);
String identityKey2 = TestHelper.getIdentityKey(identityKeys2);
assertFalse(identityKey1.equals(identityKey2));
String identityKey3 = TestHelper.getIdentityKey(identityKeysJson3);
String identityKey3 = TestHelper.getIdentityKey(identityKeys3);
assertFalse(identityKey2.equals(identityKey3));
String identityKey4 = TestHelper.getIdentityKey(identityKeysJson4);
String identityKey4 = TestHelper.getIdentityKey(identityKeys4);
assertFalse(identityKey3.equals(identityKey4));
String identityKey5 = TestHelper.getIdentityKey(identityKeysJson5);
String identityKey5 = TestHelper.getIdentityKey(identityKeys5);
assertFalse(identityKey4.equals(identityKey5));
String identityKey6 = TestHelper.getIdentityKey(identityKeysJson6);
String identityKey6 = TestHelper.getIdentityKey(identityKeys6);
assertFalse(identityKey5.equals(identityKey6));
String identityKey7 = TestHelper.getIdentityKey(identityKeysJson7);
String identityKey7 = TestHelper.getIdentityKey(identityKeys7);
assertFalse(identityKey6.equals(identityKey7));
String identityKey8 = TestHelper.getIdentityKey(identityKeysJson8);
String identityKey8 = TestHelper.getIdentityKey(identityKeys8);
assertFalse(identityKey7.equals(identityKey8));
String identityKey9 = TestHelper.getIdentityKey(identityKeysJson9);
String identityKey9 = TestHelper.getIdentityKey(identityKeys9);
assertFalse(identityKey8.equals(identityKey9));
String identityKey10 = TestHelper.getIdentityKey(identityKeysJson10);
String identityKey10 = TestHelper.getIdentityKey(identityKeys10);
assertFalse(identityKey9.equals(identityKey10));
account1.releaseAccount();

View file

@ -33,6 +33,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
@ -93,14 +94,14 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
// CREATE ALICE SESSION
@ -186,14 +187,14 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
// CREATE ALICE SESSION
@ -358,16 +359,16 @@ public class OlmSessionTest {
}
// get bob/luke identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
JSONObject aliceIdentityKeysJson = aliceAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeysJson);
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> aliceIdentityKeys = aliceAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeys);
// get bob/luke one time keys
assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
assertTrue(0 == aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj, 1);
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeys, 1);
// create alice inbound session for bob
assertTrue(0==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey1));
@ -428,14 +429,14 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
// CREATE ALICE SESSION
@ -567,15 +568,15 @@ public class OlmSessionTest {
}
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null != bobIdentityKey);
// get bob one time keys
assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj);
String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeys);
String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
// CREATE ALICE SESSION

View file

@ -27,6 +27,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.util.Map;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@ -76,9 +78,9 @@ public class OlmUtilityTest {
assertNotNull(messageSignature);
// get identities key (finger print key)
JSONObject identityKeysJson = account.identityKeys();
assertNotNull(identityKeysJson);
fingerPrintKey = TestHelper.getFingerprintKey(identityKeysJson);
Map<String, String> identityKeys = account.identityKeys();
assertNotNull(identityKeys);
fingerPrintKey = TestHelper.getFingerprintKey(identityKeys);
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
// instantiate utility object

View file

@ -19,7 +19,9 @@ package org.matrix.olm;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@ -31,15 +33,15 @@ public class TestHelper {
/**
* Return the identity key {@link OlmAccount#JSON_KEY_IDENTITY_KEY} from the JSON object.
* @param aIdentityKeysObj JSON result of {@link OlmAccount#identityKeys()}
* @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()}
* @return identity key string if operation succeed, null otherwise
*/
static public String getIdentityKey(JSONObject aIdentityKeysObj){
static public String getIdentityKey(Map<String, String> aIdentityKeysMap){
String idKey = null;
try {
idKey = aIdentityKeysObj.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
} catch (JSONException e) {
idKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_IDENTITY_KEY);
} catch (Exception e) {
assertTrue("Exception MSg=" + e.getMessage(), false);
}
return idKey;
@ -47,15 +49,15 @@ public class TestHelper {
/**
* Return the fingerprint key {@link OlmAccount#JSON_KEY_FINGER_PRINT_KEY} from the JSON object.
* @param aIdentityKeysObj JSON result of {@link OlmAccount#identityKeys()}
* @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()}
* @return fingerprint key string if operation succeed, null otherwise
*/
static public String getFingerprintKey(JSONObject aIdentityKeysObj){
static public String getFingerprintKey(Map<String, String> aIdentityKeysMap) {
String fingerprintKey = null;
try {
fingerprintKey = aIdentityKeysObj.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY);
} catch (JSONException e) {
fingerprintKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_FINGER_PRINT_KEY);
} catch (Exception e) {
assertTrue("Exception MSg=" + e.getMessage(), false);
}
return fingerprintKey;
@ -63,26 +65,19 @@ public class TestHelper {
/**
* Return the first one time key from the JSON object.
* @param aIdentityKeysObj JSON result of {@link OlmAccount#oneTimeKeys()}
* @param aIdentityKeysMap result of {@link OlmAccount#oneTimeKeys()}
* @param aKeyPosition the position of the key to be retrieved
* @return one time key string if operation succeed, null otherwise
*/
static public String getOneTimeKey(JSONObject aIdentityKeysObj, int aKeyPosition) {
static public String getOneTimeKey(Map<String, Map<String, String>> aIdentityKeysMap, int aKeyPosition) {
String firstOneTimeKey = null;
int i=0;
try {
JSONObject generatedKeys = aIdentityKeysObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
Map<String, String> generatedKeys = aIdentityKeysMap.get(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", generatedKeys);
Iterator<String> generatedKeysIt = generatedKeys.keys();
while(i<aKeyPosition) {
if (generatedKeysIt.hasNext()) {
firstOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
i++;
}
}
} catch (JSONException e) {
firstOneTimeKey = (new ArrayList<>(generatedKeys.values())).get(aKeyPosition - 1);
} catch (Exception e) {
assertTrue("Exception Msg=" + e.getMessage(), false);
}
return firstOneTimeKey;

View file

@ -26,6 +26,9 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Account class used to create Olm sessions in conjunction with {@link OlmSession} class.<br>
@ -226,16 +229,16 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
private native long createNewAccountJni();
/**
* Return the identity keys (identity and fingerprint keys) in a JSON array.<br>
* Return the identity keys (identity and fingerprint keys) in a dictionary.<br>
* Public API for {@link #identityKeysJni()}.<br>
* Ex:<tt>
* {
* "curve25519":"Vam++zZPMqDQM6ANKpO/uAl5ViJSHxV9hd+b0/fwRAg",
* "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I"
* }</tt>
* @return identity keys in JSON array if operation succeed, null otherwise
* @return identity keys dictionary if operation succeeds, null otherwise
*/
public JSONObject identityKeys() {
public Map<String, String> identityKeys() {
JSONObject identityKeysJsonObj = null;
byte identityKeysBuffer[];
@ -251,7 +254,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
Log.e(LOG_TAG, "## identityKeys(): Failure - identityKeysJni()=null");
}
return identityKeysJsonObj;
return toStringMap(identityKeysJsonObj);
}
/**
* Get the public identity keys (Ed25519 fingerprint key and Curve25519 identity key).<br>
@ -283,7 +286,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
private native int generateOneTimeKeysJni(int aNumberOfKeys);
/**
* Return the "one time keys" in a JSON array.<br>
* Return the "one time keys" in a dictionary.<br>
* The number of "one time keys", is specified by {@link #generateOneTimeKeys(int)}<br>
* Ex:<tt>
* { "curve25519":
@ -295,9 +298,9 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* }</tt><br>
* Public API for {@link #oneTimeKeysJni()}.<br>
* Note: these keys are to be published on the server.
* @return one time keys in JSON array format if operation succeed, null otherwise
* @return one time keys in string dictionary if operation succeed, null otherwise
*/
public JSONObject oneTimeKeys() {
public Map<String, Map<String, String>> oneTimeKeys() {
byte identityKeysBuffer[];
JSONObject oneTimeKeysJsonObj = null;
@ -313,7 +316,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
Log.e(LOG_TAG, "## oneTimeKeys(): Failure - identityKeysJni()=null");
}
return oneTimeKeysJsonObj;
return toStringMapMap(oneTimeKeysJsonObj);
}
/**
* Get the public parts of the unpublished "one time keys" for the account.<br>
@ -373,4 +376,65 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
public int getUnreleasedCount() {
return mUnreleasedCount;
}
/**
* Build a string-string dictionary from a jsonObject.<br>
* @param jsonObject the object to parse
* @return the map
*/
private static Map<String, String> toStringMap(JSONObject jsonObject) {
if (null != jsonObject) {
HashMap<String, String> map = new HashMap<>();
Iterator<String> keysItr = jsonObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
try {
Object value = jsonObject.get(key);
if (value instanceof String) {
map.put(key, (String) value);
} else {
Log.e(LOG_TAG, "## toStringMap(): unexpected type " + value.getClass());
}
} catch (Exception e) {
Log.e(LOG_TAG, "## toStringMap(): failed " + e.getMessage());
}
}
return map;
}
return null;
}
/**
* Build a string-string dictionary of string dictionary from a jsonObject.<br>
* @param jsonObject the object to parse
* @return the map
*/
private static Map<String, Map<String, String>> toStringMapMap(JSONObject jsonObject) {
if (null != jsonObject) {
HashMap<String, Map<String, String>> map = new HashMap<>();
Iterator<String> keysItr = jsonObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
try {
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
map.put(key, toStringMap((JSONObject) value));
} else {
Log.e(LOG_TAG, "## toStringMapMap(): unexpected type " + value.getClass());
}
} catch (Exception e) {
Log.e(LOG_TAG, "## toStringMapMap(): failed " + e.getMessage());
}
}
return map;
}
return null;
}
}

View file

@ -468,7 +468,7 @@ JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject
olm_get_library_version(&majorVer, &minorVer, &patchVer);
LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer);
snprintf(buff, sizeof(buff), " V%d.%d.%d", majorVer, minorVer, patchVer);
snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer);
returnValueStr = env->NewStringUTF((const char*)buff);
return returnValueStr;