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

Valery Kharseko
23 hours ago 324d3c5dcbb19cd2e44ec53235ba1e90880d86f7
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2024-2026 3A Systems, LLC.
 */
package org.openidentityplatform.opendj;
 
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.ldap.*;
import org.forgerock.opendj.ldap.controls.PersistentSearchChangeType;
import org.forgerock.opendj.ldap.controls.PersistentSearchRequestControl;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.opendj.server.config.client.GlobalCfgClient;
import org.forgerock.opendj.server.config.meta.GlobalCfgDefn;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
 
import org.opends.server.api.LocalBackend;
import org.opends.server.backends.MemoryBackend;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.AcceptRejectWarn;
import org.opends.server.types.Entry;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
 
@Test(sequential = true)
public class AliasTestCase extends DirectoryServerTestCase {
    /** Backend holding a naming context other than o=test, to check aliases crossing a backend boundary. */
    static final String OTHER_BACKEND_ID = "test2";
 
    Connection connection;
 
    @BeforeClass
    public void startServer() throws Exception {
        TestCaseUtils.startServer();
        TestCaseUtils.initializeTestBackend(true);
        TestCaseUtils.initializeMemoryBackend(OTHER_BACKEND_ID, "o=test2", true);
 
        TestCaseUtils.addEntries(
                 "dn: o=MyCompany, o=test",
                        "o: MyCompany",
                        "objectclass:organization",
                        "",
                        "dn: ou=Area1, o=test",
                        "objectclass: alias",
                        "objectclass: extensibleobject",
                        "ou: Area1",
                        "aliasedObjectName: o=MyCompany, o=test",
                        "",
                        "dn: cn=John Doe, o=MyCompany, o=test",
                        "cn: John Doe",
                        "sn: Doe",
                        "objectclass: person",
                        "",
                        "dn: cn=President, o=MyCompany, o=test",
                        "objectclass: alias",
                        "objectclass: extensibleobject",
                        "cn: President",
                        "aliasedobjectname: cn=John Doe, o=MyCompany, o=test",
                        "",
 
                        "dn: ou=employees,o=test",
                        "objectClass: top",
                        "objectClass: organizationalUnit",
                        "ou: employees",
                        "description: All employees",
                        "",
                        "dn: uid=jdoe,ou=employees,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "aliasedObjectName: uid=jdoe,ou=researchers,o=test",
                        "uid: jdoe",
                        "",
                        "dn: ou=researchers,o=test",
                        "objectClass: top",
                        "objectClass: organizationalUnit",
                        "ou: researchers",
                        "description: All reasearchers",
                        "",
                        "dn: uid=jdoe,ou=researchers,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "aliasedObjectName: uid=jdoe,ou=employees,o=test",
                        "uid: jdoe",
 
                        "",
                        "dn: ou=students,o=test",
                        "objectClass: top",
                        "objectClass: organizationalUnit",
                        "ou: students",
                        "description: All students",
                        "",
                        "dn: uid=janedoe,ou=students,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "aliasedObjectName: uid=janedoe,ou=researchers,o=test",
                        "uid: janedoe",
                        "",
                        "dn: uid=janedoe,ou=researchers,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "aliasedObjectName: uid=janedoe,ou=employees,o=test",
                        "uid: janedoe",
                        "",
                        "dn: uid=janedoe,ou=employees,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "aliasedObjectName: uid=janedoe,ou=students,o=test",
                        "uid: janedoe",
                        "",
 
                        //an alias pointing into the o=test2 backend, and one pointing nowhere at all
                        "dn: ou=people,o=test2",
                        "objectClass: top",
                        "objectClass: organizationalUnit",
                        "ou: people",
                        "",
                        "dn: cn=Jane Roe,ou=people,o=test2",
                        "cn: Jane Roe",
                        "sn: Roe",
                        "objectclass: person",
                        "",
                        "dn: ou=external,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "ou: external",
                        "aliasedObjectName: ou=people,o=test2",
                        "",
                        "dn: ou=nowhere,o=test",
                        "objectClass: alias",
                        "objectClass: top",
                        "objectClass: extensibleObject",
                        "ou: nowhere",
                        "aliasedObjectName: ou=people,o=missing",
                        ""
        );
 
        final LDAPConnectionFactory factory =new LDAPConnectionFactory("localhost", TestCaseUtils.getServerLdapPort());
        connection = factory.getConnection();
        connection.bind("cn=Directory Manager", "password".toCharArray());
        assertThat(connection.isValid()).isTrue();
    }
 
    @AfterClass
    public void stopServer() throws Exception {
        if (connection != null) {
            connection.close();
        }
        final MemoryBackend otherBackend = (MemoryBackend) TestCaseUtils.getServerContext()
                .getBackendConfigManager().getLocalBackendById(OTHER_BACKEND_ID);
        if (otherBackend != null) {
            otherBackend.clearMemoryBackend();
            otherBackend.finalizeBackend();
            TestCaseUtils.getServerContext().getBackendConfigManager().deregisterLocalBackend(otherBackend);
        }
    }
 
    public HashMap<String,SearchResultEntry> search(SearchScope scope,DereferenceAliasesPolicy policy) throws SearchResultReferenceIOException, LdapException {
        return search("ou=Area1,o=test", scope, policy);
    }
 
    public HashMap<String,SearchResultEntry> search(String dn, SearchScope scope,DereferenceAliasesPolicy policy) throws SearchResultReferenceIOException, LdapException {
        final SearchRequest request =Requests.newSearchRequest(dn, scope,"(objectclass=*)")
                .setDereferenceAliasesPolicy(policy);
        System.out.println("---------------------------------------------------------------------------------------");
        System.out.println(request);
 
        HashMap<String,SearchResultEntry> res=new HashMap<>();
        final ConnectionEntryReader reader = connection.search(request);
        while (reader.hasNext()) {
            final SearchResultEntry srEntry = reader.readEntry();
            System.out.println(srEntry);
            assertThat(res.put(srEntry.getName().toString(),srEntry)).isNull();
        }
        return res;
    }
 
    //https://docs.oracle.com/cd/E21043_01/oid.1111/e10029/oid_alias_entries.htm
    //
    //    16.3.1 Searching the Base with Alias Entries
    //
    //    A base search finds the top level of the alias entry you specify.
    @Test
    public void test_base_never() throws LdapException, SearchResultReferenceIOException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.BASE_OBJECT,DereferenceAliasesPolicy.NEVER);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isTrue();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
    //    Base Search with the Dereferencing Flag -a find
    //
    //    This example shows a base search of ou=Area1,o=test with a filter of "objectclass=*" with the dereferencing flag set to -a find.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a find -s base "objectclass=*"
    //    The directory server, during the base search, looks up the base specified in the search request and returns it to the user. However, if the base is an alias entry and, as in the example, -a find is specified in the search request, then the directory server automatically dereferences the alias entry and returns the entry it points to.
    //    In this example, the search dereferences ou=Area1,o=test, which is an alias entry, and returns o=MyCompany,o=test.
    @Test
    public void test_base_find() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.BASE_OBJECT,DereferenceAliasesPolicy.FINDING_BASE);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //    Base Search with the Dereferencing Flag -a search
    //
    //    This example shows a base search of ou=Area1,o=test with a filter of "objectclass=*" with the dereferencing flag set to -a search.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a search -s base "objectclass=*"
    //    The directory server, during the base search, looks up the base specified in the search request and returns it to the user without dereferencing it.
    //    It returns ou=Area1,o=test.
    @Test
    public void test_base_search() throws SearchResultReferenceIOException, LdapException  {
        HashMap<String,SearchResultEntry> res=search(SearchScope.BASE_OBJECT, DereferenceAliasesPolicy.IN_SEARCHING);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isTrue();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //    Base Search with the Dereferencing Flag -a always
    //
    //    This example shows a base search of ou=Area1,o=test with a filter of "objectclass=*" with the dereferencing flag set to -a always.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a always -s base "objectclass=*"
    //    The directory server, during the base search, looks up the base specified in the search request.
    //    If it is an alias entry, the directory server automatically dereferences the alias entry and returns the entry it points to.
    //    In this example, the search dereferences ou=Area1,o=test, which is an alias entry, and returns o=MyCompany,o=test.
    @Test
    public void test_base_always() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.BASE_OBJECT,DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //16.3.2 Searching One-Level with Alias Entries
    //
    //    A one-level search finds only the children of the base level you specify.
    @Test
    public void test_one_never() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.SINGLE_LEVEL,DereferenceAliasesPolicy.NEVER);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //    One-Level Search with the Dereferencing Flag -a find
    //
    //    This example shows a one-level search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a find.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a find -s one "objectclass=*"
    //    The directory server returns one-level entries under the base that match the filter criteria. In the example, -a find is specified in the search request, and thus the directory server automatically dereferences while looking up the base (the first step), but does not dereference alias entries that are one level under the base. Therefore, the search dereferences ou=Area1,o=test, which is an alias entry, and then looks up one-level entries under o=MyCompany,o=test. One of the one-level entries is cn=President,o=MyCompany,o=test that is not dereferenced and is returned as is.
    //
    //            Thus, the search returns cn=President,o=MyCompany,o=test and cn=John Doe,o=MyCompany,o=test.
    @Test
    public void test_one_find() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.SINGLE_LEVEL,DereferenceAliasesPolicy.FINDING_BASE);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
    
    //            One-Level Search with the Dereferencing Flag -a search
    //
    //    This example shows a one-level search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a search.
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a search -s one "objectclass=*"
    //    The directory server searches for the base that is specified in the search request.
    //    If the base entry is an alias entry, it returns nothing. (Alias entries cannot have children.)
    //    Otherwise, it returns the base entry's immediate children after dereferencing them.
    //    In this example, the base entry is "ou=Area1,o=test", which is an alias entry, so the search returns nothing
 
    @Test
    public void test_one_search() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.SINGLE_LEVEL,DereferenceAliasesPolicy.IN_SEARCHING);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //    One-Level Search with the Dereferencing Flag -a always
    //
    //    This example shows a one-level search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a always.
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a always -s one "objectclass=*"
    //    In the example, -a always is specified in the search request, and thus the directory server automatically dereferences while looking up the base (the first step),
    //    then dereference alias entries that are one level under the base.
    //    Therefore, the search dereferences ou=Area1,o=test, which is an alias entry, and then looks up one-level entries under o=MyCompany,o=test.
    //    One of the one-level entries is cn=President,o=MyCompany,o=test. That is dereferenced and is returned as cn=John Doe,o=MyCompany,o=test.
    //    The other one-level entry is cn=John Doe,o=MyCompany,o=test, which has already been returned.
    //
    //            Thus, the search returns cn=John Doe,o=MyCompany,o=test.
    @Test
    public void test_one_always() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.SINGLE_LEVEL,DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
 
    //16.3.3 Searching a Subtree with Alias Entries
    //
    //    A subtree search finds the base, children, and grand children.
    @Test
    public void test_sub_never() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.WHOLE_SUBTREE,DereferenceAliasesPolicy.NEVER);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isTrue();
        assertThat(res.containsKey("o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isFalse();
    }
 
    //    Subtree Search with the Dereferencing Flag -a find
    //
    //    This example shows a subtree search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a find.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a find -s sub "objectclass=*"
    //    The directory server returns all entries under the base that match the filter criteria.
    //    In the example, -a find is specified in the search request, and thus the directory server automatically dereferences while looking up the base (the first step),
    //    but does not dereference alias entries that are under the base.
    //    Therefore, the search dereferences ou=Area1,o=test, which is an alias entry, and then looks up entries under o=MyCompany,o=test.
    //    One of the entries is cn=President,o=MyCompany,o=test that is not dereferenced and is returned as is.
    //
    //    Thus, the search returns:
    //
    //    o=MyCompany,o=test
    //            cn=John doe,o=MyCompany,o=test
    //            cn=President,o=MyCompany,o=test
    @Test
    public void test_sub_find() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.WHOLE_SUBTREE,DereferenceAliasesPolicy.FINDING_BASE);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
 
    //    Subtree Search with the Dereferencing Flag -a search
    //
    //    This example shows a subtree search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a search.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a search -s sub "objectclass=*"
    //    The directory searches for the base that is specified in the search request.
    //    If the base is an alias entry, then it returns the base entry without dereferencing it. (Alias entries cannot have children.)
    //    Otherwise it returns all entries under the base. If any alias entries are found, it dereferences them and returns all entries under them as well.
    //
    //    In this example, the base entry is an alias entry, ou=Area1,o=test, so the directory returns ou=Area1,o=test.
    //    In this example, the base entry is ou=Area1,o=test, which is dereferenced to o=MyCompany,o=test, which is returned.
    //    There are two entries under o=MyCompany,o=test.
    //    One is cn=President,o=MyCompany,o=test, which is returned and also dereferenced to cn=John Doe,o=MyCompany,o=test, which is returned.
    //    The other entry under o=MyCompany,o=test, which has already been returned.
    //    So the result is o=MyCompany,o=test and cn=John Doe,o=MyCompany,o=test.
    @Test
    public void test_sub_search() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.WHOLE_SUBTREE,DereferenceAliasesPolicy.IN_SEARCHING);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
 
    //    Subtree Search with the Dereferencing Flag -a always
    //
    //    This example shows a subtree search of "ou=Area1,o=test" with a filter of "objectclass=*" with the dereferencing flag set to -a always.
    //
    //    ldapsearch -p port -h host -b "ou=Area1,o=test" -a always -s sub "objectclass=*"
    //    The directory server dereferences the base entry and returns it. It also returns all entries under the dereferenced base. If any alias entries are found, it dereferences them and returns all entries under them as well.
    //
    //    In this example, the base entry is ou=Area1,o=test, which is dereferenced to o=MyCompany,o=test, which is returned.
    //    There are two entries under o=MyCompany,o=test.
    //    One is cn=President,o=MyCompany,o=test, which is returned and also dereferenced to cn=John Doe,o=MyCompany,o=test, which is returned.
    //    The other entry under o=MyCompany,o=test, which has already been returned.
    //    So the result is o=MyCompany,o=test and cn=John Doe,o=MyCompany,o=test.
    @Test
    public void test_sub_always() throws SearchResultReferenceIOException, LdapException {
        HashMap<String,SearchResultEntry> res=search(SearchScope.WHOLE_SUBTREE,DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("ou=Area1,o=test")).isFalse();
        assertThat(res.containsKey("o=MyCompany,o=test")).isTrue();
        assertThat(res.containsKey("cn=President,o=MyCompany,o=test")).isFalse();
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
 
    // Dereferencing recursion avoidance test.
    @Test
    public void test_alias_recursive() throws LdapException, SearchResultReferenceIOException {
        HashMap<String, SearchResultEntry> res = search("uid=jdoe,ou=employees,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("uid=jdoe,ou=employees,o=test")).isTrue();
        assertThat(res.containsKey("uid=jdoe,ou=researchers,o=test")).isFalse();
    }
 
    @Test
    public void test_alias_recursive_loop() throws LdapException, SearchResultReferenceIOException {
        HashMap<String, SearchResultEntry> res = search("uid=janedoe,ou=students,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("uid=janedoe,ou=students,o=test")).isTrue();
        assertThat(res.containsKey("uid=janedoe,ou=researches,o=test")).isFalse();
        assertThat(res.containsKey("uid=janedoe,ou=employees,o=test")).isFalse();
    }
 
    //    The alias target does not have to live in the backend holding the alias:
    //    the search must be handed over to the backend holding the target.
    @Test
    public void test_cross_backend_alias_base_always() throws LdapException, SearchResultReferenceIOException {
        HashMap<String, SearchResultEntry> res = search("ou=external,o=test", SearchScope.BASE_OBJECT, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("ou=external,o=test")).isFalse();
        assertThat(res.containsKey("ou=people,o=test2")).isTrue();
        assertThat(res.containsKey("cn=Jane Roe,ou=people,o=test2")).isFalse();
    }
 
    @Test
    public void test_cross_backend_alias_sub_always() throws LdapException, SearchResultReferenceIOException {
        HashMap<String, SearchResultEntry> res = search("ou=external,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("ou=external,o=test")).isFalse();
        assertThat(res.containsKey("ou=people,o=test2")).isTrue();
        assertThat(res.containsKey("cn=Jane Roe,ou=people,o=test2")).isTrue();
    }
 
    //    No backend at all holds the alias target: this is a base DN that does not exist.
    @Test
    public void test_alias_target_without_backend() throws SearchResultReferenceIOException {
        try {
            search("ou=nowhere,o=test", SearchScope.BASE_OBJECT, DereferenceAliasesPolicy.ALWAYS);
            fail("dereferencing an alias whose target has no backend must fail");
        } catch (LdapException e) {
            assertThat(e.getResult().getResultCode()).isEqualTo(ResultCode.NO_SUCH_OBJECT);
        }
    }
 
    // An alias whose target lies outside the search scope: the target is only
    // reachable by dereferencing, so it must be returned by the search.
    @Test
    public void test_deref_target_outside_scope() throws Exception {
        TestCaseUtils.addEntries(
                "dn: ou=pointers,o=test",
                "objectClass: top",
                "objectClass: organizationalUnit",
                "ou: pointers",
                "",
                "dn: cn=ptr,ou=pointers,o=test",
                "objectClass: alias",
                "objectClass: top",
                "objectClass: extensibleObject",
                "cn: ptr",
                "aliasedObjectName: cn=John Doe,o=MyCompany,o=test",
                ""
        );
        HashMap<String, SearchResultEntry> res =
                search("ou=pointers,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("cn=John Doe,o=MyCompany,o=test")).isTrue();
    }
 
    // An alias pointing at an entry which does not exist is skipped, and the entries next to it are
    // still returned: aliases are not checked when they are written, so a dangling one is expected.
    @Test
    public void test_dangling_alias() throws Exception {
        TestCaseUtils.addEntries(
                "dn: ou=dangling,o=test",
                "objectClass: top",
                "objectClass: organizationalUnit",
                "ou: dangling",
                "",
                "dn: cn=broken,ou=dangling,o=test",
                "objectClass: alias",
                "objectClass: top",
                "objectClass: extensibleObject",
                "cn: broken",
                "aliasedObjectName: cn=does-not-exist,ou=dangling,o=test",
                "",
                "dn: cn=intact,ou=dangling,o=test",
                "cn: intact",
                "sn: intact",
                "objectClass: person",
                ""
        );
        HashMap<String, SearchResultEntry> res =
                search("ou=dangling,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("cn=does-not-exist,ou=dangling,o=test")).isFalse();
        assertThat(res.containsKey("ou=dangling,o=test")).isTrue();
        assertThat(res.containsKey("cn=intact,ou=dangling,o=test")).isTrue();
    }
 
    // A persistent search reports every change it is notified of. Dereferencing aliases must not
    // make it drop a change because the entry was notified before.
    @Test
    public void test_persistent_search_reports_repeated_changes() throws Exception {
        TestCaseUtils.addEntries(
                "dn: ou=psearch,o=test",
                "objectClass: top",
                "objectClass: organizationalUnit",
                "ou: psearch",
                "",
                "dn: cn=changing,ou=psearch,o=test",
                "cn: changing",
                "sn: changing",
                "objectClass: person",
                ""
        );
 
        final BlockingQueue<String> notified = new LinkedBlockingQueue<>();
        final SearchRequest request =
                Requests.newSearchRequest("ou=psearch,o=test", SearchScope.WHOLE_SUBTREE, "(objectclass=*)")
                        .setDereferenceAliasesPolicy(DereferenceAliasesPolicy.ALWAYS)
                        .addControl(PersistentSearchRequestControl.newControl(
                                true, true, false, PersistentSearchChangeType.MODIFY));
 
        final LDAPConnectionFactory factory =
                new LDAPConnectionFactory("localhost", TestCaseUtils.getServerLdapPort());
        try (Connection psearch = factory.getConnection()) {
            psearch.bind("cn=Directory Manager", "password".toCharArray());
            psearch.searchAsync(request, new SearchResultHandler() {
                @Override
                public boolean handleEntry(SearchResultEntry entry) {
                    notified.add(entry.getName().toString());
                    return true;
                }
 
                @Override
                public boolean handleReference(SearchResultReference reference) {
                    return true;
                }
            });
 
            // searchAsync returns before the server has registered the persistent search, so wait
            // until the backend reports it; otherwise the first modification below can be notified
            // before the search is listening and be missed.
            final LocalBackend<?> backend = TestCaseUtils.getServerContext()
                    .getBackendConfigManager().getLocalBackendById(TestCaseUtils.TEST_BACKEND_ID);
            for (int i = 0; backend.getPersistentSearches().isEmpty() && i < 500; i++) {
                Thread.sleep(10);
            }
            assertThat(backend.getPersistentSearches()).isNotEmpty();
 
            // The same entry is modified repeatedly: each change must reach the persistent search.
            for (int i = 1; i <= 3; i++) {
                connection.modify(Requests.newModifyRequest("cn=changing,ou=psearch,o=test")
                        .addModification(ModificationType.REPLACE, "description", "change " + i));
                assertThat(notified.poll(30, TimeUnit.SECONDS))
                        .as("notification for change " + i)
                        .isEqualTo("cn=changing,ou=psearch,o=test");
            }
        }
    }
 
    // An alias is dereferenced before its target is reached on its own: the target must still be
    // returned, and exactly once. The original regression was order-sensitive, dropping the target
    // when the alias reached it first, so this pins the alias-before-target order specifically. The
    // MemoryBackend serving o=test walks entries in insertion order, so listing the alias before the
    // target below guarantees the alias is visited first.
    @Test
    public void test_deref_alias_before_target_returns_target_once() throws Exception {
        TestCaseUtils.addEntries(
                "dn: ou=order,o=test",
                "objectClass: top",
                "objectClass: organizationalUnit",
                "ou: order",
                "",
                "dn: cn=aaa-alias,ou=order,o=test",
                "objectClass: alias",
                "objectClass: top",
                "objectClass: extensibleObject",
                "cn: aaa-alias",
                "aliasedObjectName: cn=zzz-target,ou=order,o=test",
                "",
                "dn: cn=zzz-target,ou=order,o=test",
                "cn: zzz-target",
                "sn: target",
                "objectClass: person",
                ""
        );
        // search() asserts every returned DN is unique, so a duplicated target would fail this call.
        HashMap<String, SearchResultEntry> res =
                search("ou=order,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        // The target is returned, reached through the alias visited before it ...
        assertThat(res.containsKey("cn=zzz-target,ou=order,o=test")).isTrue();
        // ... and the alias entry itself is replaced by its target, never returned as-is.
        assertThat(res.containsKey("cn=aaa-alias,ou=order,o=test")).isFalse();
    }
 
    // An alias whose aliasedObjectName is not a valid DN cannot be dereferenced. Reading it throws a
    // DirectoryException, which must be logged and the alias skipped, not turned into a failed search.
    @Test
    public void test_alias_with_malformed_target_is_skipped() throws Exception {
        // A malformed DN can only be stored while syntax enforcement is relaxed; the default REJECT
        // policy refuses the add outright.
        final AcceptRejectWarn previous = DirectoryServer.getCoreConfigManager().getSyntaxEnforcementPolicy();
        setSyntaxEnforcementPolicy(GlobalCfgDefn.InvalidAttributeSyntaxBehavior.WARN);
        try {
            TestCaseUtils.addEntries(
                    "dn: ou=malformed,o=test",
                    "objectClass: top",
                    "objectClass: organizationalUnit",
                    "ou: malformed",
                    "",
                    "dn: cn=badptr,ou=malformed,o=test",
                    "objectClass: alias",
                    "objectClass: top",
                    "objectClass: extensibleObject",
                    "cn: badptr",
                    "aliasedObjectName: not a valid dn",
                    "",
                    "dn: cn=neighbour,ou=malformed,o=test",
                    "cn: neighbour",
                    "sn: neighbour",
                    "objectClass: person",
                    ""
            );
        } finally {
            setSyntaxEnforcementPolicy(asSyntaxBehavior(previous));
        }
 
        // The whole search still succeeds: the broken alias is skipped, its neighbours returned.
        HashMap<String, SearchResultEntry> res =
                search("ou=malformed,o=test", SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
 
        assertThat(res.containsKey("cn=badptr,ou=malformed,o=test")).isFalse();
        assertThat(res.containsKey("ou=malformed,o=test")).isTrue();
        assertThat(res.containsKey("cn=neighbour,ou=malformed,o=test")).isTrue();
    }
 
    private static void setSyntaxEnforcementPolicy(GlobalCfgDefn.InvalidAttributeSyntaxBehavior value) throws Exception {
        try (ManagementContext conf = TestCaseUtils.getServer().getConfiguration()) {
            GlobalCfgClient globalCfg = conf.getRootConfiguration().getGlobalConfiguration();
            globalCfg.setInvalidAttributeSyntaxBehavior(value);
            globalCfg.commit();
        }
    }
 
    private static GlobalCfgDefn.InvalidAttributeSyntaxBehavior asSyntaxBehavior(AcceptRejectWarn policy) {
        switch (policy) {
        case ACCEPT:
            return GlobalCfgDefn.InvalidAttributeSyntaxBehavior.ACCEPT;
        case WARN:
            return GlobalCfgDefn.InvalidAttributeSyntaxBehavior.WARN;
        case REJECT:
        default:
            return GlobalCfgDefn.InvalidAttributeSyntaxBehavior.REJECT;
        }
    }
 
    @Test(expectedExceptions = LdapException.class)
    public void test_stackoverflow() throws Exception {
 
        String entryTemplate = "dn: uid={uid},ou=employees,o=test\n" +
                "objectClass: alias\n" +
                "objectClass: top\n" +
                "objectClass: extensibleObject\n" +
                "aliasedObjectName: uid={alias},ou=employees,o=test \n" +
                "uid: {uid}\n";
        final String firstDn = "uid=jdoe0,ou=employees,o=test";
        for(int i = 0; i < 10000; i++) {
            String entryStr = entryTemplate.replace("{uid}", "jdoe" + i).replace("{alias}", "jdoe" + (i + 1));
            Entry entry = TestCaseUtils.makeEntry(entryStr);
            TestCaseUtils.addEntry(entry);
        }
        search(firstDn, SearchScope.WHOLE_SUBTREE, DereferenceAliasesPolicy.ALWAYS);
    }
}