Coverage for tests/test_signonetime.py: 100%

24 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 unittest.mock import mock_open, call, patch 

25from signonetime import main 

26import ecdsa 

27import logging 

28 

29logging.disable(logging.CRITICAL) 

30 

31 

32class TestSignOneTime(TestCase): 

33 @patch("signonetime.compute_app_hash") 

34 def test_ok_two_apps(self, compute_app_hash_mock): 

35 compute_app_hash_mock.side_effect = lambda path: (path[0]*32).encode() 

36 

37 with patch("sys.stdout"): 

38 with patch("sys.argv", ["_", 

39 "-a app/path/one.hex,/bin/path/to/two.hex", 

40 "-p /some/where/public.txt"]): 

41 with patch("builtins.open", mock_open()) as open_mock: 

42 with self.assertRaises(SystemExit) as e: 

43 main() 

44 self.assertEqual(e.exception.code, 0) 

45 

46 self.assertEqual([call("/some/where/public.txt", "wb"), 

47 call("app/path/one.hex.sig", "wb"), 

48 call("/bin/path/to/two.hex.sig", "wb")], 

49 open_mock.call_args_list) 

50 write_calls = open_mock.return_value.write.call_args_list 

51 self.assertEqual(3, len(write_calls)) 

52 public_key = ecdsa.VerifyingKey.from_string( 

53 bytes.fromhex(write_calls[0][0][0].decode()), 

54 curve=ecdsa.SECP256k1) 

55 sig_one = bytes.fromhex(write_calls[1][0][0].decode()) 

56 sig_two = bytes.fromhex(write_calls[2][0][0].decode()) 

57 

58 public_key.verify_digest( 

59 sig_one, 

60 b"a"*32, 

61 sigdecode=ecdsa.util.sigdecode_der) # This throws if signature is invalid 

62 

63 public_key.verify_digest( 

64 sig_two, 

65 b"/"*32, 

66 sigdecode=ecdsa.util.sigdecode_der) # This throws if signature is invalid