Browse Source

make sure datetime strings end in Z when decoding..

move some code close and add a comment to the fuzzing test that
detected failures should be added to test_invalids..

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

+ 24
- 15
pasn1.py View File

@@ -354,6 +354,9 @@ class ASN1Coder(object):


def dec_datetime(self, data, pos, end): def dec_datetime(self, data, pos, end):
ts = data[pos:end] ts = data[pos:end]
if ts[-1] != 'Z':
raise ValueError('last character must be Z')

if '.' in ts: if '.' in ts:
fstr = '%Y%m%d%H%M%S.%fZ' fstr = '%Y%m%d%H%M%S.%fZ'
if ts.endswith('0Z'): if ts.endswith('0Z'):
@@ -457,25 +460,15 @@ class TestCode(unittest.TestCase):
self.assertEqual(dumps(.15625), '090380fb05'.decode('hex')) self.assertEqual(dumps(.15625), '090380fb05'.decode('hex'))


def test_fuzzing(self): def test_fuzzing(self):
# Make sure that when a failure is detected here, that it
# gets added to test_invalids, so that this function may be
# disabled.
genfailures(float(1)) genfailures(float(1))
genfailures([ 1, 2, 'sdlkfj' ]) genfailures([ 1, 2, 'sdlkfj' ])
genfailures({ 1: 2, 5: 'sdlkfj' }) genfailures({ 1: 2, 5: 'sdlkfj' })
genfailures(set([ 1, 2, 'sdlkfj' ])) genfailures(set([ 1, 2, 'sdlkfj' ]))

def test_consume(self):
b = dumps(5)
self.assertRaises(ValueError, loads, b + '398473', consume=True)

# XXX - still possible that an internal data member
# doesn't consume all

# XXX - test that sets are ordered properly
# XXX - test that dicts are ordered properly..

def test_nan(self):
s = dumps(float('nan'))
v = loads(s)
self.assertTrue(math.isnan(v))
genfailures(True)
genfailures(datetime.datetime.utcnow())


def test_invalids(self): def test_invalids(self):
# Add tests for base 8, 16 floats among others # Add tests for base 8, 16 floats among others
@@ -489,9 +482,25 @@ class TestCode(unittest.TestCase):
'3007020101020102040673646c6b666a', # list short string still valid '3007020101020102040673646c6b666a', # list short string still valid
'c007020101020102020105040673646c6b666a', # dict short value still valid 'c007020101020102020105040673646c6b666a', # dict short value still valid
'181632303136303231353038343031362e3539303839305a', #datetime w/ trailing zero '181632303136303231353038343031362e3539303839305a', #datetime w/ trailing zero
'181632303136303231373136343034372e3035343433367a', #datetime w/ lower z
]: ]:
self.assertRaises(ValueError, loads, v.decode('hex')) self.assertRaises(ValueError, loads, v.decode('hex'))


def test_consume(self):
b = dumps(5)
self.assertRaises(ValueError, loads, b + '398473', consume=True)

# XXX - still possible that an internal data member
# doesn't consume all

# XXX - test that sets are ordered properly
# XXX - test that dicts are ordered properly..

def test_nan(self):
s = dumps(float('nan'))
v = loads(s)
self.assertTrue(math.isnan(v))

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




Loading…
Cancel
Save