Coverage for tests/ledger/hsm2dongle_cmds/test_powhsm_attestation.py: 100%

27 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 ..test_hsm2dongle import TestHSM2DongleBase 

24from ledger.hsm2dongle import HSM2DongleErrorResult, HSM2DongleError 

25from ledgerblue.commException import CommException 

26 

27import logging 

28 

29logging.disable(logging.CRITICAL) 

30 

31 

32class TestPowHsmAttestation(TestHSM2DongleBase): 

33 SIG = "3046022100e4c30ef37a1228a2faf2a88c8fb52a1dfe006a222d0961" \ 

34 "c43792018481d0d5e2022100b206abd9c8a46336f9684a84083613fb" \ 

35 "e4d31c34f7c023e5716545a00a709318" 

36 

37 def test_ok(self): 

38 self.dongle.exchange.side_effect = [ 

39 bytes.fromhex("aabbcc" + self.SIG), 

40 bytes.fromhex("aabbcc01112233445566778899"), 

41 bytes.fromhex("aabbcc00aabbccddeeff"), 

42 bytes.fromhex("aabbcc0112345678"), 

43 bytes.fromhex("aabbcc019abcdef0"), 

44 bytes.fromhex("aabbcc001122334455"), 

45 bytes.fromhex("aabbcc334455667788aabbccdd"), 

46 ] 

47 

48 self.assertEqual({ 

49 "message": "112233445566778899aabbccddeeff", 

50 "envelope": "123456789abcdef01122334455", 

51 "app_hash": "334455667788aabbccdd", 

52 "signature": self.SIG, 

53 }, self.hsm2dongle.get_powhsm_attestation("aa" + "bb"*30 + "cc")) 

54 

55 self.assert_exchange([ 

56 bytes.fromhex("5001aa" + "bb"*30 + "cc"), 

57 bytes.fromhex("500200"), 

58 bytes.fromhex("500201"), 

59 bytes.fromhex("500400"), 

60 bytes.fromhex("500401"), 

61 bytes.fromhex("500402"), 

62 bytes.fromhex("5003"), 

63 ]) 

64 

65 def test_legacy_ok(self): 

66 self.dongle.exchange.side_effect = [ 

67 bytes.fromhex("aabbcc" + self.SIG), 

68 bytes.fromhex("aabbcc") + b"HSM:SIGNER:5.0morestuff", 

69 bytes.fromhex("aabbcc334455667788aabbccdd"), 

70 ] 

71 

72 self.assertEqual({ 

73 "message": b"HSM:SIGNER:5.0morestuff".hex(), 

74 "envelope": b"HSM:SIGNER:5.0morestuff".hex(), 

75 "app_hash": "334455667788aabbccdd", 

76 "signature": self.SIG, 

77 }, self.hsm2dongle.get_powhsm_attestation("aa" + "bb"*30 + "cc")) 

78 

79 self.assert_exchange([ 

80 bytes.fromhex("5001aa" + "bb"*30 + "cc"), 

81 bytes.fromhex("500200"), 

82 bytes.fromhex("5003"), 

83 ]) 

84 

85 def test_error_result(self): 

86 self.dongle.exchange.side_effect = [ 

87 bytes.fromhex("aabbcc" + self.SIG), 

88 bytes.fromhex("aabbcc01112233445566778899"), 

89 bytes.fromhex("aabbcc00aabbccddeeff"), 

90 CommException("an-error-result", 0x6b01) 

91 ] 

92 

93 with self.assertRaises(HSM2DongleErrorResult) as e: 

94 self.hsm2dongle.get_powhsm_attestation("aa" + "bb"*30 + "cc") 

95 self.assertEqual(e.exception.error_code, 0x6b01) 

96 

97 self.assert_exchange([ 

98 bytes.fromhex("5001aa" + "bb"*30 + "cc"), 

99 bytes.fromhex("500200"), 

100 bytes.fromhex("500201"), 

101 bytes.fromhex("500400"), 

102 ]) 

103 

104 def test_exception(self): 

105 self.dongle.exchange.side_effect = [ 

106 bytes.fromhex("aabbcc" + self.SIG), 

107 bytes.fromhex("aabbcc01112233445566778899"), 

108 bytes.fromhex("aabbcc00aabbccddeeff"), 

109 CommException("an-exception") 

110 ] 

111 

112 with self.assertRaises(HSM2DongleError) as e: 

113 self.hsm2dongle.get_powhsm_attestation("aa" + "bb"*30 + "cc") 

114 self.assertIn("an-exception", e.exception.message) 

115 

116 self.assert_exchange([ 

117 bytes.fromhex("5001aa" + "bb"*30 + "cc"), 

118 bytes.fromhex("500200"), 

119 bytes.fromhex("500201"), 

120 bytes.fromhex("500400"), 

121 ])