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

Open Identity Platform Community
05.35.2025 778c73a417f73dac86bd46418c680d6b2654b648
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
/*
 * 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.
 */
package com.forgerock.opendj.ldap.tools;
 
import static com.forgerock.opendj.cli.CliMessages.INFO_GLOBAL_HELP_REFERENCE;
import static com.forgerock.opendj.ldap.tools.ToolsMessages.ERR_ERROR_PARSING_ARGS;
import static com.forgerock.opendj.ldap.tools.Utils.runTool;
import static org.assertj.core.api.Assertions.assertThat;
import static org.forgerock.opendj.ldap.ResultCode.CLIENT_SIDE_PARAM_ERROR;
import static org.forgerock.util.Utils.closeSilently;
 
import java.io.PrintStream;
 
import org.forgerock.opendj.ldap.ByteStringBuilder;
import org.forgerock.testng.ForgeRockTestCase;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * This class tests that help reference message is prompted for all tools when
 * no arguments are provided or if they failed to be parsed.
 */
@Test
public class ArgumentParserToolsTestCase extends ForgeRockTestCase {
 
    ByteStringBuilder out;
    ByteStringBuilder err;
    PrintStream outStream;
    PrintStream errStream;
 
    @BeforeMethod
    void refreshStream() {
        out = new ByteStringBuilder();
        err = new ByteStringBuilder();
        outStream = new PrintStream(out.asOutputStream());
        errStream = new PrintStream(err.asOutputStream());
    }
 
    @AfterMethod
    void closeStream() {
        closeSilently(outStream, errStream);
    }
 
    String errOnSingleLine() {
        return err.toString().replace(System.lineSeparator(), " ");
    }
 
    @DataProvider
    public Object[][] invalidArg() throws Exception {
        return new Object[][] { { new String[] { "-42" } } };
    }
 
    @DataProvider
    public Object[][] invalidArgs() throws Exception {
        return new Object[][] { { new String[] {} }, { new String[] { "-42" } } };
    }
 
    @Test(dataProvider = "invalidArg")
    public void testBase64(final String[] args) throws LDAPToolException {
        assertThat(runTool(new Base64(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArgs")
    public void testLDAPCompare(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDAPCompare(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArg")
    public void testLDAPDelete(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDAPDelete(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArg")
    public void testLDAPModify(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDAPModify(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArg")
    public void testLDAPPasswordModify(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDAPPasswordModify(outStream, errStream), args))
                  .isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArgs")
    public void testLDAPSearch(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDAPSearch(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArgs")
    public void testLDIFDiff(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDIFDiff(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArgs")
    public void testLDIFModify(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDIFModify(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArg")
    public void testLDIFSearch(final String[] args) throws LDAPToolException {
        assertThat(runTool(new LDIFSearch(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    @Test(dataProvider = "invalidArg")
    public void testMakeLdif(final String[] args) throws LDAPToolException {
        assertThat(runTool(new MakeLDIF(outStream, errStream), args)).isEqualTo(CLIENT_SIDE_PARAM_ERROR.intValue());
        assertToolFailsWithUsage();
    }
 
    private void assertToolFailsWithUsage() {
        assertThat(out.toString()).isEmpty();
        final String streamToCheck = errOnSingleLine();
        assertThat(streamToCheck).matches(".*" + INFO_GLOBAL_HELP_REFERENCE.get("(.*)") + ".*");
        assertThat(streamToCheck).contains(ERR_ERROR_PARSING_ARGS.get(""));
    }
}