2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 05:37:43 +00:00

Fixed tests

This commit is contained in:
Albert Vaca 2016-06-12 21:07:01 +02:00
parent ae7a80e262
commit 4d19a7cdc8
5 changed files with 51 additions and 100 deletions

View File

@ -16,9 +16,10 @@ android {
minSdkVersion 9 minSdkVersion 9
targetSdkVersion 22 //Bumping to 23 means we have to support the new permissions model targetSdkVersion 22 //Bumping to 23 means we have to support the new permissions model
multiDexEnabled true multiDexEnabled true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
} }
dexOptions { dexOptions {
javaMaxHeapSize "4g" javaMaxHeapSize "2g"
} }
compileOptions { compileOptions {
// Use Java 1.7, requires minSdk 8 // Use Java 1.7, requires minSdk 8
@ -61,9 +62,11 @@ android {
} }
dependencies { dependencies {
repositories { repositories {
mavenCentral() mavenCentral()
} }
compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0' compile 'com.android.support:design:23.4.0'
@ -71,12 +74,16 @@ dependencies {
compile 'org.apache.sshd:sshd-core:0.8.0' //0.9 seems to fail on Android 6 and 1.+ requires java.nio.file, which doesn't exist in Android compile 'org.apache.sshd:sshd-core:0.8.0' //0.9 seems to fail on Android 6 and 1.+ requires java.nio.file, which doesn't exist in Android
compile 'com.madgag.spongycastle:pkix:1.54.0.0' compile 'com.madgag.spongycastle:pkix:1.54.0.0'
compile 'io.netty:netty-handler:4.1.0.Final' compile 'io.netty:netty-handler:4.1.0.Final'
// Testing
androidTestCompile 'org.mockito:mockito-core:1.10.19' androidTestCompile 'org.mockito:mockito-core:1.10.19'
// Because mockito has some problems with dex environment // Because mockito has some problems with dex environment
androidTestCompile 'com.google.dexmaker:dexmaker:1.1' androidTestCompile 'com.google.dexmaker:dexmaker:1.1'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
androidTestCompile 'org.skyscreamer:jsonassert:1.3.0'
//compile fileTree(include: '*.jar', dir: 'libs')
} }

View File

@ -26,17 +26,19 @@ import android.test.AndroidTestCase;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import org.bouncycastle.asn1.x500.X500NameBuilder; import org.kde.kdeconnect.Backends.BasePairingHandler;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.kde.kdeconnect.Backends.LanBackend.LanLink; import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.kde.kdeconnect.Backends.LanBackend.LanPairingHandler;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.spongycastle.asn1.x500.X500NameBuilder;
import org.spongycastle.asn1.x500.style.BCStyle;
import org.spongycastle.cert.X509v3CertificateBuilder;
import org.spongycastle.cert.jcajce.JcaX509CertificateConverter;
import org.spongycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.operator.ContentSigner;
import org.spongycastle.operator.jcajce.JcaContentSignerBuilder;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigInteger; import java.math.BigInteger;
@ -133,6 +135,7 @@ public class DeviceTest extends AndroidTestCase {
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider"); Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
LanLink link = Mockito.mock(LanLink.class); LanLink link = Mockito.mock(LanLink.class);
Mockito.when(link.getLinkProvider()).thenReturn(linkProvider); Mockito.when(link.getLinkProvider()).thenReturn(linkProvider);
Mockito.when(link.getPairingHandler(Mockito.any(Device.class), Mockito.any(BasePairingHandler.PairingHandlerCallback.class))).thenReturn(Mockito.mock(LanPairingHandler.class));
Device device = new Device(getContext(), fakeNetworkPackage, link); Device device = new Device(getContext(), fakeNetworkPackage, link);
KeyPair keyPair; KeyPair keyPair;
@ -213,7 +216,7 @@ public class DeviceTest extends AndroidTestCase {
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.e("KDE/initialiseCertificate", "Exception"); Log.e("KDE/initialiseCert", "Exception");
} }
NetworkPackage fakeNetworkPackage = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_IDENTITY); NetworkPackage fakeNetworkPackage = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_IDENTITY);
@ -226,6 +229,7 @@ public class DeviceTest extends AndroidTestCase {
LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class); LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class);
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider"); Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
LanLink link = Mockito.mock(LanLink.class); LanLink link = Mockito.mock(LanLink.class);
Mockito.when(link.getPairingHandler(Mockito.any(Device.class), Mockito.any(BasePairingHandler.PairingHandlerCallback.class))).thenReturn(Mockito.mock(LanPairingHandler.class));
Mockito.when(link.getLinkProvider()).thenReturn(linkProvider); Mockito.when(link.getLinkProvider()).thenReturn(linkProvider);
Device device = new Device(getContext(), fakeNetworkPackage, link); Device device = new Device(getContext(), fakeNetworkPackage, link);
device.publicKey = keyPair.getPublic(); device.publicKey = keyPair.getPublic();

View File

@ -23,24 +23,22 @@ package org.kde.kdeconnect;
import android.support.v4.util.LongSparseArray; import android.support.v4.util.LongSparseArray;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.kde.kdeconnect.Backends.LanBackend.LanLink; import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.HashMap; import java.util.HashMap;
import io.netty.channel.Channel;
public class LanLinkProviderTest extends AndroidTestCase { public class LanLinkProviderTest extends AndroidTestCase {
private NioSocketAcceptor tcpAcceptor = null;
private NioDatagramAcceptor udpAcceptor = null;
private LanLinkProvider linkProvider; private LanLinkProvider linkProvider;
@Override @Override
@ -50,61 +48,12 @@ public class LanLinkProviderTest extends AndroidTestCase {
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath()); System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
linkProvider = new LanLinkProvider(getContext()); linkProvider = new LanLinkProvider(getContext());
try {
Field field = LanLinkProvider.class.getDeclaredField("tcpAcceptor");
field.setAccessible(true);
tcpAcceptor = (NioSocketAcceptor)field.get(linkProvider);
assertNotNull(tcpAcceptor);
}catch (Exception e){
fail("Error getting tcpAcceptor from LanLinkProvider");
}
try{
Field field = LanLinkProvider.class.getDeclaredField("udpAcceptor");
field.setAccessible(true);
udpAcceptor = (NioDatagramAcceptor)field.get(linkProvider);
assertNotNull(udpAcceptor);
}catch (Exception e){
fail("Error getting udp acceptor from LanLinkProvider");
}
} }
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
super.tearDown(); super.tearDown();
tcpAcceptor.dispose();
udpAcceptor.dispose();
}
public void testTcpAcceptor(){
assertNotNull(tcpAcceptor.getHandler());
assertEquals(tcpAcceptor.getSessionConfig().isKeepAlive(), true);
assertEquals(tcpAcceptor.getSessionConfig().isReuseAddress(), true);
assertNotNull(tcpAcceptor.getFilterChain().get("codec"));
}
public void testUdpAcceptor(){
assertNull(udpAcceptor.getHandler());
assertEquals(udpAcceptor.getSessionConfig().isReuseAddress(), true);
assertNotNull(udpAcceptor.getFilterChain().get("codec"));
}
public void testOnStart() throws Exception{
IoSession session = Mockito.mock(IoSession.class);
Mockito.when(session.getId()).thenReturn(12345l);
Mockito.when(session.getRemoteAddress()).thenReturn(new InetSocketAddress(5000));
linkProvider.onStart();
assertNotNull(udpAcceptor.getHandler());
assertEquals(udpAcceptor.getLocalAddress().getPort(), 1714);
} }
public void testUdpPackageReceived() throws Exception { public void testUdpPackageReceived() throws Exception {
@ -115,19 +64,13 @@ public class LanLinkProviderTest extends AndroidTestCase {
Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity"); Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity");
Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice"); Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice");
Mockito.when(networkPackage.getString("deviceName")).thenReturn("Test Device"); Mockito.when(networkPackage.getString("deviceName")).thenReturn("Test Device");
Mockito.when(networkPackage.getInt("protocolVersion")).thenReturn(NetworkPackage.ProtocolVersion); Mockito.when(networkPackage.getInt("protocolVersion")).thenReturn(5);
Mockito.when(networkPackage.getString("deviceType")).thenReturn("phone"); Mockito.when(networkPackage.getString("deviceType")).thenReturn("phone");
Mockito.when(networkPackage.getInt("tcpPort")).thenReturn(port); Mockito.when(networkPackage.getInt("tcpPort")).thenReturn(port);
final String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5,\"tcpPort\": "+ port +"}}"; final String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5,\"tcpPort\": "+ port +"}}";
Mockito.when(networkPackage.serialize()).thenReturn(serialized); Mockito.when(networkPackage.serialize()).thenReturn(serialized);
// Mocking udp session
IoSession session = Mockito.mock(IoSession.class);
Mockito.when(session.getId()).thenReturn(12345l);
Mockito.when(session.getRemoteAddress()).thenReturn(new InetSocketAddress(port));
// Making a server socket, so that original LanLinkProvider can connect to it when it receives our fake package // Making a server socket, so that original LanLinkProvider can connect to it when it receives our fake package
final ServerSocket serverSocket = new ServerSocket(); final ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(port)); serverSocket.bind(new InetSocketAddress(port));
@ -160,7 +103,7 @@ public class LanLinkProviderTest extends AndroidTestCase {
} }
}catch (Exception e){ }catch (Exception e){
assertEquals("Exception in thread",1,5); fail("Exception in thread");
} }
} }
}); });
@ -168,7 +111,11 @@ public class LanLinkProviderTest extends AndroidTestCase {
try { try {
thread.start(); thread.start();
linkProvider.onStart(); linkProvider.onStart();
udpAcceptor.getHandler().messageReceived(session, networkPackage.serialize());
Method method = LanLinkProvider.class.getDeclaredMethod("identityPackageReceived", NetworkPackage.class, Channel.class, LanLink.ConnectionStarted.class);
method.setAccessible(true);
method.invoke(linkProvider, networkPackage, Mockito.mock(Channel.class), LanLink.ConnectionStarted.Remotely);
}catch (Exception e){ }catch (Exception e){
throw e; throw e;
} }
@ -192,34 +139,34 @@ public class LanLinkProviderTest extends AndroidTestCase {
public void testTcpIdentityPackageReceived() throws Exception{ public void testTcpIdentityPackageReceived() throws Exception{
IoSession session = Mockito.mock(IoSession.class);
Mockito.when(session.getId()).thenReturn(12345l);
NetworkPackage networkPackage = Mockito.mock(NetworkPackage.class); NetworkPackage networkPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity"); Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity");
Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice"); Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice");
Mockito.when(networkPackage.getString("deviceName")).thenReturn("Test Device"); Mockito.when(networkPackage.getString("deviceName")).thenReturn("Test Device");
Mockito.when(networkPackage.getInt("protocolVersion")).thenReturn(NetworkPackage.ProtocolVersion); Mockito.when(networkPackage.getInt("protocolVersion")).thenReturn(5);
Mockito.when(networkPackage.getString("deviceType")).thenReturn("phone"); Mockito.when(networkPackage.getString("deviceType")).thenReturn("phone");
String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5}}"; String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5}}";
Mockito.when(networkPackage.serialize()).thenReturn(serialized); Mockito.when(networkPackage.serialize()).thenReturn(serialized);
Channel channel = Mockito.mock(Channel.class);
try { try {
tcpAcceptor.getHandler().messageReceived(session, networkPackage.serialize()); Method method = LanLinkProvider.class.getDeclaredMethod("identityPackageReceived", NetworkPackage.class, Channel.class, LanLink.ConnectionStarted.class);
method.setAccessible(true);
method.invoke(linkProvider, networkPackage, channel, LanLink.ConnectionStarted.Locally);
}catch (Exception e){ }catch (Exception e){
throw e; throw e;
} }
LongSparseArray<LanLink> nioSessions; LongSparseArray<LanLink> nioLinks;
try { try {
Field field = LanLinkProvider.class.getDeclaredField("nioSessions"); Field field = LanLinkProvider.class.getDeclaredField("nioLinks");
field.setAccessible(true); field.setAccessible(true);
nioSessions = (LongSparseArray<LanLink>)field.get(linkProvider); nioLinks = (LongSparseArray<LanLink>)field.get(linkProvider);
}catch (Exception e){ }catch (Exception e){
throw e; throw e;
} }
assertNotNull(nioSessions.get(12345l)); assertNotNull(nioLinks.get(channel.hashCode()));
HashMap<String, LanLink> visibleComputers; HashMap<String, LanLink> visibleComputers;
@ -232,14 +179,8 @@ public class LanLinkProviderTest extends AndroidTestCase {
} }
assertNotNull(visibleComputers.get("testDevice")); assertNotNull(visibleComputers.get("testDevice"));
// TODO: Testing session closed
// Testing session closed //assertNull(nioSessions.get(12345l));
try { //assertNull(visibleComputers.get("testDevice"));
tcpAcceptor.getHandler().sessionClosed(session);
}catch (Exception e){
throw e;
}
assertNull(nioSessions.get(12345l));
assertNull(visibleComputers.get("testDevice"));
} }
} }

View File

@ -23,8 +23,8 @@ package org.kde.kdeconnect;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.util.Log; import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.kde.kdeconnect.Backends.BaseLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLink; import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -86,7 +86,7 @@ public class LanLinkTest extends AndroidTestCase {
Mockito.when(channelFutureFailure.channel()).thenReturn(channel); Mockito.when(channelFutureFailure.channel()).thenReturn(channel);
Mockito.when(channelFutureFailure.sync()).thenReturn(channelFutureFailure); Mockito.when(channelFutureFailure.sync()).thenReturn(channelFutureFailure);
lanLink = new LanLink(getContext(), channel, "testDevice", linkProvider, BaseLink.ConnectionStarted.Remotely); lanLink = new LanLink(getContext(), "testDevice", linkProvider, channel, LanLink.ConnectionStarted.Remotely, false);
} }
@Override @Override
@ -94,7 +94,7 @@ public class LanLinkTest extends AndroidTestCase {
super.tearDown(); super.tearDown();
} }
public void testSendPackageSuccess(){ public void testSendPackageSuccess() throws JSONException {
NetworkPackage testPackage = Mockito.mock(NetworkPackage.class); NetworkPackage testPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test"); Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test");
@ -113,7 +113,7 @@ public class LanLinkTest extends AndroidTestCase {
assertEquals(channelFutureSuccess.isSuccess(), true); assertEquals(channelFutureSuccess.isSuccess(), true);
} }
public void testSendPackageFail(){ public void testSendPackageFail() throws JSONException {
NetworkPackage testPackage = Mockito.mock(NetworkPackage.class); NetworkPackage testPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test"); Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test");

View File

@ -25,6 +25,7 @@ import android.util.Log;
import org.json.JSONException; import org.json.JSONException;
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
import org.skyscreamer.jsonassert.JSONAssert;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
@ -124,15 +125,13 @@ public class NetworkPackageTest extends AndroidTestCase{
assertEquals(decrypted.getType(), copy.getType()); assertEquals(decrypted.getType(), copy.getType());
assertEquals(decrypted.getJSONArray("body"), copy.getJSONArray("body")); 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\":\"A really long package id\",\"payloadSize\":0,\"payloadTransferInfo\":{},\"type\":\"kdeconnect.a_really_really_long_package_type\"}\n"; 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";
NetworkPackage longJsonNp = NetworkPackage.unserialize(json); NetworkPackage longJsonNp = NetworkPackage.unserialize(json);
try { try {
NetworkPackage encrypted = RsaHelper.encrypt(longJsonNp, publicKey); NetworkPackage encrypted = RsaHelper.encrypt(longJsonNp, publicKey);
decrypted = RsaHelper.decrypt(encrypted, privateKey); decrypted = RsaHelper.decrypt(encrypted, privateKey);
String decryptedJson = decrypted.serialize(); String decryptedJson = decrypted.serialize();
assertEquals(json, decryptedJson); JSONAssert.assertEquals(json, decryptedJson, true);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }