| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright New York University and the in-toto contributors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
""" |
| 7 |
<Program Name> |
| 8 |
common.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Feb 6, 2018 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Common code for in-toto unittests, import like so: |
| 21 |
`import tests.common` |
| 22 |
|
| 23 |
Tests importing this module, should be run from the project root, e.g.: |
| 24 |
`python tests/test_in_toto_run.py` |
| 25 |
or using the aggregator script (preferred way): |
| 26 |
`python tests/runtests.py`. |
| 27 |
|
| 28 |
""" |
| 29 |
import inspect |
| 30 |
import os |
| 31 |
import shutil |
| 32 |
import sys |
| 33 |
import tempfile |
| 34 |
import unittest |
| 35 |
from unittest.mock import patch |
| 36 |
|
| 37 |
from securesystemslib.interface import ( |
| 38 |
generate_and_write_ecdsa_keypair, |
| 39 |
generate_and_write_ed25519_keypair, |
| 40 |
generate_and_write_rsa_keypair, |
| 41 |
generate_and_write_unencrypted_ecdsa_keypair, |
| 42 |
generate_and_write_unencrypted_ed25519_keypair, |
| 43 |
generate_and_write_unencrypted_rsa_keypair, |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def run_with_portable_scripts(decorated): |
| 48 |
print("patching...") |
| 49 |
scripts_path = os.path.join( |
| 50 |
os.path.dirname(os.path.realpath(__file__)), "scripts" |
| 51 |
) |
| 52 |
print("scripts are located in {}".format(scripts_path)) |
| 53 |
|
| 54 |
@patch.dict( |
| 55 |
os.environ, {"PATH": "{};{}".format(scripts_path, os.environ["PATH"])} |
| 56 |
) |
| 57 |
class Patched(decorated): |
| 58 |
pass |
| 59 |
|
| 60 |
return Patched |
| 61 |
|
| 62 |
|
| 63 |
class TmpDirMixin: |
| 64 |
"""Mixin with classmethods to create and change into a temporary directory, |
| 65 |
and to change back to the original CWD and remove the temporary directory. |
| 66 |
|
| 67 |
""" |
| 68 |
|
| 69 |
@classmethod |
| 70 |
def set_up_test_dir(cls): |
| 71 |
"""Back up CWD, and create and change into temporary directory.""" |
| 72 |
cls.original_cwd = os.getcwd() |
| 73 |
cls.test_dir = os.path.realpath(tempfile.mkdtemp()) |
| 74 |
os.chdir(cls.test_dir) |
| 75 |
|
| 76 |
@classmethod |
| 77 |
def tear_down_test_dir(cls): |
| 78 |
"""Change back to original CWD and remove temporary directory.""" |
| 79 |
os.chdir(cls.original_cwd) |
| 80 |
shutil.rmtree(cls.test_dir) |
| 81 |
|
| 82 |
|
| 83 |
class GPGKeysMixin: |
| 84 |
"""Mixin with classmethod to copy GPG rsa test keyring to a subdir 'rsa' in |
| 85 |
the CWD. |
| 86 |
|
| 87 |
""" |
| 88 |
|
| 89 |
gnupg_home = "rsa" |
| 90 |
gpg_key_768C43 = "7b3abb26b97b655ab9296bd15b0bd02e1c768c43" |
| 91 |
gpg_key_85DA58 = "8288ef560ed3795f9df2c0db56193089b285da58" |
| 92 |
gpg_key_0C8A17 = "8465a1e2e0fb2b40adb2478e18fb3f537e0c8a17" |
| 93 |
gpg_key_D924E9 = "c5a0abe6ec19d0d65f85e2c39be9df5131d924e9" |
| 94 |
|
| 95 |
@classmethod |
| 96 |
def set_up_gpg_keys(cls): |
| 97 |
gpg_keys = os.path.join( |
| 98 |
os.path.dirname(os.path.realpath(__file__)), "gpg_keyrings", "rsa" |
| 99 |
) |
| 100 |
|
| 101 |
shutil.copytree(gpg_keys, os.path.join(os.getcwd(), cls.gnupg_home)) |
| 102 |
|
| 103 |
|
| 104 |
class GenKeysMixin: |
| 105 |
"""Mixin with classmethod to create cryptographic keys in cwd.""" |
| 106 |
|
| 107 |
key_pw = "pw" |
| 108 |
|
| 109 |
@classmethod |
| 110 |
def set_up_keys(cls): |
| 111 |
# Generated unencrypted keys |
| 112 |
cls.rsa_key_path = generate_and_write_unencrypted_rsa_keypair() |
| 113 |
cls.rsa_key_id = os.path.basename(cls.rsa_key_path) |
| 114 |
|
| 115 |
cls.ed25519_key_path = generate_and_write_unencrypted_ed25519_keypair() |
| 116 |
cls.ed25519_key_id = os.path.basename(cls.ed25519_key_path) |
| 117 |
|
| 118 |
cls.ecdsa_key_path = generate_and_write_unencrypted_ecdsa_keypair() |
| 119 |
cls.ecdsa_key_id = os.path.basename(cls.ecdsa_key_path) |
| 120 |
|
| 121 |
# Generate encrypted keys |
| 122 |
cls.rsa_key_enc_path = generate_and_write_rsa_keypair( |
| 123 |
password=cls.key_pw |
| 124 |
) |
| 125 |
cls.rsa_key_enc_id = os.path.basename(cls.rsa_key_enc_path) |
| 126 |
|
| 127 |
cls.ed25519_key_enc_path = generate_and_write_ed25519_keypair( |
| 128 |
password=cls.key_pw |
| 129 |
) |
| 130 |
cls.ed25519_key_enc_id = os.path.basename(cls.ed25519_key_enc_path) |
| 131 |
|
| 132 |
cls.ecdsa_key_enc_path = generate_and_write_ecdsa_keypair( |
| 133 |
password=cls.key_pw |
| 134 |
) |
| 135 |
cls.ecdsa_key_enc_id = os.path.basename(cls.ecdsa_key_enc_path) |
| 136 |
|
| 137 |
|
| 138 |
class CliTestCase(unittest.TestCase): |
| 139 |
"""TestCase subclass providing a test helper that patches sys.argv with |
| 140 |
passed arguments and asserts a SystemExit with a return code equal |
| 141 |
to the passed status argument. |
| 142 |
|
| 143 |
Subclasses of CliTestCase require a class variable that stores the main |
| 144 |
function of the cli tool to test as staticmethod, e.g.: |
| 145 |
|
| 146 |
``` |
| 147 |
import tests.common |
| 148 |
from in_toto.in_toto_run import main as in_toto_run_main |
| 149 |
|
| 150 |
class TestInTotoRunTool(tests.common.CliTestCase): |
| 151 |
cli_main_func = staticmethod(in_toto_run_main) |
| 152 |
... |
| 153 |
|
| 154 |
``` |
| 155 |
""" |
| 156 |
|
| 157 |
cli_main_func = None |
| 158 |
|
| 159 |
def __init__(self, *args, **kwargs): |
| 160 |
"""Constructor that checks for the presence of a callable cli_main_func |
| 161 |
class variable. And stores the filename of the module containing that |
| 162 |
function, to be used as first argument when patching sys.argv in |
| 163 |
self.assert_cli_sys_exit. |
| 164 |
""" |
| 165 |
if not callable(self.cli_main_func): |
| 166 |
raise Exception( |
| 167 |
"Subclasses of `CliTestCase` need to assign the main" |
| 168 |
" function of the cli tool to test using `staticmethod()`: {}".format( |
| 169 |
self.__class__.__name__ |
| 170 |
) |
| 171 |
) |
| 172 |
|
| 173 |
file_path = inspect.getmodule(self.cli_main_func).__file__ |
| 174 |
self.file_name = os.path.basename(file_path) |
| 175 |
|
| 176 |
super(CliTestCase, self).__init__(*args, **kwargs) |
| 177 |
|
| 178 |
def assert_cli_sys_exit(self, cli_args, status): |
| 179 |
"""Test helper to mock command line call and assert return value. |
| 180 |
The passed args does not need to contain the command line tool's name. |
| 181 |
This is assessed from `self.cli_main_func` |
| 182 |
""" |
| 183 |
with patch.object( |
| 184 |
sys, "argv", [self.file_name] + cli_args |
| 185 |
), self.assertRaises(SystemExit) as raise_ctx: |
| 186 |
self.cli_main_func() # pylint: disable=not-callable |
| 187 |
|
| 188 |
self.assertEqual(raise_ctx.exception.code, status) |