File: ecdsa_test.py

package info (click to toggle)
jsonpickle 3.0.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,184 kB
  • sloc: python: 6,088; javascript: 654; makefile: 90; sh: 17
file content (52 lines) | stat: -rw-r--r-- 1,399 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Test serializing ecdsa keys"""
from __future__ import absolute_import, division, unicode_literals

import unittest

import pytest
from helper import SkippableTest

import jsonpickle


@pytest.fixture(scope='module', autouse=True)
def gmpy_extension():
    """Initialize the gmpy extension for this test module"""
    jsonpickle.ext.gmpy.register_handlers()
    yield  # control to the test function.
    jsonpickle.ext.gmpy.unregister_handlers()


class EcdsaTestCase(SkippableTest):
    def setUp(self):
        try:
            from ecdsa import NIST384p
            from ecdsa.keys import SigningKey

            self.NIST384p = NIST384p
            self.SigningKey = SigningKey
            self.should_skip = False
        except ImportError:
            self.should_skip = True

    def test_roundtrip(self):
        if self.should_skip:
            return self.skip('ecdsa module is not installed')

        message = 'test'.encode('utf-8')
        key_pair = self.SigningKey.generate(curve=self.NIST384p)
        sig = key_pair.sign(message)

        serialized = jsonpickle.dumps(key_pair.get_verifying_key())
        restored = jsonpickle.loads(serialized)
        self.assertTrue(restored.verify(sig, message))


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(EcdsaTestCase, 'test'))
    return suite


if __name__ == '__main__':
    unittest.main()