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

Nicolas Capponi
12.08.2015 530e312594f469609337996570cf0ea504554a68
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
/*
 * 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 2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014-2015 ForgeRock AS
 */
package org.opends.server.admin.server;
 
import java.util.List;
 
import javax.naming.OperationNotSupportedException;
import javax.naming.ldap.LdapName;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.ldap.ResultCode;
import org.opends.server.TestCaseUtils;
import org.opends.server.admin.AdminTestCase;
import org.opends.server.admin.TestCfg;
import org.opends.server.admin.TestChildCfg;
import org.opends.server.admin.TestChildCfgDefn;
import org.opends.server.admin.TestParentCfg;
import org.opends.server.admin.client.ldap.JNDIDirContextAdaptor;
import org.opends.server.admin.std.server.RootCfg;
import org.opends.server.core.AddOperation;
import org.forgerock.opendj.config.server.ConfigChangeResult;
import org.opends.server.types.Entry;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import static org.opends.server.protocols.internal.InternalClientConnection.*;
import static org.testng.Assert.*;
 
/**
 * Test cases for constraints on the server-side.
 */
public final class ConstraintTest extends AdminTestCase {
 
  /** Child DN. */
  private static final String TEST_CHILD_1_DN = "cn=test child 1,cn=test children,cn=test parent 1,cn=test parents,cn=config";
 
 
 
  /**
   * A test child add listener.
   */
  private static class AddListener implements
      ConfigurationAddListener<TestChildCfg> {
 
    /** {@inheritDoc} */
    @Override
    public ConfigChangeResult applyConfigurationAdd(TestChildCfg configuration) {
      return new ConfigChangeResult();
    }
 
 
 
    /** {@inheritDoc} */
    @Override
    public boolean isConfigurationAddAcceptable(TestChildCfg configuration,
        List<LocalizableMessage> unacceptableReasons) {
      return true;
    }
 
  }
 
 
 
  /**
   * A test child delete listener.
   */
  private static class DeleteListener implements
      ConfigurationDeleteListener<TestChildCfg> {
 
    /** {@inheritDoc} */
    @Override
    public ConfigChangeResult applyConfigurationDelete(
        TestChildCfg configuration) {
      return new ConfigChangeResult();
    }
 
 
 
    /** {@inheritDoc} */
    @Override
    public boolean isConfigurationDeleteAcceptable(TestChildCfg configuration,
        List<LocalizableMessage> unacceptableReasons) {
      return true;
    }
 
  }
 
 
 
  /**
   * A test child change listener.
   */
  private static class ChangeListener implements
      ConfigurationChangeListener<TestChildCfg> {
 
    /** {@inheritDoc} */
    @Override
    public ConfigChangeResult applyConfigurationChange(
        TestChildCfg configuration) {
      return new ConfigChangeResult();
    }
 
 
 
    /** {@inheritDoc} */
    @Override
    public boolean isConfigurationChangeAcceptable(TestChildCfg configuration,
        List<LocalizableMessage> unacceptableReasons) {
      return true;
    }
 
  }
 
  /** Test child 1 LDIF. */
  private static final String[] TEST_CHILD_1 = new String[] {
      "dn: cn=test child 1,cn=test children,cn=test parent 1,cn=test parents,cn=config",
      "objectclass: top",
      "objectclass: ds-cfg-test-child-dummy",
      "cn: test child 1",
      "ds-cfg-enabled: true",
      "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
      "ds-cfg-attribute-type: description",
      "ds-cfg-conflict-behavior: virtual-overrides-real"
  };
 
  /** Test LDIF. */
  private static final String[] TEST_LDIF = new String[] {
      // Base entries.
      "dn: cn=test parents,cn=config",
      "objectclass: top",
      "objectclass: ds-cfg-branch",
      "cn: test parents",
      "",
      // Parent 1 - uses default values for
      // optional-multi-valued-dn-property.
      "dn: cn=test parent 1,cn=test parents,cn=config",
      "objectclass: top",
      "objectclass: ds-cfg-test-parent-dummy",
      "cn: test parent 1",
      "ds-cfg-enabled: true",
      "ds-cfg-java-class: org.opends.server.extensions.UserDefinedVirtualAttributeProvider",
      "ds-cfg-attribute-type: description",
      "ds-cfg-conflict-behavior: virtual-overrides-real",
      "",
      // Child base entries.
      "dn:cn=test children,cn=test parent 1,cn=test parents,cn=config",
      "objectclass: top",
      "objectclass: ds-cfg-branch",
      "cn: test children",
      "",
  };
 
  /** JNDI LDAP context. */
  private JNDIDirContextAdaptor adaptor;
 
 
 
  /**
   * Sets up tests
   *
   * @throws Exception
   *           If the server could not be initialized.
   */
  @BeforeClass
  public void setUp() throws Exception {
    // This test suite depends on having the schema available, so
    // we'll start the server.
    TestCaseUtils.startServer();
    TestCfg.setUp();
 
    // Add test managed objects.
    TestCaseUtils.addEntries(TEST_LDIF);
  }
 
 
 
  /**
   * Tears down test environment.
   *
   * @throws Exception
   *           If the test entries could not be removed.
   */
  @AfterClass
  public void tearDown() throws Exception {
    TestCfg.cleanup();
 
    // Remove test entries.
    deleteSubtree("cn=test parents,cn=config");
  }
 
 
 
  /**
   * Tests that retrieval can succeed.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testGetManagedObjectSuccess() throws Exception {
    MockConstraint constraint = new MockConstraint(true, false);
 
    try {
      TestCaseUtils.addEntry(TEST_CHILD_1);
      TestCfg.addConstraint(constraint);
 
      TestParentCfg parent = getParent("test parent 1");
      parent.getTestChild("test child 1");
    } finally {
      TestCfg.removeConstraint(constraint);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
 
 
  /**
   * Tests that retrieval can fail.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testGetManagedObjectFail() throws Exception {
    MockConstraint constraint = new MockConstraint(false, true);
 
    try {
      TestCaseUtils.addEntry(TEST_CHILD_1);
      TestCfg.addConstraint(constraint);
 
      TestParentCfg parent = getParent("test parent 1");
      parent.getTestChild("test child 1");
    } catch (ConfigException e) {
      Throwable cause = e.getCause();
      if (cause instanceof ConstraintViolationException) {
        ConstraintViolationException cve = (ConstraintViolationException) cause;
        Assert.assertEquals(cve.getMessages().size(), 1);
        Assert.assertSame(cve.getManagedObject().getManagedObjectDefinition(),
            TestChildCfgDefn.getInstance());
      } else {
        // Wrong cause.
        throw e;
      }
    } finally {
      TestCfg.removeConstraint(constraint);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
 
 
  /**
   * Tests that an add constraint can succeed.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testAddConstraintSuccess() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
    AddListener listener = new AddListener();
    parent.addTestChildAddListener(listener);
 
    MockConstraint constraint = new MockConstraint(true, false);
    TestCfg.addConstraint(constraint);
 
    try {
      try {
        // Add the entry.
        addEntry(ResultCode.SUCCESS, TEST_CHILD_1);
      } finally {
        deleteSubtreeNoException(TEST_CHILD_1_DN);
      }
    } finally {
      TestCfg.removeConstraint(constraint);
      parent.removeTestChildAddListener(listener);
    }
  }
 
 
 
  /**
   * Tests that an add constraint can fail.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testAddConstraintFail() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
    AddListener listener = new AddListener();
    parent.addTestChildAddListener(listener);
 
    MockConstraint constraint = new MockConstraint(false, true);
    TestCfg.addConstraint(constraint);
 
    try {
      try {
        // Add the entry.
        addEntry(ResultCode.UNWILLING_TO_PERFORM, TEST_CHILD_1);
      } finally {
        deleteSubtreeNoException(TEST_CHILD_1_DN);
      }
    } finally {
      TestCfg.removeConstraint(constraint);
      parent.removeTestChildAddListener(listener);
    }
  }
 
 
 
  /**
   * Tests that a delete constraint can succeed.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testDeleteConstraintSuccess() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
    DeleteListener listener = new DeleteListener();
    parent.addTestChildDeleteListener(listener);
 
    MockConstraint constraint = new MockConstraint(false, true);
    TestCfg.addConstraint(constraint);
 
    try {
      // Add the entry.
      TestCaseUtils.addEntry(TEST_CHILD_1);
 
      // Now delete it - this should trigger the constraint.
      deleteSubtree(TEST_CHILD_1_DN);
    } finally {
      TestCfg.removeConstraint(constraint);
      parent.removeTestChildDeleteListener(listener);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
 
 
  /**
   * Tests that a delete constraint can fail.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testDeleteConstraintFail() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
    DeleteListener listener = new DeleteListener();
    parent.addTestChildDeleteListener(listener);
 
    MockConstraint constraint = new MockConstraint(true, false);
    TestCfg.addConstraint(constraint);
 
    try {
      // Add the entry.
      TestCaseUtils.addEntry(TEST_CHILD_1);
      try {
        // Now delete it - this should trigger the constraint.
        deleteSubtree(TEST_CHILD_1_DN);
 
        // Should not have succeeded.
        Assert.fail("Delete constraint failed to prevent deletion");
      } catch (OperationNotSupportedException e) {
        // Ignore - this is the expected exception.
      }
    } finally {
      TestCfg.removeConstraint(constraint);
      parent.removeTestChildDeleteListener(listener);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
 
 
  /**
   * Tests that a modify constraint can succeed.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testChangeConstraintSuccess() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
 
    MockConstraint constraint = new MockConstraint(true, false);
 
    try {
      // Add the entry.
      TestCaseUtils.addEntry(TEST_CHILD_1);
      TestChildCfg child = parent.getTestChild("test child 1");
 
      TestCfg.addConstraint(constraint);
      ChangeListener listener = new ChangeListener();
      child.addChangeListener(listener);
 
      // Now modify it.
      String[] changes = new String[] {
          "dn: cn=test child 1,cn=test children,cn=test parent 1,cn=test parents,cn=config",
          "changetype: modify",
          "replace: ds-cfg-base-dn",
          "ds-cfg-base-dn: dc=new value 1,dc=com",
          "ds-cfg-base-dn: dc=new value 2,dc=com",
          "-",
          "replace: ds-cfg-group-dn",
          "ds-cfg-group-dn: dc=new value 3,dc=com",
          "ds-cfg-group-dn: dc=new value 4,dc=com"
      };
 
      int result = TestCaseUtils.applyModifications(true, changes);
      Assert.assertEquals(result, ResultCode.SUCCESS.intValue());
    } finally {
      TestCfg.removeConstraint(constraint);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
 
 
  /**
   * Tests that a modify constraint can fail.
   *
   * @throws Exception
   *           If the test unexpectedly fails.
   */
  @Test
  public void testChangeConstraintFail() throws Exception {
    TestParentCfg parent = getParent("test parent 1");
    MockConstraint constraint = new MockConstraint(false, true);
 
    try {
      // Add the entry.
      TestCaseUtils.addEntry(TEST_CHILD_1);
      TestChildCfg child = parent.getTestChild("test child 1");
 
      TestCfg.addConstraint(constraint);
      ChangeListener listener = new ChangeListener();
      child.addChangeListener(listener);
 
      // Now modify it.
      String[] changes = new String[] {
          "dn: cn=test child 1,cn=test children,cn=test parent 1,cn=test parents,cn=config",
          "changetype: modify",
          "replace: ds-cfg-base-dn",
          "ds-cfg-base-dn: dc=new value 1,dc=com",
          "ds-cfg-base-dn: dc=new value 2,dc=com",
          "-",
          "replace: ds-cfg-group-dn",
          "ds-cfg-group-dn: dc=new value 3,dc=com",
          "ds-cfg-group-dn: dc=new value 4,dc=com"
      };
 
      int result = TestCaseUtils.applyModifications(true, changes);
      Assert.assertEquals(result, ResultCode.UNWILLING_TO_PERFORM.intValue());
    } finally {
      TestCfg.removeConstraint(constraint);
      deleteSubtreeNoException(TEST_CHILD_1_DN);
    }
  }
 
  /** Add an entry and check its result. */
  private void addEntry(ResultCode expected, String... lines) throws Exception {
    Entry entry = TestCaseUtils.makeEntry(lines);
    AddOperation add = getRootConnection().processAdd(entry);
    assertEquals(add.getResultCode(), expected, add.getErrorMessage().toString());
  }
 
  private void deleteSubtreeNoException(String dn)
  {
    try
    {
      deleteSubtree(dn);
    }
    catch (Exception e)
    {
      // Ignore.
    }
  }
 
  /** Deletes the named sub-tree. */
  private void deleteSubtree(String dn) throws Exception {
    getAdaptor().deleteSubtree(new LdapName(dn));
  }
 
  /** Gets the JNDI connection for the test server instance. */
  private synchronized JNDIDirContextAdaptor getAdaptor() throws Exception {
    if (adaptor == null) {
      adaptor = JNDIDirContextAdaptor.simpleSSLBind("127.0.0.1", TestCaseUtils
          .getServerAdminPort(), "cn=directory manager", "password");
    }
    return adaptor;
  }
 
  /** Gets the named parent configuration. */
  private TestParentCfg getParent(String name) throws Exception {
    ServerManagementContext ctx = ServerManagementContext.getInstance();
    ServerManagedObject<RootCfg> root = ctx.getRootConfigurationManagedObject();
    return root.getChild(
        TestCfg.getTestOneToManyParentRelationDefinition(), name)
        .getConfiguration();
  }
}