| 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_metadata.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Jan 24, 2018 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test in_toto.models.metadata.Metablock class methods. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import os |
| 25 |
import unittest |
| 26 |
|
| 27 |
from securesystemslib.exceptions import FormatError |
| 28 |
|
| 29 |
from in_toto.models.layout import Layout |
| 30 |
from in_toto.models.link import Link |
| 31 |
from in_toto.models.metadata import Metablock |
| 32 |
|
| 33 |
|
| 34 |
class TestMetablockValidator(unittest.TestCase): |
| 35 |
"""Test in_toto.models.metadata.Metablock validators.""" |
| 36 |
|
| 37 |
def test_validate_signed(self): |
| 38 |
"""Test validate Metablock's 'signed' property.""" |
| 39 |
# Valid Layout Metablock |
| 40 |
metablock = Metablock(signed=Layout()) |
| 41 |
metablock._validate_signed() |
| 42 |
|
| 43 |
# Valid Link Metablock |
| 44 |
Metablock(signed=Link()) |
| 45 |
metablock._validate_signed() |
| 46 |
|
| 47 |
# Fail instantiation with empty or invalid signed property |
| 48 |
# Metablock is validated on instantiation |
| 49 |
with self.assertRaises(FormatError): |
| 50 |
Metablock() |
| 51 |
with self.assertRaises(FormatError): |
| 52 |
Metablock(signed="not-a-layout-or-link") |
| 53 |
|
| 54 |
# Fail with invalid signed property |
| 55 |
metablock = Metablock(signed=Layout()) |
| 56 |
metablock.signed._type = "bogus type" |
| 57 |
with self.assertRaises(FormatError): |
| 58 |
metablock._validate_signed() |
| 59 |
|
| 60 |
def test_validate_signatures(self): |
| 61 |
"""Test validate Metablock's 'signatures' property.""" |
| 62 |
# An empty signature list is okay |
| 63 |
metablock = Metablock(signed=Layout()) |
| 64 |
metablock._validate_signatures() |
| 65 |
|
| 66 |
# Fail with signatures property not a list |
| 67 |
metablock.signatures = "not-a-signatures-list" |
| 68 |
with self.assertRaises(FormatError): |
| 69 |
metablock._validate_signatures() |
| 70 |
|
| 71 |
# Fail with invalid signature |
| 72 |
metablock.signatures = [] |
| 73 |
metablock.signatures.append("not-a-signature") |
| 74 |
with self.assertRaises(FormatError): |
| 75 |
metablock._validate_signatures() |
| 76 |
|
| 77 |
# Load signed demo link |
| 78 |
demo_link_path = os.path.join( |
| 79 |
os.path.dirname(os.path.realpath(__file__)), |
| 80 |
"..", |
| 81 |
"demo_files", |
| 82 |
"write-code.776a00e2.link", |
| 83 |
) |
| 84 |
|
| 85 |
metablock = Metablock.load(demo_link_path) |
| 86 |
|
| 87 |
# Verify that there is a signature and that it is valid |
| 88 |
self.assertTrue(len(metablock.signatures) > 0) |
| 89 |
metablock._validate_signatures() |
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__": |
| 93 |
unittest.main() |