Skip to content

Commit

Permalink
Constraint integer PER encoding and decoding fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Apr 7, 2018
1 parent 0b115d6 commit 64b158d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion asn1tools/__init__.py
Expand Up @@ -29,7 +29,7 @@


__author__ = 'Erik Moqvist'
__version__ = '0.68.0'
__version__ = '0.69.0'


class ArgumentParserError(Exception):
Expand Down
10 changes: 7 additions & 3 deletions asn1tools/codecs/per.py
Expand Up @@ -552,8 +552,10 @@ def encode(self, data, encoder):
encoder.align()
encoder.append_unconstrained_whole_number(data)
else:
encoder.append_non_negative_binary_integer(data - self.minimum,
self.number_of_bits)
encoder.append_constrained_whole_number(data,
self.minimum,
self.maximum,
self.number_of_bits)

def decode(self, decoder):
if self.has_extension_marker:
Expand All @@ -567,7 +569,9 @@ def decode(self, decoder):
length = decoder.read_length_determinant()
value = decoder.read_unconstrained_whole_number(length)
else:
value = decoder.read_integer(self.number_of_bits)
value = decoder.read_constrained_whole_number(self.minimum,
self.maximum,
self.number_of_bits)
value += self.minimum

return value
Expand Down
57 changes: 56 additions & 1 deletion tests/test_per.py
Expand Up @@ -62,6 +62,52 @@ def test_decode_length(self):
self.assertEqual(str(cm.exception),
': Decode length is not supported for this codec.')

def test_versions(self):
foo = asn1tools.compile_files('tests/files/versions.asn', 'per')

# Encode as V1, decode as V1, V2 and V3
decoded_v1 = {
'userName': 'myUserName',
'password': 'myPassword',
'accountNumber': 54224445
}

encoded_v1 = foo.encode('V1', decoded_v1)

self.assertEqual(foo.decode('V1', encoded_v1), decoded_v1)
self.assertEqual(foo.decode('V2', encoded_v1), decoded_v1)
self.assertEqual(foo.decode('V3', encoded_v1), decoded_v1)

# Encode as V2, decode as V1, V2 and V3
decoded_v2 = {
'userName': 'myUserName',
'password': 'myPassword',
'accountNumber': 54224445,
'minutesLastLoggedIn': 5
}

encoded_v2 = foo.encode('V2', decoded_v2)

self.assertEqual(foo.decode('V1', encoded_v2), decoded_v1)
self.assertEqual(foo.decode('V2', encoded_v2), decoded_v2)
self.assertEqual(foo.decode('V3', encoded_v2), decoded_v2)

# Encode as V3, decode as V1, V2 and V3
decoded_v3 = {
'userName': 'myUserName',
'password': 'myPassword',
'accountNumber': 54224445,
'minutesLastLoggedIn': 5,
'certificate': None,
'thumb': None
}

encoded_v3 = foo.encode('V3', decoded_v3)

self.assertEqual(foo.decode('V1', encoded_v3), decoded_v1)
self.assertEqual(foo.decode('V2', encoded_v3), decoded_v2)
self.assertEqual(foo.decode('V3', encoded_v3), decoded_v3)

def test_x691_a1(self):
a1 = asn1tools.compile_files('tests/files/x691_a1.asn', 'per')

Expand Down Expand Up @@ -528,6 +574,12 @@ def test_integer(self):
"BEGIN "
"A ::= INTEGER "
"B ::= INTEGER (5..99) "
"C ::= SEQUENCE { "
" a BOOLEAN, "
" b INTEGER, "
" c BOOLEAN, "
" d INTEGER (-10..400) "
"} "
"END",
'per')

Expand All @@ -549,7 +601,10 @@ def test_integer(self):
('A', -32769, b'\x03\xff\x7f\xff'),
('B', 5, b'\x00'),
('B', 6, b'\x02'),
('B', 99, b'\xbc')
('B', 99, b'\xbc'),
('C',
{'a': True, 'b': 43554344223, 'c': False, 'd': -9},
b'\x80\x05\x0a\x24\x0a\x8d\x1f\x00\x00\x01')
]

for type_name, decoded, encoded in datas:
Expand Down

0 comments on commit 64b158d

Please sign in to comment.