Coverage for tests/admin/test_unlock.py: 100%

63 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +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. 

22 

23from types import SimpleNamespace 

24from unittest import TestCase 

25from unittest.mock import Mock, call, patch 

26from admin.misc import AdminError 

27from comm.platform import Platform 

28from admin.unlock import do_unlock 

29from ledger.hsm2dongle import HSM2Dongle 

30from parameterized import parameterized 

31 

32import logging 

33 

34logging.disable(logging.CRITICAL) 

35 

36 

37@patch("sys.stdout.write") 

38@patch("admin.unlock.get_hsm") 

39class TestUnlock(TestCase): 

40 def setUp(self): 

41 Platform.set(Platform.LEDGER) 

42 self.valid_pin = '1234ABCD' 

43 self.invalid_pin = '123456789' 

44 

45 options = { 

46 'pin': self.valid_pin, 

47 'any_pin': False, 

48 'no_exec': None, 

49 'verbose': False 

50 } 

51 self.default_options = SimpleNamespace(**options) 

52 self.dongle = Mock() 

53 

54 @patch("admin.unlock.info") 

55 def test_unlock(self, info_mock, get_hsm, _): 

56 get_hsm.return_value = self.dongle 

57 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER) 

58 self.dongle.is_onboarded = Mock(return_value=True) 

59 

60 do_unlock(self.default_options) 

61 self.assertEqual(call('PIN accepted'), info_mock.call_args_list[7]) 

62 

63 def test_unlock_invalid_pin(self, get_hsm, _): 

64 get_hsm.return_value = self.dongle 

65 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER) 

66 self.dongle.is_onboarded = Mock(return_value=True) 

67 

68 options = self.default_options 

69 options.pin = self.invalid_pin 

70 with self.assertRaises(AdminError) as e: 

71 do_unlock(options) 

72 self.assertTrue(str(e.exception).startswith('Invalid pin given.')) 

73 

74 def test_unlock_not_onboarded(self, get_hsm, _): 

75 get_hsm.return_value = self.dongle 

76 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER) 

77 self.dongle.is_onboarded = Mock(return_value=False) 

78 

79 with self.assertRaises(AdminError) as e: 

80 do_unlock(self.default_options) 

81 self.assertEqual('Device not onboarded', str(e.exception)) 

82 

83 @parameterized.expand([ 

84 (HSM2Dongle.MODE.SIGNER, ), 

85 (HSM2Dongle.MODE.UI_HEARTBEAT, ), 

86 ]) 

87 def test_unlock_invalid_mode(self, get_hsm, _, mode): 

88 get_hsm.return_value = self.dongle 

89 self.dongle.get_current_mode = Mock(return_value=mode) 

90 self.dongle.is_onboarded = Mock(return_value=True) 

91 

92 with self.assertRaises(AdminError) as e: 

93 do_unlock(self.default_options) 

94 self.assertEqual('Device already unlocked', str(e.exception)) 

95 

96 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.UNKNOWN) 

97 with self.assertRaises(AdminError) as e: 

98 do_unlock(self.default_options) 

99 

100 self.assertTrue(str(e.exception).startswith('Device mode unknown.')) 

101 

102 def test_unlock_wrong_pin(self, get_hsm, _): 

103 get_hsm.return_value = self.dongle 

104 self.dongle.get_current_mode = Mock(return_value=HSM2Dongle.MODE.BOOTLOADER) 

105 self.dongle.is_onboarded = Mock(return_value=True) 

106 self.dongle.unlock = Mock(return_value=False) 

107 

108 with self.assertRaises(AdminError) as e: 

109 do_unlock(self.default_options) 

110 self.assertEqual('Unable to unlock: PIN mismatch', str(e.exception))