Coverage for tests/comm/test_protocol_v1.py: 100%

52 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 unittest import TestCase 

24from comm.protocol_v1 import HSM1Protocol 

25 

26import logging 

27 

28logging.disable(logging.CRITICAL) 

29 

30 

31class TestHSM1Protocol(TestCase): 

32 def setUp(self): 

33 self.protocol = HSM1Protocol() 

34 

35 def test_format_error(self): 

36 self.assertEqual(self.protocol.format_error(), {"errorcode": -2}) 

37 

38 def test_format_error_type(self): 

39 self.assertEqual( 

40 self.protocol.handle_request('"{"not": "json", "only": "astring"}"'), 

41 {"errorcode": -2}, 

42 ) 

43 

44 def test_invalid_request(self): 

45 self.assertEqual(self.protocol.handle_request({"any": "thing"}), 

46 {"errorcode": -2}) 

47 

48 def test_invalid_request_no_command(self): 

49 self.assertEqual(self.protocol.handle_request({"version": 1}), {"errorcode": -2}) 

50 

51 def test_invalid_request_no_version(self): 

52 self.assertEqual(self.protocol.handle_request({"command": "sign"}), 

53 {"errorcode": -2}) 

54 

55 def test_wrong_version(self): 

56 self.assertEqual( 

57 self.protocol.handle_request({ 

58 "version": "somethingelse", 

59 "command": "whatever" 

60 }), 

61 {"errorcode": -666}, 

62 ) 

63 

64 def test_invalid_command(self): 

65 self.assertEqual( 

66 self.protocol.handle_request({ 

67 "command": "invalid", 

68 "version": 1 

69 }), 

70 {"errorcode": -2}, 

71 ) 

72 

73 def test_version(self): 

74 self.assertEqual( 

75 self.protocol.handle_request({"command": "version"}), 

76 { 

77 "errorcode": 0, 

78 "version": 1 

79 }, 

80 ) 

81 

82 def test_initialize_device_notimplemented(self): 

83 with self.assertRaises(NotImplementedError): 

84 self.protocol.initialize_device() 

85 

86 def test_getpubkey_keyId_presence(self): 

87 self.assertEqual( 

88 self.protocol.handle_request({ 

89 "command": "getPubKey", 

90 "version": 1 

91 }), 

92 {"errorcode": -2}, 

93 ) 

94 

95 def test_getpubkey_keyId_notastring(self): 

96 self.assertEqual( 

97 self.protocol.handle_request({ 

98 "command": "getPubKey", 

99 "version": 1, 

100 "keyId": 123 

101 }), 

102 {"errorcode": -2}, 

103 ) 

104 

105 def test_getpubkey_keyId_invalid(self): 

106 self.assertEqual( 

107 self.protocol.handle_request({ 

108 "command": "getPubKey", 

109 "version": 1, 

110 "keyId": "not-a-key-id" 

111 }), 

112 {"errorcode": -2}, 

113 ) 

114 

115 def test_getpubkey_notimplemented(self): 

116 with self.assertRaises(NotImplementedError): 

117 self.protocol.handle_request({ 

118 "command": "getPubKey", 

119 "version": 1, 

120 "keyId": "m/0/0/0/0/0" 

121 }) 

122 

123 def test_sign_keyId_presence(self): 

124 self.assertEqual( 

125 self.protocol.handle_request({ 

126 "version": 1, 

127 "command": "sign" 

128 }), 

129 {"errorcode": -2}, 

130 ) 

131 

132 def test_sign_keyId_not_a_string(self): 

133 self.assertEqual( 

134 self.protocol.handle_request({ 

135 "version": 1, 

136 "command": "sign", 

137 "keyId": 1234 

138 }), 

139 {"errorcode": -2}, 

140 ) 

141 

142 def test_sign_keyId_invalid(self): 

143 self.assertEqual( 

144 self.protocol.handle_request({ 

145 "version": 1, 

146 "command": "sign", 

147 "keyId": "not-a-key-id" 

148 }), 

149 {"errorcode": -2}, 

150 ) 

151 

152 def test_sign_message_presence(self): 

153 self.assertEqual( 

154 self.protocol.handle_request({ 

155 "version": 1, 

156 "command": "sign", 

157 "keyId": "m/0/0/0/0/0" 

158 }), 

159 {"errorcode": -2}, 

160 ) 

161 

162 def test_sign_message_notstring(self): 

163 self.assertEqual( 

164 self.protocol.handle_request({ 

165 "version": 1, 

166 "command": "sign", 

167 "keyId": "m/0/0/0/0/0", 

168 "message": 123 

169 }), 

170 {"errorcode": -2}, 

171 ) 

172 

173 def test_sign_message_not_hex(self): 

174 self.assertEqual( 

175 self.protocol.handle_request({ 

176 "version": 1, 

177 "command": "sign", 

178 "keyId": "m/0/0/0/0/0", 

179 "message": "not-a-hex", 

180 }), 

181 {"errorcode": -2}, 

182 ) 

183 

184 def test_sign_message_not_longenough(self): 

185 self.assertEqual( 

186 self.protocol.handle_request({ 

187 "version": 1, 

188 "command": "sign", 

189 "keyId": "m/0/0/0/0/0", 

190 "message": "aabbccddee", 

191 }), 

192 {"errorcode": -2}, 

193 ) 

194 

195 def test_sign_notimplemented(self): 

196 with self.assertRaises(NotImplementedError): 

197 self.protocol.handle_request({ 

198 "version": 1, 

199 "command": "sign", 

200 "keyId": "m/0/0/0/0/0", 

201 "message": "bb"*32, 

202 })