Temp commit: debug in progress

This commit is contained in:
pedroGitt 2016-10-12 19:04:50 +02:00
parent f2ca1ce304
commit 1679c4513f
3 changed files with 150 additions and 15 deletions

View file

@ -33,8 +33,18 @@ public class OlmSessionTest {
Log.d(LOG_TAG, "## setUpClass(): lib version="+version); Log.d(LOG_TAG, "## setUpClass(): lib version="+version);
} }
/**
* Basic test:
* - alice creates an account
* - bob creates an account
* - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey)
* - alice encrypts a message with its session
* - bob creates an inbound session based on alice's encrypted message
* - bob decrypts the encrypted message with its session
*/
@Test @Test
public void test01AliceToBob() { public void test01AliceToBob() {
final int ONE_TIME_KEYS_NUMBER = 5;
String bobIdentityKey = null; String bobIdentityKey = null;
String bobOneTimeKey=null; String bobOneTimeKey=null;
@ -60,14 +70,13 @@ public class OlmSessionTest {
} }
// get bob one time keys // get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(5)); assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys(); JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj); assertNotNull(bobOneTimeKeysJsonObj);
try { try {
JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY); JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys); assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);
// test the count of the generated one time keys:
Iterator<String> generatedKeysIt = generatedKeys.keys(); Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) { if(generatedKeysIt.hasNext()) {
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
@ -102,9 +111,132 @@ public class OlmSessionTest {
// clean objects.. // clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession.getOlmSessionId())); assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession.getOlmSessionId()));
// release accounts
bobAccount.releaseAccount();
aliceAccount.releaseAccount();
// release sessions
bobSession.releaseSession();
aliceSession.releaseSession();
}
/**
* Same as test01AliceToBob but with bob who's encrypting messages
* to alice and alice decrypt them.<br>
* - alice creates an account
* - bob creates an account
* - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey)
* - alice encrypts a message with its own session
* - bob creates an inbound session based on alice's encrypted message
* - bob decrypts the encrypted message with its own session
* - bob encrypts messages with its own session
* - alice decrypts bob's messages with its own message
*/
@Test
public void test02AliceToBobBackAndForth() {
final int ONE_TIME_KEYS_NUMBER = 1;
String bobIdentityKey = null;
String bobOneTimeKey=null;
// creates alice & bob accounts
OlmAccount aliceAccount = new OlmAccount();
aliceAccount.initNewAccount();
OlmAccount bobAccount = new OlmAccount();
bobAccount.initNewAccount();
// test accounts creation
assertTrue(0!=bobAccount.getOlmAccountId());
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson);
try {
bobIdentityKey = bobIdentityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
assertTrue(null!=bobIdentityKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj);
try {
JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession();
aliceSession.initNewSession();
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
String helloClearMsg = "Hello I'm Alice!";
String goodbyeClearMsg = "Goodbye Alice";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
//OlmMessage encryptedAliceToBobMsg2 = aliceSession.encryptMessage(goodbyeClearMsg);
assertNotNull(encryptedAliceToBobMsg1);
//assertNotNull(encryptedAliceToBobMsg2);
Log.d(LOG_TAG,"## test02AliceToBobBackAndForth(): encryptedMsg="+encryptedAliceToBobMsg1.mCipherText);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession();
bobSession.initNewSession();
assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
//String decryptedMsg02 = bobSession.decryptMessage(encryptedAliceToBobMsg2);
assertNotNull(decryptedMsg01);
//assertNotNull(decryptedMsg02);
// MESSAGE COMPARISON: decrypted vs encrypted
assertTrue(helloClearMsg.equals(decryptedMsg01));
//assertTrue(goodbyeClearMsg.equals(decryptedMsg02));
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession.getOlmSessionId()));
// BACK/FORTH MESSAGE COMPARISON
String clearMsg1 = "Hello I'm Bob!";
String clearMsg2 = "Isn't life grand?";
String clearMsg3 = "Let's go to the opera.";
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
assertNotNull(encryptedMsg1);
/*OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
assertNotNull(encryptedMsg2);
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3);*/
String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
//assertNotNull(decryptedMsg1);
/*String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
//assertNotNull(decryptedMsg2);
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
//assertNotNull(decryptedMsg3);*/
/*assertTrue(clearMsg1.equals(decryptedMsg1));
/* assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3));*/
// clean objects..
bobAccount.releaseAccount(); bobAccount.releaseAccount();
aliceAccount.releaseAccount(); aliceAccount.releaseAccount();
bobSession.releaseSession(); bobSession.releaseSession();
aliceSession.releaseSession(); aliceSession.releaseSession();
} }
} }

View file

@ -446,6 +446,8 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
jfieldID encryptedMsgFieldId; jfieldID encryptedMsgFieldId;
jfieldID typeMsgFieldId; jfieldID typeMsgFieldId;
LOGD("## encryptMessageJni(): IN ");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL"); LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL");
@ -572,6 +574,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
void *plainTextMsgPtr = NULL; void *plainTextMsgPtr = NULL;
char *tempEncryptedPtr = NULL; char *tempEncryptedPtr = NULL;
LOGD("## decryptMessage(): IN ");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
@ -623,7 +626,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
if(maxPlainTextLength == olm_error()) if(maxPlainTextLength == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); const char *errorMsgPtr = olm_session_last_error(sessionPtr);
LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); LOGE("## decryptMessage(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
} }
else else
{ {

View file

@ -136,6 +136,6 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL");
} }
LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId); //LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId);
return instanceId; return instanceId;
} }