forked from swiesend/secret-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceTest.java
More file actions
354 lines (284 loc) · 13.4 KB
/
Copy pathServiceTest.java
File metadata and controls
354 lines (284 loc) · 13.4 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package de.swiesend.secretservice.integration;
import de.swiesend.secretservice.*;
import de.swiesend.secretservice.Collection;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.types.Variant;
import de.swiesend.secretservice.errors.NoSuchObject;
import de.swiesend.secretservice.integration.test.Context;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import static de.swiesend.secretservice.Static.Convert.toStrings;
import static org.junit.jupiter.api.Assertions.*;
public class ServiceTest {
private Logger log = LoggerFactory.getLogger(getClass());
private Context context;
@BeforeEach
public void beforeEach(TestInfo info) {
log.info(info.getDisplayName());
context = new Context(log);
}
@AfterEach
public void afterEach() {
context.after();
}
@Test
public void openSession() {
context.ensureService();
Optional<Pair<Variant<byte[]>, DBusPath>> response = context.service.openSession(Static.Algorithm.PLAIN, new Variant<>(""));
log.info(response.toString());
assertEquals("s", response.get().a.getSig());
assertEquals("", response.get().a.getValue(), "the value of an empty byte[] behaves odd as it returns a String.");
DBusPath sessionPath = response.get().b;
assertTrue(sessionPath.getPath().startsWith("/org/freedesktop/secrets/session/s"));
}
@Test
public void openSessionWithTransportEncryption() {
context.ensureService();
byte[] input = new byte[]{
(byte) 0x7e, (byte) 0x71, (byte) 0x32, (byte) 0xe5, (byte) 0x66, (byte) 0xc4, (byte) 0x4b, (byte) 0xb8,
(byte) 0xf3, (byte) 0x13, (byte) 0xc3, (byte) 0x06, (byte) 0x31, (byte) 0xcf, (byte) 0xd3, (byte) 0xa0,
(byte) 0xa4, (byte) 0xf7, (byte) 0x3a, (byte) 0xbd, (byte) 0xd0, (byte) 0xa8, (byte) 0x11, (byte) 0xbe,
(byte) 0xf0, (byte) 0x04, (byte) 0xaa, (byte) 0x70, (byte) 0x7d, (byte) 0x62, (byte) 0x6a, (byte) 0x7e,
(byte) 0xd5, (byte) 0x0b, (byte) 0x2d, (byte) 0x75, (byte) 0x7e, (byte) 0x97, (byte) 0xb8, (byte) 0x22,
(byte) 0xa7, (byte) 0x1d, (byte) 0xea, (byte) 0x8a, (byte) 0xf7, (byte) 0x54, (byte) 0x1e, (byte) 0x38,
(byte) 0xdc, (byte) 0xab, (byte) 0x1a, (byte) 0x51, (byte) 0x18, (byte) 0x88, (byte) 0x7c, (byte) 0x43,
(byte) 0xe0, (byte) 0x55, (byte) 0x52, (byte) 0xa2, (byte) 0xdf, (byte) 0xae, (byte) 0x78, (byte) 0x40,
(byte) 0x3d, (byte) 0xd8, (byte) 0xd7, (byte) 0x2e, (byte) 0x2d, (byte) 0xeb, (byte) 0x0d, (byte) 0xa7,
(byte) 0x67, (byte) 0x9f, (byte) 0x18, (byte) 0x1d, (byte) 0x7e, (byte) 0xd2, (byte) 0x2a, (byte) 0x84,
(byte) 0xff, (byte) 0xd6, (byte) 0xb5, (byte) 0x0d, (byte) 0x97, (byte) 0xc4, (byte) 0x84, (byte) 0x8f,
(byte) 0x67, (byte) 0x71, (byte) 0x01, (byte) 0xc5, (byte) 0xaf, (byte) 0x2f, (byte) 0x0c, (byte) 0xe2,
(byte) 0x15, (byte) 0x91, (byte) 0x39, (byte) 0x5d, (byte) 0x38, (byte) 0xc9, (byte) 0x23, (byte) 0x8f,
(byte) 0x80, (byte) 0x96, (byte) 0x0e, (byte) 0x6b, (byte) 0xcd, (byte) 0x2f, (byte) 0xb0, (byte) 0x2c,
(byte) 0x56, (byte) 0x95, (byte) 0x41, (byte) 0xdf, (byte) 0x3b, (byte) 0x35, (byte) 0x2a, (byte) 0xa0,
(byte) 0x67, (byte) 0xf2, (byte) 0x14, (byte) 0xe3, (byte) 0x7c, (byte) 0x47, (byte) 0x5e, (byte) 0xbf
};
assertEquals(128, input.length);
Optional<Pair<Variant<byte[]>, DBusPath>> response = context.service.openSession(
Static.Algorithm.DH_IETF1024_SHA256_AES128_CBC_PKCS7, new Variant<>(input));
log.info(response.toString());
byte[] peerPublicKey = Static.Utils.toByteArray(response.get().a.getValue());
assertEquals(128, peerPublicKey.length);
DBusPath sessionPath = response.get().b;
assertTrue(sessionPath.getPath().startsWith(Static.ObjectPaths.SESSION + "/s"));
}
@Test
@Disabled
public void createCollection() {
context.ensureCollection();
DBusPath deletePrompt = context.collection.delete().orElse(new DBusPath("/"));
if (!deletePrompt.getPath().equals("/")) {
context.prompt.await(deletePrompt);
}
List<DBusPath> before = context.service.getCollections().orElse(new ArrayList<>());
Map<String, Variant> properties = Collection.createProperties("test");
Pair<DBusPath, DBusPath> response = context.service.createCollection(properties).get();
log.info(response.toString());
DBusPath collectionPath = response.a;
DBusPath createPrompt = response.b;
if (collectionPath.getPath().equals("/")) {
assertTrue(createPrompt.getPath().startsWith("/org/freedesktop/secrets/prompt/p"));
context.prompt.await(createPrompt);
} else {
assertEquals("/", createPrompt.getPath());
}
List<DBusPath> after = context.service.getCollections().get();
DBusSignal[] handled = context.prompt.getSignalHandler().getHandled();
Prompt.Completed completed = (Prompt.Completed) handled[0];
if (completed.dismissed) {
assertEquals(before.size(), after.size());
} else {
assertEquals(before.size() + 1, after.size());
}
}
@Test
public void searchItems() {
context.ensureItem();
Map<String, String> attributes = new HashMap<>();
attributes.put("Attribute1", "Value1");
Pair<List<DBusPath>, List<DBusPath>> response = context.service.searchItems(attributes).get();
List<String> unlocked = toStrings(response.a);
List<String> locked = toStrings(response.b);
assertTrue(unlocked.size() >= 0);
assertTrue(unlocked.get(0).startsWith("/org/freedesktop/secrets/collection/test/"));
assertTrue(locked.isEmpty());
}
@Test
@Disabled
public void unlockCollections() {
Pair<List<DBusPath>, DBusPath> response;
List<DBusPath> locked;
List<DBusPath> unlocked;
DBusPath prompt;
Prompt.Completed completed;
// unlock a collection
context.ensureCollection();
ArrayList<DBusPath> lockables = new ArrayList<>();
lockables.add(context.collection.getPath());
response = context.service.lock(lockables).get();
log.info(response.toString());
locked = response.a;
assertEquals(1, locked.size());
prompt = response.b;
assertEquals("/", prompt.getPath());
response = context.service.unlock(lockables).get();
log.info(response.toString());
unlocked = response.a;
assertEquals(0, unlocked.size());
prompt = response.b;
completed = context.prompt.await(prompt);
if (completed.dismissed) {
assertTrue(context.collection.isLocked());
} else {
assertFalse(context.collection.isLocked());
}
}
@Test
@Disabled
public void unlockItems() {
context.ensureItem();
Pair<List<DBusPath>, DBusPath> response;
List<DBusPath> locked, unlocked;
DBusPath prompt;
List<DBusPath> items = context.collection.getItems().get();
response = context.service.lock(items).get();
log.info(response.toString());
locked = response.a;
assertEquals(1, locked.size());
prompt = response.b;
assertEquals("/", prompt.getPath());
response = context.service.unlock(items).get();
log.info(response.toString());
unlocked = response.a;
assertEquals(0, unlocked.size());
prompt = response.b;
Prompt.Completed completed = context.prompt.await(prompt);
if (completed.dismissed) {
assertTrue(context.item.isLocked());
} else {
assertFalse(context.item.isLocked());
}
}
@Test
@Disabled
public void lockCommonCollections() throws InterruptedException, NoSuchObject {
context.ensureSession();
// lock common collections:
// * alias/default == collection/login (if not assigned otherwise)
// * collection/login
// * collection/session
ArrayList<DBusPath> collections = new ArrayList<>();
collections.add(Static.Convert.toObjectPath(Static.ObjectPaths.DEFAULT_COLLECTION));
collections.add(Static.Convert.toObjectPath(Static.ObjectPaths.LOGIN_COLLECTION));
collections.add(Static.Convert.toObjectPath(Static.ObjectPaths.SESSION_COLLECTION));
Pair<List<DBusPath>, DBusPath> response = context.service.lock(collections).get();
log.info(response.toString());
List<DBusPath> locked = response.a;
assertEquals(Static.ObjectPaths.DEFAULT_COLLECTION, locked.get(0).getPath());
assertEquals(Static.ObjectPaths.LOGIN_COLLECTION, locked.get(1).getPath());
assertEquals(Static.ObjectPaths.SESSION_COLLECTION, locked.get(2).getPath());
DBusPath prompt = response.b;
assertEquals("/", prompt.getPath());
for (int i = 0; i < collections.size(); i++) {
List<DBusPath> collection = Arrays.asList(new DBusPath[]{collections.get(i)});
response = context.service.unlock(collection).get();
prompt = response.b;
if (!prompt.getPath().equals("/")) {
context.prompt.await(prompt);
}
}
}
@Test
@Disabled
public void lockService() {
context.ensureSession();
context.service.lockService();
}
@Test
public void changeLock() {
context.ensureSession();
DBusPath obj;
DBusPath result;
obj = new DBusPath(Static.ObjectPaths.DEFAULT_COLLECTION);
result = context.service.changeLock(obj).get();
log.info(result.toString());
assertTrue(result.getPath().startsWith("/org/freedesktop/secrets/prompt/"));
obj = new DBusPath(Static.ObjectPaths.LOGIN_COLLECTION);
result = context.service.changeLock(obj).get();
log.info(result.toString());
assertTrue(result.getPath().startsWith("/org/freedesktop/secrets/prompt/"));
obj = new DBusPath(Static.ObjectPaths.SESSION_COLLECTION);
result = context.service.changeLock(obj).get();
log.info(result.toString());
assertTrue(result.getPath().startsWith("/org/freedesktop/secrets/prompt/"));
}
@Test
public void getSecrets() {
context.ensureItem();
List<DBusPath> items = context.collection.getItems().get();
Map<DBusPath, Secret> result = context.service.getSecrets(items, context.session.getPath()).get();
log.info(result.toString());
assertEquals(1, result.size());
}
@Test
public void readAlias() {
context.ensureCollection();
DBusPath collection;
collection = context.service.readAlias("default").get();
log.info(collection.toString());
assertEquals(Static.ObjectPaths.LOGIN_COLLECTION, collection.getPath(),
"the default alias should point to the login collection");
collection = context.service.readAlias("login").get();
log.info(collection.toString());
assertEquals(Static.ObjectPaths.LOGIN_COLLECTION, collection.getPath());
collection = context.service.readAlias("session").get();
log.info(collection.toString());
assertEquals(Static.ObjectPaths.SESSION_COLLECTION, collection.getPath());
collection = context.service.readAlias("test").get();
log.info(collection.toString());
assertEquals("/", collection.getPath(),
"the test collection should not have an alias");
}
@Test
@Disabled
public void setAlias() {
context.ensureCollection();
DBusPath collection;
// change the default alias to point to the test collection
context.service.setAlias("default", context.collection.getPath());
collection = context.service.readAlias("default").get();
log.info("default: " + collection);
assertEquals(context.collection.getPath().getPath(), collection.getPath());
// repair the default alias
DBusPath login = Static.Convert.toObjectPath(Static.ObjectPaths.LOGIN_COLLECTION);
context.service.setAlias("default", login);
collection = context.service.readAlias("default").get();
log.info("default: " + collection);
assertEquals(login.getPath(), collection.getPath());
}
@Test
public void getCollections() {
context.ensureCollection();
List<DBusPath> collections = context.service.getCollections().get();
log.info(Arrays.toString(collections.toArray()));
List<String> cs = toStrings(collections);
assertTrue(collections.size() >= 3);
assertTrue(cs.contains("/org/freedesktop/secrets/collection/test"));
assertTrue(cs.contains("/org/freedesktop/secrets/collection/login"));
assertTrue(cs.contains("/org/freedesktop/secrets/collection/session"));
}
@Test
public void isRemote() {
context.ensureService();
assertFalse(context.service.isRemote());
}
@Test
public void getObjectPath() {
context.ensureService();
String result = context.service.getObjectPath();
log.info(result);
assertEquals("/org/freedesktop/secrets", result);
}
}