Debian Codegraph @

tests/models/test_link.py

raw
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_link.py
9
10 <Author>
11 Lukas Puehringer <[email protected]>
12
13 <Started>
14 Sep 28, 2017
15
16 <Copyright>
17 See LICENSE for licensing information.
18
19 <Purpose>
20 Test link class functions.
21
22 """
23
24 import unittest
25
26 from securesystemslib.exceptions import FormatError
27
28 from in_toto.models.link import Link
29
30
31 class TestLinkValidator(unittest.TestCase):
32 """Test link format validators"""
33
34 def test_validate_type(self):
35 """Test `_type` field. Must be "link" """
36 test_link = Link()
37
38 # Good type
39 test_link._type = "link"
40 test_link.validate()
41
42 # Bad type
43 test_link._type = "bad link"
44 with self.assertRaises(FormatError):
45 test_link.validate()
46
47 def test_validate_materials(self):
48 """Test `materials` field. Must be a `dict` of HASH_DICTs"""
49 test_link = Link()
50
51 # Good materials
52 sha = "d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3"
53 test_link.materials = {"foo": {"sha256": sha}}
54 test_link.validate()
55
56 # Bad materials 1
57 test_link.materials = "not a dict"
58 with self.assertRaises(FormatError):
59 test_link.validate()
60
61 # Bad materials 1
62 test_link.materials = {"not": "a material dict"}
63 with self.assertRaises(FormatError):
64 test_link.validate()
65
66 def test_validate_products(self):
67 """Test `products` field. Must be a `dict` of HASH_DICTs"""
68 test_link = Link()
69
70 # Good products
71 sha = "cfdaaf1ab2e4661952a9dec5e8fa3c360c1b06b1a073e8493a7c46d2af8c504b"
72 test_link.products = {"bar": {"sha256": sha}}
73 test_link.validate()
74
75 # Bad products 1
76 test_link = Link()
77 test_link.products = "not a dict"
78 with self.assertRaises(FormatError):
79 test_link.validate()
80
81 # Bad products 2
82 test_link.products = {"not": "a product dict"}
83 with self.assertRaises(FormatError):
84 test_link.validate()
85
86 def test_validate_byproducts(self):
87 """Test `byproducts` field. Must be a `dict`"""
88 test_link = Link()
89 # Good byproducts
90 test_link.byproducts = {}
91 test_link.validate()
92
93 # Bad byproducts
94 test_link.byproducts = "not a dict"
95 with self.assertRaises(FormatError):
96 test_link.validate()
97
98 def test_validate_command(self):
99 """Test `command` field. Must be either a `list`"""
100 test_link = Link()
101
102 # Good command
103 test_link.command = ["echo", "'good command'"]
104 test_link.validate()
105
106 # Bad command
107 test_link.command = "echo 'bad command'"
108 with self.assertRaises(FormatError):
109 test_link.validate()
110
111 def test_validate_environment(self):
112 """Test `environment` field. Must be a `dict`"""
113 test_link = Link()
114
115 # good env per default
116 test_link.validate()
117
118 # Bad env
119 test_link.environment = "not a dict"
120 with self.assertRaises(FormatError):
121 test_link.validate()