1
1
package org .cryptomator .linux .keychain ;
2
2
3
+ import org .cryptomator .integrations .keychain .KeychainAccessException ;
3
4
import org .junit .jupiter .api .Assertions ;
4
5
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 ;
5
9
import org .junit .jupiter .api .Test ;
10
+ import org .junit .jupiter .api .TestMethodOrder ;
6
11
import org .junit .jupiter .api .condition .EnabledIf ;
12
+ import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
7
13
8
14
import java .io .IOException ;
9
15
import java .util .List ;
16
+ import java .util .UUID ;
10
17
import java .util .concurrent .TimeUnit ;
11
18
12
19
/**
13
20
* Unit tests for GNOME keyring access via DBUS.
14
21
*/
22
+ @ EnabledIfEnvironmentVariable (named = "DISPLAY" , matches = ".*" )
15
23
public class SecretServiceKeychainAccessTest {
16
24
17
25
private static boolean isInstalled ;
18
26
19
27
@ BeforeAll
20
28
public static void checkSystemAndSetup () throws IOException {
21
29
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" );
23
31
try {
24
32
Process end = ProcessBuilder .startPipeline (List .of (dbusSend , grep )).get (1 );
25
33
if (end .waitFor (1000 , TimeUnit .MILLISECONDS )) {
@@ -32,15 +40,51 @@ public static void checkSystemAndSetup() throws IOException {
32
40
}
33
41
}
34
42
35
-
36
43
@ Test
37
- @ EnabledIf ("isXdgDisplayEnvVarSet" )
38
44
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 ());
41
47
}
42
48
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
+ }
45
88
}
89
+
46
90
}
0 commit comments