Coverage for tests/admin/test_ledger_utils.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 patch, call, Mock 

25from admin.ledger_utils import compute_app_hash, encode_eth_message 

26from hashlib import sha256 

27 

28import logging 

29 

30logging.disable(logging.CRITICAL) 

31 

32 

33class TestComputeAppHash(TestCase): 

34 @patch("admin.ledger_utils.IntelHexParser") 

35 def test_multiple_areas(self, ihp_mock): 

36 ihp_mock.return_value.getAreas.return_value = [ 

37 Mock(data=b"123"), Mock(data=b"456"), Mock(data=b"789")] 

38 

39 hash = compute_app_hash("/a/path") 

40 

41 self.assertEqual([call("/a/path")], ihp_mock.call_args_list) 

42 self.assertEqual(hash, sha256(b"123456789").digest()) 

43 

44 @patch("admin.ledger_utils.IntelHexParser") 

45 def test_no_areas(self, ihp_mock): 

46 ihp_mock.return_value.getAreas.return_value = [] 

47 

48 hash = compute_app_hash("/a/path") 

49 

50 self.assertEqual([call("/a/path")], ihp_mock.call_args_list) 

51 self.assertEqual(hash, sha256().digest()) 

52 

53 

54class TestEncodeEthMessage(TestCase): 

55 def test_str(self): 

56 self.assertEqual(b"\x19Ethereum Signed Message:\n5hello", 

57 encode_eth_message("hello")) 

58 

59 def test_hash(self): 

60 self.assertEqual( 

61 b"\x19Ethereum Signed Message:\n660xbe5fd059b11047155e6e8018ab2f4337c84a347e" 

62 b"015d29cc1ab8e6dde8250395", 

63 encode_eth_message("0xbe5fd059b11047155e6e8018ab2f4337c84a347e015d29cc1a" 

64 "b8e6dde8250395"))