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

Nicolas Capponi
05.02.2014 fc92ff3fc5f2af6da3a20f8e371cf497cd0ee19b
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
/*
 * 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 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2012 ForgeRock AS
 */
package org.opends.server.util;
 
import static org.opends.server.util.StaticUtils.*;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.List;
 
import org.opends.messages.Message;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.Attribute;
import org.opends.server.types.Attributes;
import org.opends.server.types.DN;
import org.opends.server.types.Entry;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.Modification;
import org.opends.server.types.RawModification;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * This class defines a set of tests for the
 * {@link org.opends.server.util.LDIFWriter} class.
 */
public final class TestLDIFWriter extends UtilTestCase {
 
  // Data used in writeModifyEntry tests.
  private Object[][] MODIFY_ENTRY_DATA_LDIF;
 
  // Data used in writeModifyDNEntry tests.
  private Object[][] MODIFY_DN_ENTRY_DATA_LDIF;
 
  /**
   * Tests will be performed against a byte array output stream.
   */
  private static final class Writer {
    // The underlying output stream.
    private final ByteArrayOutputStream stream;
 
    // The underlying LDIF config.
    private final LDIFExportConfig config;
 
    // The LDIF writer.
    private final LDIFWriter writer;
 
    /**
     * Create a new string writer.
     */
    public Writer() {
      this.stream = new ByteArrayOutputStream();
      this.config = new LDIFExportConfig(stream);
      try {
        this.writer = new LDIFWriter(config);
      } catch (IOException e) {
        // Should not happen.
        throw new RuntimeException(e);
      }
    }
 
    /**
     * Get the LDIF writer.
     *
     * @return Returns the LDIF writer.
     */
    public LDIFWriter getLDIFWriter() {
      return writer;
    }
 
    /**
     * Close the writer and get a string reader for the LDIF content.
     *
     * @return Returns the string contents of the writer.
     * @throws Exception
     *           If an error occurred closing the writer.
     */
    public BufferedReader getLDIFBufferedReader() throws Exception {
      writer.close();
      String ldif = stream.toString("UTF-8");
      StringReader reader = new StringReader(ldif);
      return new BufferedReader(reader);
    }
 
    /**
     * Close the writer and get an LDIF reader for the LDIF content.
     *
     * @return Returns an LDIF Reader.
     * @throws Exception
     *           If an error occurred closing the writer.
     */
    public LDIFReader getLDIFReader() throws Exception {
      writer.close();
 
      ByteArrayInputStream istream = new ByteArrayInputStream(stream.toByteArray());
      LDIFImportConfig config = new LDIFImportConfig(istream);
      return new LDIFReader(config);
    }
  }
 
  /**
   * Once-only initialization.
   *
   * @throws Exception
   *           If an unexpected error occurred.
   */
  @BeforeClass
  public void setUp() throws Exception {
    // This test suite depends on having the schema available, so we'll
    // start the server.
    TestCaseUtils.startServer();
 
    String[] modifyEntryDataLDIF = {
        "dn: cn=Paula Jensen,ou=Product Development,dc=airius,dc=com\n" +
        "changetype: modify\n" +
        "add: postaladdress\n" +
        "postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086\n" +
        "-\n" +
        "delete: description\n" +
        "-\n" +
        "replace: telephonenumber\n" +
        "telephonenumber: +1 408 555 1234\n" +
        "telephonenumber: +1 408 555 5678\n" +
        "-\n" +
        "delete: facsimiletelephonenumber\n" +
        "facsimiletelephonenumber: +1 408 555 9876\n" +
        "\n",
        "dn: cn=Ingrid Jensen,ou=Product Support,dc=airius,dc=com\n" +
        "changetype: modify\n" +
        "replace: postaladdress\n" +
        "-\n" +
        "delete: description\n" +
        "\n",
        "dn: \n" +
        "changetype: modify\n" +
        "delete: description\n" +
        "\n",
        "dn:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz\n" +
        "changetype: modify\n" +
        "add: description\n" +
        "description:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz" +
        "\n"
    };
    List<Object[]> changes = createChangeRecords(
        ModifyChangeRecordEntry.class, modifyEntryDataLDIF);
    MODIFY_ENTRY_DATA_LDIF = changes.toArray(new Object[0][]);
 
    String[] modifyDNEntryDataLDIF = {
        "dn: cn=Paula Jensen,ou=Product Development,dc=airius,dc=com\n" +
        "changetype: modrdn\n" +
        "newrdn: cn=Paul Jensen\n" +
        "deleteoldrdn: 1\n",
        "dn: cn=Ingrid Jensen,ou=Product Support,dc=airius,dc=com\n" +
        "changetype: moddn\n" +
        "newrdn: cn=Ingrid Jensen\n" +
        "deleteoldrdn: 0\n" +
        "newsuperior: ou=Product Development,dc=airius,dc=com\n"
    };
    changes = createChangeRecords(ModifyDNChangeRecordEntry.class,
        modifyDNEntryDataLDIF);
    MODIFY_DN_ENTRY_DATA_LDIF = changes.toArray(new Object[0][]);
  }
 
  /**
   * Check that creating a writer and closing it immediately does not
   * write anything.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test
  public void TestEmptyWriter() throws Exception {
    Writer writer = new Writer();
 
    Assert.assertNull(writer.getLDIFBufferedReader().readLine());
  }
 
  /**
   * LDIF writer - example comment strings.
   *
   * @return Returns an array of comment strings and their expected LDIF
   *         form.
   */
  @DataProvider(name = "writeCommentDataProvider")
  public Object[][] createTestWriteCommentData() {
    return new Object[][] {
        { "", 40, new String[] { "# " } },
        {
            "one two three four five six seven "
                + "eight nine ten eleven twelve thirteen "
                + "fourteen fifteen sixteen seventeen "
                + "eighteen nineteen",
            40,
            new String[] { "# one two three four five six seven",
                "# eight nine ten eleven twelve thirteen",
                "# fourteen fifteen sixteen seventeen",
                "# eighteen nineteen" } },
        {
            "one two three four five six seven "
                + "eight nine ten\neleven twelve thirteen "
                + "fourteen fifteen\r\nsixteen seventeen "
                + "eighteen nineteen",
            40,
            new String[] { "# one two three four five six seven",
                "# eight nine ten", "# eleven twelve thirteen fourteen",
                "# fifteen", "# sixteen seventeen eighteen nineteen" } },
        {
            "one two three four five six seven "
                + "eight nine ten eleven twelve thirteen "
                + "fourteen fifteen sixteen seventeen "
                + "eighteen nineteen",
            -1,
            new String[] { "# one two three four five "
                + "six seven eight nine ten eleven "
                + "twelve thirteen fourteen fifteen "
                + "sixteen seventeen eighteen nineteen" } },
        {
            "onetwothreefourfivesixseven"
                + "eightnineteneleventwelvethirteen"
                + "fourteenfifteensixteenseventeen"
                + "eighteennineteen",
            40,
            new String[] { "# onetwothreefourfivesixseveneightninete",
                           "# neleventwelvethirteenfourteenfifteensi",
                           "# xteenseventeeneighteennineteen" } }, };
  }
 
  /**
   * Test the {@link LDIFWriter#writeComment(Message, int)} method.
   *
   * @param comment
   *          The input comment string.
   * @param wrapColumn
   *          The wrap column.
   * @param expectedLDIF
   *          An array of expected lines.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "writeCommentDataProvider")
  public void TestWriteComment(String comment, int wrapColumn,
      String[] expectedLDIF) throws Exception {
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
    ldifWriter.writeComment(Message.raw(comment), wrapColumn);
 
    checkLDIFOutput(writer, expectedLDIF);
  }
 
  /**
   * LDIF writer - sample entry provider.
   *
   * @return Returns an array of LDAP entry objects.
   * @throws Exception If an error occurred whilst constructing the test entries.
   */
  @DataProvider(name = "entryDataProvider")
  public Object[][] createTestEntryData() throws Exception {
    String[][] input = {
        {
          "cn=john smith, dc=com",
          "objectclass", "top",
          "objectclass", "person",
          "cn", "john smith",
          "sn", "smith",
          "description", "description of john"
        },
        {
          "",
          "objectclass", "top",
          "objectClass", "ds-root-dse",
        },
    };
 
    List<Entry[]> entries = new LinkedList<Entry[]>();
 
    for (String[] s : input) {
      DN dn = DN.decode(s[0]);
      Entry entry = new Entry(dn, null, null, null);
 
      for (int i = 1; i < s.length; i+=2) {
        String atype = toLowerCase(s[i]);
        String avalue = toLowerCase(s[i+1]);
 
        if (atype.equals("objectclass")) {
          entry.addObjectClass(DirectoryServer.getObjectClass(avalue));
        } else {
          Attribute attr = Attributes.create(atype, avalue);
 
          // Assume that there will be no duplicates.
          entry.addAttribute(attr, null);
        }
      }
 
      entries.add(new Entry[]{ entry });
    }
 
    return entries.toArray(new Object[0][]);
  }
 
  /**
   * Test the {@link LDIFWriter#writeEntry(Entry)} method.
   *
   * @param entry
   *          The entry to ouput.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "entryDataProvider")
  public void TestWriteEntry(Entry entry)
      throws Exception {
    // FIXME: This test need more work. It should really check that the
    // LDIF output is correct, rather than re-parsing it, because the
    // parser could be tolerant to malformed LDIF output.
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
    ldifWriter.writeEntry(entry);
 
    LDIFReader reader = writer.getLDIFReader();
    Entry readEntry = reader.readEntry();
    reader.close();
 
    Assert.assertEquals(readEntry.getDN(), entry.getDN());
  }
 
  /**
   * Test the {@link LDIFWriter#writeAddChangeRecord(Entry)} method.
   *
   * @param entry
   *          The entry to ouput.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "entryDataProvider")
  public void TestWriteAddEntry(Entry entry)
      throws Exception {
    // FIXME: This test need more work. It should really check that the
    // LDIF output is correct, rather than re-parsing it, because the
    // parser could be tolerant to malformed LDIF output.
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
    ldifWriter.writeAddChangeRecord(entry);
 
    LDIFReader reader = writer.getLDIFReader();
    ChangeRecordEntry add = reader.readChangeRecord(false);
    reader.close();
 
    Assert.assertTrue(add instanceof AddChangeRecordEntry);
    Assert.assertEquals(add.getDN(), entry.getDN());
  }
 
  /**
   * LDIF writer - sample modification provider.
   *
   * @return Returns an array of LDAP modification objects.
   * @throws Exception If an error occurred whilst constructing the test entries.
   */
  @DataProvider(name = "writeModifyDataProvider")
  public Object[][] createTestWriteModifyData() throws Exception {
    return MODIFY_ENTRY_DATA_LDIF;
  }
 
  /**
   * Test the {@link LDIFWriter#writeModifyChangeRecord(DN, List)}
   * method.
   *
   * @param change
   *          The modification change record.
   * @param expectedLDIF
   *          An array of expected lines.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "writeModifyDataProvider")
  public void TestWriteModifyChangeRecord(ModifyChangeRecordEntry change,
      String[] expectedLDIF) throws Exception {
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
 
    List<Modification> mods = new LinkedList<Modification>();
    for (RawModification lmod : change.getModifications()) {
      mods.add(lmod.toModification());
    }
    ldifWriter.writeModifyChangeRecord(change.getDN(), mods);
 
    checkLDIFOutput(writer, expectedLDIF);
  }
 
  /**
   * Test the {@link LDIFWriter#writeDeleteChangeRecord(Entry, boolean)} method.
   *
   * @param entry
   *          The entry to ouput.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "entryDataProvider")
  public void TestWriteDeleteEntry(Entry entry)
      throws Exception {
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
    ldifWriter.writeDeleteChangeRecord(entry, false);
 
    String[] expectedLDIF = new String[] {
      "dn: " + entry.getDN(),
      "changetype: delete"
    };
 
    checkLDIFOutput(writer, expectedLDIF);
  }
 
  /**
   * LDIF writer - sample modification DN provider.
   *
   * @return Returns an array of LDAP modification DN objects.
   * @throws Exception If an error occurred whilst constructing the test entries.
   */
  @DataProvider(name = "writeModifyDNDataProvider")
  public Object[][] createTestWriteModifyDNData() throws Exception {
    return MODIFY_DN_ENTRY_DATA_LDIF;
  }
 
  /**
   * Test the {@link LDIFWriter#writeModifyChangeRecord(DN, List)}
   * method.
   *
   * @param change
   *          The modification change record.
   * @param expectedLDIF
   *          An array of expected lines.
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dataProvider = "writeModifyDNDataProvider")
  public void TestWriteModifyDNChangeRecord(
      ModifyDNChangeRecordEntry change, String[] expectedLDIF)
      throws Exception {
    Writer writer = new Writer();
 
    LDIFWriter ldifWriter = writer.getLDIFWriter();
    ldifWriter.writeModifyDNChangeRecord(change.getDN(),
        change.getNewRDN(), change.deleteOldRDN(), change
            .getNewSuperiorDN());
 
    checkLDIFOutput(writer, expectedLDIF);
  }
 
  /**
   * Close the LDIF writer and read its content and check it against the
   * expected output.
   *
   * @param writer
   *          The LDIF writer.
   * @param expectedLDIF
   *          The expected LDIF output.
   * @throws Exception
   *           If an unexpected exception occurred.
   */
  private void checkLDIFOutput(Writer writer, String[] expectedLDIF)
      throws Exception {
    BufferedReader reader = writer.getLDIFBufferedReader();
 
    StringBuilder expected = new StringBuilder();
    StringBuilder actual = new StringBuilder();
 
    boolean failed = false;
 
    for (String expectedLine : expectedLDIF) {
      String actualLine = reader.readLine();
 
      if (!failed && !actualLine.equals(expectedLine)) {
        failed = true;
      }
 
      expected.append("    ");
      expected.append(expectedLine);
      expected.append("\n");
 
      actual.append("    ");
      actual.append(actualLine);
      actual.append("\n");
    }
 
    String actualLine = reader.readLine();
    while (actualLine != null) {
      if (actualLine.trim().length() != 0) {
        failed = true;
      }
 
      actual.append("    ");
      actual.append(actualLine);
      actual.append("\n");
      actualLine = reader.readLine();
    }
 
    if (failed) {
      Assert.fail("expected:\n" + expected.toString() + "\nbut was:\n"
          + actual.toString());
    }
  }
 
  /**
   * Generate change records of the requested type from the input LDIF
   * strings.
   *
   * @param inputLDIF
   *          The input LDIF change records.
   * @return The data provider object array.
   * @throws Exception
   *           If an unexpected exception occurred.
   */
  private <T extends ChangeRecordEntry> List<Object[]> createChangeRecords(
      Class<T> theClass, String[] inputLDIF) throws Exception {
    List<Object[]> changes = new LinkedList<Object[]>();
    for (String ldifString : inputLDIF) {
      byte[] bytes = StaticUtils.getBytes(ldifString);
 
      LDIFReader reader = new LDIFReader(new LDIFImportConfig(
          new ByteArrayInputStream(bytes)));
      ChangeRecordEntry change = reader.readChangeRecord(false);
 
      Assert.assertNotNull(change);
      Assert.assertTrue(theClass.isInstance(change));
 
      String[] lines = ldifString.split("\\n");
      Object[] objs = new Object[] { change, lines };
      changes.add(objs);
    }
 
    return changes;
  }
}