mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Peter Major
16.21.2013 4aa16886ef9ceb87b62417688829638d53004415
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2013 ForgeRock AS.
 */
package org.forgerock.opendj.ldap;
 
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldap.Connections.newInternalConnection;
import static org.forgerock.opendj.ldap.requests.Requests.newAddRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newDeleteRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newModifyRequest;
import static org.forgerock.opendj.ldap.requests.Requests.newSimpleBindRequest;
import static org.forgerock.opendj.ldif.LDIFEntryReader.valueOfLDIFEntry;
 
import java.io.IOException;
import java.util.Arrays;
 
import org.forgerock.opendj.ldap.controls.AssertionRequestControl;
import org.forgerock.opendj.ldap.controls.PermissiveModifyRequestControl;
import org.forgerock.opendj.ldap.controls.PostReadRequestControl;
import org.forgerock.opendj.ldap.controls.PostReadResponseControl;
import org.forgerock.opendj.ldap.controls.PreReadRequestControl;
import org.forgerock.opendj.ldap.controls.PreReadResponseControl;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.testng.annotations.Test;
 
/**
 * Memory backend tests.
 */
@SuppressWarnings("javadoc")
public class MemoryBackendTestCase extends SdkTestCase {
 
    @Test
    public void testAdd() throws Exception {
        final Connection connection = getConnection();
        final Entry newDomain =
                valueOfLDIFEntry("dn: dc=new domain,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: new domain");
        connection.add(newDomain);
        assertThat(connection.readEntry("dc=new domain,dc=com")).isEqualTo(newDomain);
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testAddAlreadyExists() throws Exception {
        final Connection connection = getConnection();
        connection.add(valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                "objectClass: top", "dc: example"));
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testAddNoParent() throws Exception {
        final Connection connection = getConnection();
        connection.add(valueOfLDIFEntry("dn: dc=new domain,dc=missing,dc=com",
                "objectClass: domain", "objectClass: top", "dc: new domain"));
    }
 
    @Test
    public void testAddPostRead() throws Exception {
        final Connection connection = getConnection();
        final Entry newDomain =
                valueOfLDIFEntry("dn: dc=new domain,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: new domain");
        assertThat(
                connection.add(
                        newAddRequest(newDomain)
                                .addControl(PostReadRequestControl.newControl(true))).getControl(
                        PostReadResponseControl.DECODER, new DecodeOptions()).getEntry())
                .isEqualTo(newDomain);
    }
 
    @Test(expectedExceptions = ErrorResultException.class)
    public void testAddPreRead() throws Exception {
        final Connection connection = getConnection();
        final Entry newDomain =
                valueOfLDIFEntry("dn: dc=new domain,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: new domain");
        connection.add(newAddRequest(newDomain).addControl(PreReadRequestControl.newControl(true)));
    }
 
    @Test
    public void testCompareFalse() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.compare("dc=example,dc=com", "objectclass", "person").matched())
                .isFalse();
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testCompareNoSuchObject() throws Exception {
        final Connection connection = getConnection();
        connection.compare("uid=missing,ou=people,dc=example,dc=com", "uid", "missing");
    }
 
    @Test
    public void testCompareTrue() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.compare("dc=example,dc=com", "objectclass", "domain").matched())
                .isTrue();
    }
 
    @Test(expectedExceptions = AssertionFailureException.class)
    public void testDeleteAssertionFalse() throws Exception {
        final Connection connection = getConnection();
        connection.delete(newDeleteRequest("dc=xxx,dc=com").addControl(
                AssertionRequestControl.newControl(true, Filter.valueOf("(objectclass=person)"))));
    }
 
    @Test
    public void testDeleteAssertionTrue() throws Exception {
        final Connection connection = getConnection();
        connection.delete(newDeleteRequest("dc=xxx,dc=com").addControl(
                AssertionRequestControl.newControl(true, Filter.valueOf("(objectclass=domain)"))));
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testDeleteNoSuchObject() throws Exception {
        final Connection connection = getConnection();
        connection.delete("uid=missing,ou=people,dc=example,dc=com");
    }
 
    @Test
    public void testDeleteOnLeaf() throws Exception {
        final Connection connection = getConnection();
        connection.delete("uid=test1,ou=people,dc=example,dc=com");
        try {
            connection.readEntry("dc=example,dc=com");
        } catch (final EntryNotFoundException expected) {
            // Do nothing.
        }
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testDeleteOnNonLeaf() throws Exception {
        final Connection connection = getConnection();
        try {
            connection.delete("dc=example,dc=com");
        } finally {
            assertThat(connection.readEntry("dc=example,dc=com")).isNotNull();
        }
    }
 
    @Test(expectedExceptions = ErrorResultException.class)
    public void testDeletePostRead() throws Exception {
        final Connection connection = getConnection();
        connection.delete(newDeleteRequest("dc=xxx,dc=com").addControl(
                PostReadRequestControl.newControl(true)));
    }
 
    @Test
    public void testDeletePreRead() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.delete(
                        newDeleteRequest("dc=xxx,dc=com").addControl(
                                PreReadRequestControl.newControl(true))).getControl(
                        PreReadResponseControl.DECODER, new DecodeOptions()).getEntry()).isEqualTo(
                valueOfLDIFEntry("dn: dc=xxx,dc=com", "objectClass: domain", "objectClass: top",
                        "dc: xxx"));
    }
 
    @Test
    public void testDeleteSubtree() throws Exception {
        final Connection connection = getConnection();
        connection.deleteSubtree("dc=example,dc=com");
        for (final String name : Arrays.asList("dc=example,dc=com", "ou=people,dc=example,dc=com",
                "uid=test1,ou=people,dc=example,dc=com", "uid=test2,ou=people,dc=example,dc=com")) {
            try {
                connection.readEntry(name);
            } catch (final EntryNotFoundException expected) {
                // Do nothing.
            }
        }
        assertThat(connection.readEntry("dc=xxx,dc=com")).isNotNull();
    }
 
    @Test
    public void testModify() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "add: description",
                "description: test description");
        assertThat(connection.readEntry("dc=example,dc=com")).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example", "description: test description"));
    }
 
    @Test(expectedExceptions = AssertionFailureException.class)
    public void testModifyAssertionFalse() throws Exception {
        final Connection connection = getConnection();
        connection.modify(newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                "add: description", "description: test description").addControl(
                AssertionRequestControl.newControl(true, Filter.valueOf("(objectclass=person)"))));
    }
 
    @Test
    public void testModifyAssertionTrue() throws Exception {
        final Connection connection = getConnection();
        connection.modify(newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                "add: description", "description: test description").addControl(
                AssertionRequestControl.newControl(true, Filter.valueOf("(objectclass=domain)"))));
    }
 
    @Test
    public void testModifyIncrement() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "add: integer",
                "integer: 100", "-", "increment: integer", "integer: 10");
        assertThat(connection.readEntry("dc=example,dc=com")).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example", "integer: 110"));
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testModifyIncrementBadDelta() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "add: integer",
                "integer: 100", "-", "increment: integer", "integer: nan");
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testModifyIncrementBadValue() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "add: integer",
                "integer: nan", "-", "increment: integer", "integer: 10");
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testModifyNoSuchObject() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=missing,dc=com", "changetype: modify", "add: description",
                "description: test description");
    }
 
    @Test
    public void testModifyPermissiveWithDuplicateValues() throws Exception {
        final Connection connection = getConnection();
        connection.modify(newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                "add: dc", "dc: example").addControl(
                PermissiveModifyRequestControl.newControl(true)));
        assertThat(connection.readEntry("dc=example,dc=com")).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example"));
    }
 
    @Test
    public void testModifyPermissiveWithMissingValues() throws Exception {
        final Connection connection = getConnection();
        connection.modify(newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                "delete: dc", "dc: xxx")
                .addControl(PermissiveModifyRequestControl.newControl(true)));
        assertThat(connection.readEntry("dc=example,dc=com")).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example"));
    }
 
    @Test
    public void testModifyPostRead() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.modify(
                        newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                                "add: description", "description: test description").addControl(
                                PostReadRequestControl.newControl(true))).getControl(
                        PostReadResponseControl.DECODER, new DecodeOptions()).getEntry())
                .isEqualTo(
                        valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                                "objectClass: top", "dc: example", "description: test description"));
    }
 
    @Test
    public void testModifyPostReadAttributesSelected() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.modify(
                        newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                                "add: description", "description: test description").addControl(
                                PostReadRequestControl.newControl(true, "dc", "entryDN")))
                        .getControl(PostReadResponseControl.DECODER, new DecodeOptions())
                        .getEntry()).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "dc: example",
                        "entryDN: dc=example,dc=com"));
    }
 
    @Test
    public void testModifyPreReadAttributesSelected() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.modify(
                        newModifyRequest("dn: dc=example,dc=com", "changetype: modify",
                                "add: description", "description: test description").addControl(
                                PreReadRequestControl.newControl(true, "dc", "entryDN")))
                        .getControl(PreReadResponseControl.DECODER, new DecodeOptions()).getEntry())
                .isEqualTo(
                        valueOfLDIFEntry("dn: dc=example,dc=com", "dc: example",
                                "entryDN: dc=example,dc=com"));
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testModifyStrictWithDuplicateValues() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "add: dc", "dc: example");
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testModifyStrictWithMissingValues() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "delete: dc", "dc: xxx");
    }
 
    @Test(expectedExceptions = ConstraintViolationException.class)
    public void testModifyStrictWithMissingAttribute() throws Exception {
        final Connection connection = getConnection();
        connection.modify("dn: dc=example,dc=com", "changetype: modify", "delete: cn");
    }
 
    @Test
    public void testSearchAttributesOperational() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.readEntry("uid=test1,ou=People,dc=example,dc=com", "+")).isEqualTo(
                valueOfLDIFEntry("dn: uid=test1,ou=People,dc=example,dc=com",
                        "entryDN: uid=test1,ou=people,dc=example,dc=com",
                        "entryUUID: fc252fd9-b982-3ed6-b42a-c76d2546312c"));
    }
 
    @Test
    public void testSearchAttributesSelected() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.readEntry("uid=test1,ou=People,dc=example,dc=com", "uid", "entryDN"))
                .isEqualTo(
                        valueOfLDIFEntry("dn: uid=test1,ou=People,dc=example,dc=com", "uid: test1",
                                "entryDN: uid=test1,ou=People,dc=example,dc=com"));
    }
 
    @Test
    public void testSearchAttributesSelectedTypesOnly() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=test1,ou=People,dc=example,dc=com", SearchScope.BASE_OBJECT,
                        "(objectClass=*)", "uid", "entryDN").setTypesOnly(true))).isEqualTo(
                new LinkedHashMapEntry("uid=test1,ou=People,dc=example,dc=com").addAttribute("uid")
                        .addAttribute("entryDN"));
    }
 
    @Test
    public void testSearchAttributesRenamed() throws Exception {
        final Connection connection = getConnection();
        final Entry entry =
                connection.readEntry("uid=test1,ou=People,dc=example,dc=com", "commonName",
                        "ENTRYDN");
        assertThat(entry)
                .isEqualTo(
                        valueOfLDIFEntry("dn: uid=test1,ou=People,dc=example,dc=com",
                                "commonName: test user 1",
                                "ENTRYDN: uid=test1,ou=People,dc=example,dc=com"));
        assertThat(entry.getAttribute("cn").getAttributeDescriptionAsString()).isEqualTo(
                "commonName");
        assertThat(entry.getAttribute("entryDN").getAttributeDescriptionAsString()).isEqualTo(
                "ENTRYDN");
    }
 
    @Test
    public void testSearchAttributesUser() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.readEntry("uid=test1,ou=People,dc=example,dc=com", "*")).isEqualTo(
                getUser1Entry());
    }
 
    @Test
    public void testSearchBase() throws Exception {
        final Connection connection = getConnection();
        assertThat(connection.readEntry("dc=example,dc=com")).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example"));
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testSearchBaseNoSuchObject() throws Exception {
        final Connection connection = getConnection();
        connection.readEntry("dc=missing,dc=com");
    }
 
    @Test
    public void testSearchOneLevel() throws Exception {
        final Connection connection = getConnection();
        final ConnectionEntryReader reader =
                connection.search("dc=com", SearchScope.SINGLE_LEVEL, "(objectClass=*)");
        assertThat(reader.readEntry()).isEqualTo(
                valueOfLDIFEntry("dn: dc=example,dc=com", "objectClass: domain",
                        "objectClass: top", "dc: example"));
        assertThat(reader.readEntry()).isEqualTo(
                valueOfLDIFEntry("dn: dc=xxx,dc=com", "objectClass: domain", "objectClass: top",
                        "dc: xxx"));
        assertThat(reader.hasNext()).isFalse();
    }
 
    @Test
    public void testSearchSubtree() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.searchSingleEntry("dc=example,dc=com", SearchScope.WHOLE_SUBTREE,
                        "(uid=test1)")).isEqualTo(getUser1Entry());
    }
 
    @Test(expectedExceptions = EntryNotFoundException.class)
    public void testSearchSubtreeNotFound() throws Exception {
        final Connection connection = getConnection();
        connection.searchSingleEntry("dc=example,dc=com", SearchScope.WHOLE_SUBTREE,
                "(uid=missing)");
    }
 
    @Test
    public void testSimpleBind() throws Exception {
        final Connection connection = getConnection();
        connection.bind("uid=test1,ou=people,dc=example,dc=com", "password".toCharArray());
    }
 
    @Test(expectedExceptions = AuthenticationException.class)
    public void testSimpleBindBadPassword() throws Exception {
        final Connection connection = getConnection();
        connection.bind("uid=test1,ou=people,dc=example,dc=com", "bad".toCharArray());
    }
 
    @Test(expectedExceptions = AuthenticationException.class)
    public void testSimpleBindNoSuchUser() throws Exception {
        final Connection connection = getConnection();
        connection.bind("uid=missing,ou=people,dc=example,dc=com", "password".toCharArray());
    }
 
    @Test
    public void testSimpleBindPostRead() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.bind(
                        newSimpleBindRequest("uid=test1,ou=people,dc=example,dc=com",
                                "password".toCharArray()).addControl(
                                PostReadRequestControl.newControl(true))).getControl(
                        PostReadResponseControl.DECODER, new DecodeOptions()).getEntry())
                .isEqualTo(getUser1Entry());
    }
 
    @Test
    public void testSimpleBindPreRead() throws Exception {
        final Connection connection = getConnection();
        assertThat(
                connection.bind(
                        newSimpleBindRequest("uid=test1,ou=people,dc=example,dc=com",
                                "password".toCharArray()).addControl(
                                PreReadRequestControl.newControl(true))).getControl(
                        PreReadResponseControl.DECODER, new DecodeOptions()).getEntry()).isEqualTo(
                getUser1Entry());
    }
 
    private Connection getConnection() throws IOException {
        // @formatter:off
        final MemoryBackend backend =
                new MemoryBackend(new LDIFEntryReader(
                        "dn: dc=com",
                        "objectClass: domain",
                        "objectClass: top",
                        "dc: com",
                        "",
                        "dn: dc=example,dc=com",
                        "objectClass: domain",
                        "objectClass: top",
                        "dc: example",
                        "entryDN: dc=example,dc=com",
                        "entryUUID: fc252fd9-b982-3ed6-b42a-c76d2546312c",
                        "",
                        "dn: ou=People,dc=example,dc=com",
                        "objectClass: organizationalunit",
                        "objectClass: top",
                        "ou: People",
                        "",
                        "dn: uid=test1,ou=People,dc=example,dc=com",
                        "objectClass: top",
                        "objectClass: person",
                        "uid: test1",
                        "userpassword: password",
                        "cn: test user 1",
                        "sn: user 1",
                        "entryDN: uid=test1,ou=people,dc=example,dc=com",
                        "entryUUID: fc252fd9-b982-3ed6-b42a-c76d2546312c",
                        "",
                        "dn: uid=test2,ou=People,dc=example,dc=com",
                        "objectClass: top",
                        "objectClass: person",
                        "uid: test2",
                        "userpassword: password",
                        "cn: test user 2",
                        "sn: user 2",
                        "",
                        "dn: dc=xxx,dc=com",
                        "objectClass: domain",
                        "objectClass: top",
                        "dc: xxx"
                ));
        // @formatter:on
 
        return newInternalConnection(backend);
    }
 
    private Entry getUser1Entry() {
        return valueOfLDIFEntry("dn: uid=test1,ou=People,dc=example,dc=com", "objectClass: top",
                "objectClass: person", "uid: test1", "userpassword: password", "cn: test user 1",
                "sn: user 1");
    }
 
}