Skip to content

Commit 7a8237e

Browse files
committed
test: VerifyingKey.from_string bytes-like
Document what inputs don't work in which python versions
1 parent 5b9a787 commit 7a8237e

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

src/ecdsa/test_keys.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
try:
2+
import unittest2 as unittest
3+
except ImportError:
4+
import unittest
5+
6+
try:
7+
buffer
8+
except NameError:
9+
buffer = memoryview
10+
11+
import array
12+
import six
13+
import sys
14+
import pytest
15+
16+
from .keys import VerifyingKey
17+
18+
19+
class TestVerifyingKeyFromString(unittest.TestCase):
20+
"""
21+
Verify that ecdsa.keys.VerifyingKey.from_string() can be used with
22+
bytes-like objects
23+
"""
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.key_bytes = (b'\x04L\xa2\x95\xdb\xc7Z\xd7\x1f\x93\nz\xcf\x97\xcf'
28+
b'\xd7\xc2\xd9o\xfe8}X!\xae\xd4\xfah\xfa^\rpI\xba\xd1'
29+
b'Y\xfb\x92xa\xebo+\x9cG\xfav\xca')
30+
cls.vk = VerifyingKey.from_string(cls.key_bytes)
31+
32+
def test_bytes(self):
33+
self.assertIsNotNone(self.vk)
34+
self.assertIsInstance(self.vk, VerifyingKey)
35+
self.assertEqual(
36+
self.vk.pubkey.point.x(),
37+
105419898848891948935835657980914000059957975659675736097)
38+
self.assertEqual(
39+
self.vk.pubkey.point.y(),
40+
4286866841217412202667522375431381222214611213481632495306)
41+
42+
def test_bytes_memoryview(self):
43+
vk = VerifyingKey.from_string(buffer(self.key_bytes))
44+
45+
self.assertEqual(self.vk.to_string(), vk.to_string())
46+
47+
@unittest.skipIf(sys.version_info >= (2, 7), "fails on python2.6 only")
48+
@unittest.expectedFailure
49+
def test_bytearray26(self):
50+
vk = VerifyingKey.from_string(bytearray(self.key_bytes))
51+
52+
self.assertEqual(self.vk.to_string(), vk.to_string())
53+
54+
@unittest.skipUnless(sys.version_info >= (2, 7), "fails on python2.6 only")
55+
def test_bytearray(self):
56+
vk = VerifyingKey.from_string(bytearray(self.key_bytes))
57+
58+
self.assertEqual(self.vk.to_string(), vk.to_string())
59+
60+
def test_bytesarray_memoryview(self):
61+
vk = VerifyingKey.from_string(buffer(bytearray(self.key_bytes)))
62+
63+
self.assertEqual(self.vk.to_string(), vk.to_string())
64+
65+
def test_array_array_of_bytes(self):
66+
arr = array.array('B', self.key_bytes)
67+
vk = VerifyingKey.from_string(arr)
68+
69+
self.assertEqual(self.vk.to_string(), vk.to_string())
70+
71+
def test_array_array_of_bytes_memoryview(self):
72+
arr = array.array('B', self.key_bytes)
73+
vk = VerifyingKey.from_string(buffer(arr))
74+
75+
self.assertEqual(self.vk.to_string(), vk.to_string())
76+
77+
@unittest.expectedFailure
78+
def test_array_array_of_ints(self):
79+
arr = array.array('I', self.key_bytes)
80+
vk = VerifyingKey.from_string(arr)
81+
82+
self.assertEqual(self.vk.to_string(), vk.to_string())
83+
84+
@unittest.skipIf(sys.version_info >= (3, 0), "Fails on python3")
85+
def test_array_array_of_ints_memoryview2(self):
86+
arr = array.array('I', self.key_bytes)
87+
vk = VerifyingKey.from_string(buffer(arr))
88+
89+
self.assertEqual(self.vk.to_string(), vk.to_string())
90+
91+
@unittest.expectedFailure
92+
@unittest.skipUnless(sys.version_info >= (3, 0), "Fails on python3")
93+
def test_array_array_of_ints_memoryview(self):
94+
arr = array.array('I', self.key_bytes)
95+
vk = VerifyingKey.from_string(buffer(arr))
96+
97+
self.assertEqual(self.vk.to_string(), vk.to_string())

0 commit comments

Comments
 (0)