Commit 88548f68 authored by ylecollen's avatar ylecollen
Browse files

OlmAccount methods trigger an exception when they fail.

parent 570e8bbe
......@@ -128,7 +128,12 @@ public class OlmAccountTest {
*/
@Test
public void test05IdentityKeys() {
Map<String, String> identityKeys = mOlmAccount.identityKeys();
Map<String, String> identityKeys = null;
try {
identityKeys = mOlmAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
assertNotNull(identityKeys);
Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys);
......@@ -157,8 +162,15 @@ public class OlmAccountTest {
*/
@Test
public void test07GenerateOneTimeKeys() {
int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
assertTrue(0==retValue);
String error = null;
try {
mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
error = e.getMessage();
}
assertTrue(null == error);
}
/**
......@@ -167,7 +179,13 @@ public class OlmAccountTest {
@Test
public void test08OneTimeKeysJsonFormat() {
int oneTimeKeysCount = 0;
Map<String, Map<String, String>> oneTimeKeysJson = mOlmAccount.oneTimeKeys();
Map<String, Map<String, String>> oneTimeKeysJson = null;
try {
oneTimeKeysJson = mOlmAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(oneTimeKeysJson);
try {
......@@ -195,26 +213,40 @@ public class OlmAccountTest {
long sessionId = olmSession.getOlmSessionId();
assertTrue(0 != sessionId);
int sessionRetCode = mOlmAccount.removeOneTimeKeysForSession(olmSession);
// test against no matching keys
assertTrue(1 == sessionRetCode);
String errorMessage = null;
try {
mOlmAccount.removeOneTimeKeys(olmSession);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
olmSession.releaseSession();
sessionId = olmSession.getOlmSessionId();
assertTrue("sessionRetCode="+sessionRetCode,0 == sessionId);
assertTrue(0 == sessionId);
}
@Test
public void test11MarkOneTimeKeysAsPublished() {
int retCode = mOlmAccount.markOneTimeKeysAsPublished();
// if OK => retCode=0
assertTrue(0 == retCode);
try {
mOlmAccount.markOneTimeKeysAsPublished();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
}
@Test
public void test12SignMessage() {
String clearMsg = "String to be signed by olm";
String signedMsg = mOlmAccount.signMessage(clearMsg);
String signedMsg = null;
try {
signedMsg = mOlmAccount.signMessage(clearMsg);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(signedMsg);
// additional tests are performed in test01VerifyEd25519Signing()
}
......@@ -237,12 +269,29 @@ public class OlmAccountTest {
assertTrue(e.getMessage(),false);
}
int retValue = accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
assertTrue(0==retValue);
try {
accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(),false);
}
// get keys references
Map<String, String> identityKeysRef = accountRef.identityKeys();
Map<String, Map<String, String>> oneTimeKeysRef = accountRef.oneTimeKeys();
Map<String, String> identityKeysRef = null;
try {
identityKeysRef = accountRef.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
Map<String, Map<String, String>> oneTimeKeysRef = null;
try {
oneTimeKeysRef = accountRef.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(identityKeysRef);
assertNotNull(oneTimeKeysRef);
......@@ -304,12 +353,25 @@ public class OlmAccountTest {
@Test
public void test14GenerateOneTimeKeysError() {
// keys number = 0 => no error
int retValue = mOlmAccount.generateOneTimeKeys(0);
assertTrue(0==retValue);
String errorMessage = null;
try {
mOlmAccount.generateOneTimeKeys(0);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null == errorMessage);
// keys number = negative value
retValue = mOlmAccount.generateOneTimeKeys(-50);
assertTrue(-1==retValue);
errorMessage = null;
try {
mOlmAccount.generateOneTimeKeys(-50);
} catch (Exception e) {
errorMessage = e.getMessage();
}
assertTrue(null != errorMessage);
}
@Test
......@@ -321,9 +383,16 @@ public class OlmAccountTest {
assertTrue(e.getMessage(),false);
}
int sessionRetCode = olmAccount.removeOneTimeKeysForSession(null);
boolean sessionRetCode = true;
try {
sessionRetCode = olmAccount.removeOneTimeKeys(null);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
// test against no matching keys
assertTrue(-1 == sessionRetCode);
assertFalse(sessionRetCode);
olmAccount.releaseAccount();
}
......@@ -336,7 +405,13 @@ public class OlmAccountTest {
} catch (OlmException e) {
assertTrue(e.getMessage(),false);
}
String signedMsg = olmAccount.signMessage(null);
String signedMsg = null;
try {
signedMsg = olmAccount.signMessage(null);
} catch (Exception e) {
}
assertNull(signedMsg);
olmAccount.releaseAccount();
......
......@@ -92,13 +92,32 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> bobIdentityKeys = null;
try {
bobIdentityKeys = bobAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
try {
bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
Map<String, Map<String, String>> bobOneTimeKeys = null;
try {
bobOneTimeKeys = bobAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
......@@ -141,7 +160,13 @@ public class OlmSessionTest {
assertTrue(clearMsg.equals(decryptedMsg));
// clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
boolean res = false;
try {
res = bobAccount.removeOneTimeKeys(bobSession);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(res);
// release accounts
bobAccount.releaseAccount();
......@@ -191,13 +216,32 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> bobIdentityKeys = null;
try {
bobIdentityKeys = bobAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
try {
bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
Map<String, Map<String, String>> bobOneTimeKeys = null;
try {
bobOneTimeKeys = bobAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
......@@ -279,7 +323,14 @@ public class OlmSessionTest {
assertTrue(clearMsg1.equals(decryptedMsg1));
// clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
boolean res = false;
try {
res = bobAccount.removeOneTimeKeys(bobSession);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(res);
bobAccount.releaseAccount();
aliceAccount.releaseAccount();
assertTrue(bobAccount.isReleased());
......@@ -369,15 +420,46 @@ public class OlmSessionTest {
}
// get bob/luke identity key
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> aliceIdentityKeys = aliceAccount.identityKeys();
Map<String, String> bobIdentityKeys = null;
try {
bobIdentityKeys = bobAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
Map<String, String> aliceIdentityKeys = null;
try {
aliceIdentityKeys = aliceAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
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));
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
try {
bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
try {
aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
Map<String, Map<String, String>> bobOneTimeKeys = null;
try {
bobOneTimeKeys = bobAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeys, 1);
// create alice inbound session for bob
......@@ -401,7 +483,14 @@ public class OlmSessionTest {
//assertTrue(false==bobSession.matchesInboundSessionFrom(bobIdentityKey, encryptedAliceToBobMsg1.mCipherText));
// release objects
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
boolean res = false;
try {
res = bobAccount.removeOneTimeKeys(bobSession);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(res);
aliceAccount.releaseAccount();
bobAccount.releaseAccount();
assertTrue(aliceAccount.isReleased());
......@@ -443,13 +532,32 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> bobIdentityKeys = null;
try {
bobIdentityKeys = bobAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null!=bobIdentityKey);
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
try {
bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
Map<String, Map<String, String>> bobOneTimeKeys = null;
try {
bobOneTimeKeys = bobAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
......@@ -538,7 +646,14 @@ public class OlmSessionTest {
assertTrue(clearMsg3.equals(decryptedMsg3));
// clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
boolean res = false;
try {
res = bobAccount.removeOneTimeKeys(bobSession);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(res);
bobAccount.releaseAccount();
aliceAccount.releaseAccount();
assertTrue(bobAccount.isReleased());
......@@ -588,13 +703,32 @@ public class OlmSessionTest {
}
// get bob identity key
Map<String, String> bobIdentityKeys = bobAccount.identityKeys();
Map<String, String> bobIdentityKeys = null;
try {
bobIdentityKeys = bobAccount.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys);
assertTrue(null != bobIdentityKey);
// get bob one time keys
assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
Map<String, Map<String, String>> bobOneTimeKeys = bobAccount.oneTimeKeys();
try {
bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
Map<String, Map<String, String>> bobOneTimeKeys = null;
try {
bobOneTimeKeys = bobAccount.oneTimeKeys();
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(bobOneTimeKeys);
String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1);
assertNotNull(bobOneTimeKey);
......@@ -678,7 +812,14 @@ public class OlmSessionTest {
assertTrue(!aliceSession.matchesInboundSessionFrom(null,null));
// release objects
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
boolean res = false;
try {
res = bobAccount.removeOneTimeKeys(bobSession);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertTrue(res);
aliceAccount.releaseAccount();
bobAccount.releaseAccount();
assertTrue(aliceAccount.isReleased());
......
......@@ -71,11 +71,25 @@ public class OlmUtilityTest {
assertNotNull(account);
// sign message
String messageSignature = account.signMessage(message);
String messageSignature = null;
try {
messageSignature = account.signMessage(message);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
assertNotNull(messageSignature);
// get identities key (finger print key)
Map<String, String> identityKeys = account.identityKeys();
Map<String, String> identityKeys = null;
try {
identityKeys = account.identityKeys();
} catch (Exception e) {
assertTrue("identityKeys failed " + e.getMessage(), false);
}
assertNotNull(identityKeys);
fingerPrintKey = TestHelper.getFingerprintKey(identityKeys);
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
......
......@@ -63,9 +63,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
private transient long mNativeId;
public OlmAccount() throws OlmException {
if(!initNewAccount()) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
}
initNewAccount();
}
/**
......@@ -97,11 +95,14 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* Create and initialize a native account instance.<br>
* Wrapper for {@link #initNewAccountJni()}.
* To be called before any other API call.
* @return true if init succeed, false otherwise.
* @exception OlmException the failure reason
*/
private boolean initNewAccount() {
private void initNewAccount() throws OlmException {
try {
mNativeId = initNewAccountJni();
return (0 != mNativeId);
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage());
}
}
/**
......@@ -147,10 +148,19 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I"
* }</tt>
* @return identity keys dictionary if operation succeeds, null otherwise
* @exception OlmException the failure reason
*/
public Map<String, String> identityKeys() {
public Map<String, String> identityKeys() throws OlmException {
JSONObject identityKeysJsonObj = null;
byte[] identityKeysBuffer = identityKeysJni();
byte[] identityKeysBuffer;
try {
identityKeysBuffer = identityKeysJni();
} catch (Exception e) {
Log.e(LOG_TAG, "## identityKeys(): Failure - " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS, e.getMessage());
}
if (null != identityKeysBuffer) {
try {
......@@ -165,6 +175,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
return toStringMap(identityKeysJsonObj);
}
/**
* Get the public identity keys (Ed25519 fingerprint key and Curve25519 identity key).<br>
* Keys are Base64 encoded.
......@@ -180,6 +191,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
public long maxOneTimeKeys() {
return maxOneTimeKeysJni();
}
private native long maxOneTimeKeysJni();
/**
......@@ -187,12 +199,17 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* by this account exceeds {@link #maxOneTimeKeys()}, the old keys are discarded.<br>
* The corresponding keys are retrieved by {@link #oneTimeKeys()}.
* @param aNumberOfKeys number of keys to generate
* @return 0 if operation succeed, -1 otherwise
* @exception OlmException the failure reason
*/
public int generateOneTimeKeys(int aNumberOfKeys) {
return generateOneTimeKeysJni(aNumberOfKeys);
public void generateOneTimeKeys(int aNumberOfKeys) throws OlmException {
try {
generateOneTimeKeysJni(aNumberOfKeys);
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS, e.getMessage());
}
}
private native int generateOneTimeKeysJni(int aNumberOfKeys);
private native void generateOneTimeKeysJni(int aNumberOfKeys);