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

Matthew Swift
21.30.2013 f51e4456baf7d5538f8d5e06dddba6aa25c67b33
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
/*
 * 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 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS.
 */
 
package org.forgerock.opendj.ldap;
 
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
 
import java.util.Iterator;
 
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.Schema;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * This class defines a set of tests for the
 * {@link org.forgerock.opendj.ldap.RDN} class.
 */
@SuppressWarnings("javadoc")
public final class RDNTestCase extends TypesTestCase {
 
    // Domain component attribute type.
    private static final AttributeType AT_DC;
 
    // Common name attribute type.
    private static final AttributeType AT_CN;
 
    // Test attribute value.
    private static final AVA AV_DC_ORG;
 
    static {
        AT_DC = Schema.getCoreSchema().getAttributeType("dc");
        AT_CN = Schema.getCoreSchema().getAttributeType("cn");
        // Set the avas.
        AV_DC_ORG = new AVA(AT_DC, ByteString.valueOf("org"));
    }
 
    // org bytestring.
    private static final ByteString ORG = ByteString.valueOf("org");
 
    /**
     * RDN test data provider.
     *
     * @return The array of test RDN strings.
     */
    @DataProvider(name = "testRDNs")
    public Object[][] createData() {
        return new Object[][] {
            { "dc=hello world", "dc=hello world", "dc=hello world" },
            { "dc =hello world", "dc=hello world", "dc=hello world" },
            { "dc  =hello world", "dc=hello world", "dc=hello world" },
            { "dc= hello world", "dc=hello world", "dc=hello world" },
            { "dc=  hello world", "dc=hello world", "dc=hello world" },
            { "undefined=hello", "undefined=hello", "undefined=hello" },
            { "DC=HELLO WORLD", "dc=hello world", "DC=HELLO WORLD" },
            { "dc = hello    world", "dc=hello world", "dc=hello    world" },
            { "   dc = hello world   ", "dc=hello world", "dc=hello world" },
            { "givenName=John+cn=Doe", "cn=doe+givenname=john", "givenName=John+cn=Doe" },
            { "givenName=John\\+cn=Doe", "givenname=john\\+cn\\=doe", "givenName=John\\+cn=Doe" },
            { "cn=Doe\\, John", "cn=doe\\, john", "cn=Doe\\, John" },
            { "OU=Sales+CN=J. Smith", "cn=j. smith+ou=sales", "OU=Sales+CN=J. Smith" },
            { "CN=James \\\"Jim\\\" Smith\\, III", "cn=james \\\"jim\\\" smith\\, iii",
                "CN=James \\\"Jim\\\" Smith\\, III" },
            // \0d is a hex representation of Carriage return. It is mapped
            // to a SPACE as defined in the MAP ( RFC 4518)
            { "CN=Before\\0dAfter", "cn=before after", "CN=Before\\0dAfter" },
            { "cn=#04024869",
                // Unicode codepoints from 0000-0008 are mapped to nothing.
                "cn=hi", "cn=\\04\\02Hi" },
            { "CN=Lu\\C4\\8Di\\C4\\87", "cn=lu\u010di\u0107", "CN=Lu\u010di\u0107" },
            { "ou=\\e5\\96\\b6\\e6\\a5\\ad\\e9\\83\\a8", "ou=\u55b6\u696d\u90e8",
                "ou=\u55b6\u696d\u90e8" },
            { "photo=\\ john \\ ", "photo=\\ john \\ ", "photo=\\ john \\ " },
            { "AB-global=", "ab-global=", "AB-global=" },
            { "cn=John+a=", "a=+cn=john", "cn=John+a=" },
            { "O=\"Sue, Grabbit and Runn\"", "o=sue\\, grabbit and runn",
                "O=Sue\\, Grabbit and Runn" }, };
    }
 
    /**
     * Illegal RDN test data provider.
     *
     * @return The array of illegal test RDN strings.
     */
    @DataProvider(name = "illegalRDNs")
    public Object[][] createIllegalData() {
        return new Object[][] { { null }, { "" }, { " " }, { "=" }, { "manager" }, { "manager " },
            { "cn+" }, { "cn+Jim" },
            { "cn=Jim+" },
            { "cn=Jim +" },
            { "cn=Jim+ " },
            { "cn=Jim+sn" },
            { "cn=Jim+sn " },
            { "cn=Jim+sn equals" }, // { "cn=Jim," }, { "cn=Jim;" }, {
                                    // "cn=Jim,  " },
            // { "cn=Jim+sn=a," }, { "cn=Jim, sn=Jam " }, { "cn+uid=Jim" },
            { "-cn=Jim" }, { "/tmp=a" }, { "\\tmp=a" }, { "cn;lang-en=Jim" }, { "@cn=Jim" },
            { "_name_=Jim" }, { "\u03c0=pi" }, { "v1.0=buggy" }, { "cn=Jim+sn=Bob++" },
            { "cn=Jim+sn=Bob+," }, { "1.3.6.1.4.1.1466..0=#04024869" }, };
    }
 
    /**
     * RDN equality test data provider.
     *
     * @return The array of test RDN strings.
     */
    @DataProvider(name = "createRDNEqualityData")
    public Object[][] createRDNEqualityData() {
        // @formatter:off
        return new Object[][] {
            { "cn=hello world", "cn=hello world", 0 },
            { "cn=hello world", "CN=hello world", 0 },
            { "cn=hello   world", "cn=hello world", 0 },
            { "  cn =  hello world  ", "cn=hello world", 0 },
            { "cn=hello world\\ ", "cn=hello world", 0 },
            { "cn=HELLO WORLD", "cn=hello world", 0 },
            { "cn=HELLO+sn=WORLD", "sn=world+cn=hello", 0 },
            { "cn=HELLO+sn=WORLD", "cn=hello+sn=nurse", 1 },
            { "cn=HELLO+sn=WORLD", "cn=howdy+sn=yall", -1 },
            { "cn=hello", "cn=hello+sn=world", -1 },
            { "cn=hello+sn=world", "cn=hello", 1 },
            { "cn=hello+sn=world", "cn=hello+description=world", 1 },
            { "cn=hello", "sn=world", -1 },
            { "sn=hello", "cn=world", 1 },
            // { "x-test-integer-type=10", "x-test-integer-type=9", 1 },
            // { "x-test-integer-type=999", "x-test-integer-type=1000", -1 },
            // { "x-test-integer-type=-1", "x-test-integer-type=0", -1 },
            // { "x-test-integer-type=0", "x-test-integer-type=-1", 1 },
            { "cn=aaa", "cn=aaaa", -1 },
            { "cn=AAA", "cn=aaaa", -1 },
            { "cn=aaa", "cn=AAAA", -1 },
            { "cn=aaaa", "cn=aaa", 1 },
            { "cn=AAAA", "cn=aaa", 1 },
            { "cn=aaaa", "cn=AAA", 1 },
            { "cn=aaab", "cn=aaaa", 1 },
            { "cn=aaaa", "cn=aaab", -1 },
            { RDN.maxValue(), RDN.maxValue(), 0 },
            { RDN.maxValue(), "cn=aaa", 1 },
            { "cn=aaa", RDN.maxValue(), -1 },
        };
        // @formatter:on
    }
 
    /**
     * Test RDN compareTo
     *
     * @param first
     *            First RDN to compare.
     * @param second
     *            Second RDN to compare.
     * @param result
     *            Expected comparison result.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test(dataProvider = "createRDNEqualityData")
    public void testCompareTo(final Object first, final Object second, final int result)
            throws Exception {
        final RDN rdn1 = parseRDN(first);
        final RDN rdn2 = parseRDN(second);
 
        int rc = rdn1.compareTo(rdn2);
 
        // Normalize the result.
        if (rc < 0) {
            rc = -1;
        } else if (rc > 0) {
            rc = 1;
        }
 
        assertEquals(rc, result, "Comparison for <" + first + "> and <" + second + ">.");
    }
 
    private RDN parseRDN(final Object value) {
        return (value instanceof RDN) ? ((RDN) value) : RDN.valueOf(value.toString());
    }
 
    /**
     * Test RDN construction with single AVA.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testConstructor() throws Exception {
        final RDN rdn = new RDN(AT_DC, ORG);
 
        assertEquals(rdn.size(), 1);
        assertEquals(rdn.isMultiValued(), false);
        assertEquals(rdn.getFirstAVA().getAttributeType(), AT_DC);
        assertEquals(rdn.getFirstAVA().getAttributeType().getNameOrOID(), AT_DC.getNameOrOID());
        assertEquals(rdn.getFirstAVA(), AV_DC_ORG);
    }
 
    /**
     * Test RDN construction with String attribute type and value.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testConstructorWithString() throws Exception {
        final RDN rdn = new RDN("dc", "org");
        assertEquals(rdn.size(), 1);
        assertEquals(rdn.getFirstAVA().getAttributeType(), AT_DC);
        assertEquals(rdn.getFirstAVA().getAttributeType().getNameOrOID(), "dc");
        assertEquals(rdn.getFirstAVA(), AV_DC_ORG);
    }
 
    /**
     * Test RDN string decoder against illegal strings.
     *
     * @param rawRDN
     *            Illegal RDN string representation.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test(dataProvider = "illegalRDNs", expectedExceptions = { NullPointerException.class,
            LocalizedIllegalArgumentException.class, StringIndexOutOfBoundsException.class })
    public void testDecodeIllegalString(final String rawRDN) throws Exception {
        RDN.valueOf(rawRDN);
 
        fail("Expected exception for value \"" + rawRDN + "\"");
    }
 
    /**
     * Test RDN string decoder.
     *
     * @param rawRDN
     *            Raw RDN string representation.
     * @param normRDN
     *            Normalized RDN string representation.
     * @param stringRDN
     *            String representation.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    /**
     * @Test(dataProvider = "testRDNs") public void testToString(String rawRDN,
     *                    String normRDN, String stringRDN) throws Exception {
     *                    RDN rdn = RDN.valueOf(rawRDN);
     *                    assertEquals(rdn.toString(), stringRDN); }
     **/
 
    /**
     * Test RDN string decoder.
     *
     * @param rawRDN
     *            Raw RDN string representation.
     * @param normRDN
     *            Normalized RDN string representation.
     * @param stringRDN
     *            String representation.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test(dataProvider = "testRDNs")
    public void testDecodeString(final String rawRDN, final String normRDN, final String stringRDN)
            throws Exception {
        final RDN rdn = RDN.valueOf(rawRDN);
        final RDN string = RDN.valueOf(stringRDN);
        assertEquals(rdn, string);
    }
 
    /**
     * Tests the valueof with ctor.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testDuplicateSingle() {
        final RDN rdn1 = new RDN(AT_DC, ORG);
        final RDN rdn2 = RDN.valueOf("dc=org");
 
        assertFalse(rdn1 == rdn2);
        assertEquals(rdn1, rdn2);
    }
 
    /**
     * Test RDN equality
     *
     * @param first
     *            First RDN to compare.
     * @param second
     *            Second RDN to compare.
     * @param result
     *            Expected comparison result.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test(dataProvider = "createRDNEqualityData")
    public void testEquality(final Object first, final Object second, final int result)
            throws Exception {
        final RDN rdn1 = parseRDN(first);
        final RDN rdn2 = parseRDN(second);
 
        if (result == 0) {
            assertTrue(rdn1.equals(rdn2), "RDN equality for <" + first + "> and <" + second + ">");
        } else {
            assertFalse(rdn1.equals(rdn2), "RDN equality for <" + first + "> and <" + second + ">");
        }
    }
 
    /**
     * Tests the equals method with a non-RDN argument.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testEqualityNonRDN() {
        final RDN rdn = new RDN(AT_DC, ORG);
 
        assertFalse(rdn.equals("this isn't an RDN"));
    }
 
    /**
     * Tests the equals method with a null argument.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testEqualityNull() {
        final RDN rdn = new RDN(AT_DC, ORG);
 
        assertFalse(rdn.equals(null));
    }
 
    /**
     * Test getAttributeName.
     *
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test
    public void testGetAttributeName() throws Exception {
        final RDN rdn = RDN.valueOf("dc=opendj+cn=org");
        assertTrue(rdn.isMultiValued());
        assertEquals(rdn.size(), 2);
        final Iterator<AVA> it = rdn.iterator();
        assertEquals(it.next().getAttributeType().getNameOrOID(), AT_DC.getNameOrOID());
        assertEquals(it.next().getAttributeType().getNameOrOID(), AT_CN.getNameOrOID());
    }
 
    /**
     * Test RDN hashCode
     *
     * @param first
     *            First RDN to compare.
     * @param second
     *            Second RDN to compare.
     * @param result
     *            Expected comparison result.
     * @throws Exception
     *             If the test failed unexpectedly.
     */
    @Test(dataProvider = "createRDNEqualityData")
    public void testHashCode(final Object first, final Object second, final int result)
            throws Exception {
        final RDN rdn1 = parseRDN(first);
        final RDN rdn2 = parseRDN(second);
 
        final int h1 = rdn1.hashCode();
        final int h2 = rdn2.hashCode();
 
        if (result == 0) {
            if (h1 != h2) {
                fail("Hash codes for <" + first + "> and <" + second + "> should be the same.");
            }
        } else {
            if (h1 == h2) {
                fail("Hash codes for <" + first + "> and <" + second + "> should be the same.");
            }
        }
    }
}