Coverage for tests/admin/test_lbutils.py: 99%
83 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-07-10 13:43 +0000
« 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.
23from unittest import TestCase
24from unittest.mock import call, patch
25from lbutils import main
27import logging
29logging.disable(logging.CRITICAL)
32@patch("runpy.run_module")
33class TestLbutils(TestCase):
34 def test_load(self, run_module):
35 with patch('sys.argv', ['lbutils.py', 'load']):
36 with self.assertRaises(SystemExit) as e:
37 main()
39 self.assertTrue(run_module.called)
40 self.assertEqual([call('ledgerblue.loadApp', run_name='__main__')],
41 run_module.call_args_list)
42 self.assertEqual(e.exception.code, 0)
44 def test_delete(self, run_module):
45 with patch('sys.argv', ['lbutils.py', 'delete']):
46 with self.assertRaises(SystemExit) as e:
47 main()
49 self.assertTrue(run_module.called)
50 self.assertEqual([call('ledgerblue.deleteApp', run_name='__main__')],
51 run_module.call_args_list)
52 self.assertEqual(e.exception.code, 0)
54 def test_setup_ca(self, run_module):
55 with patch('sys.argv', ['lbutils.py', 'setupCA']):
56 with self.assertRaises(SystemExit) as e:
57 main()
59 self.assertTrue(run_module.called)
60 self.assertEqual([call('ledgerblue.setupCustomCA', run_name='__main__')],
61 run_module.call_args_list)
62 self.assertEqual(e.exception.code, 0)
64 def test_reset_ca(self, run_module):
65 with patch('sys.argv', ['lbutils.py', 'resetCA']):
66 with self.assertRaises(SystemExit) as e:
67 main()
69 self.assertTrue(run_module.called)
70 self.assertEqual([call('ledgerblue.resetCustomCA', run_name='__main__')],
71 run_module.call_args_list)
72 self.assertEqual(e.exception.code, 0)
74 def test_gen_ca(self, run_module):
75 with patch('sys.argv', ['lbutils.py', 'genCA']):
76 with self.assertRaises(SystemExit) as e:
77 main()
79 self.assertTrue(run_module.called)
80 self.assertEqual([call('ledgerblue.genCAPair', run_name='__main__')],
81 run_module.call_args_list)
82 self.assertEqual(e.exception.code, 0)
84 def test_list_apps_no_apps(self, run_module):
85 def run_module_mock(module, run_name):
86 print("Generated random root public key : b'123456789'")
87 print("Using test master key b'123456789'")
88 print("Using ephemeral key b'987654321'")
89 print("Broken certificate chain - loading from user key")
91 run_module.side_effect = run_module_mock
93 with patch('sys.argv', ['lbutils.py', 'listApps']):
94 with patch('sys.stdout.write') as stdout_mock:
95 with self.assertRaises(SystemExit) as e:
96 main()
97 self.assertFalse(stdout_mock.called)
99 self.assertTrue(run_module.called)
100 self.assertEqual([call('ledgerblue.listApps', run_name='__main__')],
101 run_module.call_args_list)
102 self.assertEqual(e.exception.code, 0)
104 def test_list_apps_with_apps(self, run_module):
105 def run_module_mock(module, run_name):
106 print("Generated random root public key : b'123456789'")
107 print("Using test master key b'123456789'")
108 print("Using ephemeral key b'987654321'")
109 print("Broken certificate chain - loading from user key")
110 print("[{'name': 'first app name', 'flags': 1234, 'hash': '01020304'},"
111 " {'name': 'second app name', 'flags': 1234, 'hash': '05060708'}]")
113 run_module.side_effect = run_module_mock
115 with patch('sys.argv', ['lbutils.py', 'listApps']):
116 with patch('sys.stdout.write') as stdout_mock:
117 with self.assertRaises(SystemExit) as e:
118 main()
119 self.assertEqual([call('first app name\nsecond app name'), call('\n')],
120 stdout_mock.call_args_list)
122 self.assertTrue(run_module.called)
123 self.assertEqual([call('ledgerblue.listApps', run_name='__main__')],
124 run_module.call_args_list)
125 self.assertEqual(e.exception.code, 0)
127 def test_list_apps_error(self, run_module):
128 run_module.side_effect = Exception('error-msg')
130 with patch('sys.argv', ['lbutils.py', 'listApps']):
131 with patch('sys.stdout.write') as stdout_mock:
132 with self.assertRaises(SystemExit) as e:
133 main()
134 self.assertEqual([call('Error: error-msg'), call('\n')],
135 stdout_mock.call_args_list)
137 self.assertTrue(run_module.called)
138 self.assertEqual([call('ledgerblue.listApps', run_name='__main__')],
139 run_module.call_args_list)
140 self.assertEqual(e.exception.code, 1)