aboutsummaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorpedroGitt <pedro.contreiras@amdocs.com>2016-10-12 19:04:50 +0200
committerpedroGitt <pedro.contreiras@amdocs.com>2016-10-12 19:04:50 +0200
commit1679c4513f8e4965ad44b4fdad22cfda609e16fc (patch)
treee6aa69f1bf5a0681814d51158b505b1f8bdeab62 /java
parentf2ca1ce304deee9e8e858ea4a71ea22ba38a342b (diff)
Temp commit: debug in progress
Diffstat (limited to 'java')
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java136
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp27
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp2
3 files changed, 150 insertions, 15 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
index fb2eebc..93e70c5 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
@@ -33,8 +33,18 @@ public class OlmSessionTest {
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
public void test01AliceToBob() {
+ final int ONE_TIME_KEYS_NUMBER = 5;
String bobIdentityKey = null;
String bobOneTimeKey=null;
@@ -60,14 +70,13 @@ public class OlmSessionTest {
}
// get bob one time keys
- assertTrue(0==bobAccount.generateOneTimeKeys(5));
+ 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);
- // test the count of the generated one time keys:
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
@@ -102,9 +111,132 @@ public class OlmSessionTest {
// clean objects..
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();
+ aliceAccount.releaseAccount();
+ bobSession.releaseSession();
+ aliceSession.releaseSession();
+ }
+
}
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp
index 0febc02..23cbaee 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp
@@ -446,6 +446,8 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
jfieldID encryptedMsgFieldId;
jfieldID typeMsgFieldId;
+ LOGD("## encryptMessageJni(): IN ");
+
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{
LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL");
@@ -572,34 +574,35 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
void *plainTextMsgPtr = NULL;
char *tempEncryptedPtr = NULL;
+ LOGD("## decryptMessage(): IN ");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{
- LOGE("## decryptMessage(): failure - invalid Session ptr=NULL");
+ LOGE("## decryptMessage(): failure - invalid Session ptr=NULL");
}
else if(0 == aEncryptedMsg)
{
- LOGE("## decryptMessage(): failure - invalid clear message");
+ LOGE("## decryptMessage(): failure - invalid clear message");
}
else if(0 == (encryptedMsgJclass = env->GetObjectClass(aEncryptedMsg)))
{
- LOGE("## decryptMessage(): failure - unable to get crypted message class");
+ LOGE("## decryptMessage(): failure - unable to get crypted message class");
}
else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJclass,"mCipherText","Ljava/lang/String;")))
{
- LOGE("## decryptMessage(): failure - unable to get message field");
+ LOGE("## decryptMessage(): failure - unable to get message field");
}
else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJclass,"mType","J")))
{
- LOGE("## decryptMessage(): failure - unable to get message type field");
+ LOGE("## decryptMessage(): failure - unable to get message type field");
}
else if(0 == (encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId)))
{
- LOGE("## decryptMessage(): failure - JNI encrypted object ");
+ LOGE("## decryptMessage(): failure - JNI encrypted object ");
}
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0)))
{
- LOGE("## decryptMessage(): failure - encrypted message JNI allocation OOM");
+ LOGE("## decryptMessage(): failure - encrypted message JNI allocation OOM");
}
else
{
@@ -611,7 +614,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
// create a dedicated temp buffer to be used in next Olm API calls
tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t)));
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
- LOGD("## decryptMessageJni(): encryptedMsgType=%lld encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgType,encryptedMsgLength,encryptedMsgPtr);
+ LOGD("## decryptMessageJni(): encryptedMsgType=%lld encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgType,encryptedMsgLength,encryptedMsgPtr);
// get max plaintext length
size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr,
@@ -623,11 +626,11 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
if(maxPlainTextLength == olm_error())
{
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
{
- LOGD("## decryptMessage(): maxPlaintextLength=%lu",maxPlainTextLength);
+ LOGD("## decryptMessage(): maxPlaintextLength=%lu",maxPlainTextLength);
// allocate output decrypted message
plainTextMsgPtr = static_cast<void*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
@@ -643,12 +646,12 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz
if(plaintextLength == olm_error())
{
const char *errorMsgPtr = olm_session_last_error(sessionPtr);
- LOGE("## decryptMessage(): failure - olm_decrypt Msg=%s",errorMsgPtr);
+ LOGE("## decryptMessage(): failure - olm_decrypt Msg=%s",errorMsgPtr);
}
else
{
(static_cast<char*>(plainTextMsgPtr))[plaintextLength] = static_cast<char>('\0');
- LOGD("## decryptMessage(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, static_cast<char*>(plainTextMsgPtr));
+ LOGD("## decryptMessage(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, static_cast<char*>(plainTextMsgPtr));
decryptedMsgRetValue = env->NewStringUTF(static_cast<const char*>(plainTextMsgPtr));
}
}
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp
index 99df7f5..c27fe7c 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp
@@ -136,6 +136,6 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL");
}
- LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId);
+ //LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId);
return instanceId;
}