| 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_supply_chain_item.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Jan 25, 2018 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test SupplyChainItem class methods. |
| 21 |
SupplyChainItem is a super class for Steps and Inspections. |
| 22 |
|
| 23 |
""" |
| 24 |
import json |
| 25 |
import unittest |
| 26 |
|
| 27 |
import securesystemslib.exceptions |
| 28 |
|
| 29 |
from in_toto.models.layout import SupplyChainItem |
| 30 |
|
| 31 |
|
| 32 |
class TestSupplyChainItem(unittest.TestCase): |
| 33 |
"""Test models.SupplyChainItem.""" |
| 34 |
|
| 35 |
def test_wrong_expected_materials(self): |
| 36 |
"""Test that the material rule validators catch malformed ones.""" |
| 37 |
item = SupplyChainItem() |
| 38 |
|
| 39 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 40 |
item.expected_materials = [["NONFOO"]] |
| 41 |
item._validate_expected_materials() |
| 42 |
|
| 43 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 44 |
item.validate() |
| 45 |
|
| 46 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 47 |
item.expected_materials = "PFF" |
| 48 |
item._validate_expected_materials() |
| 49 |
|
| 50 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 51 |
item.validate() |
| 52 |
|
| 53 |
# for more thorough tests, check the test_rulelib.py module |
| 54 |
item.expected_materials = [["CREATE", "foo"]] |
| 55 |
item._validate_expected_materials() |
| 56 |
item.validate() |
| 57 |
|
| 58 |
def test_wrong_expected_products(self): |
| 59 |
"""Test that the product rule validators catch malformed values.""" |
| 60 |
item = SupplyChainItem() |
| 61 |
|
| 62 |
item.expected_products = [["NONFOO"]] |
| 63 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 64 |
item._validate_expected_products() |
| 65 |
|
| 66 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 67 |
item.validate() |
| 68 |
|
| 69 |
item.expected_products = "PFF" |
| 70 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 71 |
item._validate_expected_products() |
| 72 |
|
| 73 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 74 |
item.validate() |
| 75 |
|
| 76 |
# for more thorough tests, check the test_rulelib.py module |
| 77 |
item.expected_products = [["CREATE", "foo"]] |
| 78 |
item._validate_expected_products() |
| 79 |
item.validate() |
| 80 |
|
| 81 |
def test_repr(self): |
| 82 |
"""Test repr returns a JSON parseable string.""" |
| 83 |
json.loads(repr(SupplyChainItem())) |
| 84 |
|
| 85 |
def test_add_rule_from_string(self): |
| 86 |
"""Test that add_rule_from string methods set property correctly.""" |
| 87 |
item = SupplyChainItem() |
| 88 |
item.add_material_rule_from_string("CREATE foo") |
| 89 |
self.assertListEqual(item.expected_materials[-1], ["CREATE", "foo"]) |
| 90 |
item.add_product_rule_from_string("ALLOW bar") |
| 91 |
self.assertListEqual(item.expected_products[-1], ["ALLOW", "bar"]) |
| 92 |
|
| 93 |
item.validate() |
| 94 |
|
| 95 |
|
| 96 |
if __name__ == "__main__": |
| 97 |
unittest.main() |