Makoto Onuki | 8f028a9 | 2010-01-08 13:34:57 -0800 | [diff] [blame] | 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 | */ |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 16 | package com.android.internal.net; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 17 | |
| 18 | import com.android.internal.net.DNParser; |
Makoto Onuki | 8f028a9 | 2010-01-08 13:34:57 -0800 | [diff] [blame] | 19 | |
| 20 | import javax.security.auth.x500.X500Principal; |
| 21 | |
| 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | public 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 | } |