Commit 1694f15f authored by Benoit Marty's avatar Benoit Marty
Browse files

Fix test compilation and run test with success

Using command `./gradlew connectedAndroidTest` on an API 21 emulator
parent 4d2522a6
...@@ -19,4 +19,5 @@ ...@@ -19,4 +19,5 @@
#systemProp.https.proxyHost=batproxy #systemProp.https.proxyHost=batproxy
#systemProp.http.proxyPort=8080 #systemProp.http.proxyPort=8080
android.useAndroidX=true
org.gradle.configureondemand=false org.gradle.configureondemand=false
...@@ -6,9 +6,14 @@ android { ...@@ -6,9 +6,14 @@ android {
compileSdkVersion 30 compileSdkVersion 30
defaultConfig { defaultConfig {
minSdkVersion 11 minSdkVersion 14
targetSdkVersion 30 targetSdkVersion 30
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
} }
buildTypes { buildTypes {
debug { debug {
...@@ -118,6 +123,8 @@ dependencies { ...@@ -118,6 +123,8 @@ dependencies {
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'junit:junit:4.13.2' androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:core:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0' androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
} }
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
package org.matrix.olm; package org.matrix.olm;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.json.JSONException; import androidx.test.core.app.ApplicationProvider;
import org.json.JSONObject; import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
...@@ -41,7 +41,6 @@ import java.io.ObjectInputStream; ...@@ -41,7 +41,6 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.Map; import java.util.Map;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
...@@ -59,15 +58,15 @@ public class OlmAccountTest { ...@@ -59,15 +58,15 @@ public class OlmAccountTest {
private final String FILE_NAME = "SerialTestFile"; private final String FILE_NAME = "SerialTestFile";
@BeforeClass @BeforeClass
public static void setUpClass(){ public static void setUpClass() {
// load native lib // load native lib
mOlmManager = new OlmManager(); mOlmManager = new OlmManager();
String olmLibVersion = mOlmManager.getOlmLibVersion(); String olmLibVersion = mOlmManager.getOlmLibVersion();
assertNotNull(olmLibVersion); assertNotNull(olmLibVersion);
String olmSdkVersion = mOlmManager.getDetailedVersion(getInstrumentation().getContext()); String olmSdkVersion = mOlmManager.getDetailedVersion(ApplicationProvider.getApplicationContext());
assertNotNull(olmLibVersion); assertNotNull(olmLibVersion);
Log.d(LOG_TAG, "## setUpClass(): Versions - Android Olm SDK = "+olmSdkVersion+" Olm lib ="+olmLibVersion); Log.d(LOG_TAG, "## setUpClass(): Versions - Android Olm SDK = " + olmSdkVersion + " Olm lib =" + olmLibVersion);
} }
@AfterClass @AfterClass
...@@ -77,7 +76,7 @@ public class OlmAccountTest { ...@@ -77,7 +76,7 @@ public class OlmAccountTest {
@Before @Before
public void setUp() { public void setUp() {
if(mIsAccountCreated) { if (mIsAccountCreated) {
assertNotNull(mOlmAccount); assertNotNull(mOlmAccount);
} }
} }
...@@ -119,8 +118,8 @@ public class OlmAccountTest { ...@@ -119,8 +118,8 @@ public class OlmAccountTest {
@Test @Test
public void test04GetOlmAccountId() { public void test04GetOlmAccountId() {
long olmNativeInstance = mOlmAccount.getOlmAccountId(); long olmNativeInstance = mOlmAccount.getOlmAccountId();
Log.d(LOG_TAG,"## testGetOlmAccountId olmNativeInstance="+olmNativeInstance); Log.d(LOG_TAG, "## testGetOlmAccountId olmNativeInstance=" + olmNativeInstance);
assertTrue(0!=olmNativeInstance); assertTrue(0 != olmNativeInstance);
} }
/** /**
...@@ -137,15 +136,15 @@ public class OlmAccountTest { ...@@ -137,15 +136,15 @@ public class OlmAccountTest {
assertTrue("identityKeys failed " + e.getMessage(), false); assertTrue("identityKeys failed " + e.getMessage(), false);
} }
assertNotNull(identityKeys); assertNotNull(identityKeys);
Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys); Log.d(LOG_TAG, "## testIdentityKeys Keys=" + identityKeys);
// is JSON_KEY_FINGER_PRINT_KEY present? // is JSON_KEY_FINGER_PRINT_KEY present?
String fingerPrintKey = TestHelper.getFingerprintKey(identityKeys); String fingerPrintKey = TestHelper.getFingerprintKey(identityKeys);
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); assertTrue("fingerprint key missing", !TextUtils.isEmpty(fingerPrintKey));
// is JSON_KEY_IDENTITY_KEY present? // is JSON_KEY_IDENTITY_KEY present?
String identityKey = TestHelper.getIdentityKey(identityKeys); String identityKey = TestHelper.getIdentityKey(identityKeys);
assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); assertTrue("identity key missing", !TextUtils.isEmpty(identityKey));
} }
//**************************************************** //****************************************************
...@@ -154,9 +153,9 @@ public class OlmAccountTest { ...@@ -154,9 +153,9 @@ public class OlmAccountTest {
@Test @Test
public void test06MaxOneTimeKeys() { public void test06MaxOneTimeKeys() {
long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys(); long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys();
Log.d(LOG_TAG,"## testMaxOneTimeKeys(): maxOneTimeKeys="+maxOneTimeKeys); Log.d(LOG_TAG, "## testMaxOneTimeKeys(): maxOneTimeKeys=" + maxOneTimeKeys);
assertTrue(maxOneTimeKeys>0); assertTrue(maxOneTimeKeys > 0);
} }
/** /**
...@@ -192,15 +191,15 @@ public class OlmAccountTest { ...@@ -192,15 +191,15 @@ public class OlmAccountTest {
try { try {
Map<String, String> map = oneTimeKeysJson.get(OlmAccount.JSON_KEY_ONE_TIME_KEY); Map<String, String> map = oneTimeKeysJson.get(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=map); assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", null != map);
// test the count of the generated one time keys: // test the count of the generated one time keys:
oneTimeKeysCount = map.size(); oneTimeKeysCount = map.size();
assertTrue("Expected count="+GENERATION_ONE_TIME_KEYS_NUMBER+" found="+oneTimeKeysCount,GENERATION_ONE_TIME_KEYS_NUMBER==oneTimeKeysCount); assertTrue("Expected count=" + GENERATION_ONE_TIME_KEYS_NUMBER + " found=" + oneTimeKeysCount, GENERATION_ONE_TIME_KEYS_NUMBER == oneTimeKeysCount);
} catch (Exception e) { } catch (Exception e) {
assertTrue("Exception MSg="+e.getMessage(), false); assertTrue("Exception MSg=" + e.getMessage(), false);
} }
} }
...@@ -210,7 +209,7 @@ public class OlmAccountTest { ...@@ -210,7 +209,7 @@ public class OlmAccountTest {
try { try {
olmSession = new OlmSession(); olmSession = new OlmSession();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg=" + e.getMessage(), false);
} }
long sessionId = olmSession.getOlmSessionId(); long sessionId = olmSession.getOlmSessionId();
assertTrue(0 != sessionId); assertTrue(0 != sessionId);
...@@ -241,7 +240,7 @@ public class OlmAccountTest { ...@@ -241,7 +240,7 @@ public class OlmAccountTest {
@Test @Test
public void test12SignMessage() { public void test12SignMessage() {
String clearMsg = "String to be signed by olm"; String clearMsg = "String to be signed by olm";
String signedMsg = null; String signedMsg = null;
try { try {
signedMsg = mOlmAccount.signMessage(clearMsg); signedMsg = mOlmAccount.signMessage(clearMsg);
...@@ -268,13 +267,13 @@ public class OlmAccountTest { ...@@ -268,13 +267,13 @@ public class OlmAccountTest {
try { try {
accountRef = new OlmAccount(); accountRef = new OlmAccount();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue(e.getMessage(),false); assertTrue(e.getMessage(), false);
} }
try { try {
accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e.getMessage(),false); assertTrue(e.getMessage(), false);
} }
// get keys references // get keys references
...@@ -298,7 +297,7 @@ public class OlmAccountTest { ...@@ -298,7 +297,7 @@ public class OlmAccountTest {
assertNotNull(oneTimeKeysRef); assertNotNull(oneTimeKeysRef);
try { try {
Context context = getInstrumentation().getContext(); Context context = ApplicationProvider.getApplicationContext();
//context.getFilesDir(); //context.getFilesDir();
fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
...@@ -316,7 +315,7 @@ public class OlmAccountTest { ...@@ -316,7 +315,7 @@ public class OlmAccountTest {
assertNotNull(accountDeserial); assertNotNull(accountDeserial);
// get de-serialized keys // get de-serialized keys
Map<String, String> identityKeysDeserial = accountDeserial.identityKeys(); Map<String, String> identityKeysDeserial = accountDeserial.identityKeys();
Map<String, Map<String, String>> oneTimeKeysDeserial = accountDeserial.oneTimeKeys(); Map<String, Map<String, String>> oneTimeKeysDeserial = accountDeserial.oneTimeKeys();
assertNotNull(identityKeysDeserial); assertNotNull(identityKeysDeserial);
assertNotNull(oneTimeKeysDeserial); assertNotNull(oneTimeKeysDeserial);
...@@ -329,23 +328,19 @@ public class OlmAccountTest { ...@@ -329,23 +328,19 @@ public class OlmAccountTest {
accountRef.releaseAccount(); accountRef.releaseAccount();
accountDeserial.releaseAccount(); accountDeserial.releaseAccount();
} } catch (FileNotFoundException e) {
catch (FileNotFoundException e) { Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg==" + e.getMessage());
Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage());
assertTrue("test13Serialization failed " + e.getMessage(), false); assertTrue("test13Serialization failed " + e.getMessage(), false);
} } catch (ClassNotFoundException e) {
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
assertTrue("test13Serialization failed " + e.getMessage(), false); assertTrue("test13Serialization failed " + e.getMessage(), false);
} } catch (IOException e) {
catch (IOException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception IOException Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception IOException Msg==" + e.getMessage());
assertTrue("test13Serialization failed " + e.getMessage(), false); assertTrue("test13Serialization failed " + e.getMessage(), false);
} }
/*catch (OlmException e) { /*catch (OlmException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception OlmException Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception OlmException Msg==" + e.getMessage());
}*/ }*/ catch (Exception e) {
catch (Exception e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage());
assertTrue("test13Serialization failed " + e.getMessage(), false); assertTrue("test13Serialization failed " + e.getMessage(), false);
} }
...@@ -386,7 +381,7 @@ public class OlmAccountTest { ...@@ -386,7 +381,7 @@ public class OlmAccountTest {
try { try {
olmAccount = new OlmAccount(); olmAccount = new OlmAccount();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue(e.getMessage(),false); assertTrue(e.getMessage(), false);
} }
try { try {
...@@ -404,7 +399,7 @@ public class OlmAccountTest { ...@@ -404,7 +399,7 @@ public class OlmAccountTest {
try { try {
olmAccount = new OlmAccount(); olmAccount = new OlmAccount();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue(e.getMessage(),false); assertTrue(e.getMessage(), false);
} }
String signedMsg = null; String signedMsg = null;
...@@ -487,7 +482,7 @@ public class OlmAccountTest { ...@@ -487,7 +482,7 @@ public class OlmAccountTest {
account10.releaseAccount(); account10.releaseAccount();
} catch (OlmException e) { } catch (OlmException e) {
assertTrue(e.getMessage(),false); assertTrue(e.getMessage(), false);
} }
} }
} }
...@@ -18,10 +18,12 @@ ...@@ -18,10 +18,12 @@
package org.matrix.olm; package org.matrix.olm;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
...@@ -36,7 +38,6 @@ import java.io.IOException; ...@@ -36,7 +38,6 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -223,7 +224,7 @@ public class OlmGroupSessionTest { ...@@ -223,7 +224,7 @@ public class OlmGroupSessionTest {
// serialize alice session // serialize alice session
Context context = getInstrumentation().getContext(); Context context = ApplicationProvider.getApplicationContext();
try { try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_OUT_SESSION, Context.MODE_PRIVATE); FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_OUT_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
...@@ -312,7 +313,7 @@ public class OlmGroupSessionTest { ...@@ -312,7 +313,7 @@ public class OlmGroupSessionTest {
assertNotNull(bobInboundGroupSessionRef); assertNotNull(bobInboundGroupSessionRef);
// serialize alice session // serialize alice session
Context context = getInstrumentation().getContext(); Context context = ApplicationProvider.getApplicationContext();
try { try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_IN_SESSION, Context.MODE_PRIVATE); FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_IN_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
......
...@@ -16,20 +16,18 @@ ...@@ -16,20 +16,18 @@
package org.matrix.olm; package org.matrix.olm;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log; import android.util.Log;
import java.util.Arrays; import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import static org.junit.Assert.assertFalse; import java.util.Arrays;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
...@@ -107,14 +105,14 @@ public class OlmPkTest { ...@@ -107,14 +105,14 @@ public class OlmPkTest {
assertNotNull(mOlmPkDecryption); assertNotNull(mOlmPkDecryption);
byte[] privateKey = { byte[] privateKey = {
(byte)0x77, (byte)0x07, (byte)0x6D, (byte)0x0A, (byte) 0x77, (byte) 0x07, (byte) 0x6D, (byte) 0x0A,
(byte)0x73, (byte)0x18, (byte)0xA5, (byte)0x7D, (byte) 0x73, (byte) 0x18, (byte) 0xA5, (byte) 0x7D,
(byte)0x3C, (byte)0x16, (byte)0xC1, (byte)0x72, (byte) 0x3C, (byte) 0x16, (byte) 0xC1, (byte) 0x72,
(byte)0x51, (byte)0xB2, (byte)0x66, (byte)0x45, (byte) 0x51, (byte) 0xB2, (byte) 0x66, (byte) 0x45,
(byte)0xDF, (byte)0x4C, (byte)0x2F, (byte)0x87, (byte) 0xDF, (byte) 0x4C, (byte) 0x2F, (byte) 0x87,
(byte)0xEB, (byte)0xC0, (byte)0x99, (byte)0x2A, (byte) 0xEB, (byte) 0xC0, (byte) 0x99, (byte) 0x2A,
(byte)0xB1, (byte)0x77, (byte)0xFB, (byte)0xA5, (byte) 0xB1, (byte) 0x77, (byte) 0xFB, (byte) 0xA5,
(byte)0x1D, (byte)0xB9, (byte)0x2C, (byte)0x2A (byte) 0x1D, (byte) 0xB9, (byte) 0x2C, (byte) 0x2A
}; };
assertTrue(privateKey.length == OlmPkDecryption.privateKeyLength()); assertTrue(privateKey.length == OlmPkDecryption.privateKeyLength());
......
...@@ -16,10 +16,10 @@ ...@@ -16,10 +16,10 @@
package org.matrix.olm; package org.matrix.olm;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log; import android.util.Log;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
......
...@@ -18,11 +18,12 @@ ...@@ -18,11 +18,12 @@
package org.matrix.olm; package org.matrix.olm;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.json.JSONObject; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
...@@ -37,7 +38,6 @@ import java.io.ObjectInputStream; ...@@ -37,7 +38,6 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.Map; import java.util.Map;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -53,13 +53,13 @@ public class OlmSessionTest { ...@@ -53,13 +53,13 @@ public class OlmSessionTest {
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
@BeforeClass @BeforeClass
public static void setUpClass(){ public static void setUpClass() {
// load native lib // load native lib
mOlmManager = new OlmManager(); mOlmManager = new OlmManager();
String version = mOlmManager.getOlmLibVersion(); String version = mOlmManager.getOlmLibVersion();
assertNotNull(version); assertNotNull(version);
Log.d(LOG_TAG, "## setUpClass(): lib version="+version); Log.d(LOG_TAG, "## setUpClass(): lib version=" + version);
} }
/** /**
...@@ -75,7 +75,7 @@ public class OlmSessionTest { ...@@ -75,7 +75,7 @@ public class OlmSessionTest {
public void test01AliceToBob() { public void test01AliceToBob() {
final int ONE_TIME_KEYS_NUMBER = 5; final int ONE_TIME_KEYS_NUMBER = 5;
String bobIdentityKey = null; String bobIdentityKey = null;
String bobOneTimeKey=null; String bobOneTimeKey = null;
OlmAccount bobAccount = null; OlmAccount bobAccount = null;
OlmAccount aliceAccount = null; OlmAccount aliceAccount = null;