Coverage for tests/admin/test_changepin.py: 100%
65 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-05 20:41 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-05 20:41 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2021 RSK Labs Ltd
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy of
6# this software and associated documentation files (the "Software"), to deal in
7# the Software without restriction, including without limitation the rights to
8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9# of the Software, and to permit persons to whom the Software is furnished to do
10# so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23from types import SimpleNamespace
24from unittest import TestCase
25from unittest.mock import Mock, call, patch
26from admin.changepin import do_changepin
27from admin.misc import AdminError
28from ledger.hsm2dongle import HSM2Dongle
30import logging
32logging.disable(logging.CRITICAL)
35@patch("sys.stdout.write")
36@patch("admin.changepin.get_hsm")
37class TestChangepin(TestCase):
38 VALID_PIN = '1234ABCD'
39 INVALID_PIN = '123456789'
41 def setUp(self):
42 options = {
43 'new_pin': self.VALID_PIN,
44 'no_unlock': False,
45 'any_pin': False,
46 'verbose': False,
47 'pin': self.VALID_PIN
48 }
49 self.default_options = SimpleNamespace(**options)
50 self.dongle = Mock()
52 @patch("admin.changepin.do_unlock")
53 def test_changepin(self, do_unlock_mock, get_hsm, _):
54 get_hsm.return_value = self.dongle
55 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER)
56 self.dongle.is_onboarded = Mock(return_value=True)
57 self.dongle.new_pin = Mock(return_value=True)
59 do_changepin(self.default_options)
61 self.assertTrue(do_unlock_mock.called)
62 self.assertTrue(self.dongle.new_pin.called)
63 self.assertEqual([call(self.VALID_PIN.encode())],
64 self.dongle.new_pin.call_args_list)
66 @patch("admin.changepin.do_unlock")
67 def test_changepin_unlock_error(self, do_unlock_mock, get_hsm, _):
68 get_hsm.return_value = self.dongle
69 do_unlock_mock.side_effect = Exception('unlock-error')
70 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER)
71 self.dongle.is_onboarded = Mock(return_value=True)
72 self.dongle.new_pin = Mock(return_value=True)
74 with self.assertRaises(AdminError) as e:
75 do_changepin(self.default_options)
77 self.assertEqual('Failed to unlock device: unlock-error', str(e.exception))
79 @patch("admin.changepin.do_unlock")
80 def test_changepin_invalid_mode(self, do_unlock_mock, get_hsm, _):
81 get_hsm.return_value = self.dongle
82 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.SIGNER)
83 self.dongle.is_onboarded = Mock(return_value=True)
84 self.dongle.new_pin = Mock(return_value=True)
86 with self.assertRaises(AdminError) as e:
87 do_changepin(self.default_options)
89 self.assertTrue(do_unlock_mock.called)
90 self.assertTrue(str(e.exception).startswith('Device not in bootloader mode.'))
91 self.assertFalse(self.dongle.new_pin.called)
93 def test_changepin_invalid_pin(self, get_hsm, _):
94 get_hsm.return_value = self.dongle
96 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER)
97 self.dongle.is_onboarded = Mock(return_value=True)
98 self.dongle.new_pin = Mock(return_value=True)
100 options = self.default_options
101 options.new_pin = self.INVALID_PIN
102 with self.assertRaises(AdminError) as e:
103 do_changepin(options)
105 self.assertTrue(str(e.exception).startswith('Invalid pin given.'))
106 self.assertFalse(self.dongle.new_pin.called)
108 @patch("admin.changepin.do_unlock")
109 def test_changepin_newpin_error(self, do_unlock_mock, get_hsm, _):
110 get_hsm.return_value = self.dongle
111 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER)
112 self.dongle.is_onboarded = Mock(return_value=True)
113 self.dongle.new_pin = Mock(return_value=False)
115 with self.assertRaises(AdminError) as e:
116 do_changepin(self.default_options)
118 self.assertTrue(do_unlock_mock.called)
119 self.assertEqual('Failed to change pin', str(e.exception))