Browse Source

none -> null.. remove prints, add test cases..

[git-p4: depot-paths = "//depot/python/pypasn1/main/": change = 1818]
python2
John-Mark Gurney 9 years ago
parent
commit
9a5c72e6c9
1 changed files with 14 additions and 8 deletions
  1. +14
    -8
      pasn1.py

+ 14
- 8
pasn1.py View File

@@ -90,19 +90,20 @@ class ASN1Coder(object):
long: 'int', long: 'int',
set: 'set', set: 'set',
str: 'bytes', str: 'bytes',
type(None): 'none',
type(None): 'null',
unicode: 'unicode', unicode: 'unicode',
} }
_tagmap = { _tagmap = {
'\x01': 'bool', '\x01': 'bool',
'\x02': 'int', '\x02': 'int',
'\x04': 'bytes', '\x04': 'bytes',
'\x05': 'none',
'\x05': 'null',
'\x09': 'float', '\x09': 'float',
'\x0c': 'unicode', '\x0c': 'unicode',
'\x30': 'list', '\x30': 'list',
'\x31': 'set', '\x31': 'set',
'\xc0': 'dict', '\xc0': 'dict',
#'xxx': 'datetime',
} }


_typetag = dict((v, k) for k, v in _tagmap.iteritems()) _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 return bool(self.dec_int(d, pos, end)[0]), end


@staticmethod @staticmethod
def enc_none(obj):
def enc_null(obj):
return '\x00' return '\x00'


@staticmethod @staticmethod
def dec_none(d, pos, end):
def dec_null(d, pos, end):
return None, end return None, end


def enc_dict(self, obj): def enc_dict(self, obj):
@@ -257,8 +258,6 @@ class ASN1Coder(object):
#elif v & 0b11000000 == 0b01000000: #elif v & 0b11000000 == 0b01000000:
# raise ValueError('invalid encoding') # raise ValueError('invalid encoding')


print 'df:', `d, pos, end`

raise NotImplementedError raise NotImplementedError


def dumps(self, obj): def dumps(self, obj):
@@ -270,7 +269,6 @@ class ASN1Coder(object):
tag = data[pos] tag = data[pos]
l, b = _decodelen(data, pos + 1) l, b = _decodelen(data, pos + 1)
if len(data) < pos + 1 + b + l: if len(data) < pos + 1 + b + l:
print `data, pos, end, l, b`
raise ValueError('string not long enough') raise ValueError('string not long enough')


# XXX - enforce that len(data) == end? # XXX - enforce that len(data) == end?
@@ -320,6 +318,15 @@ class TestCode(unittest.TestCase):
v = loads(s) v = loads(s)
self.assertTrue(math.isnan(v)) 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): def test_dumps(self):
for i in [ None, for i in [ None,
True, False, True, False,
@@ -333,7 +340,6 @@ class TestCode(unittest.TestCase):
o = loads(s) o = loads(s)
self.assertEqual(i, o) self.assertEqual(i, o)


print 'done'
tobj = { 1: 'dflkj', 5: u'sdlkfj', 'float': 1, 'largeint': 1<<342, 'list': [ 1, 2, u'str', 'str' ] } tobj = { 1: 'dflkj', 5: u'sdlkfj', 'float': 1, 'largeint': 1<<342, 'list': [ 1, 2, u'str', 'str' ] }


out = dumps(tobj) out = dumps(tobj)


Loading…
Cancel
Save