Skip to content

Commit fd9aede

Browse files
committed
add functional test for SecretServiceKeychainAccess
1 parent 057a774 commit fd9aede

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed
Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package org.cryptomator.linux.keychain;
22

3+
import org.cryptomator.integrations.keychain.KeychainAccessException;
34
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.MethodOrderer;
7+
import org.junit.jupiter.api.Nested;
8+
import org.junit.jupiter.api.Order;
59
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.TestMethodOrder;
611
import org.junit.jupiter.api.condition.EnabledIf;
12+
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
713

814
import java.io.IOException;
915
import java.util.List;
16+
import java.util.UUID;
1017
import java.util.concurrent.TimeUnit;
1118

1219
/**
1320
* Unit tests for GNOME keyring access via DBUS.
1421
*/
22+
@EnabledIfEnvironmentVariable(named = "DISPLAY", matches = ".*")
1523
public class SecretServiceKeychainAccessTest {
1624

1725
private static boolean isInstalled;
1826

1927
@BeforeAll
2028
public static void checkSystemAndSetup() throws IOException {
2129
ProcessBuilder dbusSend = new ProcessBuilder("dbus-send", "--print-reply", "--dest=org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus.ListNames");
22-
ProcessBuilder grep = new ProcessBuilder("grep", "org.gnome.keyring");
30+
ProcessBuilder grep = new ProcessBuilder("grep", "-q", "org.gnome.keyring");
2331
try {
2432
Process end = ProcessBuilder.startPipeline(List.of(dbusSend, grep)).get(1);
2533
if (end.waitFor(1000, TimeUnit.MILLISECONDS)) {
@@ -32,15 +40,51 @@ public static void checkSystemAndSetup() throws IOException {
3240
}
3341
}
3442

35-
3643
@Test
37-
@EnabledIf("isXdgDisplayEnvVarSet")
3844
public void testIsSupported() {
39-
SecretServiceKeychainAccess secretService = new SecretServiceKeychainAccess();
40-
Assertions.assertEquals(isInstalled, secretService.isSupported());
45+
var gnomeKeyring = new SecretServiceKeychainAccess();
46+
Assertions.assertEquals(isInstalled, gnomeKeyring.isSupported());
4147
}
4248

43-
public boolean isXdgDisplayEnvVarSet() {
44-
return System.getenv("DISPLAY") != null;
49+
@Nested
50+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
51+
@EnabledIf("gnomeKeyringAvailableAndUnlocked")
52+
class FunctionalTests {
53+
54+
static final String KEY_ID = "cryptomator-test-" + UUID.randomUUID();
55+
final SecretServiceKeychainAccess gnomeKeyring = new SecretServiceKeychainAccess();
56+
57+
@Test
58+
@Order(1)
59+
public void testStore() throws KeychainAccessException {
60+
gnomeKeyring.storePassphrase(KEY_ID, "cryptomator-test", "p0ssw0rd");
61+
}
62+
63+
@Test
64+
@Order(2)
65+
public void testLoad() throws KeychainAccessException {
66+
var passphrase = gnomeKeyring.loadPassphrase(KEY_ID);
67+
Assertions.assertNotNull(passphrase);
68+
Assertions.assertEquals("p0ssw0rd", String.copyValueOf(passphrase));
69+
}
70+
71+
@Test
72+
@Order(3)
73+
public void testDelete() throws KeychainAccessException {
74+
gnomeKeyring.deletePassphrase(KEY_ID);
75+
}
76+
77+
@Test
78+
@Order(4)
79+
public void testLoadNotExisting() throws KeychainAccessException {
80+
var result = gnomeKeyring.loadPassphrase(KEY_ID);
81+
Assertions.assertNull(result);
82+
}
83+
84+
public static boolean gnomeKeyringAvailableAndUnlocked() {
85+
var secretServiceKeychain = new SecretServiceKeychainAccess();
86+
return secretServiceKeychain.isSupported() && !secretServiceKeychain.isLocked();
87+
}
4588
}
89+
4690
}

0 commit comments

Comments
 (0)