Пример #1
0
 def testFromBERNullKnownValues(self):
     """BERNull(encoded="...") should give known result with known input"""
     encoded=[0x05, 0x00]
     m=MutableString(apply(s,encoded))
     m.append('foo')
     result = pureber.BERNull(encoded=m, berdecoder=pureber.BERDecoderContext())
     assert m=='foo'
     assert 0x05==result.tag
Пример #2
0
 def testFromBERNullKnownValues(self):
     """BERNull(encoded="...") should give known result with known input"""
     encoded = [0x05, 0x00]
     m = MutableString(apply(s, encoded))
     m.append("foo")
     result = pureber.BERNull(encoded=m, berdecoder=pureber.BERDecoderContext())
     assert m == "foo"
     assert 0x05 == result.tag
Пример #3
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 = MutableString(pureber.BEROctetString(n * "x"))
         encoded.append("foo")
         result = pureber.BEROctetString(encoded=encoded, berdecoder=pureber.BERDecoderContext())
         result = result.value
         assert encoded == "foo"
         assert n * "x" == result
Пример #4
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 = MutableString(pureber.BEROctetString(n*'x'))
         encoded.append('foo')
         result = pureber.BEROctetString(encoded=encoded, berdecoder=pureber.BERDecoderContext())
         result = result.value
         assert encoded=='foo'
         assert n*'x'==result
Пример #5
0
 def testSanity(self):
     """BEREnumerated(encoded=BEREnumerated(n)).value==n for -1000..1000"""
     for n in range(-1000, 1001, 10):
         encoded = MutableString(pureber.BEREnumerated(n))
         encoded.append("foo")
         result = pureber.BEREnumerated(encoded=encoded, berdecoder=pureber.BERDecoderContext())
         result = result.value
         assert encoded == "foo"
         assert n == result
Пример #6
0
 def testFromBERIntegerKnownValues(self):
     """BERInteger(encoded="...") should give known result with known input"""
     for integer, encoded in self.knownValues:
         m=MutableString(apply(s,encoded))
         m.append('foo')
         result = pureber.BERInteger(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m=='foo'
         result = result.value
         assert integer==result
Пример #7
0
 def testFromBERIntegerKnownValues(self):
     """BERInteger(encoded="...") should give known result with known input"""
     for integer, encoded in self.knownValues:
         m = MutableString(apply(s, encoded))
         m.append("foo")
         result = pureber.BERInteger(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m == "foo"
         result = result.value
         assert integer == result
Пример #8
0
 def testFromBEROctetStringKnownValues(self):
     """BEROctetString(encoded="...") should give known result with known input"""
     for st, encoded in self.knownValues:
         m = MutableString(apply(s, encoded))
         m.append("foo")
         result = pureber.BEROctetString(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m == "foo"
         result = str(result)
         result = map(ord, result)
         assert encoded == result
Пример #9
0
 def testSanity(self):
     """BEREnumerated(encoded=BEREnumerated(n)).value==n for -1000..1000"""
     for n in range(-1000, 1001, 10):
         encoded = MutableString(pureber.BEREnumerated(n))
         encoded.append('foo')
         result = pureber.BEREnumerated(encoded=encoded,
                                     berdecoder=pureber.BERDecoderContext())
         result = result.value
         assert encoded=='foo'
         assert n==result
Пример #10
0
 def testFromBEROctetStringKnownValues(self):
     """BEROctetString(encoded="...") should give known result with known input"""
     for st, encoded in self.knownValues:
         m=MutableString(apply(s,encoded))
         m.append('foo')
         result = pureber.BEROctetString(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m=='foo'
         result = str(result)
         result = map(ord, result)
         assert encoded==result
Пример #11
0
 def testFromBERSequenceKnownValues(self):
     """BERSequence(encoded="...") should give known result with known input"""
     for content, encoded in self.knownValues:
         m=MutableString(apply(s,encoded))
         m.append('foo')
         result = pureber.BERSequence(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m=='foo'
         result = result.data
         assert len(content)==len(result)
         for i in xrange(len(content)):
             assert content[i]==result[i]
         assert content==result
Пример #12
0
 def testFromBERSequenceKnownValues(self):
     """BERSequence(encoded="...") should give known result with known input"""
     for content, encoded in self.knownValues:
         m = MutableString(apply(s, encoded))
         m.append("foo")
         result = pureber.BERSequence(encoded=m, berdecoder=pureber.BERDecoderContext())
         assert m == "foo"
         result = result.data
         assert len(content) == len(result)
         for i in xrange(len(content)):
             assert content[i] == result[i]
         assert content == result
Пример #13
0
def int2ber(i):
    encoded = MutableString()
    while i > 127 or i < -128:
        encoded = chr(i % 256) + encoded
        i = i >> 8
    encoded = chr(i % 256) + encoded
    return encoded
Пример #14
0
 def decode(self, encoded, berdecoder):
     e2 = MutableString(encoded)
     need(e2, 2)
     self.tag = ber2int(e2[0], signed=0) & (CLASS_MASK | TAG_MASK)
     del e2[0]
     l = berlen2int(e2)
     assert l >= 0
     need(e2, l)
     e = e2[:l]
     del e2[:l]
     encoded.set(e2)
     self.value = e
Пример #15
0
 def decode(self, encoded, berdecoder):
     e2 = MutableString(encoded)
     need(e2, 2)
     self.tag = ber2int(e2[0], signed=0) & (CLASS_MASK | TAG_MASK)
     del e2[0]
     l = berlen2int(e2)
     need(e2, l)
     content = e2[:l]
     del e2[:l]
     self[:] = []
     # decode content
     while content:
         n = ber2object(berdecoder, content)
         assert n != None
         self.append(n)
     encoded.set(e2)
Пример #16
0
 def testPartialBEREnumeratedEncodings(self):
     """BEREnumerated(encoded="...") with too short input should throw BERExceptionInsufficientData"""
     m=str(pureber.BEREnumerated(42))
     assert len(m)==3
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BEREnumerated, encoded=m[:2], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BEREnumerated, encoded=m[:1], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BEREnumerated, encoded=MutableString(""), berdecoder=pureber.BERDecoderContext())
Пример #17
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.BERSequence, encoded=m[:4], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BERSequence, encoded=m[:3], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BERSequence, encoded=m[:2], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BERSequence, encoded=m[:1], berdecoder=pureber.BERDecoderContext())
     self.assertRaises(pureber.BERExceptionInsufficientData, pureber.BERSequence, encoded=MutableString(""), berdecoder=pureber.BERDecoderContext())