Skip to content

Commit

Permalink
Try different Test structer
Browse files Browse the repository at this point in the history
  • Loading branch information
rexhoffman committed Sep 25, 2023
1 parent fcb06de commit 168e61b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .setupkeystore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ keyring="$1"

set -x
if [ "$(expr substr $(uname -s) 1 5)" == "Linux" ] && [ "$keyring" == "gnome" ]; then
killall kwalletd || true
export $(dbus-launch) # --config-file=${GITHUB_WORKSPACE}/.github/workflows/dbus.xml)
eval "$(echo '\n' | gnome-keyring-daemon --unlock)"
echo [email protected] | secret-tool store --label="main" email address
Expand All @@ -21,6 +22,7 @@ if [ "$(expr substr $(uname -s) 1 5)" == "Linux" ] && [ "$keyring" == "kde" ]; t
# install pre-filled wallet (because there is no way to interactively create key store without password)
# created with empty password
# entry added with `kwalletcli -f . -e address -p [email protected]`
killall gnome-keyring-daemon || true
mkdir -p ~/.local/share/kwalletd
cp $GITHUB_WORKSPACE/.setupkeystore/* ~/.local/share/kwalletd
chmod 600 ~/.local/share/kwalletd/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public SimpleCollection() throws IOException {
unlock();
}

public boolean isConnected() {
return this.session.getConnection().isConnected();
}

/*
* A user specified collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import org.freedesktop.dbus.annotations.DBusInterfaceName;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
//import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.interfaces.DBusInterface;

import java.io.IOException;
Expand All @@ -47,15 +47,8 @@ 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();
//connection = DBusConnectionBuilder.forSessionBus().build();
connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION);
wallet = connection.getRemoteObject("org.kde.kwalletd5", "/modules/kwalletd5", KWallet.class, true);
wallet.localWallet(); //attempt connection to wallet
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.github.javakeyring.BackendNotSupportedException;


public class KeyringBackendFactoryTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 168e61b

Please sign in to comment.