blob: 9b490a329d57a31b229af5cee35bd8d18a8dade3 [file] [log] [blame]
Makoto Onuki8f028a92010-01-08 13:34:57 -08001/*
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 */
Brett Chabot0dc59e72010-04-01 18:21:38 -070016package com.android.internal.net;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080017
18import com.android.internal.net.DNParser;
Makoto Onuki8f028a92010-01-08 13:34:57 -080019
20import javax.security.auth.x500.X500Principal;
21
22import junit.framework.TestCase;
23
24public class DNParserTest extends TestCase {
25 public void testFind() {
26 checkFind("", "cn", null);
27 checkFind("ou=xxx", "cn", null);
28 checkFind("ou=xxx,cn=xxx", "cn", "xxx");
29 checkFind("ou=xxx+cn=yyy,cn=zzz+cn=abc", "cn", "yyy");
30 checkFind("2.5.4.3=a,ou=xxx", "cn", "a"); // OID
31 checkFind("cn=a,cn=b", "cn", "a");
32 checkFind("ou=Cc,ou=Bb,ou=Aa", "ou", "Cc");
33 checkFind("cn=imap.gmail.com", "cn", "imap.gmail.com");
34
35 // Quoted string (see http://www.ietf.org/rfc/rfc2253.txt)
36 checkFind("o=\"\\\" a ,=<>#;\"", "o", "\" a ,=<>#;");
37 checkFind("o=abc\\,def", "o", "abc,def");
38
39 // UTF-8 (example in rfc 2253)
40 checkFind("cn=Lu\\C4\\8Di\\C4\\87", "cn", "\u004c\u0075\u010d\u0069\u0107");
41
42 // whitespaces
43 checkFind("ou=a, o= a b ,cn=x", "o", "a b");
44 checkFind("o=\" a b \" ,cn=x", "o", " a b ");
45 }
46
47 private void checkFind(String dn, String attrType, String expected) {
48 String actual = new DNParser(new X500Principal(dn)).find(attrType);
49 assertEquals("dn:" + dn + " attr:" + attrType, expected, actual);
50 }
51}