Package ldaptor :: Package test :: Module test_distinguishedname
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.test.test_distinguishedname

  1  """ 
  2  Test cases for ldaptor.protocols.ldap.distinguishedname module. 
  3  """ 
  4   
  5  from twisted.trial import unittest 
  6  from ldaptor.protocols.ldap import distinguishedname as dn 
  7   
8 -class TestCaseWithKnownValues(unittest.TestCase):
9 knownValues = () 10
11 - def testKnownValues(self):
12 for s, l in self.knownValues: 13 fromString = dn.DistinguishedName(s) 14 listOfRDNs = [] 15 for av in l: 16 listOfAttributeTypesAndValues = [] 17 for a,v in av: 18 listOfAttributeTypesAndValues.append(dn.LDAPAttributeTypeAndValue(attributeType=a, value=v)) 19 r=dn.RelativeDistinguishedName(listOfAttributeTypesAndValues) 20 listOfRDNs.append(r) 21 fromList = dn.DistinguishedName(listOfRDNs) 22 23 self.assertEquals(fromString, fromList) 24 25 fromStringToString = str(fromString) 26 fromListToString = str(fromList) 27 28 assert fromStringToString == fromListToString 29 30 canon = fromStringToString 31 # DNs equal their string representation. Note this does 32 # not mean they equal all the possible string 33 # representations -- just the canonical one. 34 self.assertEquals(fromString, canon) 35 self.assertEquals(fromList, canon) 36 self.assertEquals(canon, fromString) 37 self.assertEquals(canon, fromList) 38 39 # DNs can be used interchangeably with their canonical 40 # string representation as hash keys. 41 self.assertEquals(hash(fromString), hash(canon)) 42 self.assertEquals(hash(fromList), hash(canon)) 43 self.assertEquals(hash(canon), hash(fromString)) 44 self.assertEquals(hash(canon), hash(fromList))
45 46
47 -class LDAPDistinguishedName_Escaping(TestCaseWithKnownValues):
48 knownValues = ( 49 50 ('', []), 51 52 ('cn=foo', [[('cn', 'foo')]]), 53 54 (r'cn=\,bar', [[('cn', r',bar')]]), 55 (r'cn=foo\,bar', [[('cn', r'foo,bar')]]), 56 (r'cn=foo\,', [[('cn', r'foo,')]]), 57 58 (r'cn=\+bar', [[('cn', r'+bar')]]), 59 (r'cn=foo\+bar', [[('cn', r'foo+bar')]]), 60 (r'cn=foo\+', [[('cn', r'foo+')]]), 61 62 (r'cn=\"bar', [[('cn', r'"bar')]]), 63 (r'cn=foo\"bar', [[('cn', r'foo"bar')]]), 64 (r'cn=foo\"', [[('cn', r'foo"')]]), 65 66 (r'cn=\\bar', [[('cn', r'\bar')]]), 67 (r'cn=foo\\bar', [[('cn', r'foo\bar')]]), 68 (r'cn=foo\\', [[('cn', 'foo\\')]]), 69 70 (r'cn=\<bar', [[('cn', r'<bar')]]), 71 (r'cn=foo\<bar', [[('cn', r'foo<bar')]]), 72 (r'cn=foo\<', [[('cn', r'foo<')]]), 73 74 (r'cn=\>bar', [[('cn', r'>bar')]]), 75 (r'cn=foo\>bar', [[('cn', r'foo>bar')]]), 76 (r'cn=foo\>', [[('cn', r'foo>')]]), 77 78 (r'cn=\;bar', [[('cn', r';bar')]]), 79 (r'cn=foo\;bar', [[('cn', r'foo;bar')]]), 80 (r'cn=foo\;', [[('cn', r'foo;')]]), 81 82 (r'cn=\#bar', [[('cn', r'#bar')]]), 83 84 (r'cn=\ bar', [[('cn', r' bar')]]), 85 86 (r'cn=bar\ ', [[('cn', r'bar ')]]), 87 88 (r'cn=test+owner=uid\=foo\,ou\=depar' 89 +r'tment\,dc\=example\,dc\=com,dc=ex' 90 +r'ample,dc=com', [[('cn', r'test'), 91 ('owner', r'uid=foo,ou=depart' 92 +r'ment,dc=example,dc=com'), 93 ], 94 [('dc', r'example')], 95 [('dc', r'com')]]), 96 97 (r'cn=bar,dc=example,dc=com', [[('cn', 'bar')], 98 [('dc', 'example')], 99 [('dc', 'com')]]), 100 (r'cn=bar, dc=example, dc=com', [[('cn', 'bar')], 101 [('dc', 'example')], 102 [('dc', 'com')]]), 103 (r'cn=bar, dc=example,dc=com', [[('cn', 'bar')], 104 [('dc', 'example')], 105 [('dc', 'com')]]), 106 107 ) 108
109 - def testOpenLDAPEqualsEscape(self):
110 """Slapd wants = to be escaped in RDN attributeValues.""" 111 got = dn.DistinguishedName(listOfRDNs=[ 112 dn.RelativeDistinguishedName( 113 attributeTypesAndValues=[ 114 dn.LDAPAttributeTypeAndValue(attributeType='cn', value=r'test'), 115 dn.LDAPAttributeTypeAndValue(attributeType='owner', value=r'uid=foo,ou=depart' 116 +r'ment,dc=example,dc=com'), 117 ]), 118 119 dn.RelativeDistinguishedName('dc=example'), 120 dn.RelativeDistinguishedName('dc=com'), 121 ]) 122 got = str(got) 123 self.assertEquals(got, 124 r'cn=test+owner=uid\=foo\,ou\=depar' 125 +r'tment\,dc\=example\,dc\=com,dc=ex' 126 +r'ample,dc=com')
127
128 -class LDAPDistinguishedName_RFC2253_Examples(TestCaseWithKnownValues):
129 knownValues = ( 130 131 ('CN=Steve Kille,O=Isode Limited,C=GB', 132 [[('CN', 'Steve Kille')], 133 [('O', 'Isode Limited')], 134 [('C', 'GB')]]), 135 136 137 ('OU=Sales+CN=J. Smith,O=Widget Inc.,C=US', 138 [[('OU', 'Sales'), 139 ('CN', 'J. Smith')], 140 [('O', 'Widget Inc.')], 141 [('C', 'US')]]), 142 143 (r'CN=L. Eagle,O=Sue\, Grabbit and Runn,C=GB', 144 [[('CN', 'L. Eagle')], 145 [('O', 'Sue, Grabbit and Runn')], 146 [('C', 'GB')]]), 147 148 (r'CN=Before\0DAfter,O=Test,C=GB', 149 [[('CN', 'Before\x0dAfter')], 150 [('O', 'Test')], 151 [('C', 'GB')]]), 152 153 (r'1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB', 154 [[('1.3.6.1.4.1.1466.0', '#04024869')], 155 [('O', 'Test')], 156 [('C', 'GB')]]), 157 158 (u'SN=Lu\u010di\u0107'.encode('utf-8'), 159 [[('SN', u'Lu\u010di\u0107'.encode('utf-8'))]]) 160 161 )
162
163 -class LDAPDistinguishedName_InitialSpaces(TestCaseWithKnownValues):
164 knownValues = ( 165 166 ('cn=foo, ou=bar, dc=quux, \ attributeThatStartsWithSpace=Value', 167 [[('cn', 'foo')], 168 [('ou', 'bar')], 169 [('dc', 'quux')], 170 [(' attributeThatStartsWithSpace', 'Value')]]), 171 172 )
173
174 -class LDAPDistinguishedName_DomainName(unittest.TestCase):
175 - def testNonDc(self):
176 d=dn.DistinguishedName('cn=foo,o=bar,c=us') 177 assert d.getDomainName() is None
178
179 - def testNonTrailingDc(self):
180 d=dn.DistinguishedName('cn=foo,o=bar,dc=foo,c=us') 181 assert d.getDomainName() is None
182
183 - def testSimple_ExampleCom(self):
184 d=dn.DistinguishedName('dc=example,dc=com') 185 assert d.getDomainName() == 'example.com'
186
187 - def testSimple_SubExampleCom(self):
188 d=dn.DistinguishedName('dc=sub,dc=example,dc=com') 189 assert d.getDomainName() == 'sub.example.com'
190
192 d=dn.DistinguishedName('cn=host,dc=sub,dc=example,dc=com') 193 assert d.getDomainName() == 'sub.example.com'
194
196 d=dn.DistinguishedName('dc=sub2,cn=host,dc=sub,dc=example,dc=com') 197 assert d.getDomainName() == 'sub.example.com'
198
199 -class LDAPDistinguishedName_contains(unittest.TestCase):
200 shsec=dn.DistinguishedName('dc=sub2,cn=host,dc=sub,dc=example,dc=com') 201 hsec=dn.DistinguishedName('cn=host,dc=sub,dc=example,dc=com') 202 sec=dn.DistinguishedName('dc=sub,dc=example,dc=com') 203 ec=dn.DistinguishedName('dc=example,dc=com') 204 c=dn.DistinguishedName('dc=com') 205 206 soc=dn.DistinguishedName('dc=sub,dc=other,dc=com') 207 oc=dn.DistinguishedName('dc=other,dc=com') 208 209 other=dn.DistinguishedName('o=foo,c=US') 210 211 root=dn.DistinguishedName('') 212
213 - def test_selfContainment(self):
214 assert self.c.contains(self.c) 215 assert self.ec.contains(self.ec) 216 assert self.sec.contains(self.sec) 217 assert self.hsec.contains(self.hsec) 218 assert self.shsec.contains(self.shsec) 219 220 assert self.soc.contains(self.soc) 221 assert self.oc.contains(self.oc) 222 223 assert self.root.contains(self.root) 224 225 assert self.other.contains(self.other)
226
227 - def test_realContainment(self):
228 assert self.c.contains(self.ec) 229 assert self.c.contains(self.sec) 230 assert self.c.contains(self.hsec) 231 assert self.c.contains(self.shsec) 232 233 assert self.ec.contains(self.sec) 234 assert self.ec.contains(self.hsec) 235 assert self.ec.contains(self.shsec) 236 237 assert self.sec.contains(self.hsec) 238 assert self.sec.contains(self.shsec) 239 240 assert self.hsec.contains(self.shsec) 241 242 assert self.c.contains(self.oc) 243 assert self.c.contains(self.soc) 244 assert self.oc.contains(self.soc) 245 246 for x in (self.shsec, self.hsec, self.sec, self.ec, self.c, 247 self.soc, self.oc, self.other): 248 assert self.root.contains(x)
249
251 assert not self.shsec.contains(self.hsec) 252 assert not self.shsec.contains(self.sec) 253 assert not self.shsec.contains(self.ec) 254 assert not self.shsec.contains(self.c) 255 256 assert not self.hsec.contains(self.sec) 257 assert not self.hsec.contains(self.ec) 258 assert not self.hsec.contains(self.c) 259 260 assert not self.sec.contains(self.ec) 261 assert not self.sec.contains(self.c) 262 263 assert not self.ec.contains(self.c) 264 assert not self.soc.contains(self.oc) 265 266 for x in (self.shsec, self.hsec, self.sec, self.ec, self.c, 267 self.soc, self.oc, self.other): 268 assert not x.contains(self.root)
269
271 groups=([self.shsec, self.hsec, self.sec, self.ec], 272 [self.soc, self.oc], 273 [self.other]) 274 for g1 in groups: 275 for g2 in groups: 276 if g1!=g2: 277 for i1 in g1: 278 for i2 in g2: 279 assert not i1.contains(i2) 280 assert not self.c.contains(self.other) 281 assert not self.other.contains(self.c)
282
283 -class LDAPDistinguishedName_Malformed(unittest.TestCase):
284 - def testMalformed(self):
285 self.assertRaises(dn.InvalidRelativeDistinguishedName, 286 dn.DistinguishedName, 287 'foo') 288 self.assertRaises(dn.InvalidRelativeDistinguishedName, 289 dn.DistinguishedName, 290 'foo,dc=com') 291 self.assertRaises(dn.InvalidRelativeDistinguishedName, 292 dn.DistinguishedName, 293 'ou=something,foo') 294 self.assertRaises(dn.InvalidRelativeDistinguishedName, 295 dn.DistinguishedName, 296 'foo,foo')
297
298 -class LDAPDistinguishedName_Prettify(unittest.TestCase):
299 - def testPrettifySpaces(self):
300 """str(DistinguishedName(...)) prettifies the DN by removing extra whitespace.""" 301 d=dn.DistinguishedName('cn=foo, o=bar, c=us') 302 assert str(d) == 'cn=foo,o=bar,c=us'
303
304 -class DistinguishedName_Init(unittest.TestCase):
305 - def testString(self):
306 d=dn.DistinguishedName('dc=example,dc=com') 307 self.assertEquals(str(d), 'dc=example,dc=com')
308
309 - def testDN(self):
310 proto=dn.DistinguishedName('dc=example,dc=com') 311 d=dn.DistinguishedName(proto) 312 self.assertEquals(str(d), 'dc=example,dc=com')
313
314 -class RelativeDistinguishedName_Init(unittest.TestCase):
315 - def testString(self):
316 rdn=dn.RelativeDistinguishedName('dc=example') 317 self.assertEquals(str(rdn), 'dc=example')
318
319 - def testRDN(self):
320 proto=dn.RelativeDistinguishedName('dc=example') 321 rdn=dn.RelativeDistinguishedName(proto) 322 self.assertEquals(str(rdn), 'dc=example')
323
324 -class DistinguishedName_Comparison(unittest.TestCase):
325 # TODO test more carefully
326 - def testGT(self):
327 dn1=dn.DistinguishedName('dc=example,dc=com') 328 dn2=dn.DistinguishedName('dc=bar,dc=example,dc=com') 329 self.failUnless(dn1 > dn2)
330