| 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 |
test_step.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Santiago Torres-Arias <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Nov 18, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test step class functions. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import unittest |
| 25 |
|
| 26 |
import securesystemslib.exceptions |
| 27 |
import securesystemslib.keys |
| 28 |
|
| 29 |
from in_toto.models.layout import Step |
| 30 |
|
| 31 |
|
| 32 |
class TestStepValidator(unittest.TestCase): |
| 33 |
"""Test verifylib.verify_delete_rule(rule, artifact_queue)""" |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
"""Populate a base layout that we can use.""" |
| 37 |
self.step = Step(name="this-step") |
| 38 |
|
| 39 |
def test_wrong_type(self): |
| 40 |
"""Test the type field within Validate().""" |
| 41 |
|
| 42 |
self.step._type = "wrong" |
| 43 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 44 |
self.step._validate_type() |
| 45 |
|
| 46 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 47 |
self.step.validate() |
| 48 |
|
| 49 |
self.step._type = "step" |
| 50 |
self.step._validate_type() |
| 51 |
|
| 52 |
def test_wrong_threshold(self): |
| 53 |
"""Test that the threshold value is correctly checked.""" |
| 54 |
|
| 55 |
# no, python is not *this* smart |
| 56 |
self.step.threshold = "Ten" |
| 57 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 58 |
self.step._validate_threshold() |
| 59 |
|
| 60 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 61 |
self.step.validate() |
| 62 |
|
| 63 |
self.step.threshold = 10 |
| 64 |
self.step._validate_threshold() |
| 65 |
self.step.validate() |
| 66 |
|
| 67 |
def test_wrong_pubkeys(self): |
| 68 |
# FIXME: generating keys for each test are expensive processes, maybe we |
| 69 |
# should have an asset/fixture folder/loader? |
| 70 |
|
| 71 |
rsa_key_one = securesystemslib.keys.generate_rsa_key() |
| 72 |
rsa_key_two = securesystemslib.keys.generate_rsa_key() |
| 73 |
|
| 74 |
self.step.pubkeys = ["bad-keyid"] |
| 75 |
|
| 76 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 77 |
self.step._validate_pubkeys() |
| 78 |
|
| 79 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 80 |
self.step.validate() |
| 81 |
|
| 82 |
self.step.pubkeys = [rsa_key_one["keyid"], rsa_key_two["keyid"]] |
| 83 |
self.step._validate_pubkeys() |
| 84 |
self.step.validate() |
| 85 |
|
| 86 |
def test_wrong_expected_command(self): |
| 87 |
"""Test that the expected command validator catches malformed ones.""" |
| 88 |
|
| 89 |
self.step.expected_command = -1 |
| 90 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 91 |
self.step._validate_expected_command() |
| 92 |
|
| 93 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 94 |
self.step.validate() |
| 95 |
|
| 96 |
self.step.expected_command = ["somecommand"] |
| 97 |
self.step._validate_expected_command() |
| 98 |
self.step.validate() |
| 99 |
|
| 100 |
def test_set_expected_command_from_string(self): |
| 101 |
"""Test shelx parse command string to list.""" |
| 102 |
step = Step() |
| 103 |
step.set_expected_command_from_string("echo 'foo bar'") |
| 104 |
self.assertListEqual(step.expected_command, ["echo", "foo bar"]) |
| 105 |
|
| 106 |
|
| 107 |
if __name__ == "__main__": |
| 108 |
unittest.main() |