Temp commit: debug in progress
This commit is contained in:
parent
f2ca1ce304
commit
1679c4513f
3 changed files with 150 additions and 15 deletions
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,34 +574,35 @@ 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)))
|
||||||
{
|
{
|
||||||
LOGE("## decryptMessage(): failure - invalid Session ptr=NULL");
|
LOGE("## decryptMessage(): failure - invalid Session ptr=NULL");
|
||||||
}
|
}
|
||||||
else if(0 == aEncryptedMsg)
|
else if(0 == aEncryptedMsg)
|
||||||
{
|
{
|
||||||
LOGE("## decryptMessage(): failure - invalid clear message");
|
LOGE("## decryptMessage(): failure - invalid clear message");
|
||||||
}
|
}
|
||||||
else if(0 == (encryptedMsgJclass = env->GetObjectClass(aEncryptedMsg)))
|
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;")))
|
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")))
|
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)))
|
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)))
|
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
|
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
|
// create a dedicated temp buffer to be used in next Olm API calls
|
||||||
tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t)));
|
tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t)));
|
||||||
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
|
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
|
// get max plaintext length
|
||||||
size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr,
|
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())
|
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
|
||||||
{
|
{
|
||||||
LOGD("## decryptMessage(): maxPlaintextLength=%lu",maxPlainTextLength);
|
LOGD("## decryptMessage(): maxPlaintextLength=%lu",maxPlainTextLength);
|
||||||
|
|
||||||
// allocate output decrypted message
|
// allocate output decrypted message
|
||||||
plainTextMsgPtr = static_cast<void*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
|
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())
|
if(plaintextLength == olm_error())
|
||||||
{
|
{
|
||||||
const char *errorMsgPtr = olm_session_last_error(sessionPtr);
|
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
|
else
|
||||||
{
|
{
|
||||||
(static_cast<char*>(plainTextMsgPtr))[plaintextLength] = static_cast<char>('\0');
|
(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));
|
decryptedMsgRetValue = env->NewStringUTF(static_cast<const char*>(plainTextMsgPtr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue