Esempio n. 1
0
    def test_escape_and_equal(self):
        def custom_escaper(s):
            return ''.join(bin(ord(c)) for c in s)

        filter1 = pureldap.LDAPFilter_and([
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1'),
                escaper=custom_escaper
            ),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('2')
            ),
        ])
        filter2 = pureldap.LDAPFilter_and([
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1')
            ),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('2'),
                escaper=custom_escaper
            ),
        ])

        self.assertEqual(filter1, filter2)
Esempio n. 2
0
    def test_nested_equal(self):
        filter1 = pureldap.LDAPFilter_or([
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1')),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('bar'),
                assertionValue=pureldap.LDAPAttributeValue('2')),
            pureldap.LDAPFilter_and([
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('baz'),
                    assertionValue=pureldap.LDAPAttributeValue('1')),
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('bob'),
                    assertionValue=pureldap.LDAPAttributeValue('2')),
            ]),
        ])
        filter2 = pureldap.LDAPFilter_or([
            pureldap.LDAPFilter_and([
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('bob'),
                    assertionValue=pureldap.LDAPAttributeValue('2')),
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('baz'),
                    assertionValue=pureldap.LDAPAttributeValue('1')),
            ]),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('bar'),
                assertionValue=pureldap.LDAPAttributeValue('2')),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1')),
        ])

        self.assertEqual(filter1, filter2)
Esempio n. 3
0
    def addChild(self, rdn, attributes):
        self._checkState()

        a = []
        if attributes.get('objectClass', None):
            a.append(('objectClass', attributes['objectClass']))
            del attributes['objectClass']
        attributes = a + sorted(attributes.items())
        del a
        rdn = distinguishedname.RelativeDistinguishedName(rdn)
        dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) +
                                                 self.dn.split())

        ldapAttrs = []
        for attrType, values in attributes:
            ldapAttrType = pureldap.LDAPAttributeDescription(attrType)
            lst = []
            for value in values:
                if (isinstance(value, six.text_type)):
                    value = value.encode('utf-8')
                lst.append(pureldap.LDAPAttributeValue(value))
            ldapValues = pureber.BERSet(lst)
            ldapAttrs.append((ldapAttrType, ldapValues))
        op = pureldap.LDAPAddRequest(entry=dn.getText(), attributes=ldapAttrs)
        d = self.client.send(op)
        d.addCallback(self._cbAddDone, dn)
        return d
Esempio n. 4
0
 def test_add_success(self):
     dn = 'cn=new,ou=stuff,dc=example,dc=com'
     self.server.dataReceived(
         str(
             pureldap.LDAPMessage(pureldap.LDAPAddRequest(
                 entry=dn,
                 attributes=[
                     (pureldap.LDAPAttributeDescription("objectClass"),
                      pureber.BERSet(
                          value=[pureldap.LDAPAttributeValue('something')]))
                 ]),
                                  id=2)))
     self.assertEquals(
         self.server.transport.value(),
         str(
             pureldap.LDAPMessage(pureldap.LDAPAddResponse(
                 resultCode=ldaperrors.Success.resultCode),
                                  id=2)))
     # tree changed
     d = self.stuff.children()
     d.addCallback(self.assertItemsEqual, [
         self.thingie, self.another,
         inmemory.ReadOnlyInMemoryLDAPEntry(
             'cn=new,ou=stuff,dc=example,dc=com',
             {'objectClass': ['something']})
     ])
     return d
Esempio n. 5
0
 def test_add_fail_existsAlready(self):
     self.server.dataReceived(
         pureldap.LDAPMessage(
             pureldap.LDAPAddRequest(
                 entry=self.thingie.dn.getText(),
                 attributes=[
                     (
                         pureldap.LDAPAttributeDescription("objectClass"),
                         pureber.BERSet(
                             value=[
                                 pureldap.LDAPAttributeValue('something'),
                             ])
                     )
                 ]),
             id=2).toWire())
     self.assertEqual(
         self.server.transport.value(),
         pureldap.LDAPMessage(
             pureldap.LDAPAddResponse(
                 resultCode=ldaperrors.LDAPEntryAlreadyExists.resultCode,
                 errorMessage=self.thingie.dn.getText()),
             id=2).toWire())
     # tree did not change
     d = self.stuff.children()
     d.addCallback(lambda actual: six.assertCountEqual(
         self, actual, [self.thingie, self.another]))
     return d
Esempio n. 6
0
    def test_default_escaper(self):
        chars = '\\*()\0'
        escaped_chars = '\\5c\\2a\\28\\29\\00'

        filters = [
            (pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('key'),
                assertionValue=pureldap.LDAPAttributeValue(chars)),
             '(key={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_initial(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_any(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_final(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_greaterOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key>={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_lessOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key<={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_approxMatch(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key~={})'.format(escaped_chars)),
        ]

        for filt, expected in filters:
            result = filt.asText()
            self.assertEqual(expected, result)
Esempio n. 7
0
    def entry_to_attribs_(self, entry):
        """
        Convert a simple mapping to the data structures required for an
        entry in the DIT.

        Returns: (dn, attributes)
        """
        attribs = {}
        dn = None
        for prop, value in entry.items():
            if prop == 'dn':
                dn = value.encode('utf-8')
                continue
            msg = ("Account template value should be an iterable: "
                   "prop='{}', value='{}'").format(prop, value)
            assert iterable_not_string(value), msg
            attribs[prop] = set(value)
        if dn is None:
            raise Exception("Entry needs to include key, `dn`!")
        ldap_attrs = []
        for attrib, values in attribs.items():
            ldap_attrib_type = pureldap.LDAPAttributeDescription(attrib)
            l = []
            for value in values:
                if (isinstance(value, unicode)):
                    value = value.encode('utf-8')
                l.append(pureldap.LDAPAttributeValue(value))
            ldap_values = pureber.BERSet(l)
            ldap_attrs.append((ldap_attrib_type, ldap_values))
        return dn, ldap_attrs
Esempio n. 8
0
def entry_to_attributes(entry):
    """
    Convert a simple mapping to the data structures required for an
    entry in the DIT.

    Returns: (dn, attributes)
    """
    attributes = {}
    dn = None
    for prop, value in entry.items():
        if prop == "dn":
            dn = value
            continue
        attributes.setdefault(prop, set()).add(value)
    if dn is None:
        raise Exception("Entry needs to include key, `dn`!")
    ldap_attributes = []
    for attrib, values in attributes.items():
        ldap_attribute_type = pureldap.LDAPAttributeDescription(attrib)
        ldap_attribute_values = []
        for value in values:
            ldap_attribute_values.append(pureldap.LDAPAttributeValue(value))
        ldap_values = pureber.BERSet(ldap_attribute_values)
        ldap_attributes.append((ldap_attribute_type, ldap_values))
    return dn, ldap_attributes
Esempio n. 9
0
 def test_add_success(self):
     dn = "cn=new,ou=stuff,dc=example,dc=com"
     self.server.dataReceived(
         pureldap.LDAPMessage(
             pureldap.LDAPAddRequest(
                 entry=dn,
                 attributes=[(
                     pureldap.LDAPAttributeDescription("objectClass"),
                     pureber.BERSet(
                         value=[pureldap.LDAPAttributeValue("something")]),
                 )],
             ),
             id=2,
         ).toWire())
     self.assertEqual(
         self.server.transport.value(),
         pureldap.LDAPMessage(pureldap.LDAPAddResponse(
             resultCode=ldaperrors.Success.resultCode),
                              id=2).toWire(),
     )
     # tree changed
     d = self.stuff.children()
     d.addCallback(lambda actual: self.assertCountEqual(
         actual,
         [
             self.thingie,
             self.another,
             inmemory.ReadOnlyInMemoryLDAPEntry(
                 b"cn=new,ou=stuff,dc=example,dc=com",
                 {b"objectClass": [b"something"]},
             ),
         ],
     ))
     return d
Esempio n. 10
0
    def test_basic_or_not_equal(self):
        filter1 = pureldap.LDAPFilter_or([
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1')),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('bar'),
                assertionValue=pureldap.LDAPAttributeValue('2')),
        ])
        filter2 = pureldap.LDAPFilter_or([
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('bar'),
                assertionValue=pureldap.LDAPAttributeValue('1')),
            pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('foo'),
                assertionValue=pureldap.LDAPAttributeValue('1')),
        ])

        self.assertNotEqual(filter1, filter2)
Esempio n. 11
0
    def addChild(self, rdn, attributes):
        self._checkState()

        rdn = distinguishedname.RelativeDistinguishedName(rdn)
        dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) +
                                                 self.dn.split())

        ldapAttrs = []
        for attrType, values in attributes.items():
            ldapAttrType = pureldap.LDAPAttributeDescription(attrType)
            l = []
            for value in values:
                l.append(pureldap.LDAPAttributeValue(value))
            ldapValues = pureber.BERSet(l)

            ldapAttrs.append((ldapAttrType, ldapValues))
        op = pureldap.LDAPAddRequest(entry=str(dn), attributes=ldapAttrs)
        d = self.client.send(op)
        d.addCallback(self._cbAddDone, dn)
        return d
Esempio n. 12
0
    def test_custom_escaper(self):
        chars = 'HELLO'
        escaped_chars = '0b10010000b10001010b10011000b10011000b1001111'

        def custom_escaper(s):
            return ''.join(bin(ord(c)) for c in s)

        filters = [
            (pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('key'),
                assertionValue=pureldap.LDAPAttributeValue(chars),
                escaper=custom_escaper), '(key={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_initial(value=chars,
                                                    escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_any(value=chars,
                                                escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_final(value=chars,
                                                  escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_greaterOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key>={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_lessOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key<={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_approxMatch(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key~={})'.format(escaped_chars)),
        ]

        for filt, expected in filters:
            result = filt.asText()
            self.assertEqual(expected, result)