1# SPDX-License-Identifier: Apache-2.0 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import pytest 16 17from click.testing import CliRunner 18from imgtool.main import imgtool 19from imgtool import imgtool_version 20 21# all available imgtool commands 22COMMANDS = [ 23 "create", 24 "dumpinfo", 25 "getpriv", 26 "getpub", 27 "getpubhash", 28 "keygen", 29 "sign", 30 "verify", 31 "version", 32] 33 34 35def test_new_command(): 36 """Check that no new commands had been added, 37 so that tests would be updated in such case""" 38 for cmd in imgtool.commands: 39 assert cmd in COMMANDS 40 41 42def test_help(): 43 """Simple test for the imgtool's help option, 44 mostly just to see that it can be started""" 45 runner = CliRunner() 46 47 result_short = runner.invoke(imgtool, ["-h"]) 48 assert result_short.exit_code == 0 49 50 result_long = runner.invoke(imgtool, ["--help"]) 51 assert result_long.exit_code == 0 52 assert result_short.output == result_long.output 53 54 # by default help should be also produced 55 result_empty = runner.invoke(imgtool) 56 assert result_empty.exit_code == 0 57 assert result_empty.output == result_short.output 58 59 60def test_version(): 61 """Check that some version info is produced""" 62 runner = CliRunner() 63 64 result = runner.invoke(imgtool, ["version"]) 65 assert result.exit_code == 0 66 assert result.output == imgtool_version + "\n" 67 68 result_help = runner.invoke(imgtool, ["version", "-h"]) 69 assert result_help.exit_code == 0 70 assert result_help.output != result.output 71 72 73def test_unknown(): 74 """Check that unknown command will be handled""" 75 runner = CliRunner() 76 77 result = runner.invoke(imgtool, ["unknown"]) 78 assert result.exit_code != 0 79 80 81@pytest.mark.parametrize("command", COMMANDS) 82def test_cmd_help(command): 83 """Check that all commands have some help""" 84 runner = CliRunner() 85 86 result_short = runner.invoke(imgtool, [command, "-h"]) 87 assert result_short.exit_code == 0 88 89 result_long = runner.invoke(imgtool, [command, "--help"]) 90 assert result_long.exit_code == 0 91 92 assert result_short.output == result_long.output 93 94 95@pytest.mark.parametrize("command1", COMMANDS) 96@pytest.mark.parametrize("command2", COMMANDS) 97def test_cmd_dif_help(command1, command2): 98 """Check that all commands have some different help""" 99 runner = CliRunner() 100 101 result_general = runner.invoke(imgtool, "--help") 102 assert result_general.exit_code == 0 103 104 result_cmd1 = runner.invoke(imgtool, [command1, "--help"]) 105 assert result_cmd1.exit_code == 0 106 assert result_cmd1.output != result_general.output 107 108 if command1 != command2: 109 result_cmd2 = runner.invoke(imgtool, [command2, "--help"]) 110 assert result_cmd2.exit_code == 0 111 112 assert result_cmd1.output != result_cmd2.output 113