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

Gaetan Boismal
09.21.2016 3848fc4749fc7528597fb5a0cbebeb90d3bea97d
OPENDJ-2505 Makeldif supports line folding

* makeldif now supports line folding as explained in LDIF RFC 2849 (https://www.ietf.org/rfc/rfc2849.txt)
* Add unit test to ensure non regression
3 files modified
3 files added
133 ■■■■■ changed files
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java 18 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties 5 ●●●● patch | view | raw | blame | history
opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java 42 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif 11 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif 29 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif 28 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
@@ -12,7 +12,7 @@
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2009 Sun Microsystems, Inc.
 * Portions Copyright 2013-2015 ForgeRock AS.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.forgerock.opendj.ldif;
@@ -1240,12 +1240,18 @@
     */
    private List<String> readLines(final BufferedReader reader) throws IOException {
        final List<String> lines = new ArrayList<>();
        while (true) {
            final String line = reader.readLine();
            if (line == null) {
                break;
        String line;
        for (int lineNumber = 1; (line = reader.readLine()) != null; lineNumber++) {
            if (line.startsWith(" ")) {
                final int lastLineIndex = lines.size() - 1;
                final String previousLine = lines.get(lastLineIndex);
                if (lines.isEmpty() || previousLine.isEmpty()) {
                    throw DecodeException.fatalError(ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE.get(lineNumber, line));
                }
                lines.set(lastLineIndex, previousLine + line.substring(1));
            } else {
                lines.add(line);
            }
            lines.add(line);
        }
        return lines;
    }
opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
@@ -1619,7 +1619,10 @@
ERR_TRANSACTION_ID_CONTROL_DECODE_NULL=Cannot decode the provided ASN.1 \
 element as a transaction id control because it did not have a value, when \
 a value must always be provided
ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE=Unable to parse line %d ("%s") from the \
 template file because the line started with a space but there were no previous \
 lines in the entry to which this line could be appended
# Labels for generated documentation
DOC_LOCALE_TAG=Code tag: %s
DOC_LOCALE_OID=Collation order object identifier: %s
opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java
@@ -11,17 +11,22 @@
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2013-2015 ForgeRock AS.
 * Copyright 2013-2016 ForgeRock AS.
 */
package com.forgerock.opendj.ldap.tools;
import static org.fest.assertions.Assertions.*;
import static org.forgerock.util.Utils.*;
import static com.forgerock.opendj.ldap.CoreMessages.*;
import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
import static com.forgerock.opendj.cli.CliMessages.INFO_GLOBAL_HELP_REFERENCE;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.ByteStringBuilder;
@@ -33,6 +38,13 @@
@SuppressWarnings("javadoc")
public class MakeLDIFITCase extends ToolsITCase {
    private static final String TEMP_OUTPUT_FILE = ".temp_test_file.ldif";
    private static final String TEST_RESOURCE_PATH = "src/test/resources";
    private static final String VALID_TEMPLATE_FILE_PATH =
            Paths.get(TEST_RESOURCE_PATH, "valid_test_template.ldif").toString();
    private static final boolean SUCCESS = true;
    private static final boolean FAILURE = false;
    private ByteStringBuilder out;
    private ByteStringBuilder err;
    private PrintStream outStream;
@@ -93,13 +105,37 @@
    @Test(dataProvider = "validArguments")
    public void testMakeLDIFValidUseCases(final String[] arguments, final LocalizableMessage expectedOut)
            throws Exception {
        run(arguments, true, expectedOut);
        run(arguments, SUCCESS, expectedOut);
    }
    @Test(dataProvider = "invalidArguments")
    public void testMakeLDIFInvalidUseCases(final String[] arguments, final LocalizableMessage expectedErr)
            throws Exception {
        run(arguments, false, expectedErr);
        run(arguments, FAILURE, expectedErr);
    }
    /** See OPENDJ-2505 */
    @Test
    public void testMakeLDIFInvalidLineFolding() throws Exception {
        final LocalizableMessage expectedOutput = ERR_LDIF_GEN_TOOL_EXCEPTION_DURING_PARSE.get(
                ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE.get(
                        27, " \"lineFoldingTest\":\\[\"This line should not be accepted by the parser\"\\],"));
        run(args("src/test/resources/invalid_test_template.ldif"), FAILURE, expectedOutput);
    }
    /** See OPENDJ-2505 */
    @Test
    public void testMakeLDIFSupportsLineFolding() throws Exception {
        final Path tempOutputFile = Paths.get(TEST_RESOURCE_PATH, TEMP_OUTPUT_FILE);
        run(args("-o", tempOutputFile.toString(), VALID_TEMPLATE_FILE_PATH),
                SUCCESS, INFO_MAKELDIF_PROCESSING_COMPLETE.get(2));
        assertFilesAreEquals(TEMP_OUTPUT_FILE, "expected_output.ldif");
        Files.delete(tempOutputFile);
    }
    /** See OPENDJ-2505 and OPENDJ-2754 */
        assertThat(Files.readAllBytes(Paths.get(TEST_RESOURCE_PATH, outputFile))).isEqualTo(
                   Files.readAllBytes(Paths.get(TEST_RESOURCE_PATH, expectedOutputFileName)));
    }
    private void run(final String[] arguments, final boolean expectsSuccess, final LocalizableMessage expectedOutput)
opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif
New file
@@ -0,0 +1,11 @@
dn: dc=example,dc=com
dc: example
dn: coretokenid=tokenId,dc=example,dc=com
coretokenid: tokenId
objectClass: top
objectClass: frCoreToken
coretokenstring08: /myrealm
coretokenstring07: Bearer
coretokenobject: {"redirectURI":["http://fake.com"],"acr":[],"clientID":["clientOIDC"],"lineFoldingTest":["This line should have been correctly folded"],"tokenName":["refresh_token"],"authModules":["LDAP"],"realm":["/myrealm"],"id":["fakeid"],"userName":["johndoe"],"tokenType":["Bearer"]}
opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif
New file
@@ -0,0 +1,29 @@
# 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 2016 ForgeRock AS.
branch: dc=example,dc=com
subordinateTemplate: refreshToken:10
template: refreshToken
rdnAttr: coreTokenId
coreTokenId: <random:hex:8>-<random:hex:4>-<random:hex:4>-<random:hex:4>-<random:hex:12>
objectClass: top
objectClass: frCoreToken
coreTokenString08: /myrealm
coreTokenString07: Bearer
coreTokenObject: \{"redirectURI":\["http://fake.com"\],"acr":\[\],"clientID":\["clientOIDC"\],
 "lineFoldingTest":\["This line should not be accepted by the parser"\],
 "tokenName":\["refresh_token"\],"authModules":\["LDAP"\],"realm":\["{coreTokenString08}"\],
 "id":\["fakeid"\],"userName":\["johndoe"\],"tokenType":\["Bearer"\]\}
opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif
New file
@@ -0,0 +1,28 @@
# 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 2016 ForgeRock AS.
branch: dc=example,dc=com
subordinateTemplate: refreshToken:1
template: refreshToken
rdnAttr: coreTokenId
coreTokenId: tokenId
objectClass: top
objectClass: frCoreToken
coreTokenString08: /myrealm
coreTokenString07: Bearer
coreTokenObject: \{"redirectURI":\["http://fake.com"\],"acr":\[\],"clientID":\["clientOIDC"\],
 "lineFoldingTest":\["This line should have been correctly folded"\],
 "tokenName":\["refresh_token"\],"authModules":\["LDAP"\],"realm":\["{coreTokenString08}"\],
 "id":\["fakeid"\],"userName":\["johndoe"\],"tokenType":\["Bearer"\]\}