Browse Source

enforce all ones for true bool...

test long strings...

test for invalid bool..

[git-p4: depot-paths = "//depot/python/pypasn1/main/": change = 1819]
python2
John-Mark Gurney 8 years ago
parent
commit
0c8e0e355c
1 changed files with 19 additions and 4 deletions
  1. +19
    -4
      pasn1.py

+ 19
- 4
pasn1.py View File

@@ -9,8 +9,9 @@
# MUST not be used. The shorted form of length encoding MUST be used. # MUST not be used. The shorted form of length encoding MUST be used.
# A longer length encoding MUST be rejected. # A longer length encoding MUST be rejected.


import pdb
import math import math
import os
import pdb
import sys import sys
import unittest import unittest


@@ -139,10 +140,14 @@ class ASN1Coder(object):


@staticmethod @staticmethod
def enc_bool(obj): def enc_bool(obj):
return '\x01' + chr(obj)
return '\x01' + ('\xff' if obj else '\x00')


def dec_bool(self, d, pos, end): def dec_bool(self, d, pos, end):
return bool(self.dec_int(d, pos, end)[0]), end
v = self.dec_int(d, pos, end)[0]
if v not in (-1, 0):
raise ValueError('invalid bool value: %d' % v)

return bool(v), end


@staticmethod @staticmethod
def enc_null(obj): def enc_null(obj):
@@ -302,7 +307,7 @@ class TestCode(unittest.TestCase):
self.assertEqual(dumps(256), '02020100'.decode('hex')) self.assertEqual(dumps(256), '02020100'.decode('hex'))


self.assertEqual(dumps(False), '010100'.decode('hex')) self.assertEqual(dumps(False), '010100'.decode('hex'))
self.assertEqual(dumps(True), '010101'.decode('hex'))
self.assertEqual(dumps(True), '0101ff'.decode('hex'))


self.assertEqual(dumps(None), '0500'.decode('hex')) self.assertEqual(dumps(None), '0500'.decode('hex'))


@@ -318,6 +323,10 @@ class TestCode(unittest.TestCase):
v = loads(s) v = loads(s)
self.assertTrue(math.isnan(v)) self.assertTrue(math.isnan(v))


def test_invalids(self):
for v in [ '010101', ]:
self.assertRaises(ValueError, loads, v.decode('hex'))

def test_cryptoutilasn1(self): def test_cryptoutilasn1(self):
'''Test DER sequences generated by Crypto.Util.asn1.''' '''Test DER sequences generated by Crypto.Util.asn1.'''


@@ -327,6 +336,12 @@ class TestCode(unittest.TestCase):
]: ]:
self.assertEqual(loads(s), v) self.assertEqual(loads(s), v)


def test_longstrings(self):
for i in (203, 65484):
s = os.urandom(i)
v = dumps(s)
self.assertEqual(loads(v), s)

def test_dumps(self): def test_dumps(self):
for i in [ None, for i in [ None,
True, False, True, False,


Loading…
Cancel
Save