|
| 1 | +# Copyright (c) 2016 Yubico AB |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or |
| 5 | +# without modification, are permitted provided that the following |
| 6 | +# conditions are met: |
| 7 | +# |
| 8 | +# 1. Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# 2. Redistributions in binary form must reproduce the above |
| 11 | +# copyright notice, this list of conditions and the following |
| 12 | +# disclaimer in the documentation and/or other materials provided |
| 13 | +# with the distribution. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | +# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | +# POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | + |
| 29 | +import os |
| 30 | +import tempfile |
| 31 | +import unittest |
| 32 | +import json |
| 33 | + |
| 34 | +from u2flib_host.utils import websafe_encode |
| 35 | +from u2flib_host.soft import SoftU2FDevice |
| 36 | +from u2flib_host.register import register |
| 37 | +from u2flib_host.authenticate import authenticate |
| 38 | + |
| 39 | + |
| 40 | +VERSION = 'U2F_V2' |
| 41 | +FACET = 'https://example.com' |
| 42 | +CHALLENGE = 'challenge' |
| 43 | +KEY_HANDLE = websafe_encode(b'\0' * 64) |
| 44 | + |
| 45 | +REG_DATA = json.dumps({ |
| 46 | + 'version': VERSION, |
| 47 | + 'challenge': CHALLENGE, |
| 48 | + 'appId': FACET |
| 49 | +}) |
| 50 | + |
| 51 | +AUTH_DATA = json.dumps({ |
| 52 | + 'version': VERSION, |
| 53 | + 'challenge': CHALLENGE, |
| 54 | + 'appId': FACET, |
| 55 | + 'keyHandle': KEY_HANDLE |
| 56 | +}) |
| 57 | + |
| 58 | + |
| 59 | +class TestRegister(unittest.TestCase): |
| 60 | + def setUp(self): |
| 61 | + print('write') |
| 62 | + with tempfile.NamedTemporaryFile(delete=False) as f: |
| 63 | + f.write(json.dumps({"counter": 0, "keys": {}}).encode('utf8')) |
| 64 | + self.device_path = f.name |
| 65 | + |
| 66 | + def tearDown(self): |
| 67 | + os.unlink(self.device_path) |
| 68 | + |
| 69 | + def test_register(self): |
| 70 | + dev = SoftU2FDevice(self.device_path) |
| 71 | + |
| 72 | + resp = register([dev], REG_DATA, FACET) |
| 73 | + self.assertIn('registrationData', resp) |
| 74 | + |
| 75 | + def test_authenticate(self): |
| 76 | + dev = SoftU2FDevice(self.device_path) |
| 77 | + |
| 78 | + try: |
| 79 | + authenticate([dev], AUTH_DATA, FACET, False) |
| 80 | + self.fail('Key handle should not match') |
| 81 | + except ValueError: |
| 82 | + pass |
0 commit comments