From 9a5c72e6c9fe513bb802a1a7614108919141d099 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sat, 13 Feb 2016 09:48:44 -0800 Subject: [PATCH] none -> null.. remove prints, add test cases.. [git-p4: depot-paths = "//depot/python/pypasn1/main/": change = 1818] --- pasn1.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pasn1.py b/pasn1.py index 94ec31d..a5d9893 100644 --- a/pasn1.py +++ b/pasn1.py @@ -90,19 +90,20 @@ class ASN1Coder(object): long: 'int', set: 'set', str: 'bytes', - type(None): 'none', + type(None): 'null', unicode: 'unicode', } _tagmap = { '\x01': 'bool', '\x02': 'int', '\x04': 'bytes', - '\x05': 'none', + '\x05': 'null', '\x09': 'float', '\x0c': 'unicode', '\x30': 'list', '\x31': 'set', '\xc0': 'dict', + #'xxx': 'datetime', } _typetag = dict((v, k) for k, v in _tagmap.iteritems()) @@ -144,11 +145,11 @@ class ASN1Coder(object): return bool(self.dec_int(d, pos, end)[0]), end @staticmethod - def enc_none(obj): + def enc_null(obj): return '\x00' @staticmethod - def dec_none(d, pos, end): + def dec_null(d, pos, end): return None, end def enc_dict(self, obj): @@ -257,8 +258,6 @@ class ASN1Coder(object): #elif v & 0b11000000 == 0b01000000: # raise ValueError('invalid encoding') - print 'df:', `d, pos, end` - raise NotImplementedError def dumps(self, obj): @@ -270,7 +269,6 @@ class ASN1Coder(object): tag = data[pos] l, b = _decodelen(data, pos + 1) if len(data) < pos + 1 + b + l: - print `data, pos, end, l, b` raise ValueError('string not long enough') # XXX - enforce that len(data) == end? @@ -320,6 +318,15 @@ class TestCode(unittest.TestCase): v = loads(s) self.assertTrue(math.isnan(v)) + def test_cryptoutilasn1(self): + '''Test DER sequences generated by Crypto.Util.asn1.''' + + for s, v in [ ('\x02\x03$\x8a\xf9', 2394873), + ('\x05\x00', None), + ('\x02\x03\x00\x96I', 38473), + ]: + self.assertEqual(loads(s), v) + def test_dumps(self): for i in [ None, True, False, @@ -333,7 +340,6 @@ class TestCode(unittest.TestCase): o = loads(s) self.assertEqual(i, o) - print 'done' tobj = { 1: 'dflkj', 5: u'sdlkfj', 'float': 1, 'largeint': 1<<342, 'list': [ 1, 2, u'str', 'str' ] } out = dumps(tobj)