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

Gaetan Boismal
19.56.2014 d94f6d23898f7515e969517f85b8e626667a1e02
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
/*
 * 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-2014 ForgeRock AS
 */
package org.forgerock.opendj.adapter.server3x;
 
import java.security.GeneralSecurityException;
import java.util.NoSuchElementException;
 
import org.forgerock.opendj.ldap.AuthenticationException;
import org.forgerock.opendj.ldap.AuthorizationException;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.ConnectionFactory;
import org.forgerock.opendj.ldap.Connections;
import org.forgerock.opendj.ldap.ConstraintViolationException;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.DecodeOptions;
import org.forgerock.opendj.ldap.Entries;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.EntryNotFoundException;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.LdapException;
import org.forgerock.opendj.ldap.LinkedHashMapEntry;
import org.forgerock.opendj.ldap.ModificationType;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.SearchResultReferenceIOException;
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.opendj.ldap.controls.GenericControl;
import org.forgerock.opendj.ldap.controls.MatchedValuesRequestControl;
import org.forgerock.opendj.ldap.controls.PermissiveModifyRequestControl;
import org.forgerock.opendj.ldap.controls.PreReadRequestControl;
import org.forgerock.opendj.ldap.controls.PreReadResponseControl;
import org.forgerock.opendj.ldap.controls.SubentriesRequestControl;
import org.forgerock.opendj.ldap.requests.AddRequest;
import org.forgerock.opendj.ldap.requests.CompareRequest;
import org.forgerock.opendj.ldap.requests.DeleteRequest;
import org.forgerock.opendj.ldap.requests.ModifyDNRequest;
import org.forgerock.opendj.ldap.requests.ModifyRequest;
import org.forgerock.opendj.ldap.requests.PlainSASLBindRequest;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.requests.WhoAmIExtendedRequest;
import org.forgerock.opendj.ldap.responses.BindResult;
import org.forgerock.opendj.ldap.responses.CompareResult;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.WhoAmIExtendedResult;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.testng.ForgeRockTestCase;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import static org.fest.assertions.Assertions.*;
import static org.fest.assertions.Fail.*;
import static org.forgerock.opendj.adapter.server3x.EmbeddedServerTestCaseUtils.*;
 
/**
 * This class defines a set of tests for the Adapters.class.
 */
@SuppressWarnings("javadoc")
@Test()
public class AdaptersTestCase extends ForgeRockTestCase {
 
    /**
     * Provides an anonymous connection factories.
     *
     * @return Anonymous connection factories.
     */
    @DataProvider
    public Object[][] anonymousConnectionFactories() {
        return new Object[][] {
            { new LDAPConnectionFactory("localhost",
                    Integer.valueOf(CONFIG_PROPERTIES.getProperty("listen-port"))) },
            { Adapters.newAnonymousConnectionFactory() } };
    }
 
    /**
     * Provides root connection factories.
     *
     * @return Root connection factories.
     */
    @DataProvider
    public Object[][] rootConnectionFactories() {
        return new Object[][] {
            { Connections.newAuthenticatedConnectionFactory(
                   new LDAPConnectionFactory("localhost",
                           Integer.valueOf(CONFIG_PROPERTIES.getProperty("listen-port"))),
                   Requests.newSimpleBindRequest("cn=directory manager", "password".toCharArray())) },
            { Adapters.newConnectionFactoryForUser(DN.valueOf("cn=directory manager")) } };
    }
 
    /**
     * Launched before the tests, this function starts the embedded server and
     * adding data.
     *
     * @throws Exception
     *             If the server could not be initialized.
     */
    @BeforeClass
    public void startEmbeddedServer() throws Exception {
        EmbeddedServerTestCaseUtils.startServer();
 
        // Creates a root connection to add data
        final Connection connection = Adapters.newRootConnection();
        // @formatter:off
        connection.add(
                "dn: uid=user.0, dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "objectClass: inetOrgPerson",
                "objectClass: organizationalPerson",
                "cn: Aaccf Amar",
                "sn:user.0",
                "uid:user.0",
                "description: This is the description for Aaccf Amar.",
                "userPassword:: cGFzc3dvcmQ=",
                "postalAddress: Aaccf Amar$01251 Chestnut Street$Panama City, DE  50369",
                "postalCode: 50369");
 
        connection.add(
                "dn: uid=user.1, dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "objectClass: inetOrgPerson",
                "objectClass: organizationalPerson",
                "cn: Aaren Atp",
                "sn:user.1",
                "uid:user.1",
                "description: This is the description for Aaren Atp.",
                "postalAddress: Aaren Atp$70110 Fourth Street$New Haven, OH  93694",
                "postalCode: 93694");
 
        connection.add(
                "dn: uid=user.2, dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "objectClass: inetOrgPerson",
                "objectClass: organizationalPerson",
                "cn: Aarika Atpco",
                "sn: user.2",
                "uid:user.2",
                "description: This is the description for Aarika Atpco.",
                "userPassword:: cGFzc3dvcmQ=",
                "postalAddress: Aarika Atpco$00900 Maple Street$New Orleans, KS  10857",
                "postalCode: 10857");
 
        connection.add(
                "dn: uid=user.3, dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "objectClass: inetOrgPerson",
                "objectClass: organizationalPerson",
                "cn: Aaron Atrc",
                "sn:user.3",
                "uid:user.3",
                "description: This is the description for Aaron Atrc.",
                "postalAddress: Aaron Atrc$59748 Willow Street$Green Bay, TN  66239",
                "postalCode: 66239");
 
        connection.add(
                "dn: uid=user.4, dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "objectClass: inetOrgPerson",
                "objectClass: organizationalPerson",
                "cn: Samantha Carter",
                "sn: Carter",
                "uid:user.4",
                "description: This is the description for Samantha Carter.",
                "postalAddress: 59748 Willow Street$Green Bay, TN  66000",
                "postalCode: 66000");
        // @formatter:on
 
        connection.close();
    }
 
    /**
     * Stops the server at the end of the test class.
     */
    @AfterClass
    public void shutDownEmbeddedServerServer() {
        EmbeddedServerTestCaseUtils.shutDownServer();
    }
 
    /**
     * A simple LDAP connection.
     *
     * @throws LdapException
     */
    @Test()
    public void testSimpleLDAPConnectionFactorySimpleBind() throws LdapException {
        final LDAPConnectionFactory factory =
                new LDAPConnectionFactory("localhost",
                        Integer.valueOf(CONFIG_PROPERTIES.getProperty("listen-port")));
        Connection connection = null;
        try {
            connection = factory.getConnection();
            connection.bind("cn=Directory Manager", "password".toCharArray());
            assertThat(connection.isValid()).isTrue();
            assertThat(connection.isClosed()).isFalse();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
 
    /**
     * Tests a SASL Bind with an LDAP connection.
     *
     * @throws NumberFormatException
     * @throws GeneralSecurityException
     * @throws LdapException
     */
    @Test()
    public void testLDAPSASLBind() throws NumberFormatException, GeneralSecurityException, LdapException {
        LDAPConnectionFactory factory =
                new LDAPConnectionFactory("localhost",
                        Integer.valueOf(CONFIG_PROPERTIES.getProperty("listen-port")));
 
        Connection connection = factory.getConnection();
        PlainSASLBindRequest request =
                Requests.newPlainSASLBindRequest("u:user.0", ("password").toCharArray());
        try {
            connection.bind(request);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
 
    }
 
    /**
     * Tests an SASL connection with the adapter.
     *
     * @throws LdapException
     */
    @Test()
    public void testAdapterConnectionSASLBindRequest() throws LdapException,
            GeneralSecurityException {
        final Connection connection = Adapters.newRootConnection();
 
        PlainSASLBindRequest request =
                Requests.newPlainSASLBindRequest("u:user.0", ("password").toCharArray());
        try {
            connection.bind(request);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
 
    /**
     * This type of connection is not supported. Anonymous SASL Mechanisms is
     * disabled in the config.ldif file.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories", expectedExceptions = LdapException.class)
    public void testConnectionAnonymousSASLBindRequest(final ConnectionFactory factory) throws LdapException {
        final Connection connection = factory.getConnection();
        try {
            connection.bind(Requests.newAnonymousSASLBindRequest("anonymousSASLBindRequest"));
        } finally {
            connection.close();
        }
    }
 
    /**
     * Binds as a root.
     *
     * @throws Exception
     */
    @Test()
    public void testAdapterConnectionSimpleBindAsRoot() throws Exception {
 
        final Connection connection = Adapters.newRootConnection();
        final BindResult result = connection.bind("cn=Directory Manager", "password".toCharArray());
        assertThat(connection.isValid()).isTrue();
        assertThat(result.getResultCode()).isEqualTo(ResultCode.SUCCESS);
        connection.close();
    }
 
    /**
     * Binds as a known user.
     *
     * @throws Exception
     */
    @Test()
    public void testAdapterConnectionSimpleBindAsAUser() throws Exception {
        // user
        final Connection connection =
                Adapters.newConnectionForUser(DN.valueOf("uid=user.0,dc=example,dc=org"));
        final BindResult result =
                connection.bind("uid=user.0,dc=example,dc=org", "password".toCharArray());
        assertThat(result.getResultCode()).isEqualTo(ResultCode.SUCCESS);
        connection.close();
    }
 
    /**
     * Binds as a known user but using the wrong password.
     *
     * @throws Exception
     */
    @Test(expectedExceptions = AuthenticationException.class)
    public void testAdapterConnectionSimpleBindAsAUserWrongPassword() throws Exception {
        // user
        final Connection connection =
                Adapters.newConnectionForUser(DN.valueOf("uid=user.0,dc=example,dc=org"));
        try {
            // Invalid credentials
            connection.bind("uid=user.0,dc=example,dc=org", "pass".toCharArray());
        } finally {
            connection.close();
        }
    }
 
    /**
     * Tries to bind as anonymous.
     *
     * @throws Exception
     */
    @Test()
    public void testAdapterConnectionSimpleBind() throws Exception {
        // Anonymous
        final Connection connection = Adapters.newAnonymousConnection();
        final BindResult result = connection.bind("", "".toCharArray());
        assertThat(result.getDiagnosticMessage()).isEmpty();
        connection.close();
    }
 
    /**
     * Testing the adapters with a simple add request.
     *
     * @throws Exception
     */
    @Test()
    public void testAdapterAddRequest() throws Exception {
        final Connection connection = Adapters.newRootConnection();
        // @formatter:off
        final AddRequest addRequest = Requests.newAddRequest(
                "dn: sn=carter,dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "cn: scarter");
        // @formatter:on
 
        final Result r = connection.add(addRequest);
        assertThat(r.getDiagnosticMessage()).isEmpty();
        assertThat(r.isSuccess()).isTrue();
 
        // We find the added entry :
        final SearchResultEntry srEntry =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "sn=carter,dc=example,dc=org", SearchScope.BASE_OBJECT, "(cn=scarter)"));
        assertThat(srEntry.getName().toString()).isEqualTo("sn=carter,dc=example,dc=org");
 
        connection.close();
    }
 
    /**
     * Tries an add request but the entry already exists.
     *
     * @throws Exception
     */
    @Test(dataProvider = "rootConnectionFactories",
            expectedExceptions = ConstraintViolationException.class)
    public void testAdapterAddRequestFails(final ConnectionFactory factory) throws Exception {
        final Connection connection = factory.getConnection();
 
        // @formatter:off
        final AddRequest addRequest = Requests.newAddRequest(
                "dn: sn=bjensen,dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "cn: bjensen");
        // @formatter:on
 
        // First add :
        Result r = connection.add(addRequest);
        assertThat(r.getDiagnosticMessage()).isEmpty();
        assertThat(r.isSuccess()).isTrue();
        // Second :
        try {
            r = connection.add(addRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * Uses the adapter to perform a search request.
     *
     * @throws Exception
     */
    @Test()
    public void testAdapterSearchRequest() throws Exception {
 
        final Connection connection = Adapters.newRootConnection();
 
        final SearchRequest request =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                        "(sn=user.1)");
 
        // Performs the search :
        final ConnectionEntryReader reader = connection.search(request);
 
        assertThat(reader.isEntry()).isTrue();
        final SearchResultEntry srEntry = reader.readEntry();
        assertThat(srEntry).isNotNull();
 
        Entry expectedEntry =
                new LinkedHashMapEntry("dn: uid=user.1,dc=example,dc=org", "objectClass: top",
                        "objectClass: person", "objectClass: inetOrgPerson",
                        "objectClass: organizationalPerson", "sn:user.1", "uid:user.1",
                        "cn: Aaren Atp", "description: This is the description for Aaren Atp.",
                        "postalAddress: Aaren Atp$70110 Fourth Street$New Haven, OH  93694",
                        "postalCode: 93694");
        Entry original = new LinkedHashMapEntry(srEntry);
 
        // No differences expected.
        assertThat(Entries.diffEntries(original, expectedEntry).getModifications()).isEmpty();
 
        assertThat(srEntry.getName().toString()).isEqualTo("uid=user.1,dc=example,dc=org");
        assertThat(srEntry.getAttributeCount()).isEqualTo(7);
        assertThat(srEntry.getAttribute("description").firstValueAsString()).isEqualTo(
                "This is the description for Aaren Atp.");
        assertThat(srEntry.getAttribute("postalAddress").firstValueAsString()).isEqualTo(
                "Aaren Atp$70110 Fourth Street$New Haven, OH  93694");
        assertThat(srEntry.getAttribute("postalCode").firstValueAsString()).isEqualTo("93694");
        // top - person - inetOrgPerson - organizationalPerson.
        assertThat(srEntry.getAttribute("objectClass").toArray().length).isEqualTo(4);
        assertThat(reader.hasNext()).isFalse();
 
        connection.close();
    }
 
    /**
     * Tries to perform a search with the adapter. No result expected.
     *
     * @throws Exception
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterSearchRequestWithNoResults(final ConnectionFactory factory)
            throws Exception {
 
        final SearchRequest request =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                        "(uid=unknown)").addControl(
                        MatchedValuesRequestControl.newControl(true, "(uid=user.1)"));
 
        final Connection connection = factory.getConnection();
        ConnectionEntryReader reader = connection.search(request);
 
        assertThat(reader.hasNext()).isFalse();
 
        connection.close();
    }
 
    /**
     * Tries to perform a search single entry. Exception expected.
     *
     * @param factory
     *            The connection factory.
     * @throws Exception
     */
    @Test(dataProvider = "rootConnectionFactories",
            expectedExceptions = EntryNotFoundException.class)
    public void testAdapterSearchSingleEntryWithNoResults(final ConnectionFactory factory)
            throws Exception {
 
        final Connection connection = factory.getConnection();
        try {
            connection.searchSingleEntry(Requests.newSearchRequest("dc=example,dc=org",
                    SearchScope.WHOLE_SUBTREE, "(uid=unknown)"));
        } finally {
            connection.close();
        }
    }
 
    /**
     * Performs a search with a sub entries request control. Sub-entries are
     * included and the normal entries are excluded. No result expected.
     *
     * @throws LdapException
     * @throws SearchResultReferenceIOException
     */
    @Test(dataProvider = "rootConnectionFactories",
            expectedExceptions = NoSuchElementException.class)
    public void testAdapterSearchRequestSubEntriesWithNoResult(final ConnectionFactory factory)
            throws LdapException, SearchResultReferenceIOException {
        final Connection connection = factory.getConnection();
        try {
            final SearchRequest request =
                    Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                            "cn=*", "cn", "subtreeSpecification")
                    // sub-entries included, normal entries excluded.
                            .addControl(SubentriesRequestControl.newControl(true, true));
 
            final ConnectionEntryReader reader = connection.search(request);
            assertThat(reader.isEntry()).isFalse();
            assertThat(reader.isReference()).isFalse();
            reader.readEntry();
        } finally {
            connection.close();
        }
    }
 
    /**
     * Performs a search with a sub entries request control. Sub-entries are
     * excluded this time and the normal entries are included.
     *
     * @throws LdapException
     * @throws SearchResultReferenceIOException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterSearchRequestSubEntries(final ConnectionFactory factory)
            throws LdapException, SearchResultReferenceIOException {
        final Connection connection = factory.getConnection();
 
        final SearchRequest request =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE, "cn=*",
                        "cn", "subtreeSpecification")
                // sub-entries excluded, normal entries included.
                        .addControl(SubentriesRequestControl.newControl(true, false));
 
        final ConnectionEntryReader reader = connection.search(request);
        assertThat(reader.isEntry()).isTrue();
        // All the entries are present.
        int nbEntries = 0;
        while (reader.hasNext()) {
            final SearchResultEntry entry = reader.readEntry();
            assertThat(entry).isNotNull();
            nbEntries++;
        }
        // All the entries must appear (except those which were deleted by another operation/test).
        assertThat(nbEntries >= 3);
 
        connection.close();
    }
 
    /**
     * Deletes an inexistent entry.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories",
            expectedExceptions = EntryNotFoundException.class)
    public void testAdapterDeleteRequestNoSuchEntry(final ConnectionFactory factory) throws LdapException {
        final DeleteRequest deleteRequest = Requests.newDeleteRequest("cn=test");
        final Connection connection = factory.getConnection();
        try {
            connection.delete(deleteRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * Deletes an existing entry with the 'no-op' control.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterDeleteRequestNoOpControl(final ConnectionFactory factory) throws LdapException {
        final DeleteRequest deleteRequest =
                Requests.newDeleteRequest("uid=user.1, dc=example,dc=org")
                // The no-op control is specified with his OID.
                        .addControl(GenericControl.newControl("1.3.6.1.4.1.4203.1.10.2"));
 
        final Connection connection = factory.getConnection();
        final Result result = connection.delete(deleteRequest);
        assertThat(result.getResultCode()).isEqualTo(ResultCode.NO_OPERATION);
        // Verifies that the delete has no impact in this case (due to no_operation control)
        final SearchResultEntry srEntry =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.1, dc=example,dc=org", SearchScope.BASE_OBJECT, "(uid=user.1)"));
        assertThat(srEntry).isNotNull();
        assertThat(srEntry.getName().toString()).isEqualTo("uid=user.1,dc=example,dc=org");
 
        connection.close();
    }
 
    /**
     * Deletes an existing entry.
     *
     * @throws LdapException
     */
    @Test()
    public void testAdapterDeleteRequest() throws LdapException {
 
        final Connection connection = Adapters.newRootConnection();
        // Checks if the entry exists.
        SearchResultEntry sre =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.3, dc=example,dc=org", SearchScope.BASE_OBJECT, "(uid=user.3)"));
        assertThat(sre).isNotNull();
 
        final DeleteRequest deleteRequest =
                Requests.newDeleteRequest("uid=user.3, dc=example,dc=org");
 
        connection.delete(deleteRequest);
 
        // Verifies if the entry was correctly deleted.
        try {
            connection.searchSingleEntry(Requests.newSearchRequest("uid=user.3, dc=example,dc=org",
                    SearchScope.BASE_OBJECT, "(uid=user.3)"));
            fail();
        } catch (EntryNotFoundException ex) {
            // Expected - no result.
        } finally {
            connection.close();
        }
    }
 
    /**
     * Modifies an existing entry.
     *
     * @throws LdapException
     * @throws DecodeException
     */
    @Test()
    public void testAdapterModifyRequest() throws LdapException, DecodeException {
 
        final ModifyRequest changeRequest =
                Requests.newModifyRequest("uid=user.2, dc=example,dc=org").addControl(
                        PreReadRequestControl.newControl(true, "mail")).addModification(
                        ModificationType.ADD, "mail", "modified@example.com");
 
        final Connection connection = Adapters.newRootConnection();
        final Result result = connection.modify(changeRequest);
        assertThat(result.getDiagnosticMessage()).isEmpty();
        assertThat(result.getControls()).isNotEmpty();
        assertThat(result.getControls().get(0).getOID()).isEqualTo("1.3.6.1.1.13.1"); // pre read control OID.
        assertThat(result.getControls().get(0).isCritical()).isFalse(); // pre read response control.
        assertThat(result.getMatchedDN()).isEmpty();
 
        PreReadResponseControl control =
                result.getControl(PreReadResponseControl.DECODER, new DecodeOptions());
        // Pre read control sends a copy of the target entry exactly as it was
        // immediately before the processing for that operation - mail attribute doesn't exist.
        Entry unmodifiedEntry = control.getEntry();
        assertThat(unmodifiedEntry.getAttribute("mail")).isNull();
 
        //Verifies that entry has been correctly modified.
        final SearchResultEntry srEntry =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.2, dc=example,dc=org", SearchScope.BASE_OBJECT, "(uid=user.2)"));
        assertThat(srEntry.getAttribute("mail").firstValueAsString()).isEqualTo(
                "modified@example.com");
 
    }
 
    /**
     * Tries to modify the existing entry with the same values but using the
     * permissive modify control.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterUsePermissiveModifyRequest(final ConnectionFactory factory) throws LdapException {
 
        final ModifyRequest changeRequest =
                Requests.newModifyRequest("uid=user.2, dc=example,dc=org").addControl(
                        PermissiveModifyRequestControl.newControl(true)).addModification(
                        ModificationType.ADD, "uid", "user.2");
 
        final Connection connection = factory.getConnection();
        connection.modify(changeRequest);
 
        // Verifies that entry has been correctly modified.
        final SearchResultEntry srEntry =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.2, dc=example,dc=org", SearchScope.BASE_OBJECT, "(uid=user.2)"));
        assertThat(srEntry.getAttribute("uid").firstValueAsString()).isEqualTo("user.2");
    }
 
    /**
     * Tries to modify the existing entry with the same values.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories",
            expectedExceptions = ConstraintViolationException.class)
    public void testAdapterModifyRequestFails(final ConnectionFactory factory) throws LdapException {
        final ModifyRequest changeRequest =
                Requests.newModifyRequest("uid=user.2, dc=example,dc=org").addModification(
                        ModificationType.ADD, "uid", "user.2");
        final Connection connection = factory.getConnection();
        connection.modify(changeRequest);
    }
 
    /**
     * Modifies the DN.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterModifyDNRequest(final ConnectionFactory factory) throws LdapException {
        final Connection connection = factory.getConnection();
 
        // Verifies that entry has been correctly modified.
        final SearchResultEntry srEntryExists =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.4, dc=example,dc=org", SearchScope.BASE_OBJECT, "(uid=user.4)"));
        assertThat(srEntryExists).isNotNull();
 
        // Modifying the DN.
        ModifyDNRequest changeRequest =
                Requests.newModifyDNRequest("uid=user.4,dc=example,dc=org", "uid=user.test")
                        .setDeleteOldRDN(true);
 
        connection.modifyDN(changeRequest);
 
        // Checks previous mod.
        final SearchResultEntry srEntry =
                connection.searchSingleEntry(Requests.newSearchRequest(
                        "uid=user.test, dc=example,dc=org", SearchScope.BASE_OBJECT,
                        "(uid=user.test)"));
        assertThat(srEntry).isNotNull();
 
        // Modify again the DN as previously.
        changeRequest =
                Requests.newModifyDNRequest("uid=user.test,dc=example,dc=org", "uid=user.4")
                        .setDeleteOldRDN(true);
        connection.modifyDN(changeRequest);
 
    }
 
    /**
     * Compare request. The comparison returns true.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterCompareRequestTrue(final ConnectionFactory factory) throws LdapException {
        final CompareRequest compareRequest =
                Requests.newCompareRequest("uid=user.0,dc=example,dc=org", "uid", "user.0");
 
        final Connection connection = factory.getConnection();
        final CompareResult result = connection.compare(compareRequest);
        assertThat(result.getResultCode()).isEqualTo(ResultCode.COMPARE_TRUE);
        assertThat(result.getDiagnosticMessage()).isEmpty();
        assertThat(result.getControls()).isEmpty();
        assertThat(result.getMatchedDN()).isEmpty();
    }
 
    /**
     * Compare request. The comparison returns false.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterCompareRequestFalse(final ConnectionFactory factory) throws LdapException {
        final CompareRequest compareRequest =
                Requests.newCompareRequest("uid=user.0,dc=example,dc=org", "uid", "scarter");
 
        final Connection connection = factory.getConnection();
        final CompareResult result = connection.compare(compareRequest);
        assertThat(result.getResultCode()).isEqualTo(ResultCode.COMPARE_FALSE);
        assertThat(result.getDiagnosticMessage()).isEmpty();
        assertThat(result.getControls()).isEmpty();
        assertThat(result.getMatchedDN()).isEmpty();
    }
 
    /**
     * Use the Who Am I? extended request.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "rootConnectionFactories")
    public void testAdapterExtendedOperation(final ConnectionFactory factory) throws LdapException {
        final WhoAmIExtendedRequest request = Requests.newWhoAmIExtendedRequest();
        final Connection connection = factory.getConnection();
        try {
            final WhoAmIExtendedResult extResult = connection.extendedRequest(request);
            assertThat(extResult.getAuthorizationID()).isNotEmpty();
        } finally {
            connection.close();
        }
    }
 
    /**
     * If an anonymous tries to delete, sends a result code : insufficient
     * access rights.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories",
            expectedExceptions = AuthorizationException.class)
    public void testAdapterAsAnonymousCannotPerformDeleteRequest(final ConnectionFactory factory)
            throws LdapException {
 
        final DeleteRequest deleteRequest =
                Requests.newDeleteRequest("uid=user.2,dc=example,dc=org");
 
        final Connection connection = factory.getConnection();
        try {
            connection.delete(deleteRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * If an anonymous tries to do an add request, sends a result code :
     * insufficient access rights.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories",
            expectedExceptions = AuthorizationException.class)
    public void testAdapterAsAnonymousCannotPerformAddRequest(final ConnectionFactory factory)
            throws LdapException {
 
        // @formatter:off
        final AddRequest addRequest = Requests.newAddRequest(
                "dn: sn=scarter,dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "cn: scarter");
        // @formatter:on
 
        final Connection connection = factory.getConnection();
        try {
            connection.add(addRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * If an anonymous tries to do a modify DN request, sends a result code :
     * insufficient access rights.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories",
            expectedExceptions = AuthorizationException.class)
    public void testAdapterAsAnonymousCannotPerformModifyDNRequest(final ConnectionFactory factory)
            throws LdapException {
 
        final Connection connection = factory.getConnection();
 
        final ModifyDNRequest changeRequest =
                Requests.newModifyDNRequest("uid=user.2,dc=example,dc=org", "uid=user.test")
                        .setDeleteOldRDN(true);
 
        try {
            connection.modifyDN(changeRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * If an anonymous tries to do a modify request, sends a result code :
     * insufficient access rights.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories",
            expectedExceptions = LdapException.class)
    public void testAdapterAsAnonymousCannotPerformModifyRequest(final ConnectionFactory factory)
            throws LdapException {
 
        final ModifyRequest changeRequest =
                Requests.newModifyRequest("uid=user.2,dc=example,dc=org").addControl(
                        PreReadRequestControl.newControl(true, "mail")).addModification(
                        ModificationType.REPLACE, "mail", "modified@example.com");
 
        final Connection connection = factory.getConnection();
        try {
            connection.modify(changeRequest);
        } finally {
            connection.close();
        }
    }
 
    /**
     * The anonymous connection is allowed to perform compare request.
     *
     * @throws LdapException
     */
    @Test(dataProvider = "anonymousConnectionFactories")
    public void testAdapterAsAnonymousPerformsCompareRequest(final ConnectionFactory factory)
            throws LdapException {
 
        final CompareRequest compareRequest =
                Requests.newCompareRequest("uid=user.0,dc=example,dc=org", "uid", "user.0");
 
        final Connection connection = factory.getConnection();
        final CompareResult result = connection.compare(compareRequest);
        assertThat(result.getResultCode()).isEqualTo(ResultCode.COMPARE_TRUE);
 
        assertThat(result.getDiagnosticMessage()).isEmpty();
        assertThat(result.getControls()).isEmpty();
        assertThat(result.getMatchedDN()).isEmpty();
        connection.close();
    }
 
    /**
     * The anonymous connection is allowed to perform search request.
     *
     * @throws Exception
     */
    @Test(dataProvider = "anonymousConnectionFactories")
    public void testAdapterAsAnonymousPerformsSearchRequest(final ConnectionFactory factory)
            throws Exception {
 
        final SearchRequest request =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                        "(uid=user.1)");
 
        final Connection connection = factory.getConnection();
        final ConnectionEntryReader reader = connection.search(request);
 
        assertThat(reader.isEntry()).isTrue();
        final SearchResultEntry entry = reader.readEntry();
        assertThat(entry).isNotNull();
        assertThat(entry.getName().toString()).isEqualTo("uid=user.1,dc=example,dc=org");
        assertThat(reader.hasNext()).isFalse();
    }
 
    /**
     * The anonymous connection is not allowed to perform search request
     * associated with a control.
     * <p>
     * Unavailable Critical Extension: The request control with Object
     * Identifier (OID) "x.x.x" cannot be used due to insufficient access
     * rights.
     *
     * @throws Exception
     */
    @Test(dataProvider = "anonymousConnectionFactories", expectedExceptions = LdapException.class)
    public void testAdapterAsAnonymousCannotPerformSearchRequestWithControl(
            final ConnectionFactory factory) throws Exception {
        final Connection connection = factory.getConnection();
        final SearchRequest request =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                        "(uid=user.1)").addControl(
                        MatchedValuesRequestControl.newControl(true, "(uid=user.1)"));
 
        final ConnectionEntryReader reader = connection.search(request);
        reader.readEntry();
    }
 
    /**
     * Creates an LDAP Connection and performs some basic calls like
     * add/delete/search and compare results with an SDK adapter connection
     * doing the same.
     *
     * @throws LdapException
     * @throws SearchResultReferenceIOException
     */
    @Test()
    public void testLDAPConnectionAndAdapterComparison() throws LdapException, SearchResultReferenceIOException {
        // @formatter:off
        final AddRequest addRequest = Requests.newAddRequest(
                "dn: sn=babs,dc=example,dc=org",
                "objectClass: top",
                "objectClass: person",
                "cn: bjensen");
        // @formatter:on
 
        final SearchRequest searchRequest =
                Requests.newSearchRequest("dc=example,dc=org", SearchScope.WHOLE_SUBTREE,
                        "(uid=user.*)").addControl(
                        MatchedValuesRequestControl.newControl(true, "(uid=user.1)"));
 
        final DeleteRequest deleteRequest = Requests.newDeleteRequest("sn=babs,dc=example,dc=org");
 
        // LDAP Connection
        final LDAPConnectionFactory factory =
                new LDAPConnectionFactory("localhost",
                        Integer.valueOf(CONFIG_PROPERTIES.getProperty("listen-port")));
        Connection connection = null;
        connection = factory.getConnection();
        connection.bind("cn=Directory Manager", "password".toCharArray());
        assertThat(connection.isValid()).isTrue();
 
        final Result addResult = connection.add(addRequest);
        assertThat(addResult.getResultCode()).isEqualTo(ResultCode.SUCCESS);
 
        final ConnectionEntryReader reader = connection.search(searchRequest);
 
        final Result deleteResult = connection.delete(deleteRequest);
        assertThat(deleteResult.getResultCode()).isEqualTo(ResultCode.SUCCESS);
 
        // SDK Adapter connection
        final Connection adapterConnection = Adapters.newRootConnection();
        final Result sdkAddResult = adapterConnection.add(addRequest);
        final ConnectionEntryReader sdkReader = adapterConnection.search(searchRequest);
        final Result sdkDeleteResult = adapterConnection.delete(deleteRequest);
 
        // Compare the results :
        // AddRequest
        assertThat(addResult.getMatchedDN()).isEqualTo(sdkAddResult.getMatchedDN());
        assertThat(addResult.getControls()).isEqualTo(sdkAddResult.getControls());
        assertThat(addResult.getDiagnosticMessage()).isEqualTo(sdkAddResult.getDiagnosticMessage());
        assertThat(addResult.getReferralURIs()).isEqualTo(sdkAddResult.getReferralURIs());
 
        // DeleteRequest
        assertThat(deleteResult.getMatchedDN()).isEqualTo(sdkDeleteResult.getMatchedDN());
        assertThat(deleteResult.getControls()).isEqualTo(sdkDeleteResult.getControls());
        assertThat(deleteResult.getDiagnosticMessage()).isEqualTo(
                sdkDeleteResult.getDiagnosticMessage());
        assertThat(deleteResult.getReferralURIs()).isEqualTo(sdkDeleteResult.getReferralURIs());
 
        // ConnectionEntryReader
        SearchResultEntry entry = null;
        SearchResultEntry sdkEntry = null;
        while (reader.hasNext()) {
            entry = reader.readEntry();
            sdkEntry = sdkReader.readEntry();
            assertThat(entry.getName().toString()).isEqualTo(sdkEntry.getName().toString());
            assertThat(entry.getAttributeCount()).isEqualTo(sdkEntry.getAttributeCount());
            assertThat(entry.getAllAttributes().iterator().next().getAttributeDescription())
                    .isEqualTo(sdkEntry.getAllAttributes().iterator().next().getAttributeDescription());
            assertThat(entry.getControls().size()).isEqualTo(sdkEntry.getControls().size());
        }
    }
}