use of org.kde.kdeconnect.Backends.LanBackend.LanLink in project kdeconnect-android by KDE.
the class DeviceTest method testPairingDone.
// Testing pairing done using reflection since it is private
// Created an unpaired device inside this test
public void testPairingDone() {
NetworkPackage fakeNetworkPackage = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_IDENTITY);
fakeNetworkPackage.set("deviceId", "unpairedTestDevice");
fakeNetworkPackage.set("deviceName", "Unpaired Test Device");
fakeNetworkPackage.set("protocolVersion", NetworkPackage.ProtocolVersion);
fakeNetworkPackage.set("deviceType", Device.DeviceType.Phone.toString());
LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class);
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;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
keyPair = keyGen.genKeyPair();
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/initializeRsaKeys", "Exception");
return;
}
device.publicKey = keyPair.getPublic();
assertNotNull(device);
assertEquals(device.getDeviceId(), "unpairedTestDevice");
assertEquals(device.getName(), "Unpaired Test Device");
assertEquals(device.getDeviceType(), Device.DeviceType.Phone);
assertNotNull(device.publicKey);
assertNull(device.certificate);
Method method;
try {
method = Device.class.getDeclaredMethod("pairingDone");
method.setAccessible(true);
method.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(device.isPaired(), true);
SharedPreferences preferences = getContext().getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
assertEquals(preferences.getBoolean(device.getDeviceId(), false), true);
SharedPreferences settings = getContext().getSharedPreferences(device.getDeviceId(), Context.MODE_PRIVATE);
assertEquals(settings.getString("deviceName", "Unknown device"), "Unpaired Test Device");
assertEquals(settings.getString("deviceType", "tablet"), "phone");
// Cleanup for unpaired test device
preferences.edit().remove(device.getDeviceId()).apply();
settings.edit().clear().apply();
}
use of org.kde.kdeconnect.Backends.LanBackend.LanLink in project kdeconnect-android by KDE.
the class DeviceTest method testPairingDoneWithCertificate.
public void testPairingDoneWithCertificate() throws Exception {
KeyPair keyPair = null;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
keyPair = keyGen.genKeyPair();
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/initializeRsaKeys", "Exception");
}
X509Certificate certificate = null;
try {
BouncyCastleProvider BC = new BouncyCastleProvider();
X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
nameBuilder.addRDN(BCStyle.CN, "testDevice");
nameBuilder.addRDN(BCStyle.OU, "KDE Connect");
nameBuilder.addRDN(BCStyle.O, "KDE");
Date notBefore = new Date(System.currentTimeMillis());
Date notAfter = new Date(System.currentTimeMillis() + System.currentTimeMillis());
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(nameBuilder.build(), BigInteger.ONE, notBefore, notAfter, nameBuilder.build(), keyPair.getPublic());
ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(BC).build(keyPair.getPrivate());
certificate = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certificateBuilder.build(contentSigner));
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/initialiseCert", "Exception");
}
NetworkPackage fakeNetworkPackage = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_IDENTITY);
fakeNetworkPackage.set("deviceId", "unpairedTestDevice");
fakeNetworkPackage.set("deviceName", "Unpaired Test Device");
fakeNetworkPackage.set("protocolVersion", NetworkPackage.ProtocolVersion);
fakeNetworkPackage.set("deviceType", Device.DeviceType.Phone.toString());
fakeNetworkPackage.set("certificate", Base64.encodeToString(certificate.getEncoded(), 0));
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();
assertNotNull(device);
assertEquals(device.getDeviceId(), "unpairedTestDevice");
assertEquals(device.getName(), "Unpaired Test Device");
assertEquals(device.getDeviceType(), Device.DeviceType.Phone);
assertNotNull(device.publicKey);
assertNotNull(device.certificate);
Method method;
try {
method = Device.class.getDeclaredMethod("pairingDone");
method.setAccessible(true);
method.invoke(device);
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(device.isPaired(), true);
SharedPreferences preferences = getContext().getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
assertEquals(preferences.getBoolean(device.getDeviceId(), false), true);
SharedPreferences settings = getContext().getSharedPreferences(device.getDeviceId(), Context.MODE_PRIVATE);
assertEquals(settings.getString("deviceName", "Unknown device"), "Unpaired Test Device");
assertEquals(settings.getString("deviceType", "tablet"), "phone");
// Cleanup for unpaired test device
preferences.edit().remove(device.getDeviceId()).apply();
settings.edit().clear().apply();
}
use of org.kde.kdeconnect.Backends.LanBackend.LanLink in project kdeconnect-android by KDE.
the class LanLinkTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
LanLinkProvider linkProvider = Mockito.mock(LanLinkProvider.class);
Mockito.when(linkProvider.getName()).thenReturn("LanLinkProvider");
callback = Mockito.mock(Device.SendPackageStatusCallback.class);
goodOutputStream = Mockito.mock(OutputStream.class);
badOutputStream = Mockito.mock(OutputStream.class);
Mockito.doThrow(new IOException("AAA")).when(badOutputStream).write(Mockito.any(byte[].class));
Socket socketMock = Mockito.mock(Socket.class);
Mockito.when(socketMock.getRemoteSocketAddress()).thenReturn(new InetSocketAddress(5000));
Mockito.when(socketMock.getOutputStream()).thenReturn(goodOutputStream);
Socket socketBadMock = Mockito.mock(Socket.class);
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);
}
use of org.kde.kdeconnect.Backends.LanBackend.LanLink in project kdeconnect-android by KDE.
the class LanLinkProviderTest method testIdentityPackageReceived.
public void testIdentityPackageReceived() throws Exception {
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(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);
Socket channel = Mockito.mock(Socket.class);
try {
Method method = LanLinkProvider.class.getDeclaredMethod("identityPackageReceived", NetworkPackage.class, Socket.class, LanLink.ConnectionStarted.class);
method.setAccessible(true);
method.invoke(linkProvider, networkPackage, channel, LanLink.ConnectionStarted.Locally);
} catch (Exception e) {
throw e;
}
HashMap<String, LanLink> visibleComputers;
try {
Field field = LanLinkProvider.class.getDeclaredField("visibleComputers");
field.setAccessible(true);
visibleComputers = (HashMap<String, LanLink>) field.get(linkProvider);
} catch (Exception e) {
throw e;
}
assertNotNull(visibleComputers.get("testDevice"));
}
Aggregations