Exemple #1
0
    def test_wrong_pubkeys(self):
        """Check validate pubkeys fails with wrong keys."""
        # Pubkeys must be lists ...
        tmp_step = Step()
        tmp_step.pubkeys = "abcd"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            tmp_step.validate()

        # ... of keyids (hex schema)
        tmp_step.pubkeys = ["abcdefg"]
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            tmp_step.validate()
Exemple #2
0
class TestStepValidator(unittest.TestCase):
    """Test verifylib.verify_delete_rule(rule, artifact_queue) """
    def setUp(self):
        """Populate a base layout that we can use."""
        self.step = Step(name="this-step")

    def test_wrong_type(self):
        """Test the type field within Validate()."""

        self.step._type = "wrong"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_type()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step._type = "step"
        self.step._validate_type()

    def test_wrong_threshold(self):
        """Test that the threshold value is correctly checked."""

        # no, python is not *this* smart
        self.step.threshold = "Ten"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_threshold()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.threshold = 10
        self.step._validate_threshold()
        self.step.validate()

    def test_wrong_expected_materials(self):
        """Test that the material rule validators catch malformed ones."""

        self.step.expected_materials = [["NONFOO"]]
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_materials()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.expected_materials = "PFF"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_materials()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        # for more thorough tests, check the test_artifact_rules.py module
        self.step.expected_materials = [["CREATE", "foo"]]
        self.step._validate_expected_materials()
        self.step.validate()

    def test_wrong_expected_products(self):
        """Test that the product rule validators catch malformed ones."""

        self.step.expected_products = [["NONFOO"]]
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_products()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.expected_products = "PFF"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_products()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        # for more thorough tests, check the test_artifact_rules.py module
        self.step.expected_products = [["CREATE", "foo"]]
        self.step._validate_expected_products()
        self.step.validate()

    def test_wrong_pubkeys(self):
        # FIXME: generating keys for each test are expensive processes, maybe we
        # should have an asset/fixture folder/loader?

        rsa_key_one = securesystemslib.keys.generate_rsa_key()
        rsa_key_two = securesystemslib.keys.generate_rsa_key()

        self.step.pubkeys = ['bad-keyid']

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_pubkeys()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.pubkeys = [rsa_key_one['keyid'], rsa_key_two['keyid']]
        self.step._validate_pubkeys()
        self.step.validate()

    def test_wrong_expected_command(self):
        """Test that the expected command validator catches malformed ones."""

        self.step.expected_command = -1
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_command()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.expected_command = ["somecommand"]
        self.step._validate_expected_command()
        self.step.validate()
Exemple #3
0
class TestStepValidator(unittest.TestCase):
    """Test verifylib.verify_delete_rule(rule, artifact_queue) """
    def setUp(self):
        """Populate a base layout that we can use."""
        self.step = Step(name="this-step")

    def test_wrong_type(self):
        """Test the type field within Validate()."""

        self.step._type = "wrong"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_type()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step._type = "step"
        self.step._validate_type()

    def test_wrong_threshold(self):
        """Test that the threshold value is correctly checked."""

        # no, python is not *this* smart
        self.step.threshold = "Ten"
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_threshold()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.threshold = 10
        self.step._validate_threshold()
        self.step.validate()

    def test_wrong_pubkeys(self):
        # FIXME: generating keys for each test are expensive processes, maybe we
        # should have an asset/fixture folder/loader?

        rsa_key_one = securesystemslib.keys.generate_rsa_key()
        rsa_key_two = securesystemslib.keys.generate_rsa_key()

        self.step.pubkeys = ['bad-keyid']

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_pubkeys()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.pubkeys = [rsa_key_one['keyid'], rsa_key_two['keyid']]
        self.step._validate_pubkeys()
        self.step.validate()

    def test_wrong_expected_command(self):
        """Test that the expected command validator catches malformed ones."""

        self.step.expected_command = -1
        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step._validate_expected_command()

        with self.assertRaises(securesystemslib.exceptions.FormatError):
            self.step.validate()

        self.step.expected_command = ["somecommand"]
        self.step._validate_expected_command()
        self.step.validate()

    def test_set_expected_command_from_string(self):
        """Test shelx parse command string to list. """
        step = Step()
        step.set_expected_command_from_string("echo 'foo bar'")
        self.assertListEqual(step.expected_command, ["echo", "foo bar"])