-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure that the encoding is actually the minimal one for length and i…
…nteger
- Loading branch information
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
import unittest | ||
from .der import remove_integer, UnexpectedDER, read_length | ||
from six import b | ||
|
||
class TestRemoveInteger(unittest.TestCase): | ||
# DER requires the integers to be 0-padded only if they would be | ||
# interpreted as negative, check if those errors are detected | ||
def test_non_minimal_encoding(self): | ||
with self.assertRaises(UnexpectedDER): | ||
remove_integer(b('\x02\x02\x00\x01')) | ||
|
||
def test_negative_with_high_bit_set(self): | ||
with self.assertRaises(UnexpectedDER): | ||
remove_integer(b('\x02\x01\x80')) | ||
|
||
def test_minimal_with_high_bit_set(self): | ||
val, rem = remove_integer(b('\x02\x02\x00\x80')) | ||
|
||
self.assertEqual(val, 0x80) | ||
self.assertFalse(rem) | ||
|
||
def test_two_zero_bytes_with_high_bit_set(self): | ||
with self.assertRaises(UnexpectedDER): | ||
remove_integer(b('\x02\x03\x00\x00\xff')) | ||
|
||
def test_zero_length_integer(self): | ||
with self.assertRaises(UnexpectedDER): | ||
remove_integer(b('\x02\x00')) | ||
|
||
def test_empty_string(self): | ||
with self.assertRaises(UnexpectedDER): | ||
remove_integer(b('')) | ||
|
||
|
||
class TestReadLength(unittest.TestCase): | ||
# DER requires the lengths between 0 and 127 to be encoded using the short | ||
# form and lengths above that encoded with minimal number of bytes | ||
# necessary | ||
def test_zero_length(self): | ||
self.assertEqual((0, 1), read_length(b('\x00'))) | ||
|
||
def test_two_byte_zero_length(self): | ||
with self.assertRaises(UnexpectedDER): | ||
read_length(b('\x81\x00')) | ||
|
||
def test_two_byte_small_length(self): | ||
with self.assertRaises(UnexpectedDER): | ||
read_length(b('\x81\x7f')) | ||
|
||
def test_long_form_with_zero_length(self): | ||
with self.assertRaises(UnexpectedDER): | ||
read_length(b('\x80')) | ||
|
||
def test_smallest_two_byte_length(self): | ||
self.assertEqual((128, 2), read_length(b('\x81\x80'))) | ||
|
||
def test_zero_padded_length(self): | ||
with self.assertRaises(UnexpectedDER): | ||
read_length(b('\x82\x00\x80')) | ||
|
||
def test_two_three_byte_length(self): | ||
self.assertEqual((256, 3), read_length(b'\x82\x01\x00')) |