forked from swiesend/secret-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntegrationTest.java
More file actions
114 lines (94 loc) · 4.76 KB
/
Copy pathIntegrationTest.java
File metadata and controls
114 lines (94 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package de.swiesend.secretservice.integration;
import de.swiesend.secretservice.*;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.types.Variant;
import de.swiesend.secretservice.gnome.keyring.InternalUnsupportedGuiltRiddenInterface;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.security.auth.DestroyFailedException;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static de.swiesend.secretservice.integration.test.Context.label;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class IntegrationTest {
private Logger log = LoggerFactory.getLogger(getClass());
@Test
public void testWithTransportEncryption() throws
DBusException,
NoSuchAlgorithmException,
InvalidAlgorithmParameterException,
NoSuchPaddingException,
InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,
InterruptedException,
IOException {
TransportEncryption transportEncryption = new TransportEncryption();
TransportEncryption.EncryptedSession encryptedSession = transportEncryption
.initialize()
.flatMap(i -> i.openSession())
.flatMap(o -> o.generateSessionKey())
.orElseThrow(
() -> new IOException("Could not initiate transport encryption.")
);
String plain = "super secret";
Secret encrypted = encryptedSession.encrypt(plain).get();
byte[] encBase64 = Base64.getEncoder().encode(encrypted.getSecretValue());
log.info(label("encrypted secret (base64)", new String(encBase64)));
char[] decrypted = encryptedSession.decrypt(encrypted).get();
log.info(label(" decrypted secret", new String(decrypted)));
assertEquals(plain, new String(decrypted));
Service service = transportEncryption.getService();
Session session = transportEncryption.getSession();
InternalUnsupportedGuiltRiddenInterface noPrompt = new InternalUnsupportedGuiltRiddenInterface(service);
Secret master = encryptedSession.encrypt("test").get();
Collection collection = new Collection("test", service.getConnection());
List<String> collections = Static.Convert.toStrings(service.getCollections().get());
if (collections.contains(collection.getObjectPath())) {
noPrompt.unlockWithMasterPassword(collection.getPath(), master);
} else {
HashMap<String, Variant> properties = new HashMap<>();
properties.put("org.freedesktop.Secret.Collection.Label", new Variant<>("test"));
DBusPath collectionPath = noPrompt.createWithMasterPassword(properties, master).get();
log.info("created collection: " + collectionPath.getPath());
}
// create item with secret
Map<String, String> attributes = new HashMap<>();
attributes.put("transport", "encrypted");
attributes.put("algorithm", "AES");
attributes.put("bits", "128");
attributes.put("mode", "CBC");
attributes.put("padding", "PKCS5");
attributes.put("prime", "RFC 2409 Second Oakley Group");
attributes.put("generator", "RFC 2409 Second Oakley Group");
Map<String, Variant> properties = Item.createProperties("TestItemWithTransportEncryption", attributes);
if (collection.isLocked()) {
noPrompt.unlockWithMasterPassword(collection.getPath(), master);
}
Pair<DBusPath, DBusPath> createItemResponse = collection.createItem(properties, encrypted, true).get();
log.info("await signal: Collection.ItemCreated");
Thread.sleep(50L);
DBusPath itemPath = createItemResponse.a;
Item item = new Item(itemPath, service);
Secret actual = item.getSecret(session.getPath()).get();
assertEquals(encrypted.getSession().getPath(), actual.getSession().getPath());
assertEquals(encrypted.getContentType(), actual.getContentType());
decrypted = encryptedSession.decrypt(actual).get();
log.info(label(" decrypted remote secret", new String(decrypted)));
assertEquals(plain, new String(decrypted));
// finally close and clear the private key
transportEncryption.close();
}
}