Exemplo n.º 1
0
 def testFromBERNullKnownValues(self):
     """BERNull(encoded="...") should give known result with known input"""
     encoded = [0x05, 0x00]
     m = s(*encoded)
     result, bytes = pureber.berDecodeObject(pureber.BERDecoderContext(), m)
     self.assertEquals(bytes, len(m))
     assert isinstance(result, pureber.BERNull)
     assert 0x05 == result.tag
Exemplo n.º 2
0
 def testSanity(self):
     """BEREnumerated(encoded=BEREnumerated(n)).value==n for -1000..1000"""
     for n in range(-1000, 1001, 10):
         encoded = str(pureber.BEREnumerated(n))
         result, bytes = pureber.berDecodeObject(
             pureber.BERDecoderContext(), encoded)
         self.assertEquals(bytes, len(encoded))
         assert isinstance(result, pureber.BEREnumerated)
         result = result.value
         assert n == result
Exemplo n.º 3
0
 def testFromBEREnumeratedKnownValues(self):
     """BEREnumerated(encoded="...") should give known result with known input"""
     for integer, encoded in self.knownValues:
         m = s(*encoded)
         result, bytes = pureber.berDecodeObject(
             pureber.BERDecoderContext(), m)
         self.assertEquals(bytes, len(m))
         assert isinstance(result, pureber.BEREnumerated)
         result = result.value
         assert integer == result
Exemplo n.º 4
0
 def testPartialBERNullEncodings(self):
     """BERNull(encoded="...") with too short input should throw BERExceptionInsufficientData"""
     m = str(pureber.BERNull())
     assert len(m) == 2
     self.assertRaises(pureber.BERExceptionInsufficientData,
                       pureber.berDecodeObject, pureber.BERDecoderContext(),
                       m[:1])
     self.assertEquals((None, 0),
                       pureber.berDecodeObject(pureber.BERDecoderContext(),
                                               ''))
Exemplo n.º 5
0
 def testSanity(self):
     """BEROctetString(encoded=BEROctetString(n*'x')).value==n*'x' for some values of n"""
     for n in 0, 1, 2, 3, 4, 5, 6, 100, 126, 127, 128, 129, 1000, 2000:
         encoded = str(pureber.BEROctetString(n * 'x'))
         result, bytes = pureber.berDecodeObject(
             pureber.BERDecoderContext(), encoded)
         self.assertEquals(bytes, len(encoded))
         assert isinstance(result, pureber.BEROctetString)
         result = result.value
         assert n * 'x' == result
Exemplo n.º 6
0
 def testFromBEROctetStringKnownValues(self):
     """BEROctetString(encoded="...") should give known result with known input"""
     for st, encoded in self.knownValues:
         m = s(*encoded)
         result, bytes = pureber.berDecodeObject(
             pureber.BERDecoderContext(), m)
         self.assertEquals(bytes, len(m))
         assert isinstance(result, pureber.BEROctetString)
         result = str(result)
         result = map(ord, result)
         assert encoded == result
Exemplo n.º 7
0
 def testPartial(self):
     """LDAPClass(encoded="...") with too short input should throw BERExceptionInsufficientData"""
     for klass, args, kwargs, decoder, encoded in self.knownValues:
         if decoder is None:
             decoder = pureldap.LDAPBERDecoderContext(
                 fallback=pureber.BERDecoderContext())
         for i in xrange(1, len(encoded)):
             m = s(*encoded)[:i]
             self.assertRaises(pureber.BERExceptionInsufficientData,
                               pureber.berDecodeObject, decoder, m)
         self.assertEquals((None, 0), pureber.berDecodeObject(decoder, ''))
Exemplo n.º 8
0
 def dataReceived(self, recd):
     self.buffer += recd
     while 1:
         try:
             o, bytes = pureber.berDecodeObject(self.berdecoder,
                                                self.buffer)
         except pureldap.BERExceptionInsufficientData:
             o, bytes = None, 0
         self.buffer = self.buffer[bytes:]
         if not o:
             break
         self.handle(o)
Exemplo n.º 9
0
 def testFromBERSequenceKnownValues(self):
     """BERSequence(encoded="...") should give known result with known input"""
     for content, encoded in self.knownValues:
         m = s(*encoded)
         result, bytes = pureber.berDecodeObject(
             pureber.BERDecoderContext(), m)
         self.assertEquals(bytes, len(m))
         assert isinstance(result, pureber.BERSequence)
         result = result.data
         assert len(content) == len(result)
         for i in xrange(len(content)):
             assert content[i] == result[i]
         assert content == result
Exemplo n.º 10
0
    def testFromLDAP(self):
        """LDAPClass(encoded="...") should give known result with known input"""
        for klass, args, kwargs, decoder, encoded in self.knownValues:
            if decoder is None:
                decoder = pureldap.LDAPBERDecoderContext(
                    fallback=pureber.BERDecoderContext())
            m = s(*encoded)
            result, bytes = pureber.berDecodeObject(decoder, m)
            self.assertEquals(bytes, len(m))

            shouldBe = klass(*args, **kwargs)
            #TODO shouldn't use str below
            assert str(result)==str(shouldBe), \
                   "Class %s(*%s, **%s) doesn't decode properly: " \
                   "%s != %s" % (klass.__name__,
                                 repr(args), repr(kwargs),
                                 repr(result), repr(shouldBe))
Exemplo n.º 11
0
    def testPartialBERSequenceEncodings(self):
        """BERSequence(encoded="...") with too short input should throw BERExceptionInsufficientData"""
        m = str(pureber.BERSequence([pureber.BERInteger(2)]))
        assert len(m) == 5

        self.assertRaises(pureber.BERExceptionInsufficientData,
                          pureber.berDecodeObject, pureber.BERDecoderContext(),
                          m[:4])
        self.assertRaises(pureber.BERExceptionInsufficientData,
                          pureber.berDecodeObject, pureber.BERDecoderContext(),
                          m[:3])
        self.assertRaises(pureber.BERExceptionInsufficientData,
                          pureber.berDecodeObject, pureber.BERDecoderContext(),
                          m[:2])
        self.assertRaises(pureber.BERExceptionInsufficientData,
                          pureber.berDecodeObject, pureber.BERDecoderContext(),
                          m[:1])
        self.assertEquals((None, 0),
                          pureber.berDecodeObject(pureber.BERDecoderContext(),
                                                  ''))