Commit b1904a69 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Fixed all tests

Now they are local tests and not Android tests
parent 78b38b5a
......@@ -15,8 +15,6 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
//multiDexEnabled true
//testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
dexOptions {
javaMaxHeapSize "2g"
......@@ -33,7 +31,7 @@ android {
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
androidTest {
test {
java.srcDirs = ['tests']
}
}
......@@ -87,9 +85,10 @@ dependencies {
annotationProcessor 'org.atteo.classindex:classindex:3.6'
// Testing
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.1'// Because mockito has some problems with dex environment
androidTestImplementation 'org.skyscreamer:jsonassert:1.3.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-core:2.0.0'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0'
testImplementation 'org.mockito:mockito-core:2.23.0'
testImplementation 'org.skyscreamer:jsonassert:1.3.0'
}
This diff is collapsed.
......@@ -20,30 +20,44 @@
package org.kde.kdeconnect;
import android.util.Log;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.kde.kdeconnect.Helpers.DeviceHelper;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.HashMap;
class LanLinkProviderTest extends AndroidTestCase {
private LanLinkProvider linkProvider;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
@Override
protected void setUp() throws Exception {
super.setUp();
@RunWith(PowerMockRunner.class)
@PrepareForTest({DeviceHelper.class, Log.class})
public class LanLinkProviderTest {
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
@Before
public void setUp() {
PowerMockito.mockStatic(DeviceHelper.class);
PowerMockito.when(DeviceHelper.getDeviceId(any())).thenReturn("123");
linkProvider = new LanLinkProvider(getContext());
PowerMockito.mockStatic(Log.class);
}
@Test
public void testIdentityPacketReceived() throws Exception {
LanLinkProvider linkProvider = new LanLinkProvider(null);
NetworkPacket networkPacket = Mockito.mock(NetworkPacket.class);
Mockito.when(networkPacket.getType()).thenReturn("kdeconnect.identity");
Mockito.when(networkPacket.getString("deviceId")).thenReturn("testDevice");
......
......@@ -20,13 +20,21 @@
package org.kde.kdeconnect;
import android.content.Context;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.kde.kdeconnect.Helpers.DeviceHelper;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
......@@ -36,7 +44,11 @@ import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
class LanLinkTest extends AndroidTestCase {
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class})
public class LanLinkTest {
private LanLink badLanLink;
private LanLink goodLanLink;
......@@ -46,11 +58,9 @@ class LanLinkTest extends AndroidTestCase {
private Device.SendPacketStatusCallback callback;
@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Log.class);
LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class);
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
......@@ -70,10 +80,12 @@ class LanLinkTest extends AndroidTestCase {
Mockito.when(socketBadMock.getRemoteSocketAddress()).thenReturn(new InetSocketAddress(5000));
Mockito.when(socketBadMock.getOutputStream()).thenReturn(badOutputStream);
goodLanLink = new LanLink(getContext(), "testDevice", linkProvider, socketMock, LanLink.ConnectionStarted.Remotely);
badLanLink = new LanLink(getContext(), "testDevice", linkProvider, socketBadMock, LanLink.ConnectionStarted.Remotely);
Context context = Mockito.mock(Context.class);
goodLanLink = new LanLink(context, "testDevice", linkProvider, socketMock, LanLink.ConnectionStarted.Remotely);
badLanLink = new LanLink(context, "testDevice", linkProvider, socketBadMock, LanLink.ConnectionStarted.Remotely);
}
@Test
public void testSendPacketSuccess() throws JSONException {
NetworkPacket testPacket = Mockito.mock(NetworkPacket.class);
......@@ -87,6 +99,7 @@ class LanLinkTest extends AndroidTestCase {
Mockito.verify(callback).onSuccess();
}
@Test
public void testSendPacketFail() throws JSONException {
NetworkPacket testPacket = Mockito.mock(NetworkPacket.class);
......@@ -97,11 +110,11 @@ class LanLinkTest extends AndroidTestCase {
badLanLink.sendPacket(testPacket, callback);
Mockito.verify(callback).onFailure(Mockito.any(RuntimeException.class));
Mockito.verify(callback).onFailure(Mockito.any(IOException.class));
}
@Test
public void testSendPayload() throws Exception {
class Downloader extends Thread {
......@@ -127,7 +140,7 @@ class LanLinkTest extends AndroidTestCase {
int tcpPort = np.getPayloadTransferInfo().getInt("port");
InetSocketAddress address = new InetSocketAddress(5000);
socket.connect(new InetSocketAddress(address.getAddress(), tcpPort));
np.setPayload(socket.getInputStream(), np.getPayloadSize());
np.setPayload(new NetworkPacket.Payload(socket.getInputStream(), np.getPayloadSize()));
} catch (Exception e) {
socket.close();
e.printStackTrace();
......@@ -135,7 +148,7 @@ class LanLinkTest extends AndroidTestCase {
throw e;
}
final InputStream input = np.getPayload();
final InputStream input = np.getPayload().getInputStream();
final long fileLength = np.getPayloadSize();
byte data[] = new byte[1024];
......@@ -193,7 +206,7 @@ class LanLinkTest extends AndroidTestCase {
Mockito.when(sharePacket.hasPayload()).thenReturn(true);
Mockito.when(sharePacket.hasPayloadTransferInfo()).thenReturn(true);
Mockito.doAnswer(invocationOnMock -> sharePacketJson.toString()).when(sharePacket).serialize();
Mockito.when(sharePacket.getPayload()).thenReturn(new ByteArrayInputStream(data));
Mockito.when(sharePacket.getPayload()).thenReturn(new NetworkPacket.Payload(new ByteArrayInputStream(data), -1));
Mockito.when(sharePacket.getPayloadSize()).thenReturn((long) data.length);
Mockito.doAnswer(invocationOnMock -> sharePacketJson.getJSONObject("payloadTransferInfo")).when(sharePacket).getPayloadTransferInfo();
Mockito.doAnswer(invocationOnMock -> {
......
package org.kde.kdeconnect;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import androidx.annotation.Nullable;
/**
* Mock implementation of shared preference, which just saves data in memory using map.
*
* It DOES NOT support transactions, changes are immediate
*
* From https://gist.github.com/amardeshbd/354173d00b988574ee5019c4ba0c8a0b
*/
public class MockSharedPreference implements SharedPreferences {
private final HashMap<String, Object> preferenceMap;
private final MockSharedPreferenceEditor preferenceEditor;
public MockSharedPreference() {
preferenceMap = new HashMap<>();
preferenceEditor = new MockSharedPreferenceEditor(preferenceMap);
}
@Override
public Map<String, ?> getAll() {
return preferenceMap;
}
@Nullable
@Override
public String getString(final String s, @Nullable final String def) {
if (!preferenceMap.containsKey(s)) return def;
return (String) preferenceMap.get(s);
}
@Nullable
@Override
public Set<String> getStringSet(final String s, @Nullable final Set<String> def) {
if (!preferenceMap.containsKey(s)) return def;
return (Set<String>) preferenceMap.get(s);
}
@Override
public int getInt(final String s, final int def) {
if (!preferenceMap.containsKey(s)) return def;
return (int) preferenceMap.get(s);
}
@Override
public long getLong(final String s, final long def) {
if (!preferenceMap.containsKey(s)) return def;
return (long) preferenceMap.get(s);
}
@Override
public float getFloat(final String s, final float def) {
if (!preferenceMap.containsKey(s)) return def;
return (float) preferenceMap.get(s);
}
@Override
public boolean getBoolean(final String s, final boolean def) {
if (!preferenceMap.containsKey(s)) return def;
return (boolean) preferenceMap.get(s);
}
@Override
public boolean contains(final String s) {
return preferenceMap.containsKey(s);
}
@Override
public Editor edit() {
return preferenceEditor;
}
@Override
public void registerOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
}
public static class MockSharedPreferenceEditor implements Editor {
private final HashMap<String, Object> preferenceMap;
public MockSharedPreferenceEditor(final HashMap<String, Object> preferenceMap) {
this.preferenceMap = preferenceMap;
}
@Override
public Editor putString(final String s, @Nullable final String s1) {
preferenceMap.put(s, s1);
return this;
}
@Override
public Editor putStringSet(final String s, @Nullable final Set<String> set) {
preferenceMap.put(s, set);
return this;
}
@Override
public Editor putInt(final String s, final int i) {
preferenceMap.put(s, i);
return this;
}
@Override
public Editor putLong(final String s, final long l) {
preferenceMap.put(s, l);
return this;
}
@Override
public Editor putFloat(final String s, final float v) {
preferenceMap.put(s, v);
return this;
}
@Override
public Editor putBoolean(final String s, final boolean b) {
preferenceMap.put(s, b);
return this;
}
@Override
public Editor remove(final String s) {
preferenceMap.remove(s);
return this;
}
@Override
public Editor clear() {
preferenceMap.clear();
return this;
}
@Override
public boolean commit() {
return true;
}
@Override
public void apply() {
// Nothing to do, everything is saved in memory.
}
}
}
......@@ -20,19 +20,40 @@
package org.kde.kdeconnect;
import android.content.Context;
import android.util.Log;
import org.json.JSONException;
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
import org.skyscreamer.jsonassert.JSONAssert;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
class NetworkPacketTest extends AndroidTestCase {
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kde.kdeconnect.Helpers.DeviceHelper;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
@RunWith(PowerMockRunner.class)
@PrepareForTest({DeviceHelper.class, Log.class})
public class NetworkPacketTest {
@Before
public void setUp() {
PowerMockito.mockStatic(DeviceHelper.class);
PowerMockito.when(DeviceHelper.getDeviceId(any())).thenReturn("123");
PowerMockito.when(DeviceHelper.getDeviceType(any())).thenReturn(Device.DeviceType.Phone);
PowerMockito.mockStatic(Log.class);
}
@Test
public void testNetworkPacket() throws JSONException {
NetworkPacket np = new NetworkPacket("com.test");
......@@ -61,67 +82,17 @@ class NetworkPacketTest extends AndroidTestCase {
}
@Test
public void testIdentity() {
NetworkPacket np = NetworkPacket.createIdentityPacket(getContext());
Context context = Mockito.mock(Context.class);
MockSharedPreference settings = new MockSharedPreference();
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(settings);
assertEquals(np.getInt("protocolVersion"), NetworkPacket.ProtocolVersion);
NetworkPacket np = NetworkPacket.createIdentityPacket(context);
}
assertEquals(np.getInt("protocolVersion"), NetworkPacket.ProtocolVersion);
public void testEncryption() throws JSONException {
NetworkPacket original = new NetworkPacket("com.test");
original.set("hello", "hola");
NetworkPacket copy = NetworkPacket.unserialize(original.serialize());
NetworkPacket decrypted = new NetworkPacket("");
KeyPair keyPair;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
keyPair = keyGen.genKeyPair();
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/initializeRsaKeys", "Exception");
return;
}
PrivateKey privateKey = keyPair.getPrivate();
assertNotNull(privateKey);
PublicKey publicKey = keyPair.getPublic();
assertNotNull(publicKey);
// Encrypt and decrypt np
assertEquals(original.getType(), "com.test");
try {
NetworkPacket encrypted = RsaHelper.encrypt(original, publicKey);
assertEquals(encrypted.getType(), NetworkPacket.PACKET_TYPE_ENCRYPTED);
decrypted = RsaHelper.decrypt(encrypted, privateKey);
assertEquals(decrypted.getType(), "com.test");
} catch (Exception e) {
e.printStackTrace();
}
// np should be equal to np2
assertEquals(decrypted.getLong("id"), copy.getLong("id"));
assertEquals(decrypted.getType(), copy.getType());
assertEquals(decrypted.getJSONArray("body"), copy.getJSONArray("body"));
String json = "{\"body\":{\"nowPlaying\":\"A really long song name - A really long artist name\",\"player\":\"A really long player name\",\"the_meaning_of_life_the_universe_and_everything\":\"42\"},\"id\":945945945,\"type\":\"kdeconnect.a_really_really_long_package_type\"}\n";
NetworkPacket longJsonNp = NetworkPacket.unserialize(json);
try {
NetworkPacket encrypted = RsaHelper.encrypt(longJsonNp, publicKey);
decrypted = RsaHelper.decrypt(encrypted, privateKey);
String decryptedJson = decrypted.serialize();
JSONAssert.assertEquals(json, decryptedJson, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment