1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.javax.net.ssl;
18
19import javax.net.ssl.DistinguishedNameParser;
20import javax.security.auth.x500.X500Principal;
21import junit.framework.TestCase;
22
23public final class DistinguishedNameParserTest extends TestCase {
24    public void testGetFirstCn() {
25        assertFirstCn("", null);
26        assertFirstCn("ou=xxx", null);
27        assertFirstCn("ou=xxx,cn=xxx", "xxx");
28        assertFirstCn("ou=xxx+cn=yyy,cn=zzz+cn=abc", "yyy");
29        assertFirstCn("cn=a,cn=b", "a");
30        assertFirstCn("cn=Cc,cn=Bb,cn=Aa", "Cc");
31        assertFirstCn("cn=imap.gmail.com", "imap.gmail.com");
32    }
33
34    public void testGetFirstCnWithOid() {
35        assertFirstCn("2.5.4.3=a,ou=xxx", "a");
36    }
37
38    public void testGetFirstCnWithQuotedStrings() {
39        assertFirstCn("cn=\"\\\" a ,=<>#;\"", "\" a ,=<>#;");
40        assertFirstCn("cn=abc\\,def", "abc,def");
41    }
42
43    public void testGetFirstCnWithUtf8() {
44        assertFirstCn("cn=Lu\\C4\\8Di\\C4\\87", "\u004c\u0075\u010d\u0069\u0107");
45    }
46
47    public void testGetFirstCnWithWhitespace() {
48        assertFirstCn("ou=a, cn=  a  b  ,o=x", "a  b");
49        assertFirstCn("cn=\"  a  b  \" ,o=x", "  a  b  ");
50    }
51
52    private void assertFirstCn(String dn, String expected) {
53        X500Principal principal = new X500Principal(dn);
54        assertEquals(dn, expected, new DistinguishedNameParser(principal).findMostSpecific("cn"));
55    }
56}
57