|
14 | 14 | import pytest |
15 | 15 |
|
16 | 16 | from .keys import VerifyingKey |
| 17 | +from .der import unpem |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class TestVerifyingKeyFromString(unittest.TestCase): |
@@ -97,3 +98,55 @@ def test_bytearray_uncompressed(self): |
97 | 98 | vk = VerifyingKey.from_string(bytearray(b'\x02' + self.key_bytes[:24])) |
98 | 99 |
|
99 | 100 | self.assertEqual(self.vk.to_string(), vk.to_string()) |
| 101 | + |
| 102 | + |
| 103 | +class TestVerifyingKeyFromDer(unittest.TestCase): |
| 104 | + """ |
| 105 | + Verify that ecdsa.keys.VerifyingKey.from_der() can be used with |
| 106 | + bytes-like objects. |
| 107 | + """ |
| 108 | + @classmethod |
| 109 | + def setUpClass(cls): |
| 110 | + prv_key_str = ( |
| 111 | + "-----BEGIN EC PRIVATE KEY-----\n" |
| 112 | + "MF8CAQEEGF7IQgvW75JSqULpiQQ8op9WH6Uldw6xxaAKBggqhkjOPQMBAaE0AzIA\n" |
| 113 | + "BLiBd9CE7xf15FY5QIAoNg+fWbSk1yZOYtoGUdzkejWkxbRc9RWTQjqLVXucIJnz\n" |
| 114 | + "bA==\n" |
| 115 | + "-----END EC PRIVATE KEY-----\n") |
| 116 | + key_str = ( |
| 117 | + "-----BEGIN PUBLIC KEY-----\n" |
| 118 | + "MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEuIF30ITvF/XkVjlAgCg2D59ZtKTX\n" |
| 119 | + "Jk5i2gZR3OR6NaTFtFz1FZNCOotVe5wgmfNs\n" |
| 120 | + "-----END PUBLIC KEY-----\n") |
| 121 | + cls.key_bytes = unpem(key_str) |
| 122 | + assert isinstance(cls.key_bytes, bytes) |
| 123 | + cls.vk = VerifyingKey.from_pem(key_str) |
| 124 | + |
| 125 | + def test_bytes(self): |
| 126 | + vk = VerifyingKey.from_der(self.key_bytes) |
| 127 | + |
| 128 | + self.assertEqual(self.vk.to_string(), vk.to_string()) |
| 129 | + |
| 130 | + @unittest.expectedFailure |
| 131 | + def test_bytes_memoryview(self): |
| 132 | + vk = VerifyingKey.from_der(buffer(self.key_bytes)) |
| 133 | + |
| 134 | + self.assertEqual(self.vk.to_string(), vk.to_string()) |
| 135 | + |
| 136 | + def test_bytearray(self): |
| 137 | + vk = VerifyingKey.from_der(bytearray(self.key_bytes)) |
| 138 | + |
| 139 | + self.assertEqual(self.vk.to_string(), vk.to_string()) |
| 140 | + |
| 141 | + @unittest.expectedFailure |
| 142 | + def test_bytesarray_memoryview(self): |
| 143 | + vk = VerifyingKey.from_der(buffer(bytearray(self.key_bytes))) |
| 144 | + |
| 145 | + self.assertEqual(self.vk.to_string(), vk.to_string()) |
| 146 | + |
| 147 | + @unittest.expectedFailure |
| 148 | + def test_array_array_of_bytes(self): |
| 149 | + arr = array.array('B', self.key_bytes) |
| 150 | + vk = VerifyingKey.from_der(arr) |
| 151 | + |
| 152 | + self.assertEqual(self.vk.to_string(), vk.to_string()) |
0 commit comments