11package org .cryptomator .linux .keychain ;
22
3+ import org .cryptomator .integrations .keychain .KeychainAccessException ;
34import org .junit .jupiter .api .Assertions ;
45import 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 ;
59import org .junit .jupiter .api .Test ;
10+ import org .junit .jupiter .api .TestMethodOrder ;
611import org .junit .jupiter .api .condition .EnabledIf ;
12+ import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
713
814import java .io .IOException ;
915import java .util .List ;
16+ import java .util .UUID ;
1017import java .util .concurrent .TimeUnit ;
1118
1219/**
1320 * Unit tests for GNOME keyring access via DBUS.
1421 */
22+ @ EnabledIfEnvironmentVariable (named = "DISPLAY" , matches = ".*" )
1523public 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