2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 21:27:40 +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
targetSdkVersion 22 //Bumping to 23 means we have to support the new permissions model
multiDexEnabled true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
dexOptions {
javaMaxHeapSize "4g"
javaMaxHeapSize "2g"
}
compileOptions {
// Use Java 1.7, requires minSdk 8
@ -61,9 +62,11 @@ android {
}
dependencies {
repositories {
mavenCentral()
}
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:appcompat-v7: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 'com.madgag.spongycastle:pkix:1.54.0.0'
compile 'io.netty:netty-handler:4.1.0.Final'
// Testing
androidTestCompile 'org.mockito:mockito-core:1.10.19'
// Because mockito has some problems with dex environment
androidTestCompile 'com.google.dexmaker:dexmaker: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.Log;
import org.bouncycastle.asn1.x500.X500NameBuilder;
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.BasePairingHandler;
import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.kde.kdeconnect.Backends.LanBackend.LanPairingHandler;
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.math.BigInteger;
@ -133,6 +135,7 @@ public class DeviceTest extends AndroidTestCase {
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
LanLink link = Mockito.mock(LanLink.class);
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);
KeyPair keyPair;
@ -213,7 +216,7 @@ public class DeviceTest extends AndroidTestCase {
} catch(Exception e) {
e.printStackTrace();
Log.e("KDE/initialiseCertificate", "Exception");
Log.e("KDE/initialiseCert", "Exception");
}
NetworkPackage fakeNetworkPackage = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_IDENTITY);
@ -226,6 +229,7 @@ public class DeviceTest extends AndroidTestCase {
LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class);
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
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);
Device device = new Device(getContext(), fakeNetworkPackage, link);
device.publicKey = keyPair.getPublic();

View File

@ -23,24 +23,22 @@ package org.kde.kdeconnect;
import android.support.v4.util.LongSparseArray;
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.LanLinkProvider;
import org.mockito.Mockito;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import io.netty.channel.Channel;
public class LanLinkProviderTest extends AndroidTestCase {
private NioSocketAcceptor tcpAcceptor = null;
private NioDatagramAcceptor udpAcceptor = null;
private LanLinkProvider linkProvider;
@Override
@ -50,61 +48,12 @@ public class LanLinkProviderTest extends AndroidTestCase {
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
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
protected void tearDown() throws Exception {
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 {
@ -115,19 +64,13 @@ public class LanLinkProviderTest extends AndroidTestCase {
Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity");
Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice");
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.getInt("tcpPort")).thenReturn(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);
// 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
final ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(port));
@ -160,7 +103,7 @@ public class LanLinkProviderTest extends AndroidTestCase {
}
}catch (Exception e){
assertEquals("Exception in thread",1,5);
fail("Exception in thread");
}
}
});
@ -168,7 +111,11 @@ public class LanLinkProviderTest extends AndroidTestCase {
try {
thread.start();
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){
throw e;
}
@ -192,34 +139,34 @@ public class LanLinkProviderTest extends AndroidTestCase {
public void testTcpIdentityPackageReceived() throws Exception{
IoSession session = Mockito.mock(IoSession.class);
Mockito.when(session.getId()).thenReturn(12345l);
NetworkPackage networkPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(networkPackage.getType()).thenReturn("kdeconnect.identity");
Mockito.when(networkPackage.getString("deviceId")).thenReturn("testDevice");
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");
String serialized = "{\"type\":\"kdeconnect.identity\",\"id\":12345,\"body\":{\"deviceName\":\"Test Device\",\"deviceType\":\"phone\",\"deviceId\":\"testDevice\",\"protocolVersion\":5}}";
Mockito.when(networkPackage.serialize()).thenReturn(serialized);
Channel channel = Mockito.mock(Channel.class);
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){
throw e;
}
LongSparseArray<LanLink> nioSessions;
LongSparseArray<LanLink> nioLinks;
try {
Field field = LanLinkProvider.class.getDeclaredField("nioSessions");
Field field = LanLinkProvider.class.getDeclaredField("nioLinks");
field.setAccessible(true);
nioSessions = (LongSparseArray<LanLink>)field.get(linkProvider);
nioLinks = (LongSparseArray<LanLink>)field.get(linkProvider);
}catch (Exception e){
throw e;
}
assertNotNull(nioSessions.get(12345l));
assertNotNull(nioLinks.get(channel.hashCode()));
HashMap<String, LanLink> visibleComputers;
@ -232,14 +179,8 @@ public class LanLinkProviderTest extends AndroidTestCase {
}
assertNotNull(visibleComputers.get("testDevice"));
// Testing session closed
try {
tcpAcceptor.getHandler().sessionClosed(session);
}catch (Exception e){
throw e;
}
assertNull(nioSessions.get(12345l));
assertNull(visibleComputers.get("testDevice"));
// TODO: Testing session closed
//assertNull(nioSessions.get(12345l));
//assertNull(visibleComputers.get("testDevice"));
}
}

View File

@ -23,8 +23,8 @@ package org.kde.kdeconnect;
import android.test.AndroidTestCase;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.kde.kdeconnect.Backends.BaseLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLink;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.mockito.Mockito;
@ -86,7 +86,7 @@ public class LanLinkTest extends AndroidTestCase {
Mockito.when(channelFutureFailure.channel()).thenReturn(channel);
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
@ -94,7 +94,7 @@ public class LanLinkTest extends AndroidTestCase {
super.tearDown();
}
public void testSendPackageSuccess(){
public void testSendPackageSuccess() throws JSONException {
NetworkPackage testPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test");
@ -113,7 +113,7 @@ public class LanLinkTest extends AndroidTestCase {
assertEquals(channelFutureSuccess.isSuccess(), true);
}
public void testSendPackageFail(){
public void testSendPackageFail() throws JSONException {
NetworkPackage testPackage = Mockito.mock(NetworkPackage.class);
Mockito.when(testPackage.getType()).thenReturn("kdeconnect.test");

View File

@ -25,6 +25,7 @@ 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;
@ -124,15 +125,13 @@ public class NetworkPackageTest extends AndroidTestCase{
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\":\"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);
try {
NetworkPackage encrypted = RsaHelper.encrypt(longJsonNp, publicKey);
decrypted = RsaHelper.decrypt(encrypted, privateKey);
String decryptedJson = decrypted.serialize();
assertEquals(json, decryptedJson);
JSONAssert.assertEquals(json, decryptedJson, true);
}catch (Exception e){
e.printStackTrace();
}