From 15cacbf24b045351e17e66c60cfd2065e278d1c9 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 17 Feb 2016 08:47:25 -0800 Subject: [PATCH] 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] --- pasn1.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/pasn1.py b/pasn1.py index 5bfbb5d..4e58c16 100644 --- a/pasn1.py +++ b/pasn1.py @@ -354,6 +354,9 @@ class ASN1Coder(object): def dec_datetime(self, data, pos, end): ts = data[pos:end] + if ts[-1] != 'Z': + raise ValueError('last character must be Z') + if '.' in ts: fstr = '%Y%m%d%H%M%S.%fZ' if ts.endswith('0Z'): @@ -457,25 +460,15 @@ class TestCode(unittest.TestCase): self.assertEqual(dumps(.15625), '090380fb05'.decode('hex')) 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([ 1, 2, 'sdlkfj' ]) genfailures({ 1: 2, 5: '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): # Add tests for base 8, 16 floats among others @@ -489,9 +482,25 @@ class TestCode(unittest.TestCase): '3007020101020102040673646c6b666a', # list short string still valid 'c007020101020102020105040673646c6b666a', # dict short value still valid '181632303136303231353038343031362e3539303839305a', #datetime w/ trailing zero + '181632303136303231373136343034372e3035343433367a', #datetime w/ lower z ]: 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): '''Test DER sequences generated by Crypto.Util.asn1.'''