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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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())
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())