1 13 14 package org.ejbca.util.dn; 15 16 import java.util.HashMap ; 17 18 import junit.framework.TestCase; 19 20 import org.apache.log4j.Logger; 21 22 23 28 public class TestDnFieldExtractor extends TestCase { 29 private static Logger log = Logger.getLogger(TestDnFieldExtractor.class); 30 31 36 public TestDnFieldExtractor(String name) { 37 super(name); 38 } 39 40 protected void setUp() throws Exception { 41 log.debug(">setUp()"); 42 log.debug("<setUp()"); 43 } 44 45 protected void tearDown() throws Exception { 46 log.debug(">tearDown()"); 47 log.debug("<tearDown()"); 48 } 49 50 53 public void test01CheckDnFields() throws Exception { 54 String comp = DnComponents.getDnExtractorFieldFromDnId(28); 55 assertEquals("DN=", comp); 56 String dn = "cn=Tomas Gustavsson,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com"; 57 DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN); 58 HashMap i = extractor.getNumberOfFields(); 59 assertEquals(17,i.size()); 60 String cn = extractor.getField(DNFieldExtractor.CN, 0); 61 assertEquals("Tomas Gustavsson", cn); 62 cn = extractor.getField(DNFieldExtractor.CN, 1); 63 assertEquals("",cn); 64 String dc = extractor.getField(DNFieldExtractor.DC, 0); 65 assertEquals("PrimeKey", dc); 66 dc = extractor.getField(DNFieldExtractor.DC, 1); 67 assertEquals("com", dc); 68 String l = extractor.getField(DNFieldExtractor.L, 0); 69 assertEquals("Stockholm", l); 70 int num = extractor.getNumberOfFields(DNFieldExtractor.DC); 71 assertEquals(2, num); 72 num = extractor.getNumberOfFields(DNFieldExtractor.O); 73 assertEquals(1, num); 74 String fieldstr = extractor.getFieldString(DNFieldExtractor.CN); 75 assertEquals("CN=Tomas Gustavsson", fieldstr); 76 fieldstr = extractor.getFieldString(DNFieldExtractor.DC); 77 assertEquals("DC=PrimeKey,DC=com", fieldstr); 78 boolean illegal = extractor.isIllegal(); 79 assertFalse(illegal); 80 boolean other = extractor.existsOther(); 81 assertFalse(other); 82 83 dn = "dn=qualifier,cn=Tomas Gustavsson,1.1.1.1=Foo,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com"; 84 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN); 85 illegal = extractor.isIllegal(); 86 assertFalse(illegal); 87 other = extractor.existsOther(); 88 assertTrue(other); 89 num = extractor.getNumberOfFields(28); 90 assertEquals(1, num); 91 String field = extractor.getField(28,0); 92 assertEquals("qualifier", field); 93 field = extractor.getField(DNFieldExtractor.CN,0); 94 assertEquals("Tomas Gustavsson", field); 95 96 dn = "qqq,cn=Tomas Gustavsson,1.1.1.1=Foo,o=PrimeKey,L=Stockholm,dc=PrimeKey,DC=com"; 97 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDN); 98 illegal = extractor.isIllegal(); 99 assertTrue(illegal); 100 } 101 102 105 public void test01CheckAltNameFields() throws Exception { 106 String dn = "DnsName=foo.bar.se,rfc822Name=foo@bar.se"; 107 DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 108 HashMap i = extractor.getNumberOfFields(); 109 assertEquals(11,i.size()); 110 String dns = extractor.getField(DNFieldExtractor.DNSNAME, 0); 111 assertEquals("foo.bar.se", dns); 112 boolean illegal = extractor.isIllegal(); 113 assertFalse(illegal); 114 boolean other = extractor.existsOther(); 115 assertFalse(other); 116 String email= extractor.getField(DNFieldExtractor.RFC822NAME, 0); 117 assertEquals("foo@bar.se", email); 118 int num = extractor.getNumberOfFields(DNFieldExtractor.RFC822NAME); 119 assertEquals(1, num); 120 121 dn = "uniformResourceId=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1"; 122 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 123 illegal = extractor.isIllegal(); 124 assertFalse(illegal); 125 other = extractor.existsOther(); 126 assertFalse(other); 127 num = extractor.getNumberOfFields(DNFieldExtractor.URI); 128 assertEquals(1, num); 129 String field = extractor.getField(DNFieldExtractor.URI, 0); 130 assertEquals("http://www.a.se/", field); 131 dn = "uniformResourceIdentifier=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1"; 132 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 133 illegal = extractor.isIllegal(); 134 assertFalse(illegal); 135 other = extractor.existsOther(); 136 assertFalse(other); 137 num = extractor.getNumberOfFields(DNFieldExtractor.URI); 138 assertEquals(1, num); 139 dn = "uri=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1"; 140 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 141 illegal = extractor.isIllegal(); 142 assertFalse(illegal); 143 other = extractor.existsOther(); 144 assertFalse(other); 145 num = extractor.getNumberOfFields(DNFieldExtractor.URI); 146 assertEquals(1, num); 147 field = extractor.getField(DNFieldExtractor.URI, 0); 148 assertEquals("http://www.a.se/", field); 149 dn = "uniformResourceIdentifier=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1"; 150 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 151 illegal = extractor.isIllegal(); 152 assertFalse(illegal); 153 other = extractor.existsOther(); 154 assertFalse(other); 155 num = extractor.getNumberOfFields(DNFieldExtractor.URI); 156 assertEquals(1, num); 157 dn = "uniformResourceId=http://www.a.se/,upn=foo@a.se,upn=foo@b.se,rfc822name=tomas@a.se,dNSName=www.a.se,dNSName=www.b.se,iPAddress=10.1.1.1"; 158 extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTALTNAME); 159 illegal = extractor.isIllegal(); 160 assertFalse(illegal); 161 other = extractor.existsOther(); 162 assertFalse(other); 163 num = extractor.getNumberOfFields(DNFieldExtractor.URI); 164 assertEquals(1, num); 165 field = extractor.getField(DNFieldExtractor.URI, 0); 166 assertEquals("http://www.a.se/", field); 167 } 168 169 172 public void test01CheckDirAttrFields() throws Exception { 173 String dn = "PlaceOfBirth=Stockholm,DateOfBirth=10660911"; 174 DNFieldExtractor extractor = new DNFieldExtractor(dn, DNFieldExtractor.TYPE_SUBJECTDIRATTR); 175 HashMap i = extractor.getNumberOfFields(); 176 assertEquals(5,i.size()); 177 String dns = extractor.getField(DNFieldExtractor.PLACEOFBIRTH, 0); 178 assertEquals("Stockholm", dns); 179 boolean illegal = extractor.isIllegal(); 180 assertFalse(illegal); 181 boolean other = extractor.existsOther(); 182 assertFalse(other); 183 String email= extractor.getField(DNFieldExtractor.DATEOFBIRTH, 0); 184 assertEquals("10660911", email); 185 int num = extractor.getNumberOfFields(DNFieldExtractor.DATEOFBIRTH); 186 assertEquals(1, num); 187 188 extractor = new DNFieldExtractor("", DNFieldExtractor.TYPE_SUBJECTDIRATTR); 189 i = extractor.getNumberOfFields(); 190 assertEquals(5,i.size()); 191 num = extractor.getNumberOfFields(DNFieldExtractor.DATEOFBIRTH); 192 assertEquals(0, num); 193 194 } 195 196 } 197 198 | Popular Tags |