| 1 |
# Copyright New York University and the in-toto contributors |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
""" |
| 5 |
<Program Name> |
| 6 |
test_in_toto_keygen.py |
| 7 |
<Author> |
| 8 |
Sachit Malik <[email protected]> |
| 9 |
<Started> |
| 10 |
Wed Jun 28, 2017 |
| 11 |
<Copyright> |
| 12 |
See LICENSE for licensing information. |
| 13 |
<Purpose> |
| 14 |
Test in_toto_keygen command line tool. |
| 15 |
|
| 16 |
""" |
| 17 |
import sys |
| 18 |
import unittest |
| 19 |
from unittest.mock import patch |
| 20 |
|
| 21 |
from in_toto.in_toto_keygen import main as in_toto_keygen_main |
| 22 |
from tests.common import TmpDirMixin |
| 23 |
|
| 24 |
|
| 25 |
class TestInTotoKeyGenTool(unittest.TestCase, TmpDirMixin): |
| 26 |
"""Test in_toto_keygen's main() - requires sys.argv patching; error |
| 27 |
logs/exits on Exception.""" |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def setUpClass(self): |
| 31 |
self.set_up_test_dir() |
| 32 |
|
| 33 |
@classmethod |
| 34 |
def tearDownClass(self): |
| 35 |
self.tear_down_test_dir() |
| 36 |
|
| 37 |
def test_main_required_args(self): |
| 38 |
"""Test in-toto-keygen CLI tool with required arguments.""" |
| 39 |
args = ["in_toto_keygen.py"] |
| 40 |
|
| 41 |
with patch.object(sys, "argv", args + ["bob"]), self.assertRaises( |
| 42 |
SystemExit |
| 43 |
): |
| 44 |
in_toto_keygen_main() |
| 45 |
|
| 46 |
def test_main_optional_args(self): |
| 47 |
"""Test CLI command keygen with optional arguments.""" |
| 48 |
args = ["in_toto_keygen.py"] |
| 49 |
password = "123456" |
| 50 |
with patch.object(sys, "argv", args + ["-p", "bob"]), patch( |
| 51 |
"getpass.getpass", return_value=password |
| 52 |
), self.assertRaises(SystemExit): |
| 53 |
in_toto_keygen_main() |
| 54 |
with patch.object( |
| 55 |
sys, "argv", args + ["-p", "-t", "rsa", "bob"] |
| 56 |
), patch("getpass.getpass", return_value=password), self.assertRaises( |
| 57 |
SystemExit |
| 58 |
): |
| 59 |
in_toto_keygen_main() |
| 60 |
with patch.object( |
| 61 |
sys, "argv", args + ["-t", "ed25519", "bob"] |
| 62 |
), self.assertRaises(SystemExit): |
| 63 |
in_toto_keygen_main() |
| 64 |
with patch.object( |
| 65 |
sys, "argv", args + ["-t", "ecdsa", "bob"] |
| 66 |
), self.assertRaises(SystemExit): |
| 67 |
in_toto_keygen_main() |
| 68 |
with patch.object( |
| 69 |
sys, "argv", args + ["-p", "-t", "ed25519", "bob"] |
| 70 |
), patch("getpass.getpass", return_value=password), self.assertRaises( |
| 71 |
SystemExit |
| 72 |
): |
| 73 |
in_toto_keygen_main() |
| 74 |
with patch.object( |
| 75 |
sys, "argv", args + ["-p", "-b", "3072", "bob"] |
| 76 |
), patch("getpass.getpass", return_value=password), self.assertRaises( |
| 77 |
SystemExit |
| 78 |
): |
| 79 |
in_toto_keygen_main() |
| 80 |
|
| 81 |
def test_main_wrong_args(self): |
| 82 |
"""Test CLI command with missing arguments.""" |
| 83 |
wrong_args_list = [ |
| 84 |
["in_toto_keygen.py"], |
| 85 |
["in_toto_keygen.py", "-r"], |
| 86 |
["in_toto_keygen.py", "-p", "-b", "1024", "bob"], |
| 87 |
] |
| 88 |
password = "123456" |
| 89 |
|
| 90 |
for wrong_args in wrong_args_list: |
| 91 |
with patch.object(sys, "argv", wrong_args), patch( |
| 92 |
"getpass.getpass", return_value=password |
| 93 |
), self.assertRaises(SystemExit): |
| 94 |
in_toto_keygen_main() |
| 95 |
|
| 96 |
|
| 97 |
if __name__ == "__main__": |
| 98 |
unittest.main() |