Skip to content

Commit

Permalink
Add simple GenKeysMixin to generate test keys
Browse files Browse the repository at this point in the history
Mix in with subclasses of unittest.TestCase and call set_up_keys in
setUpClass method, ideally to be used together with TmpDirMixin.

Signed-off-by: Lukas Puehringer <[email protected]>
  • Loading branch information
lukpueh committed Oct 20, 2020
1 parent 28a3ed5 commit 51e626f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
22 changes: 22 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import inspect
import shutil
import tempfile
from securesystemslib.interface import (generate_and_write_rsa_keypair,
generate_and_write_ed25519_keypair)

import unittest
if sys.version_info >= (3, 3):
Expand Down Expand Up @@ -85,6 +87,26 @@ def set_up_gpg_keys(cls):
shutil.copytree(gpg_keys, os.path.join(os.getcwd(), cls.gnupg_home))


class GenKeysMixin():
"""Mixin with classmethod to create cryptographic keys in cwd. """
key_pw = "pw"

@classmethod
def set_up_keys(cls):
# Generated unencrypted keys
cls.rsa_key_path = generate_and_write_rsa_keypair()
cls.rsa_key_id = os.path.basename(cls.rsa_key_path)

cls.ed25519_key_path = generate_and_write_ed25519_keypair()
cls.ed25519_key_id = os.path.basename(cls.ed25519_key_path)

# Generate encrypted keys
cls.rsa_key_enc_path = generate_and_write_rsa_keypair(password=cls.key_pw)
cls.rsa_key_enc_id = os.path.basename(cls.rsa_key_enc_path)

cls.ed25519_key_enc_path = generate_and_write_ed25519_keypair(password=cls.key_pw)
cls.ed25519_key_enc_id = os.path.basename(cls.ed25519_key_enc_path)


class CliTestCase(unittest.TestCase):
"""TestCase subclass providing a test helper that patches sys.argv with
Expand Down
40 changes: 32 additions & 8 deletions tests/test_common_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,32 @@
"""
import unittest
import argparse
import in_toto.common_args
from in_toto.common_args import (
sort_action_groups,
title_case_action_groups,
KEY_PASSWORD_ARGS,
KEY_PASSWORD_KWARGS,
parse_password_and_prompt_args)


class TestCommonArgs(unittest.TestCase):
def test_parse_password_and_prompt_args(self):
""""Test parse -P/--password optional arg (nargs=?, const=True). """
parser = argparse.ArgumentParser()
parser.add_argument(*KEY_PASSWORD_ARGS, **KEY_PASSWORD_KWARGS)

# parameter list | expected result tuple, i.e password, prompt
tests = [
([], (None, False)),
(["--password"], (None, True)),
(["--password", "123456"], ("123456", False)),
(["-P"], (None, True)),
(["-P", "123456"], ("123456", False))
]

for idx, (params, expected) in enumerate(tests):
result = parse_password_and_prompt_args(parser.parse_args(params))
self.assertTupleEqual(result, expected, "(row {})".format(idx))


class TestArgparseActionGroupHelpers(unittest.TestCase):
Expand All @@ -41,19 +66,18 @@ def setUp(self):
def test_title_case_action_groups(self):
"""Test title_case_action_groups title cases action group titles. """
# Make titles title-case (default is asserted in setUp)
in_toto.common_args.title_case_action_groups(self.parser)
title_case_action_groups(self.parser)

# Assert successful title-casing
self.assertListEqual([group.title for group in self.parser._action_groups],
["Positional Arguments", "Optional Arguments"])


def test_sort_action_groups(self):
def test_sort_action_groups(self):
"""Test sort_action_groups sorts action groups by custom title order. """
# Create custom order for titles (default is asserted in setUp)
custom_order = ["optional arguments", "positional arguments"]
in_toto.common_args.sort_action_groups(
self.parser, title_order=custom_order)
sort_action_groups(self.parser, title_order=custom_order)
# Assert successful re-ordering
self.assertListEqual([group.title for group in self.parser._action_groups],
custom_order)
Expand All @@ -63,10 +87,10 @@ def test_sort_action_groups(self):
self.parser.add_argument_group("required named arguments")

# Test default custom order of action groups titles (which are title-cased)
in_toto.common_args.title_case_action_groups(self.parser)
in_toto.common_args.sort_action_groups(self.parser)
title_case_action_groups(self.parser)
sort_action_groups(self.parser)
default_custom_order = ["Required Named Arguments", "Positional Arguments",
"Optional Arguments"]
"Optional Arguments"]

# Assert successful(title-casing) re-ordering
self.assertListEqual([group.title for group in self.parser._action_groups],
Expand Down

0 comments on commit 51e626f

Please sign in to comment.