1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
# pylint: disable=C0114,C0116,W0621
import argparse
import libconf
import pytest
from swugenerator import main
from swugenerator.swu_sign import SWUSignCMS, SWUSignCustom, SWUSignPKCS11, SWUSignRSA
VALID_KEY = "390ad54490a4a5f53722291023c19e08ffb5c4677a59e958c96ffa6e641df040"
VALID_IV = "d5d601bacfe13100b149177318ebc7a4"
VALID_KEY_FILE = "valid_key.txt"
INVALID_KEY_FILE = "invalid_key.txt"
@pytest.fixture(scope="session")
def test_dir(tmp_path_factory):
"""Creates a directory to test"""
test_space = tmp_path_factory.mktemp("archive")
return test_space
#### Key file parsing tests ####
@pytest.fixture(scope="session")
def valid_key_file(test_dir):
key_file = test_dir / VALID_KEY_FILE
with key_file.open("w") as key_file_fd:
key_file_fd.write(f"key={VALID_KEY}\niv={VALID_IV}")
return key_file
@pytest.fixture(scope="session")
def valid_key_file_2(test_dir):
key_file = test_dir / VALID_KEY_FILE
with key_file.open("w") as key_file_fd:
key_file_fd.write(f"key={VALID_KEY}\niv ={VALID_IV}")
return key_file
@pytest.fixture(scope="session")
def invalid_key_file(test_dir):
# Create invalid key file where only the key can be parsed
key_file = test_dir / INVALID_KEY_FILE
with key_file.open("w") as key_file_fd:
key_file_fd.write(f"key foo\nkey={VALID_KEY}\nkey bar\niv\n{VALID_IV}")
return key_file
def test_extract_keys_returns_valid_tuple_from_valid_file(valid_key_file):
assert main.extract_keys(str(valid_key_file)) == (VALID_KEY, VALID_IV)
def test_extract_keys_returns_valid_tuple_from_valid_file_2(valid_key_file_2):
assert main.extract_keys(str(valid_key_file_2)) == (VALID_KEY, VALID_IV)
def test_extract_keys_returns_none_from_key_file_thats_invalid(invalid_key_file):
assert main.extract_keys(str(invalid_key_file)) == (VALID_KEY, None)
def test_extract_keys_returns_exception_from_key_file_that_dne():
with pytest.raises(main.InvalidKeyFile):
main.extract_keys("foo/bar/baz.txt")
#### Config file parsing tests ####
VALID_CONFIG = {"foo": 1, "bar": "test"}
VALID_CONFIG_FILE = "valid.cfg"
INVALID_CONFIG_FILE = "invalid.cfg"
@pytest.fixture(scope="session")
def valid_config_file(test_dir):
config_file = test_dir / VALID_CONFIG_FILE
with config_file.open("w") as config_file_fd:
config_file_fd.write(libconf.dumps({"variables": VALID_CONFIG}))
return config_file
@pytest.fixture(scope="session")
def invalid_config_file(test_dir):
config_file = test_dir / VALID_CONFIG_FILE
with config_file.open("w") as config_file_fd:
config_file_fd.write("{" + libconf.dumps({"variables": VALID_CONFIG}))
return config_file
def test_valid_config_file_is_properly_parsed(valid_config_file):
assert main.parse_config_file(str(valid_config_file)) == VALID_CONFIG
def test_invalid_config_file_throws_exception(invalid_config_file):
with pytest.raises(libconf.ConfigParseError):
main.parse_config_file(str(invalid_config_file))
def test_missing_config_file_throws_exception():
with pytest.raises(FileNotFoundError):
main.parse_config_file("foo/bar/baz.txt")
#### Signing option parsing tests ####
SIGNING_TEST_PARAMETERS = [
("CMS,foo,bar,baz,qux", SWUSignCMS("foo", "bar", "baz", "qux")),
("CMS,foo,bar,,qux", SWUSignCMS("foo", "bar", "", "qux")),
("CMS,foo,bar,baz", SWUSignCMS("foo", "bar", "baz", None)),
("CMS,foo,bar", SWUSignCMS("foo", "bar", None, None)),
("RSA,foo,bar", SWUSignRSA("foo", "bar")),
("RSA,foo", SWUSignRSA("foo", None)),
("PKCS11,foo", SWUSignPKCS11("foo")),
("PKCS11,foo,bar", SWUSignPKCS11("foo", "bar")),
("CUSTOM,foo", SWUSignCustom("foo")),
]
@pytest.mark.parametrize("arg,expected", SIGNING_TEST_PARAMETERS)
def test_valid_siging_params_parsed_to_correct_signing_obj(arg, expected):
signing_option = main.parse_signing_option(arg)
assert type(signing_option) == type(expected)
assert signing_option.type == expected.type
assert signing_option.key == expected.key
assert signing_option.cert == expected.cert
assert signing_option.passin == expected.passin
INVALID_SIGNING_TEST_PARAMETERS = [
("CMS", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
("CMS,", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
("CMS,,", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
("CMS,,,", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
("CMS,,,,", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
("CMS,,,,,", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
(
"CMS,,foo,",
"CMS requires private key, certificate, an optional password file and an optional file with additional certificates",
),
("CMS,foo", "CMS requires private key, certificate, an optional password file and an optional file with additional certificates"),
(
"CMS,foo,bar,baz,qux,jaz",
"CMS requires private key, certificate, an optional password file and an optional file with additional certificates",
),
("RSA,foo,bar,baz", "RSA requires private key and an optional password file"),
("PKCS11", "PKCS11 requires pin and optional parameters such as module path, slot or id"),
("PKCS11,", "PKCS11 requires pin and optional parameters such as module path, slot or id"),
("PKCS11,,", "PKCS11 requires pin and optional parameters such as module path, slot or id"),
("CUSTOM", "CUSTOM requires custom command"),
("CUSTOM,", "CUSTOM requires custom command"),
("CUSTOM,,", "CUSTOM requires custom command"),
("CUSTOM,foo,", "CUSTOM requires custom command"),
("FOO", "Unknown signing command"),
("FOO,bar,baz", "Unknown signing command"),
]
@pytest.mark.parametrize("arg,exception_msg", INVALID_SIGNING_TEST_PARAMETERS)
def test_invalid_signing_params_throws_exception_with_correct_msg(arg, exception_msg):
with pytest.raises(main.InvalidSigningOption) as error:
main.parse_signing_option(arg)
assert exception_msg in error.value.args
def test_parse_signing_option_cms_with_engine_and_keyform():
# Only engine
obj = main.parse_signing_option("CMS,foo,bar,baz,qux", engine="myengine")
assert isinstance(obj, SWUSignCMS)
assert obj.engine == "myengine"
assert obj.key == "foo"
assert obj.cert == "bar"
assert obj.passin == "baz"
assert obj.certfile == "qux"
# Only keyform
obj = main.parse_signing_option("CMS,foo,bar,baz,qux", keyform="mykeyform")
assert isinstance(obj, SWUSignCMS)
assert obj.keyform == "mykeyform"
assert obj.key == "foo"
assert obj.cert == "bar"
assert obj.passin == "baz"
assert obj.certfile == "qux"
# Both engine and keyform
obj = main.parse_signing_option("CMS,foo,bar,baz,qux", engine="myengine", keyform="mykeyform")
assert isinstance(obj, SWUSignCMS)
assert obj.engine == "myengine"
assert obj.keyform == "mykeyform"
assert obj.key == "foo"
assert obj.cert == "bar"
assert obj.passin == "baz"
assert obj.certfile == "qux"
# Neither engine nor keyform
obj = main.parse_signing_option("CMS,foo,bar,baz,qux")
assert isinstance(obj, SWUSignCMS)
assert obj.engine is None
assert obj.keyform is None
assert obj.key == "foo"
assert obj.cert == "bar"
assert obj.passin == "baz"
assert obj.certfile == "qux"
#### Parse args tests ####
@pytest.fixture
def mock_main_funcs(monkeypatch):
def mock_create_swu(*_):
return True
def mock_extract_keys(*_):
return "foo", "bar"
def mock_parse_signing_option(*_):
return SWUSignCMS("foo", "bar", "baz", "qux")
def mock_parse_config_file(*_):
return {}
def mock_argparse_error(*_):
raise Exception
def mock_sign_swu(*_):
return True
monkeypatch.setattr(main, "create_swu", mock_create_swu)
monkeypatch.setattr(main, "sign_swu", mock_sign_swu)
monkeypatch.setattr(main, "extract_keys", mock_extract_keys)
monkeypatch.setattr(main, "parse_signing_option", mock_parse_signing_option)
monkeypatch.setattr(main, "parse_config_file", mock_parse_config_file)
monkeypatch.setattr(argparse.ArgumentParser, "exit", mock_argparse_error)
VALID_COMMANDS = [
(["-s", "sw-description", "-o", "test.swu", "create"]),
(["--sw-description", "sw-description", "--swu-file", "test.swu", "create"]),
(
[
"-K",
"key.txt",
"-n",
"-e",
"-x",
"-k",
"CUSTOM,foo",
"-s",
"sw-description",
"-t",
"-a",
".,..",
"-o",
"test.swu",
"-c",
"test.cfg",
"-l",
"DEBUG",
"create",
]
),
(
[
"--encryption-key-file",
"key.txt",
"--no-compress",
"--no-encrypt",
"--no-ivt",
"--sign",
"CUSTOM,foo",
"--sw-description",
"sw-description",
"--encrypt-swdesc",
"--artifactory",
".,..",
"--swu-file",
"test.swu",
"--config",
"test.cfg",
"--loglevel",
"DEBUG",
"create",
]
),
(["-o", "test.swu", "sign", "-i", "in.swu"]),
]
@pytest.mark.parametrize("args", VALID_COMMANDS)
def test_parsing_valid_args_doesnt_throw(args, mock_main_funcs):
main.parse_args(args)
INVALID_COMMANDS = [
(["create"]),
(["-s", "sw-description", "-o", "test.swu"]),
(["-s", "-o", "test.swu", "create"]),
(["-s", "sw-description", "-o", "create"]),
(["-s", "-o", "create"]),
(["-s", "-o"]),
(["-K", "-s", "sw-description", "-o", "test.swu", "create"]),
(["-k", "-s", "sw-description", "-o", "test.swu", "create"]),
(["-a", "sw-description", "-o", "test.swu", "create"]),
(["-c", "sw-description", "-o", "test.swu", "create"]),
(["-l", "sw-description", "-o", "test.swu", "create"]),
(["-l", "baz", "sw-description", "-o", "test.swu", "create"]),
(["sign"]),
(["-o", "test.swu", "sign"]),
(["-i", "-o", "test.swu", "sign"]),
(["-i", "in.swu", "-o", "test.swu", "sign"]),
(["-o", "test.swu", "create"]),
(["-o", "sign", "-i", "in.swu"]),
]
@pytest.mark.parametrize("args", INVALID_COMMANDS)
def test_parsing_invalid_args_does_throw_argparse_exception(args, mock_main_funcs):
with pytest.raises(Exception):
main.parse_args(args)
|