diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java index d205391..2a6ff2d 100755 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java @@ -52,7 +52,10 @@ public class FreedesktopKeyringBackend implements KeyringBackend { public FreedesktopKeyringBackend() throws BackendNotSupportedException { try { collection = new SimpleCollection(); - } catch (IOException ex) { + if (!collection.isConnected()) { + throw new BackendNotSupportedException("Error connecting to dbus"); + } + } catch (IOException | IllegalStateException ex) { throw new BackendNotSupportedException("Error connecting to dbus", ex); } } diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java index 8efebb8..57f0c2b 100755 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java @@ -82,6 +82,10 @@ public SimpleCollection() throws IOException { unlock(); } + public boolean isConnected() { + return this.session.getConnection().isConnected(); + } + /* * A user specified collection. * diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java b/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java index b127156..91bebe4 100644 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java @@ -47,14 +47,6 @@ public class KWalletBackend implements KeyringBackend { public KWalletBackend() throws BackendNotSupportedException { try { - // connection = DBusConnectionBuilder.forAddress(BusAddress.of(System.getProperty("DBUS_TCP_SESSION"))) - // .withRegisterSelf(true) - // .withShared(true) - // .transportConfig() - // .withAdditionalConfig("TIMEOUT", 10000) - // .back() - // .build(); - connection = DBusConnectionBuilder.forSessionBus().build(); wallet = connection.getRemoteObject("org.kde.kwalletd5", "/modules/kwalletd5", KWallet.class, true); wallet.localWallet(); //attempt connection to wallet diff --git a/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java b/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java index f7fad93..e5cf7ed 100644 --- a/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java +++ b/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java @@ -27,18 +27,24 @@ package com.github.javakeyring.internal; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.junit.Assume.assumeTrue; import org.junit.Test; import com.github.javakeyring.BackendNotSupportedException; +import com.github.javakeyring.internal.freedesktop.FreedesktopKeyringBackend; +import com.github.javakeyring.internal.kde.KWalletBackend; +import com.sun.jna.Platform; + public class KeyringBackendFactoryTest { @Test public void testKeyringBackendFactory() throws BackendNotSupportedException { - assertThatThrownBy(() -> KeyringBackendFactory.create(null)).isInstanceOf(BackendNotSupportedException.class); - assertThat(KeyringBackendFactory.create()).isNotNull(); + assumeTrue(Platform.isLinux()); + assumeTrue(catchThrowable(KWalletBackend::new) instanceof BackendNotSupportedException); + assertThat(catchThrowable(FreedesktopKeyringBackend::new)).as("Setup should succeed").doesNotThrowAnyException(); } } diff --git a/java-keyring/src/test/java/com/github/javakeyring/kde/KWalletBackendTest.java b/java-keyring/src/test/java/com/github/javakeyring/kde/KWalletBackendTest.java index 2411320..8bea9c2 100644 --- a/java-keyring/src/test/java/com/github/javakeyring/kde/KWalletBackendTest.java +++ b/java-keyring/src/test/java/com/github/javakeyring/kde/KWalletBackendTest.java @@ -30,7 +30,6 @@ import com.github.javakeyring.Keyring; import com.github.javakeyring.KeyringStorageType; import com.github.javakeyring.PasswordAccessException; -import com.github.javakeyring.internal.KeyringBackend; import com.github.javakeyring.internal.freedesktop.FreedesktopKeyringBackend; import com.github.javakeyring.internal.kde.KWalletBackend; import com.sun.jna.Platform; @@ -64,18 +63,20 @@ public void testSetup() throws Exception { */ @Test public void testPasswordFlow() throws Exception { - assumeTrue(Platform.isLinux() && Keyring.create().getKeyringStorageType() == KeyringStorageType.KWALLET); - KeyringBackend backend = new KWalletBackend(); - catchThrowable(() -> backend.deletePassword(SERVICE, ACCOUNT)); - checkExistenceOfPasswordEntry(backend); - backend.setPassword(SERVICE, ACCOUNT, PASSWORD); - assertThat(backend.getPassword(SERVICE, ACCOUNT)).isEqualTo(PASSWORD); - backend.deletePassword(SERVICE, ACCOUNT); - assertThatThrownBy(() -> backend.getPassword(SERVICE, ACCOUNT)).isInstanceOf(PasswordAccessException.class); + assumeTrue(Platform.isLinux()); + assumeTrue(catchThrowable(KWalletBackend::new) == null); + Keyring keyring = Keyring.create(); + assertThat(keyring.getKeyringStorageType()).isEqualTo(KeyringStorageType.KWALLET); + catchThrowable(() -> keyring.deletePassword(SERVICE, ACCOUNT)); + checkExistenceOfPasswordEntry(keyring); + keyring.setPassword(SERVICE, ACCOUNT, PASSWORD); + assertThat(keyring.getPassword(SERVICE, ACCOUNT)).isEqualTo(PASSWORD); + keyring.deletePassword(SERVICE, ACCOUNT); + assertThatThrownBy(() -> keyring.getPassword(SERVICE, ACCOUNT)).isInstanceOf(PasswordAccessException.class); } - private static void checkExistenceOfPasswordEntry(KeyringBackend backend) { - assertThatThrownBy(() -> backend.getPassword(SERVICE, ACCOUNT)) + private static void checkExistenceOfPasswordEntry(Keyring keyring) { + assertThatThrownBy(() -> keyring.getPassword(SERVICE, ACCOUNT)) .as("Please remove password entry '%s' " + "by using Keychain Access before running the tests", SERVICE) .isNotNull(); }